Skip to content

Commit 0fff957

Browse files
committed
[Console] Added return types
1 parent 3fca3cb commit 0fff957

File tree

9 files changed

+45
-33
lines changed

9 files changed

+45
-33
lines changed

console.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ want a command to create a user::
3838
// the name of the command (the part after "bin/console")
3939
protected static $defaultName = 'app:create-user';
4040

41-
protected function configure()
41+
protected function configure(): void
4242
{
4343
// ...
4444
}
4545

46-
protected function execute(InputInterface $input, OutputInterface $output)
46+
protected function execute(InputInterface $input, OutputInterface $output): int
4747
{
4848
// ...
4949

@@ -58,7 +58,7 @@ You can optionally define a description, help message and the
5858
:doc:`input options and arguments </console/input>`::
5959

6060
// ...
61-
protected function configure()
61+
protected function configure(): void
6262
{
6363
$this
6464
// the short description shown while running "php bin/console list"
@@ -93,7 +93,7 @@ available in the ``configure()`` method::
9393
parent::__construct();
9494
}
9595

96-
protected function configure()
96+
protected function configure(): void
9797
{
9898
$this
9999
// ...
@@ -129,7 +129,7 @@ The ``execute()`` method has access to the output stream to write messages to
129129
the console::
130130

131131
// ...
132-
protected function execute(InputInterface $input, OutputInterface $output)
132+
protected function execute(InputInterface $input, OutputInterface $output): int
133133
{
134134
// outputs multiple lines to the console (adding "\n" at the end of each line)
135135
$output->writeln([
@@ -179,7 +179,7 @@ which returns an instance of
179179

180180
class MyCommand extends Command
181181
{
182-
protected function execute(InputInterface $input, OutputInterface $output)
182+
protected function execute(InputInterface $input, OutputInterface $output): int
183183
{
184184
$section1 = $output->section();
185185
$section2 = $output->section();
@@ -221,7 +221,7 @@ Use input options or arguments to pass information to the command::
221221
use Symfony\Component\Console\Input\InputArgument;
222222

223223
// ...
224-
protected function configure()
224+
protected function configure(): void
225225
{
226226
$this
227227
// configure an argument
@@ -231,7 +231,7 @@ Use input options or arguments to pass information to the command::
231231
}
232232

233233
// ...
234-
public function execute(InputInterface $input, OutputInterface $output)
234+
public function execute(InputInterface $input, OutputInterface $output): int
235235
{
236236
$output->writeln([
237237
'User Creator',
@@ -285,7 +285,7 @@ as a service, you can use normal dependency injection. Imagine you have a
285285

286286
// ...
287287

288-
protected function execute(InputInterface $input, OutputInterface $output)
288+
protected function execute(InputInterface $input, OutputInterface $output): int
289289
{
290290
// ...
291291

console/calling_commands.rst

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,32 @@ or if you want to create a "meta" command that runs a bunch of other commands
88
changed on the production servers: clearing the cache, generating Doctrine
99
proxies, dumping web assets, ...).
1010

11-
Calling a command from another one is straightforward::
11+
.. code-block:: php
1212
13+
// ...
14+
use Symfony\Component\Console\Command;
1315
use Symfony\Component\Console\Input\ArrayInput;
1416
use Symfony\Component\Console\Input\InputInterface;
1517
use Symfony\Component\Console\Output\OutputInterface;
16-
// ...
1718
18-
protected function execute(InputInterface $input, OutputInterface $output)
19+
class CreateUserCommand extends Command
1920
{
20-
$command = $this->getApplication()->find('demo:greet');
21+
// ...
2122
22-
$arguments = [
23-
'name' => 'Fabien',
24-
'--yell' => true,
25-
];
23+
protected function execute(InputInterface $input, OutputInterface $output): void
24+
{
25+
$command = $this->getApplication()->find('demo:greet');
2626
27-
$greetInput = new ArrayInput($arguments);
28-
$returnCode = $command->run($greetInput, $output);
27+
$arguments = [
28+
'name' => 'Fabien',
29+
'--yell' => true,
30+
];
2931
30-
// ...
32+
$greetInput = new ArrayInput($arguments);
33+
$returnCode = $command->run($greetInput, $output);
34+
35+
// ...
36+
}
3137
}
3238
3339
First, you :method:`Symfony\\Component\\Console\\Application::find` the

console/command_in_controller.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Run this command from inside your controller via::
3636

3737
class SpoolController extends AbstractController
3838
{
39-
public function sendSpool($messages = 10, KernelInterface $kernel)
39+
public function sendSpool(int $messages = 10, KernelInterface $kernel): Response
4040
{
4141
$application = new Application($kernel);
4242
$application->setAutoExit(false);
@@ -87,7 +87,7 @@ Now, use it in your controller::
8787

8888
class SpoolController extends AbstractController
8989
{
90-
public function sendSpool($messages = 10)
90+
public function sendSpool(int $messages = 10): Response
9191
{
9292
// ...
9393
$output = new BufferedOutput(

console/commands_as_services.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ For example, suppose you want to log something from within your command::
3535
parent::__construct();
3636
}
3737

38-
protected function configure()
38+
protected function configure(): void
3939
{
4040
$this
4141
->setDescription('Good morning!');
4242
}
4343

44-
protected function execute(InputInterface $input, OutputInterface $output)
44+
protected function execute(InputInterface $input, OutputInterface $output): int
4545
{
4646
$this->logger->info('Waking up the sun');
4747
// ...
48-
48+
4949
return 0;
5050
}
5151
}

console/hide_commands.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ In those cases, you can define the command as **hidden** by setting the
2020
{
2121
protected static $defaultName = 'app:legacy';
2222

23-
protected function configure()
23+
protected function configure(): void
2424
{
2525
$this
2626
->setHidden(true)

console/input.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ and make the ``name`` argument required::
2121
{
2222
// ...
2323

24-
protected function configure()
24+
protected function configure(): void
2525
{
2626
$this
2727
// ...
@@ -42,7 +42,7 @@ You now have access to a ``last_name`` argument in your command::
4242
{
4343
// ...
4444

45-
protected function execute(InputInterface $input, OutputInterface $output)
45+
protected function execute(InputInterface $input, OutputInterface $output): int
4646
{
4747
$text = 'Hi '.$input->getArgument('name');
4848

@@ -52,6 +52,8 @@ You now have access to a ``last_name`` argument in your command::
5252
}
5353

5454
$output->writeln($text.'!');
55+
56+
return 0;
5557
}
5658
}
5759

console/lockable_trait.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ that adds two convenient methods to lock and release commands::
2222

2323
// ...
2424

25-
protected function execute(InputInterface $input, OutputInterface $output)
25+
protected function execute(InputInterface $input, OutputInterface $output): int
2626
{
2727
if (!$this->lock()) {
2828
$output->writeln('The command is already running in another process.');
@@ -38,6 +38,8 @@ that adds two convenient methods to lock and release commands::
3838
// if not released explicitly, Symfony releases the lock
3939
// automatically when the execution of the command ends
4040
$this->release();
41+
42+
return 0;
4143
}
4244
}
4345

console/style.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Consider for example the code used to display the title of the following command
2121
{
2222
// ...
2323

24-
protected function execute(InputInterface $input, OutputInterface $output)
24+
protected function execute(InputInterface $input, OutputInterface $output): int
2525
{
2626
$output->writeln([
2727
'<info>Lorem Ipsum Dolor Sit Amet</>',
@@ -62,7 +62,7 @@ title of the command::
6262
{
6363
// ...
6464

65-
protected function execute(InputInterface $input, OutputInterface $output)
65+
protected function execute(InputInterface $input, OutputInterface $output): int
6666
{
6767
$io = new SymfonyStyle($input, $output);
6868
$io->title('Lorem Ipsum Dolor Sit Amet');
@@ -399,7 +399,7 @@ of your commands to change their appearance::
399399
{
400400
// ...
401401

402-
protected function execute(InputInterface $input, OutputInterface $output)
402+
protected function execute(InputInterface $input, OutputInterface $output): int
403403
{
404404
// Before
405405
$io = new SymfonyStyle($input, $output);

console/verbosity.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ level. For example::
4949
{
5050
// ...
5151

52-
public function execute(InputInterface $input, OutputInterface $output)
52+
public function execute(InputInterface $input, OutputInterface $output): int
5353
{
5454
$user = new User(...);
5555

@@ -68,6 +68,8 @@ level. For example::
6868
'Will only be printed in verbose mode or higher',
6969
OutputInterface::VERBOSITY_VERBOSE
7070
);
71+
72+
return 0;
7173
}
7274
}
7375

0 commit comments

Comments
 (0)