Test Failed
Push — master ( a77613...80fd82 )
by Kirill
02:23
created

HasInterfaces::getInterfaces()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
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\ProvidesInterfaces;
13
use Railt\Reflection\Contracts\Definition\InterfaceDefinition;
14
use Railt\Reflection\Contracts\Definition\TypeDefinition;
15
16
/**
17
 * Trait HasInterfaces
18
 */
19
trait HasInterfaces
20
{
21
    /**
22
     * @var array|string[]
23
     */
24
    protected $interfaces = [];
25
26
    /**
27
     * @param string|TypeDefinition $type
28
     * @return TypeDefinition
29
     */
30
    abstract protected function fetch($type): TypeDefinition;
31
32
    /**
33
     * @param string|TypeDefinition $interface
34
     * @return bool
35
     */
36
    public function isImplements($interface): bool
37
    {
38
        $definition = $this->fetch($interface);
39
40
        foreach ($this->getInterfaces() as $impl) {
41
            if ($impl->instanceOf($definition)) {
42
                return true;
43
            }
44
        }
45
46
        return false;
47
    }
48
49
    /**
50
     * @return iterable|InterfaceDefinition[]
51
     */
52
    public function getInterfaces(): iterable
53
    {
54
        foreach ($this->interfaces as $interface) {
55
            yield $this->fetch($interface);
56
        }
57
    }
58
59
    /**
60
     * @param string $name
61
     * @return bool
62
     */
63
    public function hasInterface(string $name): bool
64
    {
65
        return \in_array($name, $this->interfaces, true);
66
    }
67
68
    /**
69
     * @param string $name
70
     * @return InterfaceDefinition|TypeDefinition|null
71
     */
72
    public function getInterface(string $name): ?InterfaceDefinition
73
    {
74
        return \in_array($name, $this->interfaces, true) ? $this->fetch($name) : null;
75
    }
76
77
    /**
78
     * @param string|TypeDefinition ...$interfaces
79
     * @return ProvidesInterfaces|$this
80
     */
81
    public function withInterface(...$interfaces): ProvidesInterfaces
82
    {
83
        foreach ($interfaces as $interface) {
84
            $interface = $interface instanceof TypeDefinition ? $interface->getName() : $interface;
85
86
            $this->interfaces[] = $interface;
87
        }
88
89
        return $this;
90
    }
91
}
92
93