Skip to content

[Console] Invokable command deprecations #59474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions UPGRADE-7.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ Read more about this in the [Symfony documentation](https://symfony.com/doc/7.3/

If you're upgrading from a version below 7.1, follow the [7.2 upgrade guide](UPGRADE-7.2.md) first.

Console
-------

* Omitting parameter types in callables configured via `Command::setCode` method is deprecated

*Before*
```php
$command->setCode(function ($input, $output) {
// ...
});
```

*After*
```php
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

$command->setCode(function (InputInterface $input, OutputInterface $output) {
// ...
});
```

FrameworkBundle
---------------

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ CHANGELOG
7.3
---

* Add support for invokable commands
* Add `#[Argument]` and `#[Option]` attributes to define input arguments and options for invokable commands
* Add support for invokable commands and add `#[Argument]` and `#[Option]` attributes to define input arguments and options
* Deprecate not declaring the parameter type in callable commands defined through `setCode` method

7.2
---
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public function setCode(callable $code): static
$code = $code(...);
}

$this->code = new InvokableCommand($this, $code);
$this->code = new InvokableCommand($this, $code, triggerDeprecations: true);

return $this;
}
Expand Down
19 changes: 13 additions & 6 deletions src/Symfony/Component/Console/Command/InvokableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class InvokableCommand
public function __construct(
private readonly Command $command,
private readonly \Closure $code,
private readonly bool $triggerDeprecations = false,
) {
$this->reflection = new \ReflectionFunction($code);
}
Expand All @@ -47,10 +48,13 @@ public function __invoke(InputInterface $input, OutputInterface $output): int
$statusCode = ($this->code)(...$this->getParameters($input, $output));

if (null !== $statusCode && !\is_int($statusCode)) {
// throw new LogicException(\sprintf('The command "%s" must return either void or an integer value in the "%s" method, but "%s" was returned.', $this->command->getName(), $this->reflection->getName(), get_debug_type($statusCode)));
trigger_deprecation('symfony/console', '7.3', \sprintf('Returning a non-integer value from the command "%s" is deprecated and will throw an exception in PHP 8.0.', $this->command->getName()));
if ($this->triggerDeprecations) {
trigger_deprecation('symfony/console', '7.3', \sprintf('Returning a non-integer value from the command "%s" is deprecated and will throw an exception in PHP 8.0.', $this->command->getName()));

return 0;
return 0;
}

throw new LogicException(\sprintf('The command "%s" must return either void or an integer value in the "%s" method, but "%s" was returned.', $this->command->getName(), $this->reflection->getName(), get_debug_type($statusCode)));
}

return $statusCode ?? 0;
Expand Down Expand Up @@ -92,10 +96,13 @@ private function getParameters(InputInterface $input, OutputInterface $output):
$type = $parameter->getType();

if (!$type instanceof \ReflectionNamedType) {
// throw new LogicException(\sprintf('The parameter "$%s" must have a named type. Untyped, Union or Intersection types are not supported.', $parameter->getName()));
trigger_deprecation('symfony/console', '7.3', \sprintf('Omitting the type declaration for the parameter "$%s" is deprecated and will throw an exception in PHP 8.0.', $parameter->getName()));
if ($this->triggerDeprecations) {
trigger_deprecation('symfony/console', '7.3', \sprintf('Omitting the type declaration for the parameter "$%s" is deprecated and will throw an exception in PHP 8.0.', $parameter->getName()));

continue;
continue;
}

throw new LogicException(\sprintf('The parameter "$%s" must have a named type. Untyped, Union or Intersection types are not supported.', $parameter->getName()));
}

$parameters[] = match ($type->getName()) {
Expand Down
Loading