From 11b4dbdab659fa345598159b1a866ec383e94850 Mon Sep 17 00:00:00 2001 From: mohamed gasmi Date: Sun, 14 Aug 2022 22:59:47 +0200 Subject: [PATCH] [Console] Add return hint Add return hint for execute() & add return Command::SUCCESS as return. --- components/console/helpers/questionhelper.rst | 68 +++++++++++++++---- 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/components/console/helpers/questionhelper.rst b/components/console/helpers/questionhelper.rst index ff15953dc06..a4d61352745 100644 --- a/components/console/helpers/questionhelper.rst +++ b/components/console/helpers/questionhelper.rst @@ -34,7 +34,7 @@ the following to your command:: { // ... - public function execute(InputInterface $input, OutputInterface $output) + public function execute(InputInterface $input, OutputInterface $output): int { $helper = $this->getHelper('question'); $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:: use Symfony\Component\Console\Question\Question; // ... - public function execute(InputInterface $input, OutputInterface $output) + public function execute(InputInterface $input, OutputInterface $output): int { // ... $question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle'); $bundleName = $helper->ask($input, $output, $question); + + // ... do something with the bundleName + + return Commande::SUCCESS; } The user will be asked "Please enter the name of the bundle". They can type @@ -99,7 +103,7 @@ from a predefined list:: use Symfony\Component\Console\Question\ChoiceQuestion; // ... - public function execute(InputInterface $input, OutputInterface $output) + public function execute(InputInterface $input, OutputInterface $output): int { // ... $helper = $this->getHelper('question'); @@ -115,6 +119,8 @@ from a predefined list:: $output->writeln('You have just selected: '.$color); // ... do something with the color + + return Commande::SUCCESS; } .. versionadded:: 5.2 @@ -142,7 +148,7 @@ this use :method:`Symfony\\Component\\Console\\Question\\ChoiceQuestion::setMult use Symfony\Component\Console\Question\ChoiceQuestion; // ... - public function execute(InputInterface $input, OutputInterface $output) + public function execute(InputInterface $input, OutputInterface $output): int { // ... $helper = $this->getHelper('question'); @@ -155,6 +161,8 @@ this use :method:`Symfony\\Component\\Console\\Question\\ChoiceQuestion::setMult $colors = $helper->ask($input, $output, $question); $output->writeln('You have just selected: ' . implode(', ', $colors)); + + return Commande::SUCCESS; } Now, when the user enters ``1,2``, the result will be: @@ -172,7 +180,7 @@ will be autocompleted as the user types:: use Symfony\Component\Console\Question\Question; // ... - public function execute(InputInterface $input, OutputInterface $output) + public function execute(InputInterface $input, OutputInterface $output): int { // ... $helper = $this->getHelper('question'); @@ -182,6 +190,10 @@ will be autocompleted as the user types:: $question->setAutocompleterValues($bundles); $bundleName = $helper->ask($input, $output, $question); + + // ... do something with the bundleName + + return Commande::SUCCESS; } 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:: use Symfony\Component\Console\Question\Question; // ... - public function execute(InputInterface $input, OutputInterface $output) + public function execute(InputInterface $input, OutputInterface $output): int { $helper = $this->getHelper('question'); @@ -217,6 +229,10 @@ provide a callback function to dynamically generate suggestions:: $question->setAutocompleterCallback($callback); $filePath = $helper->ask($input, $output, $question); + + // ... do something with the filePath + + return Commande::SUCCESS; } 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 use Symfony\Component\Console\Question\Question; // ... - public function execute(InputInterface $input, OutputInterface $output) + public function execute(InputInterface $input, OutputInterface $output): int { // ... $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 $question->setTrimmable(false); // if the users inputs 'elsa ' it will not be trimmed and you will get 'elsa ' as value $name = $helper->ask($input, $output, $question); + + // ... do something with the name + + return Commande::SUCCESS; } Accept Multiline Answers @@ -255,7 +275,7 @@ the response to a question should allow multiline answers by passing ``true`` to use Symfony\Component\Console\Question\Question; // ... - public function execute(InputInterface $input, OutputInterface $output) + public function execute(InputInterface $input, OutputInterface $output): int { // ... $helper = $this->getHelper('question'); @@ -264,6 +284,10 @@ the response to a question should allow multiline answers by passing ``true`` to $question->setMultiline(true); $answer = $helper->ask($input, $output, $question); + + // ... do something with the answer + + return Commande::SUCCESS; } Multiline questions stop reading user input after receiving an end-of-transmission @@ -278,7 +302,7 @@ convenient for passwords:: use Symfony\Component\Console\Question\Question; // ... - public function execute(InputInterface $input, OutputInterface $output) + public function execute(InputInterface $input, OutputInterface $output): int { // ... $helper = $this->getHelper('question'); @@ -288,6 +312,10 @@ convenient for passwords:: $question->setHiddenFallback(false); $password = $helper->ask($input, $output, $question); + + // ... do something with the password + + return Commande::SUCCESS; } .. caution:: @@ -311,13 +339,15 @@ convenient for passwords:: use Symfony\Component\Console\Question\ChoiceQuestion; // ... - public function execute(InputInterface $input, OutputInterface $output) + public function execute(InputInterface $input, OutputInterface $output): int { // ... $helper = $this->getHelper('question'); QuestionHelper::disableStty(); // ... + + return Commande::SUCCESS; } Normalizing the Answer @@ -333,7 +363,7 @@ method:: use Symfony\Component\Console\Question\Question; // ... - public function execute(InputInterface $input, OutputInterface $output) + public function execute(InputInterface $input, OutputInterface $output): int { // ... $helper = $this->getHelper('question'); @@ -345,6 +375,10 @@ method:: }); $bundleName = $helper->ask($input, $output, $question); + + // ... do something with the bundleName + + return Commande::SUCCESS; } .. caution:: @@ -367,7 +401,7 @@ method:: use Symfony\Component\Console\Question\Question; // ... - public function execute(InputInterface $input, OutputInterface $output) + public function execute(InputInterface $input, OutputInterface $output): int { // ... $helper = $this->getHelper('question'); @@ -385,6 +419,10 @@ method:: $question->setMaxAttempts(2); $bundleName = $helper->ask($input, $output, $question); + + // ... do something with the bundleName + + return Commande::SUCCESS; } 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:: use Symfony\Component\Console\Question\Question; // ... - public function execute(InputInterface $input, OutputInterface $output) + public function execute(InputInterface $input, OutputInterface $output): int { // ... $helper = $this->getHelper('question'); @@ -443,6 +481,10 @@ You can also use a validator with a hidden question:: $question->setMaxAttempts(20); $password = $helper->ask($input, $output, $question); + + // ... do something with the password + + return Commande::SUCCESS; } Testing a Command that Expects Input