Test Failed
Push — master ( dbe410...b8c007 )
by Kirill
02:19
created

HasDefinitions   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 56
ccs 13
cts 13
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 6 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\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
 * @mixin ProvidesTypeDefinitions
18
 */
19
trait HasDefinitions
20
{
21
    /**
22
     * @var array|string[]
23
     */
24
    protected $types = [];
25
26
    /**
27
     * @param string|TypeDefinition $type
28
     * @return TypeDefinition
29
     */
30
    abstract protected function fetch($type): TypeDefinition;
31
32
    /**
33
     * @return iterable|TypeDefinition[]
34
     */
35 4
    public function getDefinitions(): iterable
36
    {
37 4
        foreach ($this->types as $type) {
38 2
            yield $this->fetch($type);
39
        }
40 4
    }
41
42
    /**
43
     * @param string $name
44
     * @return bool
45
     */
46 4
    public function hasDefinition(string $name): bool
47
    {
48 4
        return \in_array($name, $this->types, true);
49
    }
50
51
    /**
52
     * @param string $name
53
     * @return null|TypeDefinition
54
     */
55 4
    public function getDefinition(string $name): ?TypeDefinition
56
    {
57 4
        if (! \in_array($name, $this->types, true)) {
58 2
            return null;
59
        }
60
61 2
        return $this->fetch($name);
62
    }
63
64
    /**
65
     * @param string|TypeDefinition $type
66
     * @return ProvidesTypeDefinitions
67
     */
68 17
    public function withDefinition($type): ProvidesTypeDefinitions
69
    {
70 17
        $this->types[] = $this->fetch($type)->getName();
71
72 17
        return $this;
73
    }
74
}
75