Skip to content

Commit ec6807c

Browse files
authored
Merge pull request #1799 from spatie/chore/rector
Add types to the codebase
2 parents d01bf33 + 3016d99 commit ec6807c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+345
-188
lines changed

composer.json

+8-5
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
"spatie/laravel-signal-aware-command": "^1.2|^2.0",
3333
"spatie/temporary-directory": "^2.0",
3434
"symfony/console": "^6.0|^7.0",
35-
"symfony/finder": "^6.0|^7.0"
35+
"symfony/finder": "^6.0|^7.0",
36+
"ext-pcntl": "*"
3637
},
3738
"require-dev": {
38-
"ext-pcntl": "*",
3939
"composer-runtime-api": "^2.0",
4040
"larastan/larastan": "^2.7.0",
4141
"laravel/slack-notification-channel": "^2.5|^3.0",
@@ -45,7 +45,8 @@
4545
"pestphp/pest": "^1.20|^2.0",
4646
"phpstan/extension-installer": "^1.1",
4747
"phpstan/phpstan-deprecation-rules": "^1.0",
48-
"phpstan/phpstan-phpunit": "^1.1"
48+
"phpstan/phpstan-phpunit": "^1.1",
49+
"rector/rector": "^1.1"
4950
},
5051
"autoload": {
5152
"psr-4": {
@@ -61,9 +62,11 @@
6162
}
6263
},
6364
"scripts": {
64-
"test": "vendor/bin/pest",
65+
"test": "vendor/bin/pest --compact",
6566
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes",
66-
"analyse": "vendor/bin/phpstan analyse"
67+
"analyse": "vendor/bin/phpstan analyse",
68+
"rector": "./vendor/bin/rector --dry-run",
69+
"baseline": "./vendor/bin/phpstan analyse --generate-baseline --memory-limit=2G"
6770
},
6871
"suggest": {
6972
"laravel/slack-notification-channel": "Required for sending notifications via Slack"

phpstan-baseline.neon

+54
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,56 @@
11
parameters:
22
ignoreErrors:
3+
-
4+
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\\.$#"
5+
count: 1
6+
path: src/BackupDestination/BackupCollection.php
7+
8+
-
9+
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\\.$#"
10+
count: 1
11+
path: src/BackupDestination/BackupCollection.php
12+
13+
-
14+
message: "#^Unable to resolve the template type TKey in call to function collect$#"
15+
count: 1
16+
path: src/BackupDestination/BackupDestinationFactory.php
17+
18+
-
19+
message: "#^Unable to resolve the template type TValue in call to function collect$#"
20+
count: 1
21+
path: src/BackupDestination/BackupDestinationFactory.php
22+
23+
-
24+
message: "#^Parameter \\#1 \\$callback of method Illuminate\\\\Support\\\\Collection\\<int,int\\>\\:\\:map\\(\\) expects callable\\(int, int\\)\\: string, Closure\\(string\\)\\: non\\-falsy\\-string given\\.$#"
25+
count: 1
26+
path: src/Exceptions/CannotCreateDbDumper.php
27+
28+
-
29+
message: "#^Method Spatie\\\\Backup\\\\Notifications\\\\Channels\\\\Discord\\\\DiscordMessage\\:\\:toArray\\(\\) return type has no value type specified in iterable type array\\.$#"
30+
count: 1
31+
path: src/Notifications/Channels/Discord/DiscordMessage.php
32+
33+
-
34+
message: "#^Method Spatie\\\\Backup\\\\Tasks\\\\Backup\\\\FileSelection\\:\\:sanitize\\(\\) return type with generic class Illuminate\\\\Support\\\\Collection does not specify its types\\: TKey, TValue$#"
35+
count: 1
36+
path: src/Tasks/Backup/FileSelection.php
37+
38+
-
39+
message: "#^Method Spatie\\\\Backup\\\\Tasks\\\\Backup\\\\Manifest\\:\\:files\\(\\) return type has no value type specified in iterable type array\\.$#"
40+
count: 1
41+
path: src/Tasks/Backup/Manifest.php
42+
43+
-
44+
message: "#^Method Spatie\\\\Backup\\\\Tasks\\\\Backup\\\\Manifest\\:\\:files\\(\\) return type has no value type specified in iterable type array\\|Generator\\.$#"
45+
count: 1
46+
path: src/Tasks/Backup/Manifest.php
47+
48+
-
49+
message: "#^Method Spatie\\\\Backup\\\\Tasks\\\\Backup\\\\Zip\\:\\:add\\(\\) has parameter \\$files with no value type specified in iterable type iterable\\.$#"
50+
count: 1
51+
path: src/Tasks/Backup/Zip.php
52+
53+
-
54+
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\\.$#"
55+
count: 1
56+
path: src/Tasks/Cleanup/Strategies/DefaultStrategy.php

phpstan.neon.dist

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ includes:
22
- phpstan-baseline.neon
33

44
parameters:
5-
level: 5
5+
level: 6
66
paths:
77
- src
88
- config
99
tmpDir: build/phpstan
1010
checkOctaneCompatibility: true
1111
checkModelProperties: true
12-
checkMissingIterableValueType: false
1312

1413
ignoreErrors:
1514
- '#Unsafe usage of new static#'

rector.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector;
6+
use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector;
7+
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
8+
use Rector\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector;
9+
use Rector\CodingStyle\Rector\Closure\StaticClosureRector;
10+
use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
11+
use Rector\CodingStyle\Rector\If_\NullableCompareToNullRector;
12+
use Rector\CodingStyle\Rector\Plus\UseIncrementAssignRector;
13+
use Rector\CodingStyle\Rector\PostInc\PostIncDecToPreIncDecRector;
14+
use Rector\Config\RectorConfig;
15+
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
16+
use Rector\TypeDeclaration\Rector\ArrowFunction\AddArrowFunctionReturnTypeRector;
17+
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;
18+
19+
return RectorConfig::configure()
20+
->withPaths(['config', 'resources', 'src'])
21+
->withPhpSets(php81: true)
22+
->withPreparedSets(deadCode: true, codingStyle: true, typeDeclarations: true)
23+
->withSkip([
24+
ReturnNeverTypeRector::class,
25+
OptionalParametersAfterRequiredRector::class,
26+
ClosureToArrowFunctionRector::class,
27+
FlipTypeControlToUseExclusiveTypeRector::class,
28+
ExplicitBoolCompareRector::class,
29+
EncapsedStringsToSprintfRector::class,
30+
StaticClosureRector::class,
31+
StaticArrowFunctionRector::class,
32+
UseIncrementAssignRector::class,
33+
PostIncDecToPreIncDecRector::class,
34+
NullableCompareToNullRector::class,
35+
AddArrowFunctionReturnTypeRector::class,
36+
]);

resources/lang/fr/notifications.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

33
return [
4-
'exception_message' => 'Message de l\'exception : :message',
5-
'exception_trace' => 'Trace de l\'exception : :trace',
6-
'exception_message_title' => 'Message de l\'exception',
7-
'exception_trace_title' => 'Trace de l\'exception',
4+
'exception_message' => "Message de l'exception : :message",
5+
'exception_trace' => "Trace de l'exception : :trace",
6+
'exception_message_title' => "Message de l'exception",
7+
'exception_trace_title' => "Trace de l'exception",
88

99
'backup_failed_subject' => 'Échec de la sauvegarde de :application_name',
1010
'backup_failed_body' => 'Important : Une erreur est survenue lors de la sauvegarde de :application_name',
@@ -27,14 +27,14 @@
2727
'unhealthy_backup_found_subject' => 'Important : Les sauvegardes pour :application_name sont corrompues',
2828
'unhealthy_backup_found_subject_title' => 'Important : Les sauvegardes pour :application_name sont corrompues. :problem',
2929
'unhealthy_backup_found_body' => 'Les sauvegardes pour :application_name sur le disque :disk_name sont corrompues.',
30-
'unhealthy_backup_found_not_reachable' => 'La destination de la sauvegarde n\'est pas accessible. :error',
31-
'unhealthy_backup_found_empty' => 'Il n\'y a aucune sauvegarde pour cette application.',
30+
'unhealthy_backup_found_not_reachable' => "La destination de la sauvegarde n'est pas accessible. :error",
31+
'unhealthy_backup_found_empty' => "Il n'y a aucune sauvegarde pour cette application.",
3232
'unhealthy_backup_found_old' => 'La dernière sauvegarde du :date est considérée trop vieille.',
3333
'unhealthy_backup_found_unknown' => 'Désolé, une raison exacte ne peut être déterminée.',
3434
'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.',
3535

3636
'no_backups_info' => 'Aucune sauvegarde n\'a encore été effectuée',
37-
'application_name' => 'Nom de l\'application',
37+
'application_name' => "Nom de l'application",
3838
'backup_name' => 'Nom de la sauvegarde',
3939
'disk' => 'Disque',
4040
'newest_backup_size' => 'Taille de la sauvegarde la plus récente',

resources/lang/it/notifications.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

33
return [
4-
'exception_message' => 'Messaggio dell\'eccezione: :message',
5-
'exception_trace' => 'Traccia dell\'eccezione: :trace',
6-
'exception_message_title' => 'Messaggio dell\'eccezione',
7-
'exception_trace_title' => 'Traccia dell\'eccezione',
4+
'exception_message' => "Messaggio dell'eccezione: :message",
5+
'exception_trace' => "Traccia dell'eccezione: :trace",
6+
'exception_message_title' => "Messaggio dell'eccezione",
7+
'exception_trace_title' => "Traccia dell'eccezione",
88

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

3636
'no_backups_info' => 'Non sono stati ancora effettuati backup',
37-
'application_name' => 'Nome dell\'applicazione',
37+
'application_name' => "Nome dell'applicazione",
3838
'backup_name' => 'Nome di backup',
3939
'disk' => 'Disco',
4040
'newest_backup_size' => 'Dimensione backup più recente',

src/BackupDestination/Backup.php

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function sizeInBytes(): float
6666
return $this->size;
6767
}
6868

69+
/** @return resource */
6970
public function stream()
7071
{
7172
return throw_unless(

src/BackupDestination/BackupCollection.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
use Illuminate\Support\Collection;
77
use Spatie\Backup\Helpers\File;
88

9+
/** @extends Collection<int,Backup> */
910
class BackupCollection extends Collection
1011
{
1112
protected ?float $sizeCache = null;
1213

14+
/** @param array<string> $files */
1315
public static function createFromFiles(?FileSystem $disk, array $files): self
1416
{
1517
return (new static($files))

src/BackupDestination/BackupDestination.php

+3-13
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,12 @@
1010

1111
class BackupDestination
1212
{
13-
protected ?Filesystem $disk;
14-
15-
protected string $diskName;
16-
17-
protected string $backupName;
18-
1913
public ?Exception $connectionError = null;
2014

2115
protected ?BackupCollection $backupCollectionCache = null;
2216

23-
public function __construct(Filesystem $disk = null, string $backupName, string $diskName)
17+
public function __construct(protected ?Filesystem $disk, protected string $backupName, protected string $diskName)
2418
{
25-
$this->disk = $disk;
26-
27-
$this->diskName = $diskName;
28-
29-
$this->backupName = $backupName;
3019
}
3120

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

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

52-
return strtolower($filesystemType);
41+
return strtolower((string) $filesystemType);
5342
}
5443

5544
public static function create(string $diskName, string $backupName): self
@@ -125,6 +114,7 @@ public function connectionError(): Exception
125114
return $this->connectionError;
126115
}
127116

117+
/** @return array<string, string> */
128118
public function getDiskOptions(): array
129119
{
130120
return config("filesystems.disks.{$this->diskName()}.backup_options") ?? [];

src/BackupDestination/BackupDestinationFactory.php

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
class BackupDestinationFactory
88
{
9+
/**
10+
* @param array<string, mixed> $config
11+
* @return Collection<int, BackupDestination>
12+
*/
913
public static function createFromArray(array $config): Collection
1014
{
1115
return collect($config['destination']['disks'])

src/BackupServiceProvider.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function configurePackage(Package $package): void
3434
]);
3535
}
3636

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

@@ -43,7 +43,7 @@ public function packageBooted()
4343
}
4444
}
4545

46-
public function packageRegistered()
46+
public function packageRegistered(): void
4747
{
4848
$this->app->singleton(ConsoleOutput::class);
4949

@@ -52,7 +52,7 @@ public function packageRegistered()
5252
$this->registerDiscordChannel();
5353
}
5454

55-
protected function registerDiscordChannel()
55+
protected function registerDiscordChannel(): void
5656
{
5757
Notification::resolved(function (ChannelManager $service) {
5858
$service->extend('discord', function ($app) {

src/Commands/BackupCommand.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function handle(): int
3636
if ($this->option('only-db')) {
3737
$backupJob->dontBackupFilesystem();
3838
}
39+
3940
if ($this->option('db-name')) {
4041
$backupJob->onlyDbName($this->option('db-name'));
4142
}
@@ -94,7 +95,7 @@ public function handle(): int
9495
}
9596
}
9697

97-
protected function guardAgainstInvalidOptions()
98+
protected function guardAgainstInvalidOptions(): void
9899
{
99100
if (! $this->option('only-db')) {
100101
return;

src/Commands/BaseCommand.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
abstract class BaseCommand extends SignalAwareCommand
1212
{
13+
/** @var array<int> */
1314
protected array $handlesSignals = [];
1415

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

3132
protected function runningInConsole(): bool
3233
{
33-
return in_array(php_sapi_name(), ['cli', 'phpdbg']);
34+
return in_array(PHP_SAPI, ['cli', 'phpdbg']);
3435
}
3536

37+
/** @return array<int> */
3638
public function getSubscribedSignals(): array
3739
{
3840
return $this->handlesSignals;

src/Commands/CleanupCommand.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,9 @@ class CleanupCommand extends BaseCommand implements Isolatable
2020
/** @var string */
2121
protected $description = 'Remove all backups older than specified number of days in config.';
2222

23-
protected CleanupStrategy $strategy;
24-
25-
public function __construct(CleanupStrategy $strategy)
23+
public function __construct(protected CleanupStrategy $strategy)
2624
{
2725
parent::__construct();
28-
29-
$this->strategy = $strategy;
3026
}
3127

3228
public function handle(): int

0 commit comments

Comments
 (0)