Test Failed
Pull Request — master (#58)
by Alexander
05:03 queued 02:29
created

Gii::getGenerator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 10
c 1
b 0
f 0
ccs 6
cts 6
cp 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii;
6
7
use Yiisoft\Yii\Gii\Exception\GeneratorNotFoundException;
8
9
final class Gii implements GiiInterface
10
{
11
    /**
12
     * @param array<string, GeneratorInterface> $generators
13
     */
14
    public function __construct(private array $generators)
15
    {
16 4
        $this->generators = array_combine(
17
            array_map(fn (GeneratorInterface $generator) => $generator::getId(), $generators),
18
            array_values($this->generators)
19
        );
20 3
    }
21
22 3
    public function addGenerator(GeneratorInterface $generator): void
23
    {
24
        $this->generators[$generator::getId()] = $generator;
25 4
    }
26
27 4
    public function getGenerator(string $id): GeneratorInterface
28 1
    {
29
        if (!isset($this->generators[$id])) {
30 3
            var_dump($this->generators);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->generators) looks like debug code. Are you sure you do not want to remove it?
Loading history...
31 3
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Yiisoft\Yii\Gii\GeneratorInterface. 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...
32 1
            throw new GeneratorNotFoundException('Generator "' . $id . '" not found');
0 ignored issues
show
Unused Code introduced by
ThrowNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
33
        }
34
35 3
        return $this->generators[$id];
36 1
    }
37
}
38