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

@ -23,13 +23,13 @@ class SSH
{
public Server $server;
public ?ServerLog $log;
public ?ServerLog $log = null;
protected SSH2|SFTP|null $connection = null;
protected ?string $user;
protected string $user = '';
protected ?string $asUser;
protected ?string $asUser = null;
protected string $publicKey;
@ -42,11 +42,11 @@ 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;
}
$this->privateKey = PublicKeyLoader::loadPrivateKey(
file_get_contents($this->server->sshKey()['private_key_path'])
(string) file_get_contents($this->server->sshKey()['private_key_path'])
);
return $this;
@ -94,9 +94,9 @@ 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 = ServerLog::make($this->server, $log);
if ($siteId) {
if (! $this->log instanceof ServerLog && $log) {
$this->log = ServerLog::newLog($this->server, $log);
if ($siteId !== null && $siteId !== 0) {
$this->log->forSite($siteId);
}
$this->log->save();
@ -111,14 +111,15 @@ public function exec(string $command, string $log = '', ?int $siteId = null, ?bo
}
try {
if ($this->asUser) {
if ($this->asUser !== null && $this->asUser !== '' && $this->asUser !== '0') {
$command = addslashes($command);
$command = str_replace('\\\'', '\'', $command);
$command = 'sudo su - '.$this->asUser.' -c '.'"'.trim($command).'"';
}
$this->connection->setTimeout(0);
if ($stream) {
if ($stream === true) {
/** @var callable $streamCallback */
$this->connection->exec($command, function ($output) use ($streamCallback) {
$this->log?->write($output);
@ -126,22 +127,20 @@ public function exec(string $command, string $log = '', ?int $siteId = null, ?bo
});
return '';
} else {
$output = '';
$this->connection->exec($command, function ($out) use (&$output) {
$this->log?->write($out);
$output .= $out;
});
if ($this->connection->getExitStatus() !== 0 || Str::contains($output, 'VITO_SSH_ERROR')) {
throw new SSHCommandError(
message: 'SSH command failed with an error',
log: $this->log
);
}
return $output;
}
$output = '';
$this->connection->exec($command, function (string $out) use (&$output): void {
$this->log?->write($out);
$output .= $out;
});
if ($this->connection->getExitStatus() !== 0 || Str::contains($output, 'VITO_SSH_ERROR')) {
throw new SSHCommandError(
message: 'SSH command failed with an error',
log: $this->log
);
}
return $output;
} catch (Throwable $e) {
Log::error('Error executing command', [
'msg' => $e->getMessage(),
@ -168,10 +167,11 @@ public function upload(string $local, string $remote, ?string $owner = null): vo
$tmpName = Str::random(10).strtotime('now');
$tempPath = home_path($this->user).'/'.$tmpName;
/** @phpstan-ignore-next-line */
$this->connection->put($tempPath, $local, SFTP::SOURCE_LOCAL_FILE);
$this->exec(sprintf('sudo mv %s %s', $tempPath, $remote));
if (! $owner) {
if ($owner === null || $owner === '' || $owner === '0') {
$owner = $this->user;
}
$this->exec(sprintf('sudo chown %s:%s %s', $owner, $owner, $remote));
@ -189,6 +189,7 @@ public function download(string $local, string $remote): void
$this->connect(true);
}
/** @phpstan-ignore-next-line */
$this->connection->get($remote, $local);
}
@ -220,7 +221,7 @@ public function write(string $remotePath, string $content, ?string $owner = null
*/
public function disconnect(): void
{
if ($this->connection) {
if ($this->connection instanceof SSH2) {
$this->connection->disconnect();
$this->connection = null;
}