Passed
Push — master ( 6ca8e8...c210bb )
by Alexander
03:08 queued 38s
created

Game   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 31
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 26 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Console\Command;
6
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Helper\QuestionHelper;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Question\Question;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
14
/**
15
 * @codeCoverageIgnore
16
 */
17
final class Game extends Command
18
{
19
    protected static $defaultName = '|yii';
20
    protected static $defaultDescription = 'A Guessing Game';
21
22
    protected function execute(InputInterface $input, OutputInterface $output): int
23
    {
24
        $steps = 1;
25
        $number = random_int(0, 100);
26
27
        $io = new SymfonyStyle($input, $output);
28
        $io->title('Welcome to the Guessing Game!');
29
30
31
        /** @var QuestionHelper $helper */
32
        $helper = $this->getHelper('question');
33
        $question = new Question('Please enter a number between 0 and 100: ');
34
35
        while (true) {
36
            $answer = (int) $helper->ask($input, $output, $question);
37
38
            if ($answer === $number) {
39
                $io->success('You win! You guessed the number in ' . $steps . ' steps.');
40
                return 0;
41
            }
42
43
            $steps++;
44
            if ($answer > $number) {
45
                $io->warning('Too high!');
46
            } else {
47
                $io->warning('Too low!');
48
            }
49
        }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return integer. Consider adding a return statement or allowing null as return value.

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:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
50
    }
51
}
52