Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 71d0be1

Browse files
ycerutochalasr
authored andcommitted
[Console] Invokable command deprecations
1 parent 5fca27b commit 71d0be1

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

UPGRADE-7.3.md

+22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@ Read more about this in the [Symfony documentation](https://symfony.com/doc/7.3/
88

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

11+
Console
12+
-------
13+
14+
* Omitting parameter types in callables configured via `Command::setCode` method is deprecated
15+
16+
*Before*
17+
```php
18+
$command->setCode(function ($input, $output) {
19+
// ...
20+
});
21+
```
22+
23+
*After*
24+
```php
25+
use Symfony\Component\Console\Input\InputInterface;
26+
use Symfony\Component\Console\Output\OutputInterface;
27+
28+
$command->setCode(function (InputInterface $input, OutputInterface $output) {
29+
// ...
30+
});
31+
```
32+
1133
FrameworkBundle
1234
---------------
1335

src/Symfony/Component/Console/CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ CHANGELOG
44
7.3
55
---
66

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

1010
7.2
1111
---

src/Symfony/Component/Console/Command/Command.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public function setCode(callable $code): static
328328
$code = $code(...);
329329
}
330330

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

333333
return $this;
334334
}

src/Symfony/Component/Console/Command/InvokableCommand.php

+13-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class InvokableCommand
3535
public function __construct(
3636
private readonly Command $command,
3737
private readonly \Closure $code,
38+
private readonly bool $triggerDeprecations = false,
3839
) {
3940
$this->reflection = new \ReflectionFunction($code);
4041
}
@@ -47,10 +48,13 @@ public function __invoke(InputInterface $input, OutputInterface $output): int
4748
$statusCode = ($this->code)(...$this->getParameters($input, $output));
4849

4950
if (null !== $statusCode && !\is_int($statusCode)) {
50-
// 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)));
51-
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()));
51+
if ($this->triggerDeprecations) {
52+
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()));
5253

53-
return 0;
54+
return 0;
55+
}
56+
57+
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)));
5458
}
5559

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

9498
if (!$type instanceof \ReflectionNamedType) {
95-
// throw new LogicException(\sprintf('The parameter "$%s" must have a named type. Untyped, Union or Intersection types are not supported.', $parameter->getName()));
96-
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()));
99+
if ($this->triggerDeprecations) {
100+
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()));
97101

98-
continue;
102+
continue;
103+
}
104+
105+
throw new LogicException(\sprintf('The parameter "$%s" must have a named type. Untyped, Union or Intersection types are not supported.', $parameter->getName()));
99106
}
100107

101108
$parameters[] = match ($type->getName()) {

0 commit comments

Comments
 (0)