From 0243c5a08487eb5f2562b4361ae1fc3cfb208b75 Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Fri, 7 Jun 2024 10:59:48 +0200 Subject: [PATCH] add more types --- composer.json | 4 ++-- src/BackupDestination/Backup.php | 1 + src/BackupDestination/BackupCollection.php | 2 ++ src/BackupDestination/BackupDestination.php | 2 ++ src/BackupDestination/BackupDestinationFactory.php | 4 ++++ src/BackupServiceProvider.php | 2 +- src/Commands/BackupCommand.php | 2 +- src/Commands/BaseCommand.php | 2 ++ src/Commands/ListCommand.php | 11 ++++++++--- src/Exceptions/CannotCreateDbDumper.php | 8 +++++--- src/Exceptions/NotificationCouldNotBeSent.php | 2 +- src/Helpers/ConsoleOutput.php | 2 +- src/Tasks/Backup/Zip.php | 2 +- src/Tasks/Cleanup/CleanupJob.php | 2 +- src/Tasks/Cleanup/CleanupStrategy.php | 2 +- src/Tasks/Monitor/HealthCheck.php | 2 +- src/Traits/Retryable.php | 10 +++++----- 17 files changed, 39 insertions(+), 21 deletions(-) diff --git a/composer.json b/composer.json index ffaea00b..d41ff2e0 100644 --- a/composer.json +++ b/composer.json @@ -32,10 +32,10 @@ "spatie/laravel-signal-aware-command": "^1.2|^2.0", "spatie/temporary-directory": "^2.0", "symfony/console": "^6.0|^7.0", - "symfony/finder": "^6.0|^7.0" + "symfony/finder": "^6.0|^7.0", + "ext-pcntl": "*" }, "require-dev": { - "ext-pcntl": "*", "composer-runtime-api": "^2.0", "larastan/larastan": "^2.7.0", "laravel/slack-notification-channel": "^2.5|^3.0", diff --git a/src/BackupDestination/Backup.php b/src/BackupDestination/Backup.php index b4b4c0f7..6a0d6d31 100644 --- a/src/BackupDestination/Backup.php +++ b/src/BackupDestination/Backup.php @@ -66,6 +66,7 @@ public function sizeInBytes(): float return $this->size; } + /** @return resource */ public function stream() { return throw_unless( diff --git a/src/BackupDestination/BackupCollection.php b/src/BackupDestination/BackupCollection.php index 514c1bfa..dc998fdf 100644 --- a/src/BackupDestination/BackupCollection.php +++ b/src/BackupDestination/BackupCollection.php @@ -6,10 +6,12 @@ use Illuminate\Support\Collection; use Spatie\Backup\Helpers\File; +/** @extends Collection */ class BackupCollection extends Collection { protected ?float $sizeCache = null; + /** @param array $files */ public static function createFromFiles(?FileSystem $disk, array $files): self { return (new static($files)) diff --git a/src/BackupDestination/BackupDestination.php b/src/BackupDestination/BackupDestination.php index 73d6f684..c9b8f6ce 100644 --- a/src/BackupDestination/BackupDestination.php +++ b/src/BackupDestination/BackupDestination.php @@ -114,8 +114,10 @@ public function connectionError(): Exception return $this->connectionError; } + /** @return array */ public function getDiskOptions(): array { + ray(config("filesystems.disks.{$this->diskName()}.backup_options") ?? []); return config("filesystems.disks.{$this->diskName()}.backup_options") ?? []; } diff --git a/src/BackupDestination/BackupDestinationFactory.php b/src/BackupDestination/BackupDestinationFactory.php index 3212470a..99c085b4 100644 --- a/src/BackupDestination/BackupDestinationFactory.php +++ b/src/BackupDestination/BackupDestinationFactory.php @@ -6,6 +6,10 @@ class BackupDestinationFactory { + /** + * @param array $config + * @return Collection + */ public static function createFromArray(array $config): Collection { return collect($config['destination']['disks']) diff --git a/src/BackupServiceProvider.php b/src/BackupServiceProvider.php index 8e460d53..ecd087bb 100644 --- a/src/BackupServiceProvider.php +++ b/src/BackupServiceProvider.php @@ -52,7 +52,7 @@ public function packageRegistered(): void $this->registerDiscordChannel(); } - protected function registerDiscordChannel() + protected function registerDiscordChannel(): void { Notification::resolved(function (ChannelManager $service) { $service->extend('discord', function ($app) { diff --git a/src/Commands/BackupCommand.php b/src/Commands/BackupCommand.php index b69d5cfd..07dfdaa7 100644 --- a/src/Commands/BackupCommand.php +++ b/src/Commands/BackupCommand.php @@ -95,7 +95,7 @@ public function handle(): int } } - protected function guardAgainstInvalidOptions() + protected function guardAgainstInvalidOptions(): void { if (! $this->option('only-db')) { return; diff --git a/src/Commands/BaseCommand.php b/src/Commands/BaseCommand.php index 0762f723..b05aaf56 100644 --- a/src/Commands/BaseCommand.php +++ b/src/Commands/BaseCommand.php @@ -10,6 +10,7 @@ abstract class BaseCommand extends SignalAwareCommand { + /** @var array */ protected array $handlesSignals = []; public function __construct() @@ -33,6 +34,7 @@ protected function runningInConsole(): bool return in_array(PHP_SAPI, ['cli', 'phpdbg']); } + /** @return array */ public function getSubscribedSignals(): array { return $this->handlesSignals; diff --git a/src/Commands/ListCommand.php b/src/Commands/ListCommand.php index cf63ca23..94d961e3 100644 --- a/src/Commands/ListCommand.php +++ b/src/Commands/ListCommand.php @@ -30,7 +30,10 @@ public function handle(): int return static::SUCCESS; } - protected function displayOverview(Collection $backupDestinationStatuses) + /** + * @param Collection $backupDestinationStatuses + */ + protected function displayOverview(Collection $backupDestinationStatuses): static { $headers = ['Name', 'Disk', 'Reachable', 'Healthy', '# of backups', 'Newest backup', 'Used storage']; @@ -46,6 +49,7 @@ protected function displayOverview(Collection $backupDestinationStatuses) return $this; } + /** @return array{0: string, 1: string, 2: string, disk: string, amount: integer, newest: string, usedStorage: string} */ public function convertToRow(BackupDestinationStatus $backupDestinationStatus): array { $destination = $backupDestinationStatus->backupDestination(); @@ -73,7 +77,8 @@ public function convertToRow(BackupDestinationStatus $backupDestinationStatus): return $row; } - protected function displayFailures(Collection $backupDestinationStatuses) + /** @param Collection $backupDestinationStatuses */ + protected function displayFailures(Collection $backupDestinationStatuses): static { $failed = $backupDestinationStatuses ->filter(function (BackupDestinationStatus $backupDestinationStatus) { @@ -98,7 +103,7 @@ protected function displayFailures(Collection $backupDestinationStatuses) return $this; } - protected function getFormattedBackupDate(?Backup $backup = null) + protected function getFormattedBackupDate(?Backup $backup = null): string { return is_null($backup) ? 'No backups present' diff --git a/src/Exceptions/CannotCreateDbDumper.php b/src/Exceptions/CannotCreateDbDumper.php index 3c756a23..5fabe436 100644 --- a/src/Exceptions/CannotCreateDbDumper.php +++ b/src/Exceptions/CannotCreateDbDumper.php @@ -6,11 +6,13 @@ class CannotCreateDbDumper extends Exception { - public static function unsupportedDriver(string $driver): self + public static function unsupportedDriver(string $driver): static { - $supportedDrivers = collect(config('database.connections'))->keys(); + /** @var array $supportedDrivers */ + $supportedDrivers = config('database.connections'); - $formattedSupportedDrivers = $supportedDrivers + $formattedSupportedDrivers = collect($supportedDrivers) + ->keys() ->map(fn (string $supportedDriver) => "`{$supportedDriver}`") ->join(glue: ', ', finalGlue: ' or '); diff --git a/src/Exceptions/NotificationCouldNotBeSent.php b/src/Exceptions/NotificationCouldNotBeSent.php index b01d0c17..c09dfad1 100644 --- a/src/Exceptions/NotificationCouldNotBeSent.php +++ b/src/Exceptions/NotificationCouldNotBeSent.php @@ -6,7 +6,7 @@ class NotificationCouldNotBeSent extends Exception { - public static function noNotificationClassForEvent($event): self + public static function noNotificationClassForEvent(object $event): static { $eventClass = $event::class; diff --git a/src/Helpers/ConsoleOutput.php b/src/Helpers/ConsoleOutput.php index 72107288..cb3eaec1 100644 --- a/src/Helpers/ConsoleOutput.php +++ b/src/Helpers/ConsoleOutput.php @@ -18,7 +18,7 @@ public function setCommand(Command $command): void $this->command = $command; } - public function __call(string $method, array $arguments) + public function __call(string $method, array $arguments): void { $consoleOutput = app(static::class); diff --git a/src/Tasks/Backup/Zip.php b/src/Tasks/Backup/Zip.php index 1234649a..83b0010f 100644 --- a/src/Tasks/Backup/Zip.php +++ b/src/Tasks/Backup/Zip.php @@ -30,7 +30,7 @@ public static function createForManifest(Manifest $manifest, string $pathToZip): return $zip; } - protected static function determineNameOfFileInZip(string $pathToFile, string $pathToZip, string $relativePath) + protected static function determineNameOfFileInZip(string $pathToFile, string $pathToZip, string $relativePath): string { $fileDirectory = pathinfo($pathToFile, PATHINFO_DIRNAME).DIRECTORY_SEPARATOR; diff --git a/src/Tasks/Cleanup/CleanupJob.php b/src/Tasks/Cleanup/CleanupJob.php index c782e426..058db320 100644 --- a/src/Tasks/Cleanup/CleanupJob.php +++ b/src/Tasks/Cleanup/CleanupJob.php @@ -49,7 +49,7 @@ public function run(): void }); } - protected function sendNotification($notification): void + protected function sendNotification(string|object $notification): void { if ($this->sendNotifications) { rescue( diff --git a/src/Tasks/Cleanup/CleanupStrategy.php b/src/Tasks/Cleanup/CleanupStrategy.php index 49bf3438..59d698f9 100644 --- a/src/Tasks/Cleanup/CleanupStrategy.php +++ b/src/Tasks/Cleanup/CleanupStrategy.php @@ -15,7 +15,7 @@ public function __construct( ) { } - abstract public function deleteOldBackups(BackupCollection $backups); + abstract public function deleteOldBackups(BackupCollection $backups): void; public function setBackupDestination(BackupDestination $backupDestination): self { diff --git a/src/Tasks/Monitor/HealthCheck.php b/src/Tasks/Monitor/HealthCheck.php index 6d04f141..499c12e5 100644 --- a/src/Tasks/Monitor/HealthCheck.php +++ b/src/Tasks/Monitor/HealthCheck.php @@ -8,7 +8,7 @@ abstract class HealthCheck { - abstract public function checkHealth(BackupDestination $backupDestination); + abstract public function checkHealth(BackupDestination $backupDestination): void; public function name(): string { diff --git a/src/Traits/Retryable.php b/src/Traits/Retryable.php index 69a973c2..bae1cf1a 100644 --- a/src/Traits/Retryable.php +++ b/src/Traits/Retryable.php @@ -10,7 +10,7 @@ trait Retryable protected int $currentTry = 1; - protected function shouldRetry() + protected function shouldRetry(): bool { if ($this->tries <= 1) { return false; @@ -19,17 +19,17 @@ protected function shouldRetry() return $this->currentTry < $this->tries; } - protected function hasRetryDelay(string $type) + protected function hasRetryDelay(string $type): bool { return ! empty($this->getRetryDelay($type)); } - protected function sleepFor(int $seconds = 0) + protected function sleepFor(int $seconds = 0): void { Sleep::for($seconds)->seconds(); } - protected function setTries(string $type) + protected function setTries(string $type): void { if ($this->option('tries')) { $this->tries = (int) $this->option('tries'); @@ -40,7 +40,7 @@ protected function setTries(string $type) $this->tries = (int) config('backup.'.$type.'.tries', 1); } - protected function getRetryDelay(string $type) + protected function getRetryDelay(string $type): int { return (int) config('backup.'.$type.'.retry_delay', 0); }