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

@ -11,6 +11,9 @@
abstract class AbstractDatabase extends AbstractService implements Database
{
/**
* @var array<string>
*/
protected array $systemDbs = [];
protected string $defaultCharset;
@ -21,8 +24,12 @@ abstract class AbstractDatabase extends AbstractService implements Database
protected bool $removeLastRow = false;
/**
* @phpstan-return view-string
*/
protected function getScriptView(string $script): string
{
/** @phpstan-ignore-next-line */
return 'ssh.services.database.'.$this->service->name.'.'.$script;
}
@ -31,7 +38,7 @@ public function creationRules(array $input): array
return [
'type' => [
'required',
function (string $attribute, mixed $value, Closure $fail) {
function (string $attribute, mixed $value, Closure $fail): void {
$databaseExists = $this->service->server->database();
if ($databaseExists) {
$fail('You already have a database service on the server.');
@ -62,7 +69,7 @@ public function deletionRules(): array
{
return [
'service' => [
function (string $attribute, mixed $value, Closure $fail) {
function (string $attribute, mixed $value, Closure $fail): void {
$hasDatabase = $this->service->server->databases()->exists();
if ($hasDatabase) {
$fail('You have database(s) on the server.');
@ -306,6 +313,7 @@ public function syncDatabases(bool $createNew = true): void
continue;
}
/** @var ?\App\Models\Database $db */
$db = $this->service->server->databases()
->where('name', $database[0])
->first();
@ -331,6 +339,9 @@ public function syncDatabases(bool $createNew = true): void
}
}
/**
* @return array<array<string>>
*/
protected function tableToArray(string $data, bool $keepHeader = false): array
{
$lines = explode("\n", trim($data));
@ -347,7 +358,8 @@ protected function tableToArray(string $data, bool $keepHeader = false): array
$rows = [];
foreach ($lines as $line) {
$row = explode($this->separator, $line);
$separator = $this->separator === '' || $this->separator === '0' ? "\t" : $this->separator;
$row = explode($separator, $line);
$row = array_map('trim', $row);
$rows[] = $row;
}

View File

@ -3,8 +3,9 @@
namespace App\SSH\Services\Database;
use App\Models\BackupFile;
use App\SSH\Services\ServiceInterface;
interface Database
interface Database extends ServiceInterface
{
public function create(string $name, string $charset, string $collation): void;
@ -14,6 +15,9 @@ public function createUser(string $username, string $password, string $host): vo
public function deleteUser(string $username, string $host): void;
/**
* @param array<string> $databases
*/
public function link(string $username, string $host, array $databases): void;
public function unlink(string $username, string $host): void;

View File

@ -2,7 +2,9 @@
namespace App\SSH\Services\Firewall;
interface Firewall
use App\SSH\Services\ServiceInterface;
interface Firewall extends ServiceInterface
{
public function applyRules(): void;
}

View File

@ -35,7 +35,7 @@ public function applyRules(): void
->get();
$this->service->server->ssh()->exec(
view('ssh.services.firewall.ufw.apply-rules', compact('rules')),
view('ssh.services.firewall.ufw.apply-rules', ['rules' => $rules]),
'apply-rules'
);
}

View File

@ -13,7 +13,7 @@ public function creationRules(array $input): array
{
return [
'type' => [
function (string $attribute, mixed $value, Closure $fail) {
function (string $attribute, mixed $value, Closure $fail): void {
$monitoringExists = $this->service->server->monitoring();
if ($monitoringExists) {
$fail('You already have a monitoring service on the server.');

View File

@ -21,7 +21,7 @@ public function creationRules(array $input): array
{
return [
'type' => [
function (string $attribute, mixed $value, Closure $fail) {
function (string $attribute, mixed $value, Closure $fail): void {
$monitoringExists = $this->service->server->monitoring();
if ($monitoringExists) {
$fail('You already have a monitoring service on the server.');

View File

@ -27,7 +27,7 @@ public function deletionRules(): array
{
return [
'service' => [
function (string $attribute, mixed $value, Closure $fail) {
function (string $attribute, mixed $value, Closure $fail): void {
$hasSite = $this->service->server->sites()
->where('nodejs_version', $this->service->version)
->exists();

View File

@ -29,7 +29,7 @@ public function deletionRules(): array
{
return [
'service' => [
function (string $attribute, mixed $value, Closure $fail) {
function (string $attribute, mixed $value, Closure $fail): void {
$hasSite = $this->service->server->sites()
->where('php_version', $this->service->version)
->exists();
@ -88,7 +88,7 @@ public function setDefaultCli(): void
/**
* @throws SSHError
*/
public function installExtension($name): void
public function installExtension(string $name): void
{
$result = $this->service->server->ssh()->exec(
view('ssh.services.php.install-php-extension', [
@ -97,7 +97,11 @@ public function installExtension($name): void
]),
'install-php-extension-'.$name
);
$result = Str::substr($result, strpos($result, '[PHP Modules]'));
$pos = strpos($result, '[PHP Modules]');
if ($pos === false) {
throw new SSHCommandError('Failed to install extension');
}
$result = Str::substr($result, $pos);
if (! Str::contains($result, $name)) {
throw new SSHCommandError('Failed to install extension');
}
@ -127,7 +131,7 @@ public function getPHPIni(string $type): string
/**
* @throws SSHError
*/
public function createFpmPool(string $user, string $version, $site_id): void
public function createFpmPool(string $user, string $version): void
{
$this->service->server->ssh()->write(
"/etc/php/{$version}/fpm/pool.d/{$user}.conf",
@ -144,7 +148,7 @@ public function createFpmPool(string $user, string $version, $site_id): void
/**
* @throws SSHError
*/
public function removeFpmPool(string $user, string $version, $site_id): void
public function removeFpmPool(string $user, string $version, ?int $siteId): void
{
$this->service->server->ssh()->exec(
view('ssh.services.php.remove-fpm-pool', [
@ -152,7 +156,7 @@ public function removeFpmPool(string $user, string $version, $site_id): void
'version' => $version,
]),
"remove-{$version}fpm-pool-{$user}",
$site_id
$siteId
);
}
}

View File

@ -12,7 +12,7 @@ public function creationRules(array $input): array
return [
'type' => [
'required',
function (string $attribute, mixed $value, Closure $fail) {
function (string $attribute, mixed $value, Closure $fail): void {
$processManagerExists = $this->service->server->processManager();
if ($processManagerExists) {
$fail('You already have a process manager service on the server.');
@ -26,7 +26,7 @@ public function deletionRules(): array
{
return [
'service' => [
function (string $attribute, mixed $value, Closure $fail) {
function (string $attribute, mixed $value, Closure $fail): void {
$hasQueue = $this->service->server->queues()->exists();
if ($hasQueue) {
$fail('You have queue(s) on the server.');

View File

@ -2,7 +2,9 @@
namespace App\SSH\Services\ProcessManager;
interface ProcessManager
use App\SSH\Services\ServiceInterface;
interface ProcessManager extends ServiceInterface
{
public function create(
int $id,

View File

@ -14,7 +14,7 @@ public function creationRules(array $input): array
return [
'type' => [
'required',
function (string $attribute, mixed $value, Closure $fail) {
function (string $attribute, mixed $value, Closure $fail): void {
$redisExists = $this->service->server->memoryDatabase();
if ($redisExists) {
$fail('You already have a Redis service on the server.');

View File

@ -4,12 +4,26 @@
interface ServiceInterface
{
/**
* @param array<string, mixed> $input
* @return array<string, mixed>
*/
public function creationRules(array $input): array;
/**
* @param array<string, mixed> $input
* @return array<string, mixed>
*/
public function creationData(array $input): array;
/**
* @return array<string, mixed>
*/
public function deletionRules(): array;
/**
* @return array<string, mixed>
*/
public function data(): array;
public function install(): void;

View File

@ -38,7 +38,7 @@ public function deletionRules(): array
{
return [
'service' => [
function (string $attribute, mixed $value, Closure $fail) {
function (string $attribute, mixed $value, Closure $fail): void {
$hasSite = $this->service->server->sites()
->exists();
if ($hasSite) {
@ -145,7 +145,7 @@ public function deleteSite(Site $site): void
/**
* @throws SSHError
*/
public function changePHPVersion(Site $site, $version): void
public function changePHPVersion(Site $site, string $version): void
{
$this->service->server->ssh()->exec(
view('ssh.services.webserver.nginx.change-php-version', [

View File

@ -4,8 +4,9 @@
use App\Models\Site;
use App\Models\Ssl;
use App\SSH\Services\ServiceInterface;
interface Webserver
interface Webserver extends ServiceInterface
{
public function createVHost(Site $site): void;