Passed
Pull Request — master (#183)
by Alexander
03:25 queued 55s
created

Game::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
c 2
b 0
f 0
nc 4
nop 2
dl 0
loc 26
ccs 0
cts 16
cp 0
crap 20
rs 9.7333
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
final class Game extends Command
15
{
16
    protected static $defaultName = '|yii';
17
    protected static $defaultDescription = 'A Guessing Game';
18
19
    protected function execute(InputInterface $input, OutputInterface $output): int
20
    {
21
        $steps = 1;
22
        $number = random_int(0, 100);
23
24
        $io = new SymfonyStyle($input, $output);
25
        $io->title('Welcome to the Guessing Game!');
26
27
28
        /** @var QuestionHelper $helper */
29
        $helper = $this->getHelper('question');
30
        $question = new Question('Please enter a number between 0 and 100: ');
31
32
        while (true) {
33
            $answer = (int) $helper->ask($input, $output, $question);
34
35
            if ($answer === $number) {
36
                $io->success('You win! You guessed the number in ' . $steps . ' steps.');
37
                return 0;
38
            }
39
40
            $steps++;
41
            if ($answer > $number) {
42
                $io->warning('Too high!');
43
            } else {
44
                $io->warning('Too low!');
45
            }
46
        }
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...
47
    }
48
}
49