Skip to content

Commit 01d2d3e

Browse files
committed
feature #45361 [Console] Deprecate the $defaultName property (derrabus)
This PR was merged into the 6.1 branch. Discussion ---------- [Console] Deprecate the `$defaultName` property | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | yes | Tickets | Part of #43600 | License | MIT | Doc PR | TODO Commits ------- ca3458c Deprecate the $defaultName property
2 parents d88c30d + ca3458c commit 01d2d3e

File tree

12 files changed

+120
-10
lines changed

12 files changed

+120
-10
lines changed

UPGRADE-6.1.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
UPGRADE FROM 6.0 to 6.1
22
=======================
33

4+
Console
5+
-------
6+
7+
* Deprecate `Command::$defaultName` and `Command::$defaultDescription`, use the `AsCommand` attribute instead.
8+
49
Serializer
510
----------
611

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add method `__toString()` to `InputInterface`
8+
* Deprecate `Command::$defaultName` and `Command::$defaultDescription`, use the `AsCommand` attribute instead.
89

910
6.0
1011
---

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,15 @@ class Command
3939

4040
/**
4141
* @var string|null The default command name
42+
*
43+
* @deprecated since Symfony 6.1, use the AsCommand attribute instead
4244
*/
4345
protected static $defaultName;
4446

4547
/**
4648
* @var string|null The default command description
49+
*
50+
* @deprecated since Symfony 6.1, use the AsCommand attribute instead
4751
*/
4852
protected static $defaultDescription;
4953

@@ -72,7 +76,13 @@ public static function getDefaultName(): ?string
7276

7377
$r = new \ReflectionProperty($class, 'defaultName');
7478

75-
return $class === $r->class ? static::$defaultName : null;
79+
if ($class !== $r->class || null === static::$defaultName) {
80+
return null;
81+
}
82+
83+
trigger_deprecation('symfony/console', '6.1', 'Relying on the static property "$defaultName" for setting a command name is deprecated. Add the "%s" attribute to the "%s" class instead.', AsCommand::class, static::class);
84+
85+
return static::$defaultName;
7686
}
7787

7888
public static function getDefaultDescription(): ?string
@@ -85,7 +95,13 @@ public static function getDefaultDescription(): ?string
8595

8696
$r = new \ReflectionProperty($class, 'defaultDescription');
8797

88-
return $class === $r->class ? static::$defaultDescription : null;
98+
if ($class !== $r->class || null === static::$defaultDescription) {
99+
return null;
100+
}
101+
102+
trigger_deprecation('symfony/console', '6.1', 'Relying on the static property "$defaultDescription" for setting a command description is deprecated. Add the "%s" attribute to the "%s" class instead.', AsCommand::class, static::class);
103+
104+
return static::$defaultDescription;
89105
}
90106

91107
/**

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Console\Command;
1313

14+
use Symfony\Component\Console\Attribute\AsCommand;
1415
use Symfony\Component\Console\Completion\CompletionInput;
1516
use Symfony\Component\Console\Completion\CompletionSuggestions;
1617
use Symfony\Component\Console\Completion\Output\BashCompletionOutput;
@@ -27,9 +28,17 @@
2728
*
2829
* @author Wouter de Jong <wouter@wouterj.nl>
2930
*/
31+
#[AsCommand(name: '|_complete', description: 'Internal command to provide shell completion suggestions')]
3032
final class CompleteCommand extends Command
3133
{
34+
/**
35+
* @deprecated since Symfony 6.1
36+
*/
3237
protected static $defaultName = '|_complete';
38+
39+
/**
40+
* @deprecated since Symfony 6.1
41+
*/
3342
protected static $defaultDescription = 'Internal command to provide shell completion suggestions';
3443

3544
private $completionOutputs;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Console\Command;
1313

14+
use Symfony\Component\Console\Attribute\AsCommand;
1415
use Symfony\Component\Console\Completion\CompletionInput;
1516
use Symfony\Component\Console\Completion\CompletionSuggestions;
1617
use Symfony\Component\Console\Input\InputArgument;
@@ -25,9 +26,17 @@
2526
*
2627
* @author Wouter de Jong <wouter@wouterj.nl>
2728
*/
29+
#[AsCommand(name: 'completion', description: 'Dump the shell completion script')]
2830
final class DumpCompletionCommand extends Command
2931
{
32+
/**
33+
* @deprecated since Symfony 6.1
34+
*/
3035
protected static $defaultName = 'completion';
36+
37+
/**
38+
* @deprecated since Symfony 6.1
39+
*/
3140
protected static $defaultDescription = 'Dump the shell completion script';
3241

3342
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Application;
16+
use Symfony\Component\Console\Attribute\AsCommand;
1617
use Symfony\Component\Console\Command\Command;
1718
use Symfony\Component\Console\Command\HelpCommand;
1819
use Symfony\Component\Console\Command\LazyCommand;
@@ -1968,13 +1969,12 @@ public function isEnabled(): bool
19681969
}
19691970
}
19701971

