Skip to content

Commit 3082fe5

Browse files
committed
minor #17176 [Console] Add return for execute() (mohamedGasmii)
This PR was merged into the 5.4 branch. Discussion ---------- [Console] Add return for execute() Add return hint for execute() & add return Command::SUCCESS as return. <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/releases for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `6.x` for features of unreleased versions). --> Commits ------- 11b4dbd [Console] Add return hint
2 parents 0dde9fd + 11b4dbd commit 3082fe5

File tree

1 file changed

+55
-13
lines changed

1 file changed

+55
-13
lines changed

components/console/helpers/questionhelper.rst

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ the following to your command::
3434
{
3535
// ...
3636

37-
public function execute(InputInterface $input, OutputInterface $output)
37+
public function execute(InputInterface $input, OutputInterface $output): int
3838
{
3939
$helper = $this->getHelper('question');
4040
$question = new ConfirmationQuestion('Continue with this action?', false);
@@ -75,12 +75,16 @@ if you want to know a bundle name, you can add this to your command::
7575
use Symfony\Component\Console\Question\Question;
7676

7777
// ...
78-
public function execute(InputInterface $input, OutputInterface $output)
78+
public function execute(InputInterface $input, OutputInterface $output): int
7979
{
8080
// ...
8181
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
8282

8383
$bundleName = $helper->ask($input, $output, $question);
84+
85+
// ... do something with the bundleName
86+
87+
return Commande::SUCCESS;
8488
}
8589

8690
The user will be asked "Please enter the name of the bundle". They can type
@@ -99,7 +103,7 @@ from a predefined list::
99103
use Symfony\Component\Console\Question\ChoiceQuestion;
100104

101105
// ...
102-
public function execute(InputInterface $input, OutputInterface $output)
106+
public function execute(InputInterface $input, OutputInterface $output): int
103107
{
104108
// ...
105109
$helper = $this->getHelper('question');
@@ -115,6 +119,8 @@ from a predefined list::
115119
$output->writeln('You have just selected: '.$color);
116120

117121
// ... do something with the color
122+
123+
return Commande::SUCCESS;
118124
}
119125

120126
.. versionadded:: 5.2
@@ -142,7 +148,7 @@ this use :method:`Symfony\\Component\\Console\\Question\\ChoiceQuestion::setMult
142148
use Symfony\Component\Console\Question\ChoiceQuestion;
143149

144150
// ...
145-
public function execute(InputInterface $input, OutputInterface $output)
151+
public function execute(InputInterface $input, OutputInterface $output): int
146152
{
147153
// ...
148154
$helper = $this->getHelper('question');
@@ -155,6 +161,8 @@ this use :method:`Symfony\\Component\\Console\\Question\\ChoiceQuestion::setMult
155161

156162
$colors = $helper->ask($input, $output, $question);
157163
$output->writeln('You have just selected: ' . implode(', ', $colors));
164+
165+
return Commande::SUCCESS;
158166
}
159167

160168
Now, when the user enters ``1,2``, the result will be:
@@ -172,7 +180,7 @@ will be autocompleted as the user types::
172180
use Symfony\Component\Console\Question\Question;
173181

174182
// ...
175-
public function execute(InputInterface $input, OutputInterface $output)
183+
public function execute(InputInterface $input, OutputInterface $output): int
176184
{
177185
// ...
178186
$helper = $this->getHelper('question');
@@ -182,6 +190,10 @@ will be autocompleted as the user types::
182190
$question->setAutocompleterValues($bundles);
183191

184192
$bundleName = $helper->ask($input, $output, $question);
193+
194+
// ... do something with the bundleName
195+
196+
return Commande::SUCCESS;
185197
}
186198

187199
In more complex use cases, it may be necessary to generate suggestions on the
@@ -191,7 +203,7 @@ provide a callback function to dynamically generate suggestions::
191203
use Symfony\Component\Console\Question\Question;
192204

193205
// ...
194-
public function execute(InputInterface $input, OutputInterface $output)
206+
public function execute(InputInterface $input, OutputInterface $output): int
195207
{
196208
$helper = $this->getHelper('question');
197209

@@ -217,6 +229,10 @@ provide a callback function to dynamically generate suggestions::
217229
$question->setAutocompleterCallback($callback);
218230

219231
$filePath = $helper->ask($input, $output, $question);
232+
233+
// ... do something with the filePath
234+
235+
return Commande::SUCCESS;
220236
}
221237

222238
Do not Trim the Answer
@@ -228,7 +244,7 @@ You can also specify if you want to not trim the answer by setting it directly w
228244
use Symfony\Component\Console\Question\Question;
229245

230246
// ...
231-
public function execute(InputInterface $input, OutputInterface $output)
247+
public function execute(InputInterface $input, OutputInterface $output): int
232248
{
233249
// ...
234250
$helper = $this->getHelper('question');
@@ -237,6 +253,10 @@ You can also specify if you want to not trim the answer by setting it directly w
237253
$question->setTrimmable(false);
238254
// if the users inputs 'elsa ' it will not be trimmed and you will get 'elsa ' as value
239255
$name = $helper->ask($input, $output, $question);
256+
257+
// ... do something with the name
258+
259+
return Commande::SUCCESS;
240260
}
241261

242262
Accept Multiline Answers
@@ -255,7 +275,7 @@ the response to a question should allow multiline answers by passing ``true`` to
255275
use Symfony\Component\Console\Question\Question;
256276

257277
// ...
258-
public function execute(InputInterface $input, OutputInterface $output)
278+
public function execute(InputInterface $input, OutputInterface $output): int
259279
{
260280
// ...
261281
$helper = $this->getHelper('question');
@@ -264,6 +284,10 @@ the response to a question should allow multiline answers by passing ``true`` to
264284
$question->setMultiline(true);
265285

266286
$answer = $helper->ask($input, $output, $question);
287+
288+
// ... do something with the answer
289+
290+
return Commande::SUCCESS;
267291
}
268292

269293
Multiline questions stop reading user input after receiving an end-of-transmission
@@ -278,7 +302,7 @@ convenient for passwords::
278302
use Symfony\Component\Console\Question\Question;
279303

280304
// ...
281-
public function execute(InputInterface $input, OutputInterface $output)
305+
public function execute(InputInterface $input, OutputInterface $output): int
282306
{
283307
// ...
284308
$helper = $this->getHelper('question');
@@ -288,6 +312,10 @@ convenient for passwords::
288312
$question->setHiddenFallback(false);
289313

290314
$password = $helper->ask($input, $output, $question);
315+
316+
// ... do something with the password
317+
318+
return Commande::SUCCESS;
291319
}
292320

293321
.. caution::
@@ -311,13 +339,15 @@ convenient for passwords::
311339
use Symfony\Component\Console\Question\ChoiceQuestion;
312340

313341
// ...
314-
public function execute(InputInterface $input, OutputInterface $output)
342+
public function execute(InputInterface $input, OutputInterface $output): int
315343
{
316344
// ...
317345
$helper = $this->getHelper('question');
318346
QuestionHelper::disableStty();
319347

320348
// ...
349+
350+
return Commande::SUCCESS;
321351
}
322352

323353
Normalizing the Answer
@@ -333,7 +363,7 @@ method::
333363
use Symfony\Component\Console\Question\Question;
334364

335365
// ...
336-
public function execute(InputInterface $input, OutputInterface $output)
366+
public function execute(InputInterface $input, OutputInterface $output): int
337367
{
338368
// ...
339369
$helper = $this->getHelper('question');
@@ -345,6 +375,10 @@ method::
345375
});
346376

