Commit 0270644
committed
feature #61677 [Console] Allow callables for the description and help of
This PR was merged into the 8.2 branch.
Discussion
----------
[Console] Allow callables for the description and help of `#[AsCommand]`, `#[Argument]` and `#[Option]`
| Q | A
| ------------- | ---
| Branch? | 8.2
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Issues | -
| License | MIT
| Doc PR | TODO
Attribute arguments are constant expressions, and PHP allows no function calls there. A description that has to be computed, typically one listing the accepted values, is therefore not expressible today:
```php
public function __invoke(
SymfonyStyle $io,
// Fatal error: Constant expression contains invalid operations
#[Argument(description: self::generateDescription())]
string $status,
): int {
}
```
This PR makes `description` on `#[Argument]` and `#[Option]`, and `description` and `help` on `#[AsCommand]`, accept a callable next to a string:
```php
public function __invoke(
SymfonyStyle $io,
#[Argument(description: [self::class, 'generateDescription'])]
string $status,
): int {
}
public static function generateDescription(): string
{
return 'One of: '.implode(', ', array_column(Status::cases(), 'value'));
}
```
The callable is called once, when the attribute is instantiated, and the attribute exposes the resolved string, so nothing downstream sees a callable.
A `string` is never resolved as a callable: `description: 'define'` is the description "define", not a call to `define()`.
On PHP 8.5 no separate method is needed, since constant expressions accept static closures and first-class callable syntax:
```php
#[Argument(description: static function (): string {
return 'One of: '.implode(', ', array_column(Status::cases(), 'value'));
})]
string $status = '',
```
Arrow functions stay rejected there, as they capture by value.
`usages` on `#[AsCommand]` is deliberately left out, since an array of strings and an array callable cannot be told apart.
Commits
-------
a6a5fa7 [Console] Allow callable for the `description` and `help` options of `#[AsCommand]`, `#[Argument]` and `#[Option]`#[AsCommand], #[Argument] and #[Option] (MacDada)6 files changed
+88-6Lines changed: 88 additions & 6 deletions
File tree
Expand file treeCollapse file tree
Open diff view settings
Filter options
- src/Symfony/Component/Console
- Attribute
- Tests/Command
Expand file treeCollapse file tree
Open diff view settings
Collapse file
src/Symfony/Component/Console/Attribute/Argument.php
Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Attribute/Argument.php+4-1Lines changed: 4 additions & 1 deletion
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
21 | 21 | |
22 | 22 | |
23 | 23 | |
| 24 | + |
24 | 25 | |
25 | 26 | |
26 | 27 | |
| ||
38 | 39 | |
39 | 40 | |
40 | 41 | |
| 42 | + |
41 | 43 | |
42 | 44 | |
43 | 45 | |
44 | | - |
| 46 | + |
45 | 47 | |
46 | 48 | |
47 | 49 | |
| 50 | + |
48 | 51 | |
49 | 52 | |
50 | 53 | |
|
Collapse file
src/Symfony/Component/Console/Attribute/AsCommand.php
Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Attribute/AsCommand.php+10-4Lines changed: 10 additions & 4 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
19 | 19 | |
20 | 20 | |
21 | 21 | |
| 22 | + |
| 23 | + |
| 24 | + |
22 | 25 | |
23 | 26 | |
24 | | - |
| 27 | + |
25 | 28 | |
26 | 29 | |
27 | | - |
| 30 | + |
28 | 31 | |
29 | 32 | |
30 | 33 | |
31 | 34 | |
32 | 35 | |
33 | | - |
| 36 | + |
34 | 37 | |
35 | 38 | |
36 | | - |
| 39 | + |
37 | 40 | |
38 | 41 | |
39 | 42 | |
| 43 | + |
| 44 | + |
| 45 | + |
40 | 46 | |
41 | 47 | |
42 | 48 | |
|
Collapse file
src/Symfony/Component/Console/Attribute/Option.php
Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Attribute/Option.php+4-1Lines changed: 4 additions & 1 deletion
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
22 | 22 | |
23 | 23 | |
24 | 24 | |
| 25 | + |
25 | 26 | |
26 | 27 | |
27 | 28 | |
| ||
42 | 43 | |
43 | 44 | |
44 | 45 | |
| 46 | + |
45 | 47 | |
46 | 48 | |
47 | 49 | |
48 | 50 | |
49 | | - |
| 51 | + |
50 | 52 | |
51 | 53 | |
52 | 54 | |
53 | 55 | |
54 | 56 | |
55 | 57 | |
| 58 | + |
56 | 59 | |
57 | 60 | |
58 | 61 | |
|
Collapse file
src/Symfony/Component/Console/CHANGELOG.md
Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/CHANGELOG.md+1Lines changed: 1 addition & 0 deletions
- Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
6 | 6 | |
7 | 7 | |
8 | 8 | |
| 9 | + |
9 | 10 | |
10 | 11 | |
11 | 12 | |
|
Collapse file
src/Symfony/Component/Console/Tests/Command/CommandTest.php
Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Command/CommandTest.php+35Lines changed: 35 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
511 | 511 | |
512 | 512 | |
513 | 513 | |
| 514 | + |
| 515 | + |
| 516 | + |
| 517 | + |
| 518 | + |
| 519 | + |
| 520 | + |
| 521 | + |
| 522 | + |
| 523 | + |
| 524 | + |
| 525 | + |
| 526 | + |
| 527 | + |
| 528 | + |
| 529 | + |
| 530 | + |
514 | 531 | |
515 | 532 | |
516 | 533 | |
| ||
559 | 576 | |
560 | 577 | |
561 | 578 | |
| 579 | + |
| 580 | + |
| 581 | + |
| 582 | + |
| 583 | + |
| 584 | + |
| 585 | + |
| 586 | + |
| 587 | + |
| 588 | + |
| 589 | + |
| 590 | + |
| 591 | + |
| 592 | + |
| 593 | + |
| 594 | + |
| 595 | + |
| 596 | + |
Collapse file
src/Symfony/Component/Console/Tests/Command/InvokableCommandTest.php
Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Command/InvokableCommandTest.php+34Lines changed: 34 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
92 | 92 | |
93 | 93 | |
94 | 94 | |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + |
95 | 124 | |
96 | 125 | |
97 | 126 | |
| ||
600 | 629 | |
601 | 630 | |
602 | 631 | |
| 632 | + |
| 633 | + |
| 634 | + |
| 635 | + |
| 636 | + |
603 | 637 | |
604 | 638 | |
605 | 639 | |
|
0 commit comments