Skip to content

Commit 427fac0

Browse files
authored
refactor: commands - move the null exit code deprecation earlier (#10112)
* refactor: commands - move the null exit code deprecation earlier * add fixes * code review fixes
1 parent 2d4eb7c commit 427fac0

36 files changed

+137
-66
lines changed

system/Boot.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -428,17 +428,6 @@ protected static function initializeConsole(): Console
428428

429429
protected static function runCommand(Console $console): int
430430
{
431-
$exitCode = $console->initialize()->run();
432-
433-
if (! is_int($exitCode)) {
434-
@trigger_error(sprintf(
435-
'Since v4.8.0, commands must return an integer exit code. Last command "%s" exited with %s. Defaulting to EXIT_SUCCESS.',
436-
$console->getCommand(),
437-
get_debug_type($exitCode),
438-
), E_USER_DEPRECATED);
439-
$exitCode = EXIT_SUCCESS;
440-
}
441-
442-
return $exitCode;
431+
return $console->initialize()->run();
443432
}
444433
}

system/CLI/BaseCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function __construct(LoggerInterface $logger, Commands $commands)
101101
*
102102
* @param array<int|string, string|null> $params
103103
*
104-
* @return int|void
104+
* @return int|null
105105
*/
106106
abstract public function run(array $params);
107107

@@ -110,7 +110,7 @@ abstract public function run(array $params);
110110
*
111111
* @param array<int|string, string|null> $params
112112
*
113-
* @return int|void
113+
* @return int|null
114114
*
115115
* @throws ReflectionException
116116
*/

system/CLI/Commands.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,20 @@ public function run(string $command, array $params)
6969

7070
Events::trigger('pre_command');
7171

72-
$exit = $class->run($params);
72+
$exitCode = $class->run($params);
7373

7474
Events::trigger('post_command');
7575

76-
return $exit;
76+
if (! is_int($exitCode)) {
77+
@trigger_error(sprintf(
78+
'Since v4.8.0, commands must return an integer exit code. Last command "%s" exited with %s. Defaulting to EXIT_SUCCESS.',
79+
$command,
80+
get_debug_type($exitCode),
81+
), E_USER_DEPRECATED);
82+
$exitCode = EXIT_SUCCESS;
83+
}
84+
85+
return $exitCode;
7786
}
7887

7988
/**

system/CLI/Console.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Console
3939
*
4040
* @param list<string> $tokens
4141
*
42-
* @return int|null Exit code or null for legacy commands that don't return an exit code.
42+
* @return int Exit code
4343
*/
4444
public function run(array $tokens = [])
4545
{

system/Commands/Cache/InfoCache.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function run(array $params)
6363
if ($config->handler !== 'file') {
6464
CLI::error('This command only supports the file cache handler.');
6565

66-
return;
66+
return EXIT_ERROR;
6767
}
6868

6969
$cache = CacheFactory::getHandler($config);
@@ -87,5 +87,7 @@ public function run(array $params)
8787
];
8888

8989
CLI::table($tbody, $thead);
90+
91+
return EXIT_SUCCESS;
9092
}
9193
}

system/Commands/Database/CreateDatabase.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function run(array $params)
115115
CLI::error("Database \"{$dbName}\" already exists.", 'light_gray', 'red');
116116
CLI::newLine();
117117

118-
return;
118+
return EXIT_ERROR;
119119
}
120120

121121
unset($dbName);
@@ -130,22 +130,26 @@ public function run(array $params)
130130
CLI::error('Database creation failed.', 'light_gray', 'red');
131131
CLI::newLine();
132132

133-
return;
133+
return EXIT_ERROR;
134134
// @codeCoverageIgnoreEnd
135135
}
136136
} elseif (! Database::forge()->createDatabase($name)) {
137137
// @codeCoverageIgnoreStart
138138
CLI::error('Database creation failed.', 'light_gray', 'red');
139139
CLI::newLine();
140140

141-
return;
141+
return EXIT_ERROR;
142142
// @codeCoverageIgnoreEnd
143143
}
144144

