Skip to content

Commit

Permalink
add more types
Browse files Browse the repository at this point in the history
  • Loading branch information
Nielsvanpach committed Jun 7, 2024
1 parent 1b1fa88 commit a93a5b8
Show file tree
Hide file tree
Showing 34 changed files with 195 additions and 49 deletions.
4 changes: 2 additions & 2 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 Down
56 changes: 56 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +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
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ includes:
- phpstan-baseline.neon

parameters:
level: 5
level: 6
paths:
- src
- config
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
1 change: 1 addition & 0 deletions src/BackupDestination/BackupDestination.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,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
2 changes: 1 addition & 1 deletion src/BackupServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/BackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function handle(): int
}
}

protected function guardAgainstInvalidOptions()
protected function guardAgainstInvalidOptions(): void
{
if (! $this->option('only-db')) {
return;
Expand Down
2 changes: 2 additions & 0 deletions 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 @@ -33,6 +34,7 @@ protected function runningInConsole(): bool
return in_array(PHP_SAPI, ['cli', 'phpdbg']);
}

/** @return array<int> */
public function getSubscribedSignals(): array
{
return $this->handlesSignals;
Expand Down
11 changes: 8 additions & 3 deletions src/Commands/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public function handle(): int
return static::SUCCESS;
}

protected function displayOverview(Collection $backupDestinationStatuses)
/**
* @param Collection<int, BackupDestinationStatus> $backupDestinationStatuses
*/
protected function displayOverview(Collection $backupDestinationStatuses): static
{
$headers = ['Name', 'Disk', 'Reachable', 'Healthy', '# of backups', 'Newest backup', 'Used storage'];

Expand All @@ -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();
Expand Down Expand Up @@ -73,7 +77,8 @@ public function convertToRow(BackupDestinationStatus $backupDestinationStatus):
return $row;
}

protected function displayFailures(Collection $backupDestinationStatuses)
/** @param Collection<int, BackupDestinationStatus> $backupDestinationStatuses */
protected function displayFailures(Collection $backupDestinationStatuses): static
{
$failed = $backupDestinationStatuses
->filter(function (BackupDestinationStatus $backupDestinationStatus) {
Expand All @@ -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'
Expand Down
8 changes: 5 additions & 3 deletions src/Exceptions/CannotCreateDbDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, string> $supportedDrivers */
$supportedDrivers = config('database.connections');

$formattedSupportedDrivers = $supportedDrivers
$formattedSupportedDrivers = collect($supportedDrivers)
->keys()
->map(fn (string $supportedDriver) => "`{$supportedDriver}`")
->join(glue: ', ', finalGlue: ' or ');

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/NotificationCouldNotBeSent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class NotificationCouldNotBeSent extends Exception
{
public static function noNotificationClassForEvent($event): self
public static function noNotificationClassForEvent(object $event): static
{
$eventClass = $event::class;

Expand Down
3 changes: 2 additions & 1 deletion src/Helpers/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public function setCommand(Command $command): void
$this->command = $command;
}

public function __call(string $method, array $arguments)
/** @param array<int, mixed> $arguments */
public function __call(string $method, array $arguments): void
{
$consoleOutput = app(static::class);

Expand Down
1 change: 1 addition & 0 deletions src/Helpers/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class File
{
/** @var array<string> */
protected static array $allowedMimeTypes = [
'application/zip',
'application/x-zip',
Expand Down
2 changes: 2 additions & 0 deletions src/Notifications/BaseNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

abstract class BaseNotification extends Notification
{
/** @return array<string, string> */
public function via(): array
{
$notificationChannels = config('backup.notifications.notifications.'.static::class);
Expand All @@ -34,6 +35,7 @@ public function diskName(): string
return $this->backupDestination()->diskName();
}

/** @return Collection<string, string> */
protected function backupDestinationProperties(): Collection
{
$backupDestination = $this->backupDestination();
Expand Down
3 changes: 2 additions & 1 deletion src/Notifications/Channels/Discord/DiscordChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Http;
use Spatie\Backup\Notifications\Notifiable;

class DiscordChannel
{
public function send($notifiable, Notification $notification): void
public function send(Notifiable $notifiable, Notification $notification): void
{
$discordMessage = $notification->toDiscord(); // @phpstan-ignore-line

Expand Down
4 changes: 3 additions & 1 deletion src/Notifications/Channels/Discord/DiscordMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class DiscordMessage

protected string $description = '';

/** @var array<string> */
protected array $fields = [];

protected ?string $timestamp = null;
Expand Down Expand Up @@ -48,7 +49,7 @@ public function url(string $url): self
return $this;
}

public function title($title): self
public function title(string $title): self
{
$this->title = $title;

Expand Down Expand Up @@ -97,6 +98,7 @@ public function error(): self
return $this;
}

/** @param array<string, string> $fields */
public function fields(array $fields, bool $inline = true): self
{
foreach ($fields as $label => $value) {
Expand Down
10 changes: 7 additions & 3 deletions src/Notifications/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,21 @@ public function subscribe(Dispatcher $events): void
});
}

protected function determineNotifiable()
protected function determineNotifiable(): Notifiable
{
$notifiableClass = $this->config->get('backup.notifications.notifiable');

return app($notifiableClass);
}

protected function determineNotification($event): Notification
protected function determineNotification(object $event): Notification
{
$lookingForNotificationClass = class_basename($event).'Notification';

$notificationClass = collect($this->config->get('backup.notifications.notifications'))
/** @var array<class-string, array<int, string>> $notificationClasses */
$notificationClasses = $this->config->get('backup.notifications.notifications');

$notificationClass = collect($notificationClasses)
->keys()
->first(fn (string $notificationClass) => class_basename($notificationClass) === $lookingForNotificationClass);

Expand All @@ -53,6 +56,7 @@ protected function determineNotification($event): Notification
return new $notificationClass($event);
}

/** @return array<int, class-string> */
protected function allBackupEventClasses(): array
{
return [
Expand Down
1 change: 1 addition & 0 deletions src/Notifications/Notifiable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Notifiable
{
use NotifiableTrait;

/** @return string|array{int, string} */
public function routeNotificationForMail(): string|array
{
return config('backup.notifications.mail.to');
Expand Down
Loading

0 comments on commit a93a5b8

Please sign in to comment.