Passed
Pull Request — master (#124)
by Dmitriy
02:44
created

Gii   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 6
eloc 10
c 5
b 1
f 0
dl 0
loc 33
ccs 10
cts 14
cp 0.7143
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addGenerator() 0 3 1
A __construct() 0 5 1
A getGenerators() 0 5 1
A getGenerator() 0 8 3
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|GeneratorProxy> $proxies
13
     * @param array<string, GeneratorInterface> $instances
14
     */
15 8
    public function __construct(
16
        private readonly array $proxies,
17
        private array $instances,
18
    )
19
    {
20 8
    }
21
22
    public function addGenerator(GeneratorInterface $generator): void
23
    {
24
        $this->instances[$generator::getId()] = $generator;
25
    }
26
27 1
    public function getGenerator(string $id): GeneratorInterface
28
    {
29 1
        if (isset($this->instances[$id])) {
30
            return $this->instances[$id];
31
        }
32 1
        return isset($this->proxies[$id])
33
            ? $this->proxies[$id]->loadGenerator()
34 1
            : throw new GeneratorNotFoundException('Generator "' . $id . '" not found');
35
    }
36
37 4
    public function getGenerators(): array
38
    {
39 4
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($this->instances, $this->proxies) returns the type array<integer,array> which is incompatible with the return type mandated by Yiisoft\Yii\Gii\GiiInterface::getGenerators() of Yiisoft\Yii\Gii\GeneratorInterface[].

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
40 4
            ...$this->instances,
41 4
            ...$this->proxies,
42 4
        ];
43
    }
44
}
45