Skip to content

Commit

Permalink
Code: add types
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jan 3, 2024
1 parent e4b13bc commit 5593061
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 35 deletions.
11 changes: 4 additions & 7 deletions src/CommandLoader/ContainerCommandLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
Expand All @@ -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);
}
Expand Down
5 changes: 2 additions & 3 deletions src/DI/ConsoleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down
16 changes: 5 additions & 11 deletions tests/fixtures/FooCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions tests/fixtures/HelperSetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
15 changes: 4 additions & 11 deletions tests/fixtures/ThrowingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5593061

Please sign in to comment.