From 56f9326b081cb5c04a6d21bec316739703635241 Mon Sep 17 00:00:00 2001 From: Gregor Harlan Date: Tue, 13 Jun 2023 09:19:00 +0200 Subject: [PATCH] Code-Modernisierung --- lib/api_protected_page.php | 6 +++--- lib/command/abstract.php | 9 +++------ lib/command/diff.php | 7 ++++--- lib/command/migrate.php | 11 ++++++----- lib/diff_file.php | 12 ++++++------ lib/handler.php | 2 ++ lib/ydeploy.php | 36 ++++++++++++------------------------ psalm.xml | 1 - 8 files changed, 36 insertions(+), 48 deletions(-) diff --git a/lib/api_protected_page.php b/lib/api_protected_page.php index b4a45cd..aa0f25c 100644 --- a/lib/api_protected_page.php +++ b/lib/api_protected_page.php @@ -5,9 +5,9 @@ */ final class rex_api_ydeploy_protected_page extends rex_api_function { - public function execute() + public function execute(): rex_api_result { - if (!rex::getUser()->isAdmin()) { + if (!rex::requireUser()->isAdmin()) { throw new rex_api_exception('Protected pages can be (un)locked only by admins.'); } @@ -50,7 +50,7 @@ public function execute() return $result; } - protected function requiresCsrfProtection() + protected function requiresCsrfProtection(): bool { return true; } diff --git a/lib/command/abstract.php b/lib/command/abstract.php index 7f6cdee..19b321b 100644 --- a/lib/command/abstract.php +++ b/lib/command/abstract.php @@ -5,15 +5,12 @@ */ abstract class rex_ydeploy_command_abstract extends rex_console_command { - /** @var rex_addon */ - protected $addon; - - /** @var string */ - protected $migrationTable; + protected readonly rex_addon $addon; + protected readonly string $migrationTable; public function __construct() { - $this->addon = rex_addon::get('ydeploy'); + $this->addon = rex_addon::require('ydeploy'); $this->migrationTable = rex::getTable('ydeploy_migration'); parent::__construct(); diff --git a/lib/command/diff.php b/lib/command/diff.php index 9cb48e4..960e3c5 100644 --- a/lib/command/diff.php +++ b/lib/command/diff.php @@ -1,5 +1,6 @@ setName('ydeploy:diff') @@ -19,7 +20,7 @@ protected function configure() ; } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { $io = $this->getStyle($input, $output); @@ -57,7 +58,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $io->success('Updated schema and fixtures files, nothing changed.'); } - return 0; + return Command::SUCCESS; } /** diff --git a/lib/command/migrate.php b/lib/command/migrate.php index c15369a..8893ca3 100644 --- a/lib/command/migrate.php +++ b/lib/command/migrate.php @@ -1,5 +1,6 @@ setName('ydeploy:migrate') @@ -18,7 +19,7 @@ protected function configure() ; } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { $io = $this->getStyle($input, $output); @@ -50,7 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output) if (!$paths) { $io->success('Nothing to migrate.'); - return 0; + return Command::SUCCESS; } $io->text(count($paths).' migrations to execute'); @@ -77,12 +78,12 @@ protected function execute(InputInterface $input, OutputInterface $output) if ($countMigrated === count($paths)) { $io->success(sprintf('%s %d migrations.', $fake ? 'Faked' : 'Executed', $countMigrated)); - return 0; + return Command::SUCCESS; } $io->error(sprintf('%s %d of %d migrations, aborted with "%s".', $fake ? 'Faked' : 'Executed', $countMigrated, count($paths), basename($path))); - return 1; + return Command::FAILURE; } } diff --git a/lib/diff_file.php b/lib/diff_file.php index 265dae9..cf5c37f 100644 --- a/lib/diff_file.php +++ b/lib/diff_file.php @@ -6,7 +6,7 @@ final class rex_ydeploy_diff_file { /** @var array */ - private $create = []; + private array $create = []; /** * @var array * }> */ - private $alter = []; + private array $alter = []; /** @var list */ - private $drop = []; + private array $drop = []; /** @var array */ - private $views = []; + private array $views = []; /** @var list */ - private $dropViews = []; + private array $dropViews = []; /** @var array>, remove?: list>}> */ - private $fixtures = []; + private array $fixtures = []; public function createTable(rex_sql_table $table): void { diff --git a/lib/handler.php b/lib/handler.php index 92c32f8..8934366 100644 --- a/lib/handler.php +++ b/lib/handler.php @@ -5,6 +5,7 @@ */ final class rex_ydeploy_handler { + /** @param rex_extension_point> $ep */ public static function addBodyClasses(rex_extension_point $ep): array { $ydeploy = rex_ydeploy::factory(); @@ -24,6 +25,7 @@ public static function addBodyClasses(rex_extension_point $ep): array return $attr; } + /** @param rex_extension_point $ep */ public static function addBadge(rex_extension_point $ep): ?string { $ydeploy = rex_ydeploy::factory(); diff --git a/lib/ydeploy.php b/lib/ydeploy.php index 416629e..31002a4 100644 --- a/lib/ydeploy.php +++ b/lib/ydeploy.php @@ -2,26 +2,14 @@ final class rex_ydeploy { - /** @var self|null */ - private static $instance; + private static ?self $instance = null; - /** @var bool */ - private $deployed; - - /** @var string|null */ - private $host; - - /** @var string|null */ - private $stage; - - /** @var string|null */ - private $branch; - - /** @var string|null */ - private $commit; - - /** @var DateTimeImmutable|null */ - private $timestamp; + private bool $deployed; + private ?string $host = null; + private ?string $stage = null; + private ?string $branch = null; + private ?string $commit = null; + private ?DateTimeImmutable $timestamp = null; private function __construct() { @@ -37,11 +25,11 @@ private function __construct() $info = rex_file::getCache($path); - $this->host = $info['host']; - $this->stage = $info['stage']; - $this->branch = $info['branch']; - $this->commit = $info['commit']; - $this->timestamp = DateTimeImmutable::createFromFormat('U', $info['timestamp']); + $this->host = rex_type::string($info['host']); + $this->stage = rex_type::string($info['stage']); + $this->branch = rex_type::string($info['branch']); + $this->commit = rex_type::string($info['commit']); + $this->timestamp = rex_type::instanceOf(DateTimeImmutable::createFromFormat('U', $info['timestamp']), DateTimeImmutable::class); } public static function factory(): self diff --git a/psalm.xml b/psalm.xml index b39b468..aa27f2a 100644 --- a/psalm.xml +++ b/psalm.xml @@ -25,6 +25,5 @@ -