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

HasInterfaces::hasInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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