8000 [Console] Command::execute() should always return int - deprecate ret… · symfony/symfony@98c4f6a · GitHub
[go: up one dir, main page]

Skip to content

Commit 98c4f6a

Browse files
jschaedlnicolas-grekas
authored andcommitted
[Console] Command::execute() should always return int - deprecate returning null
- added deprecation message for non-int return value in Command::execute() - fixed all core commands to return proper int values - added proper return type-hint to Command::execute() method in all core Commands
1 parent 3354bac commit 98c4f6a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+128
-54
lines changed

UPGRADE-4.4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Console
1010
-------
1111

1212
* Deprecated finding hidden commands using an abbreviation, use the full name instead
13+
* Deprecated returning `null` from `Command::execute()`, return `0` instead
1314

1415
Debug
1516
-----

UPGRADE-5.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Console
3737
* Removed the `getHorizontalBorderChar()` method in favor of the `getBorderChars()` method in `TableStyle`.
3838
* Removed the `setVerticalBorderChar()` method in favor of the `setVerticalBorderChars()` method in `TableStyle`.
3939
* Removed the `getVerticalBorderChar()` method in favor of the `getBorderChars()` method in `TableStyle`.
40+
* Removed support for returning `null` from `Command::execute()`, return `0` instead
4041
* The `ProcessHelper::run()` method takes the command as an array of arguments.
4142

4243
Before:

src/Symfony/Bridge/Twig/Command/DebugCommand.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
111111

112112
switch ($input->getOption('format')) {
113113
case 'text':
114-
return $name ? $this->displayPathsText($io, $name) : $this->displayGeneralText($io, $filter);
114+
$name ? $this->displayPathsText($io, $name) : $this->displayGeneralText($io, $filter);
115+
break;
115116
case 'json':
116-
return $name ? $this->displayPathsJson($io, $name) : $this->displayGeneralJson($io, $filter);
117+
$name ? $this->displayPathsJson($io, $name) : $this->displayGeneralJson($io, $filter);
118+
break;
117119
default:
118120
throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $input->getOption('format')));
119121
}
122+
123+
return 0;
120124
}
121125

122126
private function displayPathsText(SymfonyStyle $io, string $name)

src/Symfony/Bundle/FrameworkBundle/Command/AboutCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function configure()
5454
/**
5555
* {@inheritdoc}
5656
*/
57-
protected function execute(InputInterface $input, OutputInterface $output)
57+
protected function execute(InputInterface $input, OutputInterface $output): int
5858
{
5959
$io = new SymfonyStyle($input, $output);
6060

@@ -100,6 +100,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
100100
}
101101

102102
$io->table([], $rows);
103+
104+
return 0;
103105
}
104106

105107
private static function formatPath(string $path, string $baseDir): string

src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected function configure()
7272
/**
7373
* {@inheritdoc}
7474
*/
75-
protected function execute(InputInterface $input, OutputInterface $output)
75+
protected function execute(InputInterface $input, OutputInterface $output): int
7676
{
7777
$fs = $this->filesystem;
7878
$io = new SymfonyStyle($input, $output);
@@ -175,6 +175,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
175175
}
176176

177177
$io->success(sprintf('Cache for the "%s" environment (debug=%s) was successfully cleared.', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
178+
179+
return 0;
178180
}
179181

180182
private function warmup(string $warmupDir, string $realCacheDir, bool $enableOptionalWarmers = true)

src/Symfony/Bundle/FrameworkBundle/Command/CachePoolClearCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected function configure()
6060
/**
6161
* {@inheritdoc}
6262
*/
63-
protected function execute(InputInterface $input, OutputInterface $output)
63+
protected function execute(InputInterface $input, OutputInterface $output): int
6464
{
6565
$io = new SymfonyStyle($input, $output);
6666
$kernel = $this->getApplication()->getKernel();
@@ -99,5 +99,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
9999
}
100100

101101
$io->success('Cache was successfully cleared.');
102+
103+
return 0;
102104
}
103105
}

src/Symfony/Bundle/FrameworkBundle/Command/CachePoolDeleteCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected function configure()
5959
/**
6060
* {@inheritdoc}
6161
*/
62-
protected function execute(InputInterface $input, OutputInterface $output)
62+
protected function execute(InputInterface $input, OutputInterface $output): int
6363
{
6464
$io = new SymfonyStyle($input, $output);
6565
$pool = $input->getArgument('pool');
@@ -69,13 +69,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
6969
if (!$cachePool->hasItem($key)) {
7070
$io->note(sprintf('Cache item "%s" does not exist in cache pool "%s".', $key, $pool));
7171

72-
return;
72+
return 0;
7373
}
7474

7575
if (!$cachePool->deleteItem($key)) {
7676
throw new \Exception(sprintf('Cache item "%s" could not be deleted.', $key));
7777
}
7878

7979
$io->success(sprintf('Cache item "%s" was successfully deleted.', $key));
80+
81+
return 0;
8082
}
8183
}

