Test Failed
Push — master ( ee8b01...2ca085 )
by Kirill
03:55
created

HasInheritance::isExtends()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 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 $extends = [];
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 8
    public function getParents(): iterable
35
    {
36 8
        foreach ($this->extends as $parent) {
37 8
            yield $this->fetch($parent);
38
        }
39 8
    }
40
41
    /**
42
     * @param string $name
43
     * @return bool
44
     */
45
    public function hasParent(string $name): bool
46
    {
47
        return \in_array($name, $this->extends, 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->extends, true) ? $this->fetch($name) : null;
57
    }
58
59
    /**
60
     * @param TypeDefinition|string ...$definitions
61
     * @return ProvidesInheritance|$this
62
     */
63 89
    public function extends(...$definitions): ProvidesInheritance
64
    {
65 89
        foreach ($definitions as $definition) {
66 89
            $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...
67
        }
68
69 89
        return $this;
70
    }
71
72
    /**
73
     * @param string|TypeDefinition $type
74
     * @return bool
75
     */
76 8
    public function isExtends($type): bool
77
    {
78 8
        $definition = $this->fetch($type);
79
80 8
        foreach ($this->getParents() as $parent) {
81 8
            if ($parent->instanceOf($definition)) {
82 8
                return true;
83
            }
84
        }
85
86 8
        return false;
87
    }
88
}
89