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

@ -3,6 +3,7 @@
namespace App\Models;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\File;
@ -18,12 +19,13 @@
* @property string $type
* @property string $name
* @property string $disk
* @property bool $is_remote
* @property Server $server
* @property ?Site $site
* @property bool $is_remote
*/
class ServerLog extends AbstractModel
{
/** @use HasFactory<\Database\Factories\ServerLogFactory> */
use HasFactory;
protected $fillable = [
@ -45,7 +47,7 @@ public static function boot(): void
{
parent::boot();
static::deleting(function (ServerLog $log) {
self::deleting(function (ServerLog $log): void {
if ($log->is_remote) {
try {
if (Storage::disk($log->disk)->exists($log->name)) {
@ -63,11 +65,17 @@ public function getRouteKey(): string
return 'log';
}
/**
* @return BelongsTo<Server, $this>
*/
public function server(): BelongsTo
{
return $this->belongsTo(Server::class);
}
/**
* @return BelongsTo<Site, $this>
*/
public function site(): BelongsTo
{
return $this->belongsTo(Site::class);
@ -84,7 +92,7 @@ public function download(): StreamedResponse
$this->server->ssh()->download($tmpPath, $this->name);
dispatch(function () use ($tmpPath) {
dispatch(function () use ($tmpPath): void {
if (File::exists($tmpPath)) {
File::delete($tmpPath);
}
@ -98,18 +106,22 @@ public function download(): StreamedResponse
return Storage::disk($this->disk)->download($this->name);
}
public static function getRemote($query, bool $active = true, ?Site $site = null)
/**
* @param Builder<ServerLog> $query
* @return Builder<ServerLog>
*/
public static function getRemote(Builder $query, bool $active = true, ?Site $site = null): Builder
{
$query->where('is_remote', $active);
if ($site) {
if ($site instanceof \App\Models\Site) {
$query->where('name', 'like', $site->path.'%');
}
return $query;
}
public function write($buf): void
public function write(string $buf): void
{
if (Str::contains($buf, 'VITO_SSH_ERROR')) {
$buf = str_replace('VITO_SSH_ERROR', '', $buf);
@ -121,14 +133,14 @@ public function write($buf): void
}
}
public function getContent($lines = null): ?string
public function getContent(?int $lines = null): ?string
{
if ($this->is_remote) {
return $this->server->os()->tail($this->name, $lines ?? 150);
}
if (Storage::disk($this->disk)->exists($this->name)) {
if ($lines) {
if ($lines !== null && $lines !== 0) {
return tail(Storage::disk($this->disk)->path($this->name), $lines);
}
@ -140,9 +152,9 @@ public function getContent($lines = null): ?string
return "Log file doesn't exist!";
}
public static function log(Server $server, string $type, string $content, ?Site $site = null): static
public static function log(Server $server, string $type, string $content, ?Site $site = null): ServerLog
{
$log = new static([
$log = new self([
'server_id' => $server->id,
'site_id' => $site?->id,
'name' => $server->id.'-'.strtotime('now').'-'.$type.'.log',
@ -155,9 +167,9 @@ public static function log(Server $server, string $type, string $content, ?Site
return $log;
}
public static function make(Server $server, string $type): ServerLog
public static function newLog(Server $server, string $type): ServerLog
{
return new static([
return new self([
'server_id' => $server->id,
'name' => $server->id.'-'.strtotime('now').'-'.$type.'.log',
'type' => $type,
@ -169,11 +181,11 @@ public function forSite(Site|int $site): ServerLog
{
if ($site instanceof Site) {
$site = $site->id;
return $this;
}
if (is_int($site)) {
$this->site_id = $site;
}
$this->site_id = $site;
return $this;
}