src/Symfony/Bundle/FrameworkBundle/Command/CachePoolListCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ protected function configure()
5151
/**
5252
* {@inheritdoc}
5353
*/
54-
protected function execute(InputInterface $input, OutputInterface $output)
54+
protected function execute(InputInterface $input, OutputInterface $output): int
5555
{
5656
$io = new SymfonyStyle($input, $output);
5757

5858
$io->table(['Pool name'], array_map(function ($pool) {
5959
return [$pool];
6060
}, $this->poolNames));
61+
62+
return 0;
6163
}
6264
}

src/Symfony/Bundle/FrameworkBundle/Command/CachePoolPruneCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected function configure()
5757
/**
5858
* {@inheritdoc}
5959
*/
60-
protected function execute(InputInterface $input, OutputInterface $output)
60+
protected function execute(InputInterface $input, OutputInterface $output): int
6161
{
6262
$io = new SymfonyStyle($input, $output);
6363

@@ -67,5 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6767
}
6868

6969
$io->success('Successfully pruned cache pool(s).');
70+
71+
return 0;
7072
}
7173
}

src/Symfony/Bundle/FrameworkBundle/Command/CacheWarmupCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected function configure()
6666
/**
6767
* {@inheritdoc}
6868
*/
69-
protected function execute(InputInterface $input, OutputInterface $output)
69+
protected function execute(InputInterface $input, OutputInterface $output): int
7070
{
7171
$io = new SymfonyStyle($input, $output);
7272

@@ -80,5 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
8080
$this->cacheWarmer->warmUp($kernel->getContainer()->getParameter('kernel.cache_dir'));
8181

8282
$io->success(sprintf('Cache for the "%s" environment (debug=%s) was successfully warmed.', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
83+
84+
return 0;
8385
}
8486
}

src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected function configure()
6363
/**
6464
* {@inheritdoc}
6565
*/
66-
protected function execute(InputInterface $input, OutputInterface $output)
66+
protected function execute(InputInterface $input, OutputInterface $output): int
6767
{
6868
$io = new SymfonyStyle($input, $output);
6969
$errorIo = $io->getErrorStyle();
@@ -73,7 +73,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7373
$errorIo->comment('Provide the name of a bundle as the first argument of this command to dump its configuration. (e.g. <comment>debug:config FrameworkBundle</comment>)');
7474
$errorIo->comment('For dumping a specific option, add its path as the second argument of this command. (e.g. <comment>debug:config FrameworkBundle serializer</comment> to dump the <comment>framework.serializer</comment> configuration)');
7575

76-
return;
76+
return 0;
7777
}
7878

7979
$extension = $this->findExtension($name);
@@ -101,7 +101,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
101101

102102
$io->writeln(Yaml::dump([$extensionAlias => $config], 10));
103103

104-
return;
104+
return 0;
105105
}
106106

107107
try {
@@ -115,6 +115,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
115115
$io->title(sprintf('Current configuration for "%s.%s"', $extensionAlias, $path));
116116

117117
$io->writeln(Yaml::dump($config, 10));
118+
119+
return 0;
118120
}
119121

120122
private function compileContainer(): ContainerBuilder

src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected function configure()
114114
/**
115115
* {@inheritdoc}
116116
*/
117-
protected function execute(InputInterface $input, OutputInterface $output)
117+
protected function execute(InputInterface $input, OutputInterface $output): int
118118
{
119119
if ($input->getOption('show-private')) {
120120
@trigger_error('The "--show-private" option no longer has any effect and is deprecated since Symfony 4.1.', E_USER_DEPRECATED);
@@ -184,6 +184,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
184184
$errorIo->comment('To search for a specific service, re-run this command with a search term. (e.g. <comment>debug:container log</comment>)');
185185
}
186186
}
187+
188+
return 0;
187189
}
188190

189191
/**

src/Symfony/Bundle/FrameworkBundle/Command/EventDispatcherDebugCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected function configure()
6969
*
7070
* @throws \LogicException
7171
*/
72-
protected function execute(InputInterface $input, OutputInterface $output)
72+
protected function execute(InputInterface $input, OutputInterface $output): int
7373
{
7474
$io = new SymfonyStyle($input, $output);
7575

@@ -78,7 +78,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7878
if (!$this->dispatcher->hasListeners($event)) {
7979
$io->getErrorStyle()->warning(sprintf('The event "%s" does not have any registered listeners.', $event));
8080

81-
return;
81+
return 0;
8282
}
8383

8484
$options = ['event' => $event];
@@ -89,5 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
8989
$options['raw_text'] = $input->getOption('raw');
9090
$options['output'] = $io;
9191
$helper->describe($io, $this->dispatcher, $options);
92+
93+
return 0;
9294
}
9395
}

