yiisoft /
yii-console
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Yiisoft\Yii\Console\Command; |
||
| 6 | |||
| 7 | use Symfony\Component\Console\Attribute\AsCommand; |
||
| 8 | use Symfony\Component\Console\Command\Command; |
||
| 9 | use Symfony\Component\Console\Helper\QuestionHelper; |
||
| 10 | use Symfony\Component\Console\Input\InputInterface; |
||
| 11 | use Symfony\Component\Console\Output\OutputInterface; |
||
| 12 | use Symfony\Component\Console\Question\Question; |
||
| 13 | use Symfony\Component\Console\Style\SymfonyStyle; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @codeCoverageIgnore |
||
| 17 | */ |
||
| 18 | #[AsCommand('|yii', 'A Guessing Game')] |
||
| 19 | final class Game extends Command |
||
| 20 | { |
||
| 21 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 22 | { |
||
| 23 | $steps = 1; |
||
| 24 | $number = random_int(0, 100); |
||
| 25 | |||
| 26 | $io = new SymfonyStyle($input, $output); |
||
| 27 | $io->title('Welcome to the Guessing Game!'); |
||
| 28 | |||
| 29 | |||
| 30 | /** @var QuestionHelper $helper */ |
||
| 31 | $helper = $this->getHelper('question'); |
||
| 32 | $question = new Question('Please enter a number between 0 and 100: '); |
||
| 33 | |||
| 34 | while (true) { |
||
| 35 | $answer = (int) $helper->ask($input, $output, $question); |
||
| 36 | |||
| 37 | if ($answer === $number) { |
||
| 38 | $io->success('You win! You guessed the number in ' . $steps . ' steps.'); |
||
| 39 | return 0; |
||
| 40 | } |
||
| 41 | |||
| 42 | $steps++; |
||
| 43 | if ($answer > $number) { |
||
| 44 | $io->warning('Too high!'); |
||
| 45 | } else { |
||
| 46 | $io->warning('Too low!'); |
||
| 47 | } |
||
| 48 | } |
||
|
0 ignored issues
–
show
|
|||
| 49 | } |
||
| 50 | } |
||
| 51 |
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: