Completed
Push — master ( 296743...12eab9 )
by Kirill
36:17
created

Repository::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Reflection;
11
12
use Railt\SDL\Contracts\Definitions\Definition;
13
use Railt\SDL\Contracts\Definitions\TypeDefinition;
14
use Railt\SDL\Exceptions\TypeConflictException;
15
use Railt\SDL\Exceptions\TypeNotFoundException;
16
use Railt\SDL\Reflection\Builder\Process\Compilable;
17
use Railt\SDL\Runtime\CallStackInterface;
18
use Railt\SDL\Support;
19
20
/**
21
 * Class Repository
22
 */
23
class Repository implements Dictionary, \Countable, \IteratorAggregate
24
{
25
    use Support;
26
27
    /**
28
     * @var array|TypeDefinition[]
29
     */
30
    private $definitions = [];
31
32
    /**
33
     * @var CallStackInterface
34
     */
35
    protected $stack;
36
37
    /**
38
     * Repository constructor.
39
     * @param CallStackInterface $stack
40
     */
41 283
    public function __construct(CallStackInterface $stack)
42
    {
43 283
        $this->stack = $stack;
44 283
    }
45
46
    /**
47
     * @param TypeDefinition $type
48
     * @param bool $force
49
     * @return Dictionary
50
     * @throws TypeConflictException
51
     */
52 6577
    public function register(TypeDefinition $type, bool $force = false): Dictionary
53
    {
54 6577
        if (! $force && $this->has($type->getName())) {
55
            $error = \sprintf(
56
                'Can not declare %s, because the name %s already in use',
57
                $this->typeToString($type),
58
                $type->getName()
59
            );
60
61
            throw new TypeConflictException($error, $this->stack);
62
        }
63
64 6577
        $this->definitions[$type->getName()] = $type;
65
66 6577
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Railt\SDL\Reflection\Repository) is incompatible with the return type declared by the interface Railt\SDL\Reflection\Dictionary::register of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
67
    }
68
69
    /**
70
     * @param string $name
71
     * @param Definition|null $from
72
     * @return TypeDefinition
73
     */
74 6552
    public function get(string $name, Definition $from = null): TypeDefinition
75
    {
76 6552
        if ($this->has($name)) {
77 6552
            $result = $this->definitions[$name];
78
79 6552
            if ($result instanceof Compilable) {
80 2323
                $result->compile();
81
            }
82
83 6552
            return $result;
84
        }
85
86
        $error = \sprintf('Type "%s" not found', $name);
87
        throw new TypeNotFoundException($error, $this->stack);
88
    }
89
90
    /**
91
     * @param string $type
92
     * @return iterable|TypeDefinition[]
93
     */
94
    public function only(string $type): iterable
95
    {
96
        foreach ($this->definitions as $definition) {
97
            if ($definition instanceof $type) {
98
                yield $definition;
99
            }
100
        }
101
    }
102
103
    /**
104
     * @return iterable|TypeDefinition[]
105
     */
106
    public function all(): iterable
107
    {
108
        yield from $this->getIterator();
109
    }
110
111
    /**
112
     * @param string $name
113
     * @return bool
114
     */
115 6583
    public function has(string $name): bool
116
    {
117 6583
        return \array_key_exists($name, $this->definitions);
118
    }
119
120
    /**
121
     * @return \Traversable|TypeDefinition[]
122
     */
123
    public function getIterator(): \Traversable
124
    {
125
        return new \ArrayIterator(\array_values($this->definitions));
126
    }
127
128
    /**
129
     * @return int
130
     */
131
    public function count(): int
132
    {
133
        return \count($this->definitions);
134
    }
135
}
136