src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected function configure()
7373
*
7474
* @throws InvalidArgumentException When route does not exist
7575
*/
76-
protected function execute(InputInterface $input, OutputInterface $output)
76+
protected function execute(InputInterface $input, OutputInterface $output): int
7777
{
7878
$io = new SymfonyStyle($input, $output);
7979
$name = $input->getArgument('name');
@@ -105,6 +105,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
105105
'output' => $io,
106106
]);
107107
}
108+
109+
return 0;
108110
}
109111

110112
private function findRouteNameContaining(string $name, RouteCollection $routes): array

src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ protected function configure()
124124
/**
125125
* {@inheritdoc}
126126
*/
127-
protected function execute(InputInterface $input, OutputInterface $output)
127+
protected function execute(InputInterface $input, OutputInterface $output): int
128128
{
129129
$io = new SymfonyStyle($input, $output);
130130

@@ -246,7 +246,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
246246

247247
$io->getErrorStyle()->warning($outputMessage);
248248

249-
return;
249+
return 0;
250250
}
251251

252252
// Load the fallback catalogues
@@ -295,6 +295,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
295295
}
296296

297297
$io->table($headers, $rows);
298+
299+
return 0;
298300
}
299301

300302
private function formatState(int $state): string

src/Symfony/Bundle/FrameworkBundle/Command/WorkflowDumpCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected function configure()
5959
/**
6060
* {@inheritdoc}
6161
*/
62-
protected function execute(InputInterface $input, OutputInterface $output)
62+
protected function execute(InputInterface $input, OutputInterface $output): int
6363
{
6464
$container = $this->getApplication()->getKernel()->getContainer();
6565
$serviceId = $input->getArgument('name');
@@ -97,5 +97,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
9797
],
9898
];
9999
$output->writeln($dumper->dump($workflow->getDefinition(), $marking, $options));
100+
101+
return 0;
100102
}
101103
}

src/Symfony/Bundle/WebServerBundle/Command/ServerLogCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
123123

124124
$this->displayLog($input, $output, $clientId, $record);
125125
}
126+
127+
return 0;
126128
}
127129

128130
private function getLogs($socket)

src/Symfony/Bundle/WebServerBundle/Command/ServerStartCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
164164
return 1;
165165
}
166166

167-
return null;
167+
return 0;
168168
}
169169
}

src/Symfony/Bundle/WebServerBundle/Command/ServerStatusCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
103103
}
104104
}
105105

106-
return null;
106+
return 0;
107107
}
108108
}

src/Symfony/Bundle/WebServerBundle/Command/ServerStopCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
7777
return 1;
7878
}
7979

80-
return null;
80+
return 0;
8181
}
8282
}

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* `Application` implements `ResetInterface`
1111
* marked all dispatched event classes as `@final`
1212
* added support for displaying table horizontally
13+
* deprecated returning `null` from `Command::execute()`, return `0` instead
1314

1415
4.3.0
1516
-----

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ protected function configure()
150150
* execute() method, you set the code to execute by passing
151151
* a Closure to the setCode() method.
152152
*
153-
* @return int|void void or 0 if everything went fine, or an exit code
153+
* @return int 0 if everything went fine, or an exit code
154154
*
155155
* @throws LogicException When this abstract method is not implemented
156156
*
@@ -253,6 +253,10 @@ public function run(InputInterface $input, OutputInterface $output)
253253
$statusCode = ($this->code)($input, $output);
254254
} else {
255255
$statusCode = $this->execute($input, $output);
256+
257+
if (!\is_int($statusCode)) {
258+
@trigger_error(sprintf('A non numeric or nullable $statusCode returned by Command::execute() is deprecated since Symfony 4.4, return an integer value instead.'), E_USER_DEPRECATED);
259+
}
256260
}
257261

258262
return is_numeric($statusCode) ? (int) $statusCode : 0;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7777
]);
7878

7979
$this->command = null;
80+
81+
return 0;
8082
}
8183
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
7474
'raw_text' => $input->getOption('raw'),
7575
'namespace' => $input->getArgument('namespace'),
7676
]);
77+
78+
return 0;
7779
}
7880

7981
private function createDefinition(): InputDefinition

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1837,9 +1837,11 @@ public function __construct()
18371837

18381838
class LazyCommand extends Command
18391839
{
1840-
public function execute(InputInterface $input, OutputInterface $output)
1840+
public function execute(InputInterface $input, OutputInterface $output): int
18411841
{
18421842
$output->writeln('lazy-command called');
1843+
1844+
return 0;
18431845
}
18441846
}
18451847

0 commit comments

Comments
 (0)
0