1972+
#[AsCommand(name: 'signal')]
19711973
class SignableCommand extends Command implements SignalableCommandInterface
19721974
{
19731975
public $signaled = false;
19741976
public $loop = 100;
19751977

1976-
protected static $defaultName = 'signal';
1977-
19781978
public function getSubscribedSignals(): array
19791979
{
19801980
return SignalRegistry::isSupported() ? [\SIGALRM] : [];

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Console\Tests\Command;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\Console\Application;
1617
use Symfony\Component\Console\Attribute\AsCommand;
1718
use Symfony\Component\Console\Command\Command;
@@ -28,6 +29,8 @@
2829

2930
class CommandTest extends TestCase
3031
{
32+
use ExpectDeprecationTrait;
33+
3134
protected static $fixturesPath;
3235

3336
public static function setUpBeforeClass(): void
@@ -420,6 +423,48 @@ public function testCommandAttribute()
420423
$this->assertSame(['f'], $command->getAliases());
421424
}
422425

426+
/**
427+
* @group legacy
428+
*/
429+
public function testDefaultNameProperty()
430+
{
431+
$this->expectDeprecation('Since symfony/console 6.1: Relying on the static property "$defaultName" for setting a command name is deprecated. Add the "Symfony\Component\Console\Attribute\AsCommand" attribute to the "Symfony\Component\Console\Tests\Command\MyCommand" class instead.');
432+
433+
$this->assertSame('my:command', MyCommand::getDefaultName());
434+
}
435+
436+
/**
437+
* @group legacy
438+
*/
439+
public function testDefaultDescriptionProperty()
440+
{
441+
$this->expectDeprecation('Since symfony/console 6.1: Relying on the static property "$defaultDescription" for setting a command description is deprecated. Add the "Symfony\Component\Console\Attribute\AsCommand" attribute to the "Symfony\Component\Console\Tests\Command\MyCommand" class instead.');
442+
443+
$this->assertSame('This is a command I wrote all by myself', MyCommand::getDefaultDescription());
444+
}
445+
446+
/**
447+
* @group legacy
448+
*/
449+
public function testStaticDefaultProperties()
450+
{
451+
$command = new MyCommand();
452+
453+
$this->assertSame('my:command', $command->getName());
454+
$this->assertSame('This is a command I wrote all by myself', $command->getDescription());
455+
}
456+
457+
public function testAttributeOverridesProperty()
458+
{
459+
$this->assertSame('my:command', MyAnnotatedCommand::getDefaultName());
460+
$this->assertSame('This is a command I wrote all by myself', MyAnnotatedCommand::getDefaultDescription());
461+
462+
$command = new MyAnnotatedCommand();
463+
464+
$this->assertSame('my:command', $command->getName());
465+
$this->assertSame('This is a command I wrote all by myself', $command->getDescription());
466+
}
467+
423468
public function testDefaultCommand()
424469
{
425470
$apl = new Application();
@@ -455,3 +500,16 @@ class Php8Command extends Command
455500
class Php8Command2 extends Command
456501
{
457502
}
503+
504+
class MyCommand extends Command
505+
{
506+
protected static $defaultName = 'my:command';
507+
protected static $defaultDescription = 'This is a command I wrote all by myself';
508+
}
509+
510+
#[AsCommand(name: 'my:command', description: 'This is a command I wrote all by myself')]
511+
class MyAnnotatedCommand extends Command
512+
{
513+
protected static $defaultName = 'i-shall-be-ignored';
514+
protected static $defaultDescription = 'This description should be ignored.';
515+
}

src/Symfony/Component/Console/Tests/ConsoleEventsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Application;
16+
use Symfony\Component\Console\Attribute\AsCommand;
1617
use Symfony\Component\Console\Command\Command;
1718
use Symfony\Component\Console\ConsoleEvents;
1819
use Symfony\Component\Console\Event\ConsoleCommandEvent;
@@ -75,10 +76,9 @@ public function observe(object $event): void
7576
}
7677
}
7778

79+
#[AsCommand(name: 'fail')]
7880
class FailingCommand extends Command
7981
{
80-
protected static $defaultName = 'fail';
81-
8282
protected function execute(InputInterface $input, OutputInterface $output): int
8383
{
8484
throw new \RuntimeException('I failed. Sorry.');

src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Console\Tests\DependencyInjection;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Attribute\AsCommand;
1516
use Symfony\Component\Console\Command\Command;
1617
use Symfony\Component\Console\Command\LazyCommand;
1718
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
@@ -281,18 +282,16 @@ class MyCommand extends Command
281282
{
282283
}
283284

285+
#[AsCommand(name: 'default')]
284286
class NamedCommand extends Command
285287
{
286-
protected static $defaultName = 'default';
287288
}
288289

290+
#[AsCommand(name: '|cmdname|cmdalias', description: 'Just testing')]
289291
class DescribedCommand extends Command
290292
{
291293
public static $initCounter = 0;
292294

293-
protected static $defaultName = '|cmdname|cmdalias';
294-
protected static $defaultDescription = 'Just testing';
295-
296295
public function __construct()
297296
{
298297
++self::$initCounter;

src/Symfony/Component/Console/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
"require": {
1919
"php": ">=8.0.2",
20+
"symfony/deprecation-contracts": "^2.1|^3",
2021
"symfony/polyfill-mbstring": "~1.0",
2122
"symfony/service-contracts": "^1.1|^2|^3",
2223
"symfony/string": "^5.4|^6.0"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy