Skip to content

Commit

Permalink
Merge pull request #1799 from spatie/chore/rector
Browse files Browse the repository at this point in the history
Add types to the codebase
  • Loading branch information
Nielsvanpach authored Jun 7, 2024
2 parents d01bf33 + 3016d99 commit ec6807c
Show file tree
Hide file tree
Showing 60 changed files with 345 additions and 188 deletions.
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -45,7 +45,8 @@
"pestphp/pest": "^1.20|^2.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.1"
"phpstan/phpstan-phpunit": "^1.1",
"rector/rector": "^1.1"
},
"autoload": {
"psr-4": {
Expand All @@ -61,9 +62,11 @@
}
},
"scripts": {
"test": "vendor/bin/pest",
"test": "vendor/bin/pest --compact",
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes",
"analyse": "vendor/bin/phpstan analyse"
"analyse": "vendor/bin/phpstan analyse",
"rector": "./vendor/bin/rector --dry-run",
"baseline": "./vendor/bin/phpstan analyse --generate-baseline --memory-limit=2G"
},
"suggest": {
"laravel/slack-notification-channel": "Required for sending notifications via Slack"
Expand Down
54 changes: 54 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,2 +1,56 @@
parameters:
ignoreErrors:
-
message: "#^Parameter \\#1 \\$callback of method Illuminate\\\\Support\\\\Collection\\<int,Spatie\\\\Backup\\\\BackupDestination\\\\Backup\\>\\:\\:filter\\(\\) expects \\(callable\\(Spatie\\\\Backup\\\\BackupDestination\\\\Backup, int\\)\\: bool\\)\\|null, Closure\\(string\\)\\: bool given\\.$#"
count: 1
path: src/BackupDestination/BackupCollection.php

-
message: "#^Parameter \\#1 \\$callback of method Illuminate\\\\Support\\\\Collection\\<int,Spatie\\\\Backup\\\\BackupDestination\\\\Backup\\>\\:\\:map\\(\\) expects callable\\(Spatie\\\\Backup\\\\BackupDestination\\\\Backup, int\\)\\: Spatie\\\\Backup\\\\BackupDestination\\\\Backup, Closure\\(string\\)\\: Spatie\\\\Backup\\\\BackupDestination\\\\Backup given\\.$#"
count: 1
path: src/BackupDestination/BackupCollection.php

-
message: "#^Unable to resolve the template type TKey in call to function collect$#"
count: 1
path: src/BackupDestination/BackupDestinationFactory.php

-
message: "#^Unable to resolve the template type TValue in call to function collect$#"
count: 1
path: src/BackupDestination/BackupDestinationFactory.php

-
message: "#^Parameter \\#1 \\$callback of method Illuminate\\\\Support\\\\Collection\\<int,int\\>\\:\\:map\\(\\) expects callable\\(int, int\\)\\: string, Closure\\(string\\)\\: non\\-falsy\\-string given\\.$#"
count: 1
path: src/Exceptions/CannotCreateDbDumper.php

-
message: "#^Method Spatie\\\\Backup\\\\Notifications\\\\Channels\\\\Discord\\\\DiscordMessage\\:\\:toArray\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Notifications/Channels/Discord/DiscordMessage.php

-
message: "#^Method Spatie\\\\Backup\\\\Tasks\\\\Backup\\\\FileSelection\\:\\:sanitize\\(\\) return type with generic class Illuminate\\\\Support\\\\Collection does not specify its types\\: TKey, TValue$#"
count: 1
path: src/Tasks/Backup/FileSelection.php

-
message: "#^Method Spatie\\\\Backup\\\\Tasks\\\\Backup\\\\Manifest\\:\\:files\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Tasks/Backup/Manifest.php

-
message: "#^Method Spatie\\\\Backup\\\\Tasks\\\\Backup\\\\Manifest\\:\\:files\\(\\) return type has no value type specified in iterable type array\\|Generator\\.$#"
count: 1
path: src/Tasks/Backup/Manifest.php

-
message: "#^Method Spatie\\\\Backup\\\\Tasks\\\\Backup\\\\Zip\\:\\:add\\(\\) has parameter \\$files with no value type specified in iterable type iterable\\.$#"
count: 1
path: src/Tasks/Backup/Zip.php

-
message: "#^Parameter \\#1 \\$callback of method Illuminate\\\\Support\\\\Collection\\<int,Spatie\\\\Backup\\\\BackupDestination\\\\Backup\\>\\:\\:each\\(\\) expects callable\\(Spatie\\\\Backup\\\\BackupDestination\\\\Backup, int\\)\\: mixed, Closure\\(Spatie\\\\Backup\\\\BackupDestination\\\\BackupCollection\\)\\: void given\\.$#"
count: 1
path: src/Tasks/Cleanup/Strategies/DefaultStrategy.php
3 changes: 1 addition & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ includes:
- phpstan-baseline.neon

parameters:
level: 5
level: 6
paths:
- src
- config
tmpDir: build/phpstan
checkOctaneCompatibility: true
checkModelProperties: true
checkMissingIterableValueType: false

ignoreErrors:
- '#Unsafe usage of new static#'
36 changes: 36 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector;
use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector;
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
use Rector\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector;
use Rector\CodingStyle\Rector\Closure\StaticClosureRector;
use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
use Rector\CodingStyle\Rector\If_\NullableCompareToNullRector;
use Rector\CodingStyle\Rector\Plus\UseIncrementAssignRector;
use Rector\CodingStyle\Rector\PostInc\PostIncDecToPreIncDecRector;
use Rector\Config\RectorConfig;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\TypeDeclaration\Rector\ArrowFunction\AddArrowFunctionReturnTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;

return RectorConfig::configure()
->withPaths(['config', 'resources', 'src'])
->withPhpSets(php81: true)
->withPreparedSets(deadCode: true, codingStyle: true, typeDeclarations: true)
->withSkip([
ReturnNeverTypeRector::class,
OptionalParametersAfterRequiredRector::class,
ClosureToArrowFunctionRector::class,
FlipTypeControlToUseExclusiveTypeRector::class,
ExplicitBoolCompareRector::class,
EncapsedStringsToSprintfRector::class,
StaticClosureRector::class,
StaticArrowFunctionRector::class,
UseIncrementAssignRector::class,
PostIncDecToPreIncDecRector::class,
NullableCompareToNullRector::class,
AddArrowFunctionReturnTypeRector::class,
]);
14 changes: 7 additions & 7 deletions resources/lang/fr/notifications.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

return [
'exception_message' => 'Message de l\'exception : :message',
'exception_trace' => 'Trace de l\'exception : :trace',
'exception_message_title' => 'Message de l\'exception',
'exception_trace_title' => 'Trace de l\'exception',
'exception_message' => "Message de l'exception : :message",
'exception_trace' => "Trace de l'exception : :trace",
'exception_message_title' => "Message de l'exception",
'exception_trace_title' => "Trace de l'exception",

'backup_failed_subject' => 'Échec de la sauvegarde de :application_name',
'backup_failed_body' => 'Important : Une erreur est survenue lors de la sauvegarde de :application_name',
Expand All @@ -27,14 +27,14 @@
'unhealthy_backup_found_subject' => 'Important : Les sauvegardes pour :application_name sont corrompues',
'unhealthy_backup_found_subject_title' => 'Important : Les sauvegardes pour :application_name sont corrompues. :problem',
'unhealthy_backup_found_body' => 'Les sauvegardes pour :application_name sur le disque :disk_name sont corrompues.',
'unhealthy_backup_found_not_reachable' => 'La destination de la sauvegarde n\'est pas accessible. :error',
'unhealthy_backup_found_empty' => 'Il n\'y a aucune sauvegarde pour cette application.',
'unhealthy_backup_found_not_reachable' => "La destination de la sauvegarde n'est pas accessible. :error",
'unhealthy_backup_found_empty' => "Il n'y a aucune sauvegarde pour cette application.",
'unhealthy_backup_found_old' => 'La dernière sauvegarde du :date est considérée trop vieille.',
'unhealthy_backup_found_unknown' => 'Désolé, une raison exacte ne peut être déterminée.',
'unhealthy_backup_found_full' => 'Les sauvegardes utilisent trop d\'espace disque. L\'utilisation actuelle est de :disk_usage alors que la limite autorisée est de :disk_limit.',

'no_backups_info' => 'Aucune sauvegarde n\'a encore été effectuée',
'application_name' => 'Nom de l\'application',
'application_name' => "Nom de l'application",
'backup_name' => 'Nom de la sauvegarde',
'disk' => 'Disque',
'newest_backup_size' => 'Taille de la sauvegarde la plus récente',
Expand Down
10 changes: 5 additions & 5 deletions resources/lang/it/notifications.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

return [
'exception_message' => 'Messaggio dell\'eccezione: :message',
'exception_trace' => 'Traccia dell\'eccezione: :trace',
'exception_message_title' => 'Messaggio dell\'eccezione',
'exception_trace_title' => 'Traccia dell\'eccezione',
'exception_message' => "Messaggio dell'eccezione: :message",
'exception_trace' => "Traccia dell'eccezione: :trace",
'exception_message_title' => "Messaggio dell'eccezione",
'exception_trace_title' => "Traccia dell'eccezione",

'backup_failed_subject' => 'Fallito il backup di :application_name',
'backup_failed_body' => 'Importante: Si è verificato un errore durante il backup di :application_name',
Expand Down Expand Up @@ -34,7 +34,7 @@
'unhealthy_backup_found_full' => 'I backup utilizzano troppa memoria. L\'utilizzo corrente è :disk_usage che è superiore al limite consentito di :disk_limit.',

'no_backups_info' => 'Non sono stati ancora effettuati backup',
'application_name' => 'Nome dell\'applicazione',
'application_name' => "Nome dell'applicazione",
'backup_name' => 'Nome di backup',
'disk' => 'Disco',
'newest_backup_size' => 'Dimensione backup più recente',
Expand Down
1 change: 1 addition & 0 deletions src/BackupDestination/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function sizeInBytes(): float
return $this->size;
}

/** @return resource */
public function stream()
{
return throw_unless(
Expand Down
2 changes: 2 additions & 0 deletions src/BackupDestination/BackupCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
use Illuminate\Support\Collection;
use Spatie\Backup\Helpers\File;

/** @extends Collection<int,Backup> */
class BackupCollection extends Collection
{
protected ?float $sizeCache = null;

/** @param array<string> $files */
public static function createFromFiles(?FileSystem $disk, array $files): self
{
return (new static($files))
Expand Down
16 changes: 3 additions & 13 deletions src/BackupDestination/BackupDestination.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,12 @@

class BackupDestination
{
protected ?Filesystem $disk;

protected string $diskName;

protected string $backupName;

public ?Exception $connectionError = null;

protected ?BackupCollection $backupCollectionCache = null;

public function __construct(Filesystem $disk = null, string $backupName, string $diskName)
public function __construct(protected ?Filesystem $disk, protected string $backupName, protected string $diskName)
{
$this->disk = $disk;

$this->diskName = $diskName;

$this->backupName = $backupName;
}

public function disk(): Filesystem
Expand All @@ -49,7 +38,7 @@ public function filesystemType(): string

$filesystemType = last(explode('\\', $adapterClass));

return strtolower($filesystemType);
return strtolower((string) $filesystemType);
}

public static function create(string $diskName, string $backupName): self
Expand Down Expand Up @@ -125,6 +114,7 @@ public function connectionError(): Exception
return $this->connectionError;
}

/** @return array<string, string> */
public function getDiskOptions(): array
{
return config("filesystems.disks.{$this->diskName()}.backup_options") ?? [];
Expand Down
4 changes: 4 additions & 0 deletions src/BackupDestination/BackupDestinationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

class BackupDestinationFactory
{
/**
* @param array<string, mixed> $config
* @return Collection<int, BackupDestination>
*/
public static function createFromArray(array $config): Collection
{
return collect($config['destination']['disks'])
Expand Down
6 changes: 3 additions & 3 deletions src/BackupServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function configurePackage(Package $package): void
]);
}

public function packageBooted()
public function packageBooted(): void
{
$this->app['events']->subscribe(EventHandler::class);

Expand All @@ -43,7 +43,7 @@ public function packageBooted()
}
}

public function packageRegistered()
public function packageRegistered(): void
{
$this->app->singleton(ConsoleOutput::class);

Expand All @@ -52,7 +52,7 @@ public function packageRegistered()
$this->registerDiscordChannel();
}

protected function registerDiscordChannel()
protected function registerDiscordChannel(): void
{
Notification::resolved(function (ChannelManager $service) {
$service->extend('discord', function ($app) {
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/BackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function handle(): int
if ($this->option('only-db')) {
$backupJob->dontBackupFilesystem();
}

if ($this->option('db-name')) {
$backupJob->onlyDbName($this->option('db-name'));
}
Expand Down Expand Up @@ -94,7 +95,7 @@ public function handle(): int
}
}

protected function guardAgainstInvalidOptions()
protected function guardAgainstInvalidOptions(): void
{
if (! $this->option('only-db')) {
return;
Expand Down
4 changes: 3 additions & 1 deletion src/Commands/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

abstract class BaseCommand extends SignalAwareCommand
{
/** @var array<int> */
protected array $handlesSignals = [];

public function __construct()
Expand All @@ -30,9 +31,10 @@ public function run(InputInterface $input, OutputInterface $output): int

protected function runningInConsole(): bool
{
return in_array(php_sapi_name(), ['cli', 'phpdbg']);
return in_array(PHP_SAPI, ['cli', 'phpdbg']);
}

/** @return array<int> */
public function getSubscribedSignals(): array
{
return $this->handlesSignals;
Expand Down
6 changes: 1 addition & 5 deletions src/Commands/CleanupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ class CleanupCommand extends BaseCommand implements Isolatable
/** @var string */
protected $description = 'Remove all backups older than specified number of days in config.';

protected CleanupStrategy $strategy;

public function __construct(CleanupStrategy $strategy)
public function __construct(protected CleanupStrategy $strategy)
{
parent::__construct();

$this->strategy = $strategy;
}

public function handle(): int
Expand Down
Loading

0 comments on commit ec6807c

Please sign in to comment.