Add phpstan level 7(#544)

This commit is contained in:
Saeed Vaziry
2025-03-12 13:31:10 +01:00
committed by GitHub
parent c22bb1fa80
commit 493cbb0849
437 changed files with 4505 additions and 2193 deletions

View File

@ -19,6 +19,7 @@
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
@ -31,43 +32,43 @@
* @property string $name
* @property string $ssh_user
* @property string $ip
* @property string $local_ip
* @property ?string $local_ip
* @property int $port
* @property string $os
* @property string $type
* @property array $type_data
* @property array<string, mixed> $type_data
* @property string $provider
* @property int $provider_id
* @property array $provider_data
* @property array $authentication
* @property array<string, mixed> $provider_data
* @property array<string, mixed> $authentication
* @property string $public_key
* @property string $status
* @property bool $auto_update
* @property int $available_updates
* @property int $security_updates
* @property int $progress
* @property string $progress_step
* @property int|float $progress
* @property ?string $progress_step
* @property Project $project
* @property User $creator
* @property ServerProvider $serverProvider
* @property ServerLog[] $logs
* @property Site[] $sites
* @property Service[] $services
* @property Database[] $databases
* @property DatabaseUser[] $databaseUsers
* @property FirewallRule[] $firewallRules
* @property CronJob[] $cronJobs
* @property Queue[] $queues
* @property Backup[] $backups
* @property Queue[] $daemons
* @property SshKey[] $sshKeys
* @property Tag[] $tags
* @property Collection<int, ServerLog> $logs
* @property Collection<int, Site> $sites
* @property Collection<int, Service> $services
* @property Collection<int, Database> $databases
* @property Collection<int, DatabaseUser> $databaseUsers
* @property Collection<int, FirewallRule> $firewallRules
* @property Collection<int, CronJob> $cronJobs
* @property Collection<int, Queue> $queues
* @property Collection<int, Backup> $backups
* @property Collection<int, SshKey> $sshKeys
* @property Collection<int, Tag> $tags
* @property string $hostname
* @property int $updates
* @property Carbon $last_update_check
* @property ?Carbon $last_update_check
*/
class Server extends AbstractModel
{
/** @use HasFactory<\Database\Factories\ServerFactory> */
use HasFactory;
protected $fillable = [
@ -106,7 +107,7 @@ class Server extends AbstractModel
'auto_update' => 'boolean',
'available_updates' => 'integer',
'security_updates' => 'integer',
'progress' => 'integer',
'progress' => 'float',
'updates' => 'integer',
'last_update_check' => 'datetime',
];
@ -119,17 +120,19 @@ public static function boot(): void
{
parent::boot();
static::deleting(function (Server $server) {
static::deleting(function (Server $server): void {
DB::beginTransaction();
try {
$server->sites()->each(function (Site $site) {
$server->sites()->each(function ($site): void {
/** @var Site $site */
$site->queues()->delete();
$site->ssls()->delete();
$site->deployments()->delete();
$site->deploymentScript()->delete();
});
$server->sites()->delete();
$server->logs()->each(function (ServerLog $log) {
$server->logs()->each(function ($log): void {
/** @var ServerLog $log */
$log->delete();
});
$server->services()->delete();
@ -155,6 +158,9 @@ public static function boot(): void
});
}
/**
* @var array<string, string>
*/
public static array $statusColors = [
ServerStatus::READY => 'success',
ServerStatus::INSTALLING => 'warning',
@ -178,76 +184,121 @@ public function isInstallationFailed(): bool
return $this->status === ServerStatus::INSTALLATION_FAILED;
}
/**
* @return BelongsTo<Project, covariant $this>
*/
public function project(): BelongsTo
{
return $this->belongsTo(Project::class, 'project_id');
}
/**
* @return BelongsTo<User, covariant $this>
*/
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
/**
* @return BelongsTo<ServerProvider, covariant $this>
*/
public function serverProvider(): BelongsTo
{
return $this->belongsTo(ServerProvider::class, 'provider_id');
}
/**
* @return HasMany<ServerLog, covariant $this>
*/
public function logs(): HasMany
{
return $this->hasMany(ServerLog::class);
}
/**
* @return HasMany<Site, covariant $this>
*/
public function sites(): HasMany
{
return $this->hasMany(Site::class);
}
/**
* @return HasMany<Service, covariant $this>
*/
public function services(): HasMany
{
return $this->hasMany(Service::class);
}
/**
* @return HasMany<Database, covariant $this>
*/
public function databases(): HasMany
{
return $this->hasMany(Database::class);
}
/**
* @return HasMany<DatabaseUser, covariant $this>
*/
public function databaseUsers(): HasMany
{
return $this->hasMany(DatabaseUser::class);
}
/**
* @return HasMany<FirewallRule, covariant $this>
*/
public function firewallRules(): HasMany
{
return $this->hasMany(FirewallRule::class);
}
/**
* @return HasMany<CronJob, covariant $this>
*/
public function cronJobs(): HasMany
{
return $this->hasMany(CronJob::class);
}
/**
* @return HasMany<Queue, covariant $this>
*/
public function queues(): HasMany
{
return $this->hasMany(Queue::class);
}
/**
* @return HasMany<Backup, covariant $this>
*/
public function backups(): HasMany
{
return $this->hasMany(Backup::class);
}
/**
* @return HasMany<Queue, covariant $this>
*/
public function daemons(): HasMany
{
return $this->queues()->whereNull('site_id');
}
/**
* @return HasMany<Metric, covariant $this>
*/
public function metrics(): HasMany
{
return $this->hasMany(Metric::class);
}
/**
* @return BelongsToMany<SshKey, covariant $this>
*/
public function sshKeys(): BelongsToMany
{
return $this->belongsToMany(SshKey::class, 'server_ssh_keys')
@ -255,6 +306,9 @@ public function sshKeys(): BelongsToMany
->withTimestamps();
}
/**
* @return MorphToMany<Tag, covariant $this>
*/
public function tags(): MorphToMany
{
return $this->morphToMany(Tag::class, 'taggable');
@ -269,6 +323,9 @@ public function getSshUser(): string
return config('core.ssh_user');
}
/**
* @return array<string>
*/
public function getSshUsers(): array
{
$users = ['root', $this->getSshUser()];
@ -278,11 +335,11 @@ public function getSshUsers(): array
return array_unique($users);
}
public function service($type, $version = null): ?Service
public function service(string $type, mixed $version = null): ?Service
{
/* @var Service $service */
/** @var ?Service $service */
$service = $this->services()
->where(function ($query) use ($type, $version) {
->where(function ($query) use ($type, $version): void {
$query->where('type', $type);
if ($version) {
$query->where('version', $version);
@ -293,9 +350,9 @@ public function service($type, $version = null): ?Service
return $service;
}
public function defaultService($type): ?Service
public function defaultService(string $type): ?Service
{
/* @var Service $service */
/** @var ?Service $service */
$service = $this->services()
->where('type', $type)
->where('is_default', 1)
@ -303,13 +360,13 @@ public function defaultService($type): ?Service
// If no default service found, get the first service with status ready or stopped
if (! $service) {
/** @var Service $service */
/** @var ?Service $service */
$service = $this->services()
->where('type', $type)
->whereIn('status', [ServiceStatus::READY, ServiceStatus::STOPPED])
->first();
if ($service) {
$service->is_default = 1;
$service->is_default = true;
$service->save();
}
}
@ -322,10 +379,14 @@ public function ssh(?string $user = null): \App\Helpers\SSH|SSHFake
return SSH::init($this, $user);
}
/**
* @return array<int, string>
*/
public function installedPHPVersions(): array
{
$versions = [];
$phps = $this->services()->where('type', 'php')->get(['version']);
/** @var Service $php */
foreach ($phps as $php) {
$versions[] = $php->version;
}
@ -333,10 +394,14 @@ public function installedPHPVersions(): array
return $versions;
}
/**
* @return array<int, string>
*/
public function installedNodejsVersions(): array
{
$versions = [];
$nodes = $this->services()->where('type', 'nodejs')->get(['version']);
/** @var Service $node */
foreach ($nodes as $node) {
$versions[] = $node->version;
}
@ -348,19 +413,25 @@ public function type(): ServerType
{
$typeClass = config('core.server_types_class')[$this->type];
return new $typeClass($this);
/** @var ServerType $type */
$type = new $typeClass($this);
return $type;
}
public function provider(): \App\ServerProviders\ServerProvider
{
$providerClass = config('core.server_providers_class')[$this->provider];
return new $providerClass($this->serverProvider, $this);
/** @var \App\ServerProviders\ServerProvider $provider */
$provider = new $providerClass($this->serverProvider ?? new ServerProvider, $this);
return $provider;
}
public function webserver(?string $version = null): ?Service
{
if (! $version) {
if ($version === null || $version === '' || $version === '0') {
return $this->defaultService('webserver');
}
@ -369,7 +440,7 @@ public function webserver(?string $version = null): ?Service
public function database(?string $version = null): ?Service
{
if (! $version) {
if ($version === null || $version === '' || $version === '0') {
return $this->defaultService('database');
}
@ -378,7 +449,7 @@ public function database(?string $version = null): ?Service
public function firewall(?string $version = null): ?Service
{
if (! $version) {
if ($version === null || $version === '' || $version === '0') {
return $this->defaultService('firewall');
}
@ -387,7 +458,7 @@ public function firewall(?string $version = null): ?Service
public function processManager(?string $version = null): ?Service
{
if (! $version) {
if ($version === null || $version === '' || $version === '0') {
return $this->defaultService('process_manager');
}
@ -396,7 +467,7 @@ public function processManager(?string $version = null): ?Service
public function php(?string $version = null): ?Service
{
if (! $version) {
if ($version === null || $version === '' || $version === '0') {
return $this->defaultService('php');
}
@ -405,7 +476,7 @@ public function php(?string $version = null): ?Service
public function nodejs(?string $version = null): ?Service
{
if (! $version) {
if ($version === null || $version === '' || $version === '0') {
return $this->defaultService('nodejs');
}
@ -414,7 +485,7 @@ public function nodejs(?string $version = null): ?Service
public function memoryDatabase(?string $version = null): ?Service
{
if (! $version) {
if ($version === null || $version === '' || $version === '0') {
return $this->defaultService('memory_database');
}
@ -423,20 +494,23 @@ public function memoryDatabase(?string $version = null): ?Service
public function monitoring(?string $version = null): ?Service
{
if (! $version) {
if ($version === null || $version === '' || $version === '0') {
return $this->defaultService('monitoring');
}
return $this->service('monitoring', $version);
}
/**
* @return array<string, string>
*/
public function sshKey(): array
{
/** @var FilesystemAdapter $storageDisk */
$storageDisk = Storage::disk(config('core.key_pairs_disk'));
return [
'public_key' => Str::replace("\n", '', Storage::disk(config('core.key_pairs_disk'))->get($this->id.'.pub')),
'public_key' => str(Storage::disk(config('core.key_pairs_disk'))->get($this->id.'.pub'))->replace("\n", '')->toString(),
'public_key_path' => $storageDisk->path($this->id.'.pub'),
'private_key_path' => $storageDisk->path((string) $this->id),
];
@ -479,7 +553,7 @@ public function checkForUpdates(): void
public function getAvailableUpdatesAttribute(?int $value): int
{
if (! $value) {
if ($value === null || $value === 0) {
return 0;
}