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

HasDefinitions   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 60
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
fetch() 0 1 ?
A getDefinitions() 0 6 2
A hasDefinition() 0 4 1
A getDefinition() 0 8 2
A withDefinition() 0 10 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