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

@ -59,11 +59,9 @@ class Agent extends MobileDetect
*/
public function platform()
{
return $this->retrieveUsingCacheOrResolve('paymently.platform', function () {
return $this->findDetectionRulesAgainstUserAgent(
$this->mergeRules(MobileDetect::getOperatingSystems(), static::$additionalOperatingSystems)
);
});
return $this->retrieveUsingCacheOrResolve('paymently.platform', fn () => $this->findDetectionRulesAgainstUserAgent(
$this->mergeRules(MobileDetect::getOperatingSystems(), static::$additionalOperatingSystems)
));
}
/**
@ -73,11 +71,9 @@ public function platform()
*/
public function browser()
{
return $this->retrieveUsingCacheOrResolve('paymently.browser', function () {
return $this->findDetectionRulesAgainstUserAgent(
$this->mergeRules(static::$additionalBrowsers, MobileDetect::getBrowsers())
);
});
return $this->retrieveUsingCacheOrResolve('paymently.browser', fn () => $this->findDetectionRulesAgainstUserAgent(
$this->mergeRules(static::$additionalBrowsers, MobileDetect::getBrowsers())
));
}
/**
@ -87,7 +83,7 @@ public function browser()
*/
public function isDesktop()
{
return $this->retrieveUsingCacheOrResolve('paymently.desktop', function () {
return $this->retrieveUsingCacheOrResolve('paymently.desktop', function (): bool {
// Check specifically for cloudfront headers if the useragent === 'Amazon CloudFront'
if (
$this->getUserAgent() === static::$cloudFrontUA
@ -103,19 +99,24 @@ public function isDesktop()
/**
* Match a detection rule and return the matched key.
*
* @param array<mixed, string> $rules
* @return string|null
*/
protected function findDetectionRulesAgainstUserAgent(array $rules)
{
$userAgent = $this->getUserAgent();
if ($userAgent === null || $userAgent === '' || $userAgent === '0') {
return null;
}
foreach ($rules as $key => $regex) {
if (empty($regex)) {
continue;
}
if ($this->match($regex, $userAgent)) {
return $key ?: reset($this->matchesArray);
return $key !== 0 && ($key !== '' && $key !== '0') ? $key : reset($this->matchesArray);
}
}
@ -136,7 +137,7 @@ protected function retrieveUsingCacheOrResolve(string $key, Closure $callback)
return $cacheItem;
}
return tap(call_user_func($callback), function ($result) use ($cacheKey) {
return tap(call_user_func($callback), function ($result) use ($cacheKey): void {
$this->store[$cacheKey] = $result;
});
}
@ -144,10 +145,10 @@ protected function retrieveUsingCacheOrResolve(string $key, Closure $callback)
/**
* Merge multiple rules into one array.
*
* @param array $all
* @param array<mixed> $all
* @return array<string, string>
*/
protected function mergeRules(...$all)
protected function mergeRules(...$all): array
{
$merged = [];

View File

@ -6,7 +6,7 @@
class FTP
{
public function connect(string $host, string $port, bool $ssl = false): bool|Connection
public function connect(string $host, int $port, bool $ssl = false): bool|Connection
{
if ($ssl) {
return ftp_ssl_connect($host, $port, 5);
@ -15,22 +15,22 @@ public function connect(string $host, string $port, bool $ssl = false): bool|Con
return ftp_connect($host, $port, 5);
}
public function login(string $username, string $password, bool|Connection $connection): bool
public function login(string $username, string $password, Connection $connection): bool
{
return ftp_login($connection, $username, $password);
}
public function close(bool|Connection $connection): void
public function close(Connection $connection): void
{
ftp_close($connection);
}
public function passive(bool|Connection $connection, bool $passive): bool
public function passive(Connection $connection, bool $passive): bool
{
return ftp_pasv($connection, $passive);
}
public function delete(bool|Connection $connection, string $path): bool
public function delete(Connection $connection, string $path): bool
{
return ftp_delete($connection, $path);
}

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;
}