Skip to content

Commit 2169177

Browse files
committed
refactor: deprecate BaseCommand and GeneratorTrait
1 parent dc9c9f0 commit 2169177

13 files changed

Lines changed: 92 additions & 15 deletions

‎system/CLI/BaseCommand.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
* @property string $name
3030
* @property array<string, string> $options
3131
* @property string $usage
32+
*
33+
* @deprecated 4.8.0 Extend `AbstractCommand` instead.
3234
*/
3335
abstract class BaseCommand
3436
{

‎system/CLI/Commands.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ public function runLegacy(string $command, array $params): int
102102
return EXIT_ERROR;
103103
}
104104

105+
@trigger_error(sprintf(
106+
'Since v4.8.0, "%s" is deprecated. Command "%s" (%s) should extend "%s" instead.',
107+
BaseCommand::class,
108+
$command,
109+
$this->commands[$command]['class'],
110+
AbstractCommand::class,
111+
), E_USER_DEPRECATED);
112+
105113
Events::trigger('pre_command');
106114

107115
$exitCode = $this->getCommand($command, legacy: true)->run($params);

‎system/CLI/GeneratorTrait.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
/**
2020
* GeneratorTrait contains a collection of methods
2121
* to build the commands that generates a file.
22+
*
23+
* @deprecated 4.8.0 Extend `AbstractGeneratorCommand` instead.
2224
*/
2325
trait GeneratorTrait
2426
{

‎tests/system/CLI/BaseCommandTest.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ protected function resetCli(): void
4141
CLI::reset();
4242
}
4343

