Test Failed
Push — master ( ab5890...690c53 )
by Kirill
02:48
created

HasInheritance::instanceOf()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 0
cp 0
rs 9.7
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
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 string|null
22
     */
23
    protected $extends;
24
25
    /**
26
     * @var string[]|array
27
     */
28
    protected $extendedBy = [];
29
30
    /**
31
     * @param string|TypeDefinition $type
32
     * @return TypeDefinition
33
     */
34
    abstract protected function fetch($type): TypeDefinition;
35
36
    /**
37
     * @return iterable|TypeDefinition[]
38
     */
39
    public function inheritedBy(): iterable
40
    {
41
        foreach ($this->extendedBy as $parent) {
42
            yield $this->fetch($parent);
43
        }
44
    }
45
46
    /**
47
     * @return null|TypeDefinition
48
     */
49 8
    public function getInheritedParent(): ?TypeDefinition
50
    {
51 8
        return $this->extends ? $this->fetch($this->extends) : null;
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function hasInheritance(): bool
58
    {
59
        return $this->extends !== null;
60
    }
61
62
    /**
63
     * @param TypeDefinition|string $definition
64
     * @return ProvidesInheritance|$this
65
     */
66
    public function extends($definition): ProvidesInheritance
67 89
    {
68
        $this->extends = $this->nameOf($definition);
0 ignored issues
show
Bug introduced by
It seems like nameOf() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
69 89
70
        if ($definition instanceof ProvidesInheritance) {
71 89
            /** @var HasInheritance $definition */
72 89
            $definition->extendedBy[] = $this->extends;
73
        }
74
75 89
        return $this;
76
    }
77
78
    /**
79
     * @param TypeDefinition|string $definition
80
     * @return ProvidesInheritance|$this
81 89
     */
82
    public function extendsBy($definition): ProvidesInheritance
83 89
    {
84 89
        /** @var HasInheritance $definition */
85
        $definition = $this->fetch($definition);
86
87
        $definition->extends($this);
88
89
        return $this;
90 8
    }
91
92 8
    /**
93
     * @param TypeDefinition|string $type
94 8
     * @return bool
95 8
     */
96
    public function extendsOf($type): bool
97
    {
98 8
        return $this->extends === $this->nameOf($type);
0 ignored issues
show
Bug introduced by
It seems like nameOf() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
99
    }
100
101
    /**
102
     * @param TypeDefinition|string $type
103
     * @return bool
104
     */
105
    public function instanceOf($type): bool
106
    {
107
        $context = $this->extends;
108
109
        while ($context) {
110
            /** @var TypeDefinition $context */
111
            $context = $this->fetch($context);
112
113
            if ($context->extendsOf($type)) {
114
                return true;
115
            }
116
117
            $context = $context->getInheritedParent();
118
        }
119
120
        return false;
121
    }
122
}
123