From 73257fbe6beb34f2a53719d6ba2a343602bb67f7 Mon Sep 17 00:00:00 2001 From: Lewis Larsen Date: Mon, 24 Jun 2024 17:14:44 +0100 Subject: [PATCH] chore: improved phpstan level 6 support --- .../RemoteServer/CheckRemoteServerConnection.php | 8 ++++++++ app/Console/Commands/CheckVersion.php | 5 +++++ app/Events/BackupDestinationConnectionCheck.php | 5 +++++ app/Events/BackupTaskStatusChanged.php | 5 +++++ app/Events/CreatedBackupTaskLog.php | 10 ++++++++++ .../RemoteServerConnectivityStatusChanged.php | 5 +++++ app/Events/StreamBackupTaskLogEvent.php | 5 +++++ .../BackupDestinations/CheckConnectionButton.php | 5 +++++ app/Livewire/BackupDestinations/IndexItem.php | 5 +++++ app/Livewire/BackupTasks/BackupTaskHistory.php | 5 +++++ app/Livewire/BackupTasks/ClearLogButton.php | 5 +++++ app/Livewire/BackupTasks/CreateBackupTaskForm.php | 4 ++++ app/Livewire/BackupTasks/IndexItem.php | 6 ++++++ app/Livewire/BackupTasks/IndexTable.php | 5 +++++ app/Livewire/BackupTasks/LogModal.php | 10 ++++++++++ app/Livewire/BackupTasks/RunTaskButton.php | 5 +++++ app/Livewire/BackupTasks/TogglePauseButton.php | 5 +++++ app/Livewire/BackupTasks/UpdateBackupTaskForm.php | 4 ++++ .../RemoteServers/CheckConnectionButton.php | 5 +++++ app/Livewire/RemoteServers/IndexItem.php | 5 +++++ app/Models/BackupTask.php | 10 ++++++++++ app/Models/User.php | 5 +++++ app/Services/Backup/Backup.php | 14 ++++++++++++-- app/Services/Backup/BackupDestinations/S3.php | 2 +- .../Contracts/BackupDestinationInterface.php | 5 +++++ app/Services/Backup/Traits/BackupHelpers.php | 15 +++++++++++++++ app/helpers.php | 7 +++++++ phpstan.neon.dist | 8 -------- 28 files changed, 167 insertions(+), 11 deletions(-) diff --git a/app/Actions/RemoteServer/CheckRemoteServerConnection.php b/app/Actions/RemoteServer/CheckRemoteServerConnection.php index 7a62216f..92f9dbd0 100644 --- a/app/Actions/RemoteServer/CheckRemoteServerConnection.php +++ b/app/Actions/RemoteServer/CheckRemoteServerConnection.php @@ -13,6 +13,8 @@ class CheckRemoteServerConnection { /** + * @return array + * * @throws Exception */ public function byRemoteServerId(int $remoteServerId): array @@ -29,6 +31,9 @@ public function byRemoteServerId(int $remoteServerId): array } /** + * @param array $remoteServerConnectionDetails + * @return array + * * @throws Exception */ public function byRemoteServerConnectionDetails(array $remoteServerConnectionDetails): array @@ -39,6 +44,9 @@ public function byRemoteServerConnectionDetails(array $remoteServerConnectionDet } /** + * @param array $data + * @return array + * * @throws Exception */ private function checkServerConnection(array $data, ?RemoteServer $remoteServer = null): array diff --git a/app/Console/Commands/CheckVersion.php b/app/Console/Commands/CheckVersion.php index 1bc0b9b9..72c5c267 100644 --- a/app/Console/Commands/CheckVersion.php +++ b/app/Console/Commands/CheckVersion.php @@ -76,6 +76,11 @@ protected function getCurrentVersion(): ?string return str_replace("\n", '', File::get($versionFile)); } + /** + * Get the latest version from the GitHub API. + * + * @return array|null + */ protected function getLatestVersion(): ?array { $url = 'https://api.github.com/repos/vanguardsh/vanguard/releases/latest'; diff --git a/app/Events/BackupDestinationConnectionCheck.php b/app/Events/BackupDestinationConnectionCheck.php index 0a468d1a..9fcef143 100644 --- a/app/Events/BackupDestinationConnectionCheck.php +++ b/app/Events/BackupDestinationConnectionCheck.php @@ -20,6 +20,11 @@ public function __construct( $this->status = $status ?? $this->backupDestination->status; } + /** + * Get the channels the event should broadcast on. + * + * @return array + */ public function broadcastOn(): array { return [ diff --git a/app/Events/BackupTaskStatusChanged.php b/app/Events/BackupTaskStatusChanged.php index 4a1aab50..fddcf518 100644 --- a/app/Events/BackupTaskStatusChanged.php +++ b/app/Events/BackupTaskStatusChanged.php @@ -20,6 +20,11 @@ public function __construct( $this->status = $status ?? $backupTask->status; } + /** + * Get the channels the event should broadcast on. + * + * @return array + */ public function broadcastOn(): array { return [ diff --git a/app/Events/CreatedBackupTaskLog.php b/app/Events/CreatedBackupTaskLog.php index 5c763b76..2be21698 100644 --- a/app/Events/CreatedBackupTaskLog.php +++ b/app/Events/CreatedBackupTaskLog.php @@ -24,6 +24,11 @@ public function __construct(BackupTaskLog $backupTaskLog) $this->backupTask = $backupTaskLog->backupTask; } + /** + * Get the channels the event should broadcast on. + * + * @return array + */ public function broadcastOn(): array { return [ @@ -31,6 +36,11 @@ public function broadcastOn(): array ]; } + /** + * Get the data to broadcast. + * + * @return array + */ public function broadcastWith(): array { return ['logId' => $this->backupTaskLog->id]; diff --git a/app/Events/RemoteServerConnectivityStatusChanged.php b/app/Events/RemoteServerConnectivityStatusChanged.php index 7b243614..e08667e7 100644 --- a/app/Events/RemoteServerConnectivityStatusChanged.php +++ b/app/Events/RemoteServerConnectivityStatusChanged.php @@ -20,6 +20,11 @@ public function __construct( $this->connectivityStatus = $connectivityStatus ?? $remoteServer->connectivity_status; } + /** + * Get the channels the event should broadcast on. + * + * @return array + */ public function broadcastOn(): array { return [ diff --git a/app/Events/StreamBackupTaskLogEvent.php b/app/Events/StreamBackupTaskLogEvent.php index 1cdf2b84..31c2f4ed 100644 --- a/app/Events/StreamBackupTaskLogEvent.php +++ b/app/Events/StreamBackupTaskLogEvent.php @@ -30,6 +30,11 @@ public function __construct(BackupTaskLog $backupTaskLog, string $logOutput) } + /** + * Get the channels the event should broadcast on. + * + * @return array + */ public function broadcastOn(): array { return [ diff --git a/app/Livewire/BackupDestinations/CheckConnectionButton.php b/app/Livewire/BackupDestinations/CheckConnectionButton.php index c8697a7e..78a0283e 100644 --- a/app/Livewire/BackupDestinations/CheckConnectionButton.php +++ b/app/Livewire/BackupDestinations/CheckConnectionButton.php @@ -11,6 +11,11 @@ class CheckConnectionButton extends Component { public BackupDestination $backupDestination; + /** + * Get the listeners array. + * + * @return array + */ public function getListeners(): array { return [ diff --git a/app/Livewire/BackupDestinations/IndexItem.php b/app/Livewire/BackupDestinations/IndexItem.php index 4880280f..65a21e6c 100644 --- a/app/Livewire/BackupDestinations/IndexItem.php +++ b/app/Livewire/BackupDestinations/IndexItem.php @@ -12,6 +12,11 @@ class IndexItem extends Component { public BackupDestination $backupDestination; + /** + * Get the listeners array. + * + * @return array + */ public function getListeners(): array { return [ diff --git a/app/Livewire/BackupTasks/BackupTaskHistory.php b/app/Livewire/BackupTasks/BackupTaskHistory.php index 675d38ca..2ba5894e 100644 --- a/app/Livewire/BackupTasks/BackupTaskHistory.php +++ b/app/Livewire/BackupTasks/BackupTaskHistory.php @@ -29,6 +29,11 @@ public function render(): View ]); } + /** + * Get the listeners array. + * + * @return array + */ protected function getListeners(): array { return [ diff --git a/app/Livewire/BackupTasks/ClearLogButton.php b/app/Livewire/BackupTasks/ClearLogButton.php index 53e293a4..13bdacc0 100644 --- a/app/Livewire/BackupTasks/ClearLogButton.php +++ b/app/Livewire/BackupTasks/ClearLogButton.php @@ -27,6 +27,11 @@ public function render(): View return view('livewire.backup-tasks.clear-log-button'); } + /** + * Get the listeners array. + * + * @return array + */ protected function getListeners(): array { return [ diff --git a/app/Livewire/BackupTasks/CreateBackupTaskForm.php b/app/Livewire/BackupTasks/CreateBackupTaskForm.php index 37a39a04..9640400b 100644 --- a/app/Livewire/BackupTasks/CreateBackupTaskForm.php +++ b/app/Livewire/BackupTasks/CreateBackupTaskForm.php @@ -70,6 +70,10 @@ class CreateBackupTaskForm extends Component * @var \Illuminate\Database\Eloquent\Collection|null */ public ?Collection $availableTags; + + /** + * @var array|null + */ public ?array $selectedTags; public bool $useIsolatedCredentials = false; diff --git a/app/Livewire/BackupTasks/IndexItem.php b/app/Livewire/BackupTasks/IndexItem.php index 773068e8..cda3ea41 100644 --- a/app/Livewire/BackupTasks/IndexItem.php +++ b/app/Livewire/BackupTasks/IndexItem.php @@ -13,6 +13,9 @@ class IndexItem extends Component public BackupTask $backupTask; public ?BackupTaskLog $backupTaskLog; + /** + * @return array + */ public function getListeners(): array { return [ @@ -26,6 +29,9 @@ public function getListeners(): array ]; } + /** + * @param array $event + */ public function echoBackupTaskLogCreatedEvent(array $event): void { Log::debug('Received the CreatedBackupTaskLog event. Fetching the log.', ['new_log_id' => $event['logId']]); diff --git a/app/Livewire/BackupTasks/IndexTable.php b/app/Livewire/BackupTasks/IndexTable.php index 70f7facb..d13b60bb 100644 --- a/app/Livewire/BackupTasks/IndexTable.php +++ b/app/Livewire/BackupTasks/IndexTable.php @@ -21,6 +21,11 @@ public function render(): View return view('livewire.backup-tasks.index-table', ['backupTasks' => $backupTasks]); } + /** + * Get the listeners array. + * + * @return array + */ protected function getListeners(): array { return [ diff --git a/app/Livewire/BackupTasks/LogModal.php b/app/Livewire/BackupTasks/LogModal.php index 91bc25a3..81b0dd9c 100644 --- a/app/Livewire/BackupTasks/LogModal.php +++ b/app/Livewire/BackupTasks/LogModal.php @@ -37,6 +37,11 @@ public function boot(): void $this->resetLog(); } + /** + * Update the log output with the new data from the event. + * + * @param array $event + */ public function updateLogOutput(array $event): void { Log::debug('Received the StreamBackupTaskLogEvent event. Updating log output.', ['event' => $event]); @@ -72,6 +77,11 @@ public function render(): View return view('livewire.backup-tasks.log-modal'); } + /** + * Get the listeners array. + * + * @return array + */ protected function getListeners(): array { return [ diff --git a/app/Livewire/BackupTasks/RunTaskButton.php b/app/Livewire/BackupTasks/RunTaskButton.php index b35e5729..77453d3e 100644 --- a/app/Livewire/BackupTasks/RunTaskButton.php +++ b/app/Livewire/BackupTasks/RunTaskButton.php @@ -11,6 +11,11 @@ class RunTaskButton extends Component { public BackupTask $backupTask; + /** + * Get the listeners array. + * + * @return array + */ public function getListeners(): array { return [ diff --git a/app/Livewire/BackupTasks/TogglePauseButton.php b/app/Livewire/BackupTasks/TogglePauseButton.php index 8dcd6a62..212b4c5e 100644 --- a/app/Livewire/BackupTasks/TogglePauseButton.php +++ b/app/Livewire/BackupTasks/TogglePauseButton.php @@ -11,6 +11,11 @@ class TogglePauseButton extends Component { public BackupTask $backupTask; + /** + * Get the listeners array. + * + * @return array + */ public function getListeners(): array { return [ diff --git a/app/Livewire/BackupTasks/UpdateBackupTaskForm.php b/app/Livewire/BackupTasks/UpdateBackupTaskForm.php index f7783e0c..3ce0b1b5 100644 --- a/app/Livewire/BackupTasks/UpdateBackupTaskForm.php +++ b/app/Livewire/BackupTasks/UpdateBackupTaskForm.php @@ -72,6 +72,10 @@ class UpdateBackupTaskForm extends Component * @var \Illuminate\Database\Eloquent\Collection|null */ public ?Collection $availableTags; + + /** + * @var array|null + */ public ?array $selectedTags; public bool $useIsolatedCredentials = false; diff --git a/app/Livewire/RemoteServers/CheckConnectionButton.php b/app/Livewire/RemoteServers/CheckConnectionButton.php index 1cbd77b8..6cd7706d 100644 --- a/app/Livewire/RemoteServers/CheckConnectionButton.php +++ b/app/Livewire/RemoteServers/CheckConnectionButton.php @@ -11,6 +11,11 @@ class CheckConnectionButton extends Component { public RemoteServer $remoteServer; + /** + * Get the listeners array. + * + * @return array + */ public function getListeners(): array { return [ diff --git a/app/Livewire/RemoteServers/IndexItem.php b/app/Livewire/RemoteServers/IndexItem.php index e8571ba4..9dbdc582 100644 --- a/app/Livewire/RemoteServers/IndexItem.php +++ b/app/Livewire/RemoteServers/IndexItem.php @@ -12,6 +12,11 @@ class IndexItem extends Component { public RemoteServer $remoteServer; + /** + * Get the listeners array. + * + * @return array + */ public function getListeners(): array { return [ diff --git a/app/Models/BackupTask.php b/app/Models/BackupTask.php index 8a010f6b..d8abd0f3 100644 --- a/app/Models/BackupTask.php +++ b/app/Models/BackupTask.php @@ -37,6 +37,11 @@ class BackupTask extends Model 'last_scheduled_weekly_run_at' => 'datetime', ]; + /** + * Get the count of logs per month for the last six months for a given user. + * + * @return array + */ public static function logsCountPerMonthForLastSixMonths(int $userId): array { $sixMonthsAgo = now()->subMonths(6); @@ -54,6 +59,11 @@ public static function logsCountPerMonthForLastSixMonths(int $userId): array ->toArray(); } + /** + * Get the count of backup tasks by type for a given user. + * + * @return array + */ public static function backupTasksCountByType(int $userId): array { return self::query() diff --git a/app/Models/User.php b/app/Models/User.php index 7a2319aa..1f02b37a 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -125,6 +125,11 @@ protected function lastName(): Attribute ); } + /** + * Get the casts array. + * + * @return array + */ protected function casts(): array { return [ diff --git a/app/Services/Backup/Backup.php b/app/Services/Backup/Backup.php index cacea57c..4e7ebf0d 100644 --- a/app/Services/Backup/Backup.php +++ b/app/Services/Backup/Backup.php @@ -208,6 +208,14 @@ protected function establishSFTPConnection(object $remoteServer, object $backupT return $sftp; } + /** + * Zip a remote directory, excluding specified directories. + * + * @param array $excludeDirs + * + * @throws BackupTaskZipException + * @throws SFTPConnectionException + */ protected function zipRemoteDirectory(SFTP $sftp, string $sourcePath, string $remoteZipPath, array $excludeDirs = []): void { $this->logInfo('Zipping remote directory.', ['source_path' => $sourcePath, 'remote_zip_path' => $remoteZipPath]); @@ -341,9 +349,11 @@ protected function rotateOldBackups( while (count($files) > $backupLimit) { $oldestFile = array_pop($files); - $this->logDebug('Deleting old backup.', ['file' => $oldestFile['Key'] ?? $oldestFile['name']]); + $file = $oldestFile['Key'] ?? $oldestFile['name']; // @phpstan-ignore-line - $backupDestination->deleteFile($oldestFile['Key'] ?? $oldestFile['name']); + $this->logDebug('Deleting old backup.', ['file' => $file]); + + $backupDestination->deleteFile($file); } $this->logInfo('Old backups rotation completed.', ['remaining_files' => count($files)]); diff --git a/app/Services/Backup/BackupDestinations/S3.php b/app/Services/Backup/BackupDestinations/S3.php index e1ec2e19..a173a510 100644 --- a/app/Services/Backup/BackupDestinations/S3.php +++ b/app/Services/Backup/BackupDestinations/S3.php @@ -26,7 +26,7 @@ public function __construct(S3Client $client, string $bucketName) public function listFiles(string $pattern): array { - /** @var array{Contents: array} $result */ + /** @var array{Contents: array} $result */ $result = $this->client->listObjects([ 'Bucket' => $this->bucketName, ]); diff --git a/app/Services/Backup/Contracts/BackupDestinationInterface.php b/app/Services/Backup/Contracts/BackupDestinationInterface.php index 60719b80..7f6a71a5 100644 --- a/app/Services/Backup/Contracts/BackupDestinationInterface.php +++ b/app/Services/Backup/Contracts/BackupDestinationInterface.php @@ -6,6 +6,11 @@ interface BackupDestinationInterface { + /** + * List files matching the given pattern. + * + * @return array + */ public function listFiles(string $pattern): array; public function deleteFile(string $filePath): void; diff --git a/app/Services/Backup/Traits/BackupHelpers.php b/app/Services/Backup/Traits/BackupHelpers.php index 2287e6ef..910b1b42 100644 --- a/app/Services/Backup/Traits/BackupHelpers.php +++ b/app/Services/Backup/Traits/BackupHelpers.php @@ -61,26 +61,41 @@ protected function cleanUpTempFile(string $tempFile): void } } + /** + * @param array $context + */ protected function logInfo(string $message, array $context = []): void { Log::info($message, $context); } + /** + * @param array $context + */ protected function logDebug(string $message, array $context = []): void { Log::debug($message, $context); } + /** + * @param array $context + */ protected function logWarning(string $message, array $context = []): void { Log::warning($message, $context); } + /** + * @param array $context + */ protected function logError(string $message, array $context = []): void { Log::error($message, $context); } + /** + * @param array $context + */ protected function logCritical(string $message, array $context = []): void { Log::critical($message, $context); diff --git a/app/helpers.php b/app/helpers.php index 37ebee1c..9495c5df 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -15,6 +15,13 @@ function get_ssh_private_key(): string return file_get_contents(config('app.ssh.private_key')); } +/** + * Format timezones into a user-friendly format. + * + * @return array Formatted timezones with keys as timezone identifiers and values as formatted strings. + * + * @throws Exception + */ function formatTimezones(): array { $timezones = DateTimeZone::listIdentifiers(DateTimeZone::ALL); diff --git a/phpstan.neon.dist b/phpstan.neon.dist index a1708784..658ff649 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -7,11 +7,3 @@ parameters: # Level 9 is the highest level level: 6 - - ignoreErrors: - - - identifier: missingType.iterableValue - - # Uncomment and adjust the following lines if needed - # excludePaths: - # - ./*/*/FileToBeExcluded.php