44+
protected function tearDown(): void
45+
{
46+
parent::tearDown();
47+
48+
service('superglobals')->setServer('CODEIGNITER_SCREAM_DEPRECATIONS', '1');
49+
}
50+
4451
public function testRunCommand(): void
4552
{
4653
$command = new AppInfo(single_service('logger'), single_service('commands'));
@@ -54,6 +61,8 @@ public function testRunCommand(): void
5461

5562
public function testCallingOtherCommands(): void
5663
{
64+
service('superglobals')->unsetServer('CODEIGNITER_SCREAM_DEPRECATIONS');
65+
5766
$command = new AppInfo(single_service('logger'), single_service('commands'));
5867

5968
$this->assertSame(0, $command->helpMe());

‎tests/system/CLI/CommandsTest.php‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ protected function resetAll(): void
6363
{
6464
$this->resetServices();
6565

66+
service('superglobals')->setServer('CODEIGNITER_SCREAM_DEPRECATIONS', '1');
67+
6668
CLI::reset();
6769
}
6870

@@ -220,6 +222,8 @@ public function testRunOnAbstractLegacyCommandCannotBeRun(): void
220222

221223
public function testRunOnKnownLegacyCommand(): void
222224
{
225+
service('superglobals')->unsetServer('CODEIGNITER_SCREAM_DEPRECATIONS');
226+
223227
$commands = new Commands();
224228

225229
$this->assertSame(EXIT_SUCCESS, $commands->runLegacy('app:info', []));
@@ -242,12 +246,20 @@ public function testRunOnKnownModernCommand(): void
242246
);
243247
}
244248

245-
public function testRunOnLegacyCommandReturningNullIsDeprecated(): void
249+
public function testRunOnLegacyCommandIsDeprecated(): void
246250
{
247251
$this->expectException(ErrorException::class);
248-
$this->expectExceptionMessage('Since v4.8.0, commands must return an integer exit code. Last command "null:return" exited with null. Defaulting to EXIT_SUCCESS.');
252+
$this->expectExceptionMessage('Since v4.8.0, "CodeIgniter\\CLI\\BaseCommand" is deprecated. Command "app:info" (Tests\\Support\\Commands\\Legacy\\AppInfo) should extend "CodeIgniter\\CLI\\AbstractCommand" instead.');
249253

250-
(new Commands())->runLegacy('null:return', []);
254+
(new Commands())->runLegacy('app:info', []);
255+
}
256+
257+
public function testRunOnLegacyCommandReturningNullIsDeprecated(): void
258+
{
259+
service('superglobals')->unsetServer('CODEIGNITER_SCREAM_DEPRECATIONS');
260+
261+
$this->assertSame(EXIT_SUCCESS, (new Commands())->runLegacy('null:return', []));
262+
$this->assertLogContains('warning', '[DEPRECATED] Since v4.8.0, commands must return an integer exit code. Last command "null:return" exited with null. Defaulting to EXIT_SUCCESS.');
251263
}
252264

253265
public function testRunMethodIsDeprecatedInFavorOfRunLegacy(): void
@@ -503,6 +515,8 @@ public function testAliasClashingWithAnotherAliasFailsHard(): void
503515

504516
public function testDestructiveCommandIsNotRisky(): void
505517
{
518+
service('superglobals')->unsetServer('CODEIGNITER_SCREAM_DEPRECATIONS');
519+
506520
$this->expectException(RuntimeException::class);
507521

508522
command('app:destructive');
@@ -699,6 +713,8 @@ public function testGetCommandAlternativesThrowsDeprecationWhenCommandsArrayIsPa
699713

700714
public function testDiscoveredLegacyCommandsCanBeOverridden(): void
701715
{
716+
service('superglobals')->unsetServer('CODEIGNITER_SCREAM_DEPRECATIONS');
717+
702718
$this->injectFixtureLocator([
703719
AppInfoOverride::class => $this->loadOverrideFixture('AppInfo.php'),
704720
AppInfo::class => SUPPORTPATH . 'Commands/Legacy/AppInfo.php',

‎tests/system/CLI/ConsoleTest.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ protected function tearDown(): void
5353
{
5454
parent::tearDown();
5555

56+
service('superglobals')->setServer('CODEIGNITER_SCREAM_DEPRECATIONS', '1');
57+
5658
CLI::reset();
5759
}
5860

@@ -144,6 +146,8 @@ public function testBadCommand(): void
144146

145147
public function testUnknownCommandRunsConfirmedSuggestion(): void
146148
{
149+
service('superglobals')->unsetServer('CODEIGNITER_SCREAM_DEPRECATIONS');
150+
147151
$this->initializeConsole('app:inf', '--no-header');
148152
$io = $this->useInputs('y');
149153

@@ -346,6 +350,8 @@ public function testHelpArgumentAndHelpOptionCombined(): void
346350

347351
public function testRunRoutesDiscoveredLegacyCommandThroughRunLegacy(): void
348352
{
353+
service('superglobals')->unsetServer('CODEIGNITER_SCREAM_DEPRECATIONS');
354+
349355
// `app:info` is a legacy BaseCommand fixture. Console must take the
350356
// legacy branch of run() and delegate to Commands::runLegacy().
351357
$this->initializeConsole('app:info');

‎tests/system/Commands/ConfigurableSortImportsTest.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ final class ConfigurableSortImportsTest extends CIUnitTestCase
2525
{
2626
use StreamFilterTrait;
2727

28+
public static function setUpBeforeClass(): void
29+
{
30+
parent::setUpBeforeClass();
31+
32+
service('superglobals')->unsetServer('CODEIGNITER_SCREAM_DEPRECATIONS');
33+
}
34+
35+
public static function tearDownAfterClass(): void
36+
{
37+
parent::tearDownAfterClass();
38+
39+
service('superglobals')->setServer('CODEIGNITER_SCREAM_DEPRECATIONS', '1');
40+
}
41+
2842
public function testPublishLanguageWithoutOptions(): void
2943
{
3044
command('publish:language');

‎tests/system/Commands/Generators/GeneratorsTest.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ final class GeneratorsTest extends CIUnitTestCase
2525
{
2626
use StreamFilterTrait;
2727

28+
protected function tearDown(): void
29+
{
30+
parent::tearDown();
31+
32+
service('superglobals')->setServer('CODEIGNITER_SCREAM_DEPRECATIONS', '1');
33+
}
34+
2835
public function testGenerateFileCreated(): void
2936
{
3037
command('make:seeder categories');
@@ -98,6 +105,8 @@ public function testGenerateFileInSubfolders(): void
98105

99106
public function testSuffixingHasNoEffect(): void
100107
{
108+
service('superglobals')->unsetServer('CODEIGNITER_SCREAM_DEPRECATIONS');
109+
101110
command('make:foo bar --suffix');
102111
$file1 = APPPATH . 'Commands/Bar.php';
103112
$file2 = APPPATH . 'Commands/BarCommand.php';

‎user_guide_src/source/changelogs/v4.8.0.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,8 @@ Deprecations
431431
- **CLI:** Returning a non-integer exit code from a command is now deprecated and will trigger a deprecation notice. Command methods should return an integer exit code (e.g., ``0`` for success, non-zero for errors) to ensure proper behavior across all platforms.
432432
- **CLI:** ``Commands::run()`` is now deprecated in favor of ``Commands::runLegacy()`` for legacy ``BaseCommand`` commands, and ``Commands::runCommand()`` for modern ``AbstractCommand`` commands.
433433
- **CLI:** The ``$commands`` parameter of ``Commands::verifyCommand()`` and the ``$collection`` parameter of ``Commands::getCommandAlternatives()`` are no longer used. Passing a non-empty array for either will trigger a deprecation notice.
434+
- **CLI:** ``BaseCommand`` is now deprecated in favor of ``AbstractCommand``. Running a command that extends it triggers a deprecation notice. See :doc:`../cli/cli_modern_commands`.
435+
- **CLI:** ``GeneratorTrait`` is now deprecated in favor of ``AbstractGeneratorCommand``. See :doc:`../cli/cli_modern_generators`.
434436
- **CLI:** The ``CLI.generator.viewName.cell`` language string is deprecated. It was never displayed, since ``make:cell`` only prompts for the class name.
435437
- **HTTP:** The ``CLIRequest::parseCommand()`` method is now deprecated and will be removed in a future release. The ``CLIRequest`` class now uses the new ``CommandLineParser`` class to handle command-line argument parsing.
436438
- **HTTP:** ``URI::setSilent()`` is now hard deprecated. This method was only previously marked as deprecated. It will now trigger a deprecation notice when used.

‎user_guide_src/source/cli/cli_commands.rst‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ need to have routes defined for, making them perfect for building tools that dev
88
their jobs simpler, whether by handling migrations or database seeding, checking cronjob status, or even
99
building out custom code generators for your company.
1010

11+
.. deprecated:: 4.8.0
12+
13+
``BaseCommand`` is deprecated, and running a command that extends it triggers a
14+
deprecation notice. Write new commands against ``AbstractCommand`` instead.
15+
See :doc:`cli_modern_commands`.
16+
1117
.. contents::
1218
:local:
1319
:depth: 2

0 commit comments

Comments
 (0)