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

HasInheritance   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 73
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
fetch() 0 1 ?
A getParents() 0 6 2
A hasParent() 0 4 1
A getParent() 0 4 2
A extends() 0 10 3
A isExtends() 0 12 3
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\ProvidesInheritance;
13
use Railt\Reflection\Contracts\Definition\TypeDefinition;
14
15
/**
16
 * Trait HasInheritance
17
 */
18
trait HasInheritance
19
{
20
    /**
21
     * @var array|string[]
22
     */
23
    protected $parents = [];
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
    public function getParents(): iterable
35
    {
36
        foreach ($this->parents as $parent) {
37
            yield $this->fetch($parent);
38
        }
39
    }
40
41
    /**
42
     * @param string $name
43
     * @return bool
44
     */
45
    public function hasParent(string $name): bool
46
    {
47
        return \in_array($name, $this->parents, true);
48
    }
49
50
    /**
51
     * @param string $name
52
     * @return null|TypeDefinition
53
     */
54
    public function getParent(string $name): ?TypeDefinition
55
    {
56
        return \in_array($name, $this->parents, true) ? $this->fetch($name) : null;
57
    }
58
59
    /**
60
     * @param TypeDefinition|string ...$definitions
61
     * @return ProvidesInheritance|$this
62
     */
63
    public function extends(...$definitions): ProvidesInheritance
64
    {
65
        foreach ($definitions as $definition) {
66
            $definition = $definition instanceof TypeDefinition ? $definition->getName() : $definition;
67
68
            $this->parents[] = $definition->getName();
0 ignored issues
show
Bug introduced by
The method getName cannot be called on $definition (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
69
        }
70
71
        return $this;
72
    }
73
74
    /**
75
     * @param string|TypeDefinition $type
76
     * @return bool
77
     */
78
    public function isExtends($type): bool
79
    {
80
        $definition = $this->fetch($type);
81
82
        foreach ($this->getParents() as $parent) {
83
            if ($parent->instanceOf($definition)) {
84
                return true;
85
            }
86
        }
87
88
        return false;
89
    }
90
}
91