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

@ -7,20 +7,26 @@
class FTPFake
{
/**
* @var array<array{host: string, port: string, ssl: bool}>
*/
protected array $connections = [];
/**
* @var array<array{username: string, password: string}>
*/
protected array $logins = [];
public function connect(string $host, string $port, bool $ssl = false): bool|Connection
{
$this->connections[] = compact('host', 'port', 'ssl');
$this->connections[] = ['host' => $host, 'port' => $port, 'ssl' => $ssl];
return true;
}
public function login(string $username, string $password, bool|Connection $connection): bool
{
$this->logins[] = compact('username', 'password');
$this->logins[] = ['username' => $username, 'password' => $password];
return true;
}
@ -42,7 +48,7 @@ public function delete(bool|Connection $connection, string $path): void
public function assertConnected(string $host): void
{
if (! $this->connections) {
if ($this->connections === []) {
Assert::fail('No connections are made');
}
$connected = false;
@ -55,6 +61,5 @@ public function assertConnected(string $host): void
if (! $connected) {
Assert::fail('The expected host is not connected');
}
Assert::assertTrue(true, $connected);
}
}

View File

@ -12,10 +12,9 @@ class SSHFake extends SSH
{
use ReflectsClosures;
/** @var array<string> */
protected array $commands = [];
protected ?string $output;
protected bool $connectionWillFail = false;
protected string $uploadedLocalPath;
@ -24,10 +23,7 @@ class SSHFake extends SSH
protected string $uploadedContent;
public function __construct(?string $output = null)
{
$this->output = $output;
}
public function __construct(protected ?string $output = null) {}
public function init(Server $server, ?string $asUser = null): self
{
@ -36,7 +32,7 @@ public function init(Server $server, ?string $asUser = null): self
$this->asUser = null;
$this->server = $server->refresh();
$this->user = $server->getSshUser();
if ($asUser && $asUser != $server->getSshUser()) {
if ($asUser && $asUser !== $server->getSshUser()) {
$this->asUser = $asUser;
}
@ -57,13 +53,15 @@ public function connect(bool $sftp = false): void
public function exec(string $command, string $log = '', ?int $siteId = null, ?bool $stream = false, ?callable $streamCallback = null): string
{
if (! $this->log && $log) {
$this->log = $this->server->logs()->create([
if (! $this->log instanceof \App\Models\ServerLog && $log) {
/** @var \App\Models\ServerLog $log */
$log = $this->server->logs()->create([
'site_id' => $siteId,
'name' => $this->server->id.'-'.strtotime('now').'-'.$log.'.log',
'type' => $log,
'disk' => config('core.logs_disk'),
]);
$this->log = $log;
}
$this->commands[] = $command;
@ -71,7 +69,7 @@ public function exec(string $command, string $log = '', ?int $siteId = null, ?bo
$output = $this->output ?? 'fake output';
$this->log?->write($output);
if ($stream) {
if ($stream === true) {
echo $output;
ob_flush();
flush();
@ -86,13 +84,16 @@ public function upload(string $local, string $remote, ?string $owner = null): vo
{
$this->uploadedLocalPath = $local;
$this->uploadedRemotePath = $remote;
$this->uploadedContent = file_get_contents($local);
$this->uploadedContent = file_get_contents($local) ?: '';
$this->log = null;
}
/**
* @param array<string>|string $commands
*/
public function assertExecuted(array|string $commands): void
{
if (! $this->commands) {
if ($this->commands === []) {
Assert::fail('No commands are executed');
}
if (! is_array($commands)) {
@ -107,12 +108,11 @@ public function assertExecuted(array|string $commands): void
if (! $allExecuted) {
Assert::fail('The expected commands are not executed. executed commands: '.implode(', ', $this->commands));
}
Assert::assertTrue(true, $allExecuted);
}
public function assertExecutedContains(string $command): void
{
if (! $this->commands) {
if ($this->commands === []) {
Assert::fail('No commands are executed');
}
$executed = false;
@ -127,18 +127,17 @@ public function assertExecutedContains(string $command): void
'The expected command is not executed in the executed commands: '.implode(', ', $this->commands)
);
}
Assert::assertTrue(true, $executed);
}
public function assertFileUploaded(string $toPath, ?string $content = null): void
{
if (! $this->uploadedLocalPath || ! $this->uploadedRemotePath) {
if ($this->uploadedLocalPath === '' || $this->uploadedLocalPath === '0' || ($this->uploadedRemotePath === '' || $this->uploadedRemotePath === '0')) {
Assert::fail('File is not uploaded');
}
Assert::assertEquals($toPath, $this->uploadedRemotePath);
if ($content) {
if ($content !== null && $content !== '' && $content !== '0') {
Assert::assertEquals($content, $this->uploadedContent);
}
}

View File

@ -8,13 +8,13 @@
use Illuminate\Support\Facades\Route;
use Illuminate\Validation\ValidationException;
function generate_public_key($privateKeyPath, $publicKeyPath): void
function generate_public_key(string $privateKeyPath, string $publicKeyPath): void
{
chmod($privateKeyPath, 0400);
exec("ssh-keygen -y -f {$privateKeyPath} > {$publicKeyPath}");
}
function generate_key_pair($path): void
function generate_key_pair(string $path): void
{
exec("ssh-keygen -t ed25519 -m PEM -N '' -f {$path}");
chmod($path, 0400);
@ -23,10 +23,14 @@ function generate_key_pair($path): void
/**
* @throws Exception
*/
function date_with_timezone($date, $timezone): string
function date_with_timezone(mixed $date, string $timezone): string
{
$dt = new DateTime('now', new DateTimeZone($timezone));
$dt->setTimestamp(strtotime($date));
$time = strtotime((string) $date);
if ($time === false) {
throw new Exception('Invalid date');
}
$dt->setTimestamp($time);
return $dt->format('Y-m-d H:i:s');
}
@ -42,13 +46,13 @@ function show_vito_version(): string
return $version;
}
function convert_time_format($string): string
function convert_time_format(string $string): string
{
$string = preg_replace('/(\d+)m/', '$1 minutes', $string);
$string = preg_replace('/(\d+)s/', '$1 seconds', $string);
$string = preg_replace('/(\d+)d/', '$1 days', $string);
$string = preg_replace('/(\d+)s/', '$1 seconds', (string) $string);
$string = preg_replace('/(\d+)d/', '$1 days', (string) $string);
return preg_replace('/(\d+)h/', '$1 hours', $string);
return (string) preg_replace('/(\d+)h/', '$1 hours', (string) $string);
}
function get_public_key_content(): string
@ -57,7 +61,13 @@ function get_public_key_content(): string
Artisan::call('ssh-key:generate --force');
}
return str(file_get_contents(storage_path(config('core.ssh_public_key_name'))))
$content = file_get_contents(storage_path(config('core.ssh_public_key_name')));
if ($content === false) {
return '';
}
return str($content)
->replace("\n", '')
->toString();
}
@ -68,7 +78,7 @@ function run_action(object $static, Closure $callback): void
$callback();
} catch (SSHError $e) {
$actions = [];
if ($e->getLog()) {
if ($e->getLog() instanceof \App\Models\ServerLog) {
$actions[] = Action::make('View Logs')
->url(App\Web\Pages\Servers\Logs\Index::getUrl([
'server' => $e->getLog()->server_id,
@ -102,7 +112,7 @@ function run_action(object $static, Closure $callback): void
/**
* Credit: https://gist.github.com/lorenzos/1711e81a9162320fde20
*/
function tail($filepath, $lines = 1, $adaptive = true): string
function tail(string $filepath, int $lines = 1, bool $adaptive = true): string
{
// Open file
$f = @fopen($filepath, 'rb');
@ -143,10 +153,10 @@ function tail($filepath, $lines = 1, $adaptive = true): string
$output = ($chunk = fread($f, $seek)).$output;
// Jump back to where we started reading
fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
fseek($f, -mb_strlen($chunk !== false ? $chunk : '', '8bit'), SEEK_CUR);
// Decrease our line counter
$lines -= substr_count($chunk, "\n");
$lines -= substr_count($chunk !== false ? $chunk : '', "\n");
}
// While we have too many lines