145145
CLI::write("Database \"{$name}\" successfully created.", 'green');
146146
CLI::newLine();
147+
148+
return EXIT_SUCCESS;
147149
} catch (Throwable $e) {
148150
$this->showError($e);
151+
152+
return EXIT_ERROR;
149153
} finally {
150154
Factories::reset('config');
151155
Database::connect(null, false);

system/Commands/Database/Migrate.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,12 @@ public function run(array $params)
9999

100100
CLI::write(lang('Migrations.migrated'), 'green');
101101

102+
return EXIT_SUCCESS;
102103
// @codeCoverageIgnoreStart
103104
} catch (Throwable $e) {
104105
$this->showError($e);
106+
107+
return EXIT_ERROR;
105108
// @codeCoverageIgnoreEnd
106109
}
107110
}

system/Commands/Database/MigrateRefresh.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,15 @@ public function run(array $params)
7979
$force = array_key_exists('f', $params) || CLI::getOption('f');
8080

8181
if (! $force && CLI::prompt(lang('Migrations.refreshConfirm'), ['y', 'n']) === 'n') {
82-
return;
82+
return EXIT_ERROR;
8383
}
8484

8585
$params['f'] = null;
8686
// @codeCoverageIgnoreEnd
8787
}
8888

89-
$this->withSignalsBlocked(function () use ($params): void {
90-
$this->call('migrate:rollback', $params);
91-
$this->call('migrate', $params);
92-
});
89+
return $this->withSignalsBlocked(
90+
fn (): int => $this->call('migrate:rollback', $params) | $this->call('migrate', $params),
91+
);
9392
}
9493
}

system/Commands/Database/MigrateRollback.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function run(array $params)
7777
$force = array_key_exists('f', $params) || CLI::getOption('f');
7878

7979
if (! $force && CLI::prompt(lang('Migrations.rollBackConfirm'), ['y', 'n']) === 'n') {
80-
return null;
80+
return EXIT_ERROR;
8181
}
8282
// @codeCoverageIgnoreEnd
8383
}
@@ -101,12 +101,20 @@ public function run(array $params)
101101

102102
CLI::write(lang('Migrations.rollingBack') . ' ' . $batch, 'yellow');
103103

104-
$this->withSignalsBlocked(static function () use ($runner, $batch): void {
104+
$exit = $this->withSignalsBlocked(static function () use ($runner, $batch): int {
105105
if (! $runner->regress($batch)) {
106106
CLI::error(lang('Migrations.generalFault'), 'light_gray', 'red'); // @codeCoverageIgnore
107+
108+
return EXIT_ERROR;
107109
}
110+
111+
return EXIT_SUCCESS;
108112
});
109113

114+
if ($exit !== EXIT_SUCCESS) {
115+
return $exit;
116+
}
117+
110118
$messages = $runner->getCliMessages();
111119

112120
foreach ($messages as $message) {
@@ -115,12 +123,13 @@ public function run(array $params)
115123

116124
CLI::write('Done rolling back migrations.', 'green');
117125

126+
return EXIT_SUCCESS;
118127
// @codeCoverageIgnoreStart
119128
} catch (Throwable $e) {
120129
$this->showError($e);
130+
131+
return EXIT_ERROR;
121132
// @codeCoverageIgnoreEnd
122133
}
123-
124-
return null;
125134
}
126135
}

system/Commands/Database/MigrateStatus.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function run(array $params)
150150
CLI::error(lang('Migrations.noneFound'), 'light_gray', 'red');
151151
CLI::newLine();
152152

153-
return;
153+
return EXIT_ERROR;
154154
// @codeCoverageIgnoreEnd
155155
}
156156

@@ -164,5 +164,7 @@ public function run(array $params)
164164
];
165165

166166
CLI::table($status, $headers);
167+
168+
return EXIT_SUCCESS;
167169
}
168170
}

0 commit comments

Comments
 (0)