Skip to content

[Console] Deprecate the $defaultName property #45361

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
Feb 8, 2022
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
5 changes: 5 additions & 0 deletions UPGRADE-6.1.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
UPGRADE FROM 6.0 to 6.1
=======================

Console
-------

* Deprecate `Command::$defaultName` and `Command::$defaultDescription`, use the `AsCommand` attribute instead.

Serializer
----------

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

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

6.0
---
Expand Down
20 changes: 18 additions & 2 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ class Command

/**
* @var string|null The default command name
*
* @deprecated since Symfony 6.1, use the AsCommand attribute instead
*/
protected static $defaultName;

/**
* @var string|null The default command description
*
* @deprecated since Symfony 6.1, use the AsCommand attribute instead
*/
protected static $defaultDescription;

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

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

return $class === $r->class ? static::$defaultName : null;
if ($class !== $r->class || null === static::$defaultName) {
return null;
}

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);

return static::$defaultName;
}

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

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

return $class === $r->class ? static::$defaultDescription : null;
if ($class !== $r->class || null === static::$defaultDescription) {
return null;
}

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);

return static::$defaultDescription;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Console/Command/CompleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Console\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Completion\Output\BashCompletionOutput;
Expand All @@ -27,9 +28,17 @@
*
* @author Wouter de Jong <wouter@wouterj.nl>
*/
#[AsCommand(name: '|_complete', description: 'Internal command to provide shell completion suggestions')]
final class CompleteCommand extends Command
{
/**
* @deprecated since Symfony 6.1
*/
protected static $defaultName = '|_complete';

/**
* @deprecated since Symfony 6.1
*/
protected static $defaultDescription = 'Internal command to provide shell completion suggestions';

private $completionOutputs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Console\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -25,9 +26,17 @@
*
* @author Wouter de Jong <wouter@wouterj.nl>
*/
#[AsCommand(name: 'completion', description: 'Dump the shell completion script')]
final class DumpCompletionCommand extends Command
{
/**
* @deprecated since Symfony 6.1
*/
protected static $defaultName = 'completion';

/**
* @deprecated since Symfony 6.1
*/
protected static $defaultDescription = 'Dump the shell completion script';

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\HelpCommand;
use Symfony\Component\Console\Command\LazyCommand;
Expand Down Expand Up @@ -1968,13 +1969,12 @@ public function isEnabled(): bool
}
}

#[AsCommand(name: 'signal')]
class SignableCommand extends Command implements SignalableCommandInterface
{
public $signaled = false;
public $loop = 100;

protected static $defaultName = 'signal';

public function getSubscribedSignals(): array
{
return SignalRegistry::isSupported() ? [\SIGALRM] : [];
Expand Down
58 changes: 58 additions & 0 deletions src/Symfony/Component/Console/Tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Console\Tests\Command;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand All @@ -28,6 +29,8 @@

class CommandTest extends TestCase
{
use ExpectDeprecationTrait;

protected static $fixturesPath;

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

/**
* @group legacy
*/
public function testDefaultNameProperty()
{
$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.');

$this->assertSame('my:command', MyCommand::getDefaultName());
}

/**
* @group legacy
*/
public function testDefaultDescriptionProperty()
{
$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.');

$this->assertSame('This is a command I wrote all by myself', MyCommand::getDefaultDescription());
}

/**
* @group legacy
*/
public function testStaticDefaultProperties()
{
$command = new MyCommand();

$this->assertSame('my:command', $command->getName());
$this->assertSame('This is a command I wrote all by myself', $command->getDescription());
}

public function testAttributeOverridesProperty()
{
$this->assertSame('my:command', MyAnnotatedCommand::getDefaultName());
$this->assertSame('This is a command I wrote all by myself', MyAnnotatedCommand::getDefaultDescription());

$command = new MyAnnotatedCommand();

$this->assertSame('my:command', $command->getName());
$this->assertSame('This is a command I wrote all by myself', $command->getDescription());
}

public function testDefaultCommand()
{
$apl = new Application();
Expand Down Expand Up @@ -455,3 +500,16 @@ class Php8Command extends Command
class Php8Command2 extends Command
{
}

class MyCommand extends Command
{
protected static $defaultName = 'my:command';
protected static $defaultDescription = 'This is a command I wrote all by myself';
}

#[AsCommand(name: 'my:command', description: 'This is a command I wrote all by myself')]
class MyAnnotatedCommand extends Command
{
protected static $defaultName = 'i-shall-be-ignored';
protected static $defaultDescription = 'This description should be ignored.';
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/Console/Tests/ConsoleEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
Expand Down Expand Up @@ -75,10 +76,9 @@ public function observe(object $event): void
}
}

#[AsCommand(name: 'fail')]
class FailingCommand extends Command
{
protected static $defaultName = 'fail';

protected function execute(InputInterface $input, OutputInterface $output): int
{
throw new \RuntimeException('I failed. Sorry.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Console\Tests\DependencyInjection;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\LazyCommand;
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
Expand Down Expand Up @@ -281,18 +282,16 @@ class MyCommand extends Command
{
}

#[AsCommand(name: 'default')]
class NamedCommand extends Command
{
protected static $defaultName = 'default';
}

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

protected static $defaultName = '|cmdname|cmdalias';
protected static $defaultDescription = 'Just testing';

public function __construct()
{
++self::$initCounter;
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
],
"require": {
"php": ">=8.0.2",
"symfony/deprecation-contracts": "^2.1|^3",
"symfony/polyfill-mbstring": "~1.0",
"symfony/service-contracts": "^1.1|^2|^3",
"symfony/string": "^5.4|^6.0"
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Dotenv/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Dotenv\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -22,9 +23,17 @@
*
* @author Christopher Hertel <mail@christopher-hertel.de>
*/
#[AsCommand(name: 'debug:dotenv', description: 'Lists all dotenv files with variables and values')]
final class DebugCommand extends Command
{
/**
* @deprecated since Symfony 6.1
*/
protected static $defaultName = 'debug:dotenv';

/**
* @deprecated since Symfony 6.1
*/
protected static $defaultDescription = 'Lists all dotenv files with variables and values';

private $kernelEnvironment;
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Dotenv/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"symfony/console": "^5.4|^6.0",
"symfony/process": "^5.4|^6.0"
},
"conflict": {
"symfony/console": "<5.4"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Dotenv\\": "" },
"exclude-from-classmap": [
Expand Down