Test Failed
Push — master ( dbe410...b8c007 )
by Kirill
02:19
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
use Railt\Reflection\Exception\TypeConflictException;
15
16
/**
17
 * Trait HasInheritance
18
 * @mixin ProvidesInheritance
19
 * @mixin TypeDefinition
20
 */
21
trait HasInheritance
22
{
23
    /**
24
     * @var array|string[]
25
     */
26
    protected $parents = [];
27
28
    /**
29
     * @param string|TypeDefinition $type
30
     * @return TypeDefinition
31
     */
32
    abstract protected function fetch($type): TypeDefinition;
33
34
    /**
35
     * @return iterable|TypeDefinition[]
36
     */
37 8
    public function getParents(): iterable
38
    {
39 8
        foreach ($this->parents as $parent) {
40 8
            yield $this->fetch($parent);
41
        }
42 8
    }
43
44
    /**
45
     * @param string $name
46
     * @return bool
47
     */
48
    public function hasParent(string $name): bool
49
    {
50
        return \in_array($name, $this->parents, true);
51
    }
52
53
    /**
54
     * @param string $name
55
     * @return null|TypeDefinition
56
     */
57
    public function getParent(string $name): ?TypeDefinition
58
    {
59
        return \in_array($name, $this->parents, true) ? $this->fetch($name) : null;
60
    }
61
62
    /**
63
     * @param TypeDefinition ...$definitions
64
     * @return ProvidesInheritance|$this
65
     * @throws TypeConflictException
66
     */
67 81
    public function extends(TypeDefinition ...$definitions): ProvidesInheritance
68
    {
69 81
        foreach ($definitions as $definition) {
70 81
            $this->verifyExtensionType($definition);
71
72 25
            $this->parents[] = $definition->getName();
73
        }
74
75 25
        return $this;
76
    }
77
78
    /**
79
     * @param TypeDefinition $def
80
     * @throws TypeConflictException
81
     */
82 81
    private function verifyExtensionType(TypeDefinition $def): void
83
    {
84 81
        if (! $def::getType()->is(static::getType()->getName())) {
85 56
            $error = \sprintf('Type %s can extends only %s types, but %s given.', $this, static::getType(), $def);
86 56
            throw $this->error(new TypeConflictException($error));
0 ignored issues
show
Bug introduced by
It seems like error() 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...
87
        }
88 25
    }
89
90
    /**
91
     * @param string|TypeDefinition $type
92
     * @return bool
93
     */
94 8
    public function isExtends($type): bool
95
    {
96 8
        $definition = $this->fetch($type);
97
98 8
        foreach ($this->getParents() as $parent) {
99 8
            if ($parent->instanceOf($definition)) {
100 8
                return true;
101
            }
102
        }
103
104 8
        return false;
105
    }
106
}
107