From 55930614334942742e82daee7431209a1e1af401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Felix=20=C5=A0ulc?= Date: Wed, 3 Jan 2024 16:29:58 +0100 Subject: [PATCH] Code: add types --- src/CommandLoader/ContainerCommandLoader.php | 11 ++++------- src/DI/ConsoleExtension.php | 5 ++--- tests/fixtures/FooCommand.php | 16 +++++----------- tests/fixtures/HelperSetCommand.php | 7 ++++--- tests/fixtures/ThrowingCommand.php | 15 ++++----------- 5 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/CommandLoader/ContainerCommandLoader.php b/src/CommandLoader/ContainerCommandLoader.php index bce31a2..3117d84 100644 --- a/src/CommandLoader/ContainerCommandLoader.php +++ b/src/CommandLoader/ContainerCommandLoader.php @@ -10,11 +10,10 @@ class ContainerCommandLoader implements CommandLoaderInterface { - /** @var Container */ - private $container; + private Container $container; /** @var string[] */ - private $commandMap; + private array $commandMap; /** * @param string[] $commandMap @@ -28,11 +27,10 @@ public function __construct(Container $container, array $commandMap) /** * Loads a command. * - * @param string $name * @throws CommandNotFoundException * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint */ - public function get($name): Command + public function get(string $name): Command { if (!$this->has($name)) { throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name)); @@ -44,10 +42,9 @@ public function get($name): Command /** * Checks if a command exists. * - * @param string $name * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint */ - public function has($name): bool + public function has(string $name): bool { return array_key_exists($name, $this->commandMap); } diff --git a/src/DI/ConsoleExtension.php b/src/DI/ConsoleExtension.php index 211adbc..dd8944d 100644 --- a/src/DI/ConsoleExtension.php +++ b/src/DI/ConsoleExtension.php @@ -30,8 +30,7 @@ class ConsoleExtension extends CompilerExtension public const COMMAND_TAG = 'console.command'; - /** @var bool */ - private $cliMode; + private bool $cliMode; public function __construct(bool $cliMode = false) { @@ -177,7 +176,7 @@ public function beforeCompile(): void } } else { // Parse it from static property - $entry['name'] = call_user_func([$service->getType(), 'getDefaultName']); + $entry['name'] = call_user_func([$service->getType(), 'getDefaultName']); // @phpstan-ignore-line } // Validate command name diff --git a/tests/fixtures/FooCommand.php b/tests/fixtures/FooCommand.php index 9f30ecb..413543d 100644 --- a/tests/fixtures/FooCommand.php +++ b/tests/fixtures/FooCommand.php @@ -2,24 +2,18 @@ namespace Tests\Fixtures; +use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +#[AsCommand( + name: 'app:foo', + description: 'Foo command', +)] final class FooCommand extends Command { - /** @var string */ - protected static $defaultName = 'app:foo'; - - /** - * Configure command - */ - protected function configure(): void - { - $this->setName('foo'); - } - protected function execute(InputInterface $input, OutputInterface $output): int { return 0; diff --git a/tests/fixtures/HelperSetCommand.php b/tests/fixtures/HelperSetCommand.php index 5e84837..3cf6fc3 100644 --- a/tests/fixtures/HelperSetCommand.php +++ b/tests/fixtures/HelperSetCommand.php @@ -2,16 +2,17 @@ namespace Tests\Fixtures; +use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +#[AsCommand( + name: 'helper-set' +)] final class HelperSetCommand extends Command { - /** - * Configure command - */ protected function configure(): void { $this->setName('helper-set'); diff --git a/tests/fixtures/ThrowingCommand.php b/tests/fixtures/ThrowingCommand.php index d551ddb..e00bd9e 100644 --- a/tests/fixtures/ThrowingCommand.php +++ b/tests/fixtures/ThrowingCommand.php @@ -3,26 +3,19 @@ namespace Tests\Fixtures; use Exception; +use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +#[AsCommand( + name: 'throwing' +)] final class ThrowingCommand extends Command { - /** @var string */ - protected static $defaultName = 'throwing'; - public const ERROR_MESSAGE = 'I am internally broken.'; - /** - * Configure command - */ - protected function configure(): void - { - $this->setName(self::$defaultName); - } - protected function execute(InputInterface $input, OutputInterface $output): int { throw new Exception(self::ERROR_MESSAGE);