Test Failed
Push — master ( 63826d...8b4c91 )
by Kirill
02:24
created

HasInheritance::getChildrenInheritance()   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
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
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\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 getChildrenInheritance(): 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 getParentInheritance(): ?TypeDefinition
50
    {
51 8
        return $this->extends ? $this->fetch($this->extends) : null;
52
    }
53
54
    /**
55
     * @param string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
56
     * @return bool
57
     */
58
    public function hasParent(): bool
59
    {
60
        return $this->extends !== null;
61
    }
62
63
    /**
64
     * @param TypeDefinition|string $definition
65
     * @return ProvidesInheritance|$this
66
     */
67 89
    public function extends($definition): ProvidesInheritance
68
    {
69 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...
70
71 89
        if ($definition instanceof ProvidesInheritance) {
72 89
            $definition->extendsBy($this);
73
        }
74
75 89
        return $this;
76
    }
77
78
    /**
79
     * @param string|TypeDefinition $definition
80
     */
81 89
    private function extendsBy($definition): void
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
82
    {
83 89
        $this->extendedBy[] = $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...
84 89
    }
85
86
    /**
87
     * @param string|TypeDefinition $type
88
     * @return bool
89
     */
90 8
    public function isExtends($type): bool
91
    {
92 8
        $definition = $this->fetch($type);
93
94 8
        if ($parent = $this->getParentInheritance()) {
95 8
            return $parent->instanceOf($definition);
96
        }
97
98 8
        return false;
99
    }
100
}
101