Skip to content

Commit 855d60a

Browse files
authored
refactor: rename -h option of routes command as --handler (#10113)
1 parent c8ae390 commit 855d60a

File tree

5 files changed

+58
-18
lines changed

5 files changed

+58
-18
lines changed

system/Commands/Utilities/Routes.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,26 @@ class Routes extends BaseCommand
7373
* @var array<string, string>
7474
*/
7575
protected $options = [
76-
'-h' => 'Sort by Handler.',
77-
'--host' => 'Specify hostname in request URI.',
76+
'--handler' => 'Sort by Handler.',
77+
'--host' => 'Specify hostname in request URI.',
7878
];
7979

8080
/**
8181
* Displays the help for the spark cli script itself.
8282
*/
8383
public function run(array $params)
8484
{
85-
$sortByHandler = array_key_exists('h', $params);
86-
$host = $params['host'] ?? null;
85+
$sortByHandler = array_key_exists('handler', $params);
86+
87+
if (! $sortByHandler && array_key_exists('h', $params)) {
88+
// Support -h as a shortcut but print a warning that it is not the intended use of -h.
89+
CLI::write('Warning: -h will be used as shortcut for --help in v4.8.0. Please use --handler to sort by handler.', 'yellow');
90+
CLI::newLine();
91+
92+
$sortByHandler = true;
93+
}
94+
95+
$host = $params['host'] ?? null;
8796

8897
// Set HTTP_HOST
8998
if ($host !== null) {

tests/system/Commands/Utilities/RoutesTest.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private function getCleanRoutes(): RouteCollection
5656

5757
public function testRoutesCommand(): void
5858
{
59-
Services::injectMock('routes', null);
59+
Services::resetSingle('routes');
6060

6161
command('routes');
6262

@@ -92,11 +92,43 @@ public function testRoutesCommand(): void
9292

9393
public function testRoutesCommandSortByHandler(): void
9494
{
95-
Services::injectMock('routes', null);
95+
Services::resetSingle('routes');
96+
97+
command('routes --handler');
98+
99+
$expected = <<<'EOL'
100+
+---------+---------+---------------+----------------------------------------+----------------+---------------+
101+
| Method | Route | Name | Handler ↓ | Before Filters | After Filters |
102+
+---------+---------+---------------+----------------------------------------+----------------+---------------+
103+
| GET | closure | » | (Closure) | | |
104+
| GET | / | » | \App\Controllers\Home::index | | |
105+
| GET | testing | testing-index | \App\Controllers\TestController::index | | |
106+
| HEAD | testing | testing-index | \App\Controllers\TestController::index | | |
107+
| POST | testing | testing-index | \App\Controllers\TestController::index | | |
108+
| PATCH | testing | testing-index | \App\Controllers\TestController::index | | |
109+
| PUT | testing | testing-index | \App\Controllers\TestController::index | | |
110+
| DELETE | testing | testing-index | \App\Controllers\TestController::index | | |
111+
| OPTIONS | testing | testing-index | \App\Controllers\TestController::index | | |
112+
| TRACE | testing | testing-index | \App\Controllers\TestController::index | | |
113+
| CONNECT | testing | testing-index | \App\Controllers\TestController::index | | |
114+
| CLI | testing | testing-index | \App\Controllers\TestController::index | | |
115+
+---------+---------+---------------+----------------------------------------+----------------+---------------+
116+
EOL;
117+
$this->assertStringContainsString($expected, $this->getBuffer());
118+
}
119+
120+
/**
121+
* @todo To remove this test and the backward compatibility for -h in v4.8.0.
122+
*/
123+
public function testRoutesCommandSortByHandlerUsingShortcutForBc(): void
124+
{
125+
Services::resetSingle('routes');
96126

97127
command('routes -h');
98128

99129
$expected = <<<'EOL'
130+
Warning: -h will be used as shortcut for --help in v4.8.0. Please use --handler to sort by handler.
131+
100132
+---------+---------+---------------+----------------------------------------+----------------+---------------+
101133
| Method | Route | Name | Handler ↓ | Before Filters | After Filters |
102134
+---------+---------+---------------+----------------------------------------+----------------+---------------+
@@ -119,7 +151,7 @@ public function testRoutesCommandSortByHandler(): void
119151

120152
public function testRoutesCommandHostHostname(): void
121153
{
122-
Services::injectMock('routes', null);
154+
Services::resetSingle('routes');
123155

124156
command('routes --host blog.example.com');
125157

@@ -148,7 +180,7 @@ public function testRoutesCommandHostHostname(): void
148180

149181
public function testRoutesCommandHostSubdomain(): void
150182
{
151-
Services::injectMock('routes', null);
183+
Services::resetSingle('routes');
152184

153185
command('routes --host sub.example.com');
154186

@@ -181,8 +213,7 @@ public function testRoutesCommandAutoRouteImproved(): void
181213

182214
$routes->setAutoRoute(true);
183215
config('Feature')->autoRoutesImproved = true;
184-
$namespace = 'Tests\Support\Controllers';
185-
$routes->setDefaultNamespace($namespace);
216+
$routes->setDefaultNamespace('Tests\Support\Controllers');
186217

187218
command('routes');
188219

@@ -214,7 +245,8 @@ public function testRoutesCommandRouteLegacy(): void
214245
$routes = $this->getCleanRoutes();
215246
$routes->loadRoutes();
216247

217-
$featureConfig = config(Feature::class);
248+
$featureConfig = config(Feature::class);
249+
218250
$featureConfig->autoRoutesImproved = false;
219251
$routes->setAutoRoute(true);
220252

user_guide_src/source/changelogs/v4.7.3.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ Message Changes
2424
Changes
2525
*******
2626

27+
- **Commands:** The ``-h`` option for the ``routes`` command is renamed to ``--handler`` to avoid conflict with the common use of ``-h`` as a shortcut for ``--help``.
28+
The old ``-h`` option will continue to work until v4.8.0, at which point it will be removed and repurposed as a shortcut for ``--help``.
29+
A warning message is displayed when using the old ``-h`` option to encourage users to switch to the new ``--handler`` option.
30+
2731
************
2832
Deprecations
2933
************

utils/phpstan-baseline/argument.type.neon

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 82 errors
1+
# total 78 errors
22

33
parameters:
44
ignoreErrors:
@@ -47,11 +47,6 @@ parameters:
4747
count: 1
4848
path: ../../tests/system/CodeIgniterTest.php
4949

50-
-
51-
message: '#^Parameter \#2 \$mock of static method CodeIgniter\\Config\\BaseService\:\:injectMock\(\) expects object, null given\.$#'
52-
count: 4
53-
path: ../../tests/system/Commands/Utilities/RoutesTest.php
54-
5550
-
5651
message: '#^Parameter \#1 \$expected of method PHPUnit\\Framework\\Assert\:\:assertInstanceOf\(\) expects class\-string\<Config\\TestRegistrar\>, string given\.$#'
5752
count: 1

utils/phpstan-baseline/loader.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 2059 errors
1+
# total 2055 errors
22

33
includes:
44
- argument.type.neon

0 commit comments

Comments
 (0)