Test Failed
Push — master ( 80fd82...ee8b01 )
by Kirill
02:18
created

HasDefinitions::withDefinition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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\Reflection\Definition\Behaviour;
11
12
use Railt\Reflection\Contracts\Definition\Behaviour\ProvidesTypeDefinitions;
13
use Railt\Reflection\Contracts\Definition\TypeDefinition;
14
15
/**
16
 * Trait HasDefinitions
17
 */
18
trait HasDefinitions
19
{
20
    /**
21
     * @var array|string[]
22
     */
23
    protected $types = [];
24
25
    /**
26
     * @param string|TypeDefinition $type
27
     * @return TypeDefinition
28
     */
29
    abstract protected function fetch($type): TypeDefinition;
30
31
    /**
32
     * @return iterable|TypeDefinition[]
33
     */
34 4
    public function getDefinitions(): iterable
35
    {
36 4
        foreach ($this->types as $type) {
37 2
            yield $this->fetch($type);
38
        }
39 4
    }
40
41
    /**
42
     * @param string $name
43
     * @return bool
44
     */
45 4
    public function hasDefinition(string $name): bool
46
    {
47 4
        return \in_array($name, $this->types, true);
48
    }
49
50
    /**
51
     * @param string $name
52
     * @return null|TypeDefinition
53
     */
54 4
    public function getDefinition(string $name): ?TypeDefinition
55
    {
56 4
        if (! \in_array($name, $this->types, true)) {
57 2
            return null;
58
        }
59
60 2
        return $this->fetch($name);
61
    }
62
63
    /**
64
     * @param string|TypeDefinition $type
65
     * @return ProvidesTypeDefinitions
66
     */
67 17
    public function withDefinition($type): ProvidesTypeDefinitions
68
    {
69 17
        if ($type instanceof TypeDefinition) {
70 17
            $type = $type->getName();
71
        }
72
73 17
        $this->types[] = $type;
74
75 17
        return $this;
76
    }
77
}
78