347377
$bundleName = $helper->ask($input, $output, $question);
378+
379+
// ... do something with the bundleName
380+
381+
return Commande::SUCCESS;
348382
}
349383

350384
.. caution::
@@ -367,7 +401,7 @@ method::
367401
use Symfony\Component\Console\Question\Question;
368402

369403
// ...
370-
public function execute(InputInterface $input, OutputInterface $output)
404+
public function execute(InputInterface $input, OutputInterface $output): int
371405
{
372406
// ...
373407
$helper = $this->getHelper('question');
@@ -385,6 +419,10 @@ method::
385419
$question->setMaxAttempts(2);
386420

387421
$bundleName = $helper->ask($input, $output, $question);
422+
423+
// ... do something with the bundleName
424+
425+
return Commande::SUCCESS;
388426
}
389427

390428
The ``$validator`` is a callback which handles the validation. It should
@@ -423,7 +461,7 @@ You can also use a validator with a hidden question::
423461
use Symfony\Component\Console\Question\Question;
424462

425463
// ...
426-
public function execute(InputInterface $input, OutputInterface $output)
464+
public function execute(InputInterface $input, OutputInterface $output): int
427465
{
428466
// ...
429467
$helper = $this->getHelper('question');
@@ -443,6 +481,10 @@ You can also use a validator with a hidden question::
443481
$question->setMaxAttempts(20);
444482

445483
$password = $helper->ask($input, $output, $question);
484+
485+
// ... do something with the password
486+
487+
return Commande::SUCCESS;
446488
}
447489

448490
Testing a Command that Expects Input

0 commit comments

Comments
 (0)