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
|
|
|
*/ |
20
|
|
|
trait HasInheritance |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array|string[] |
24
|
|
|
*/ |
25
|
|
|
protected $parents = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return iterable|TypeDefinition[] |
29
|
|
|
*/ |
30
|
6 |
|
public function getParents(): iterable |
31
|
|
|
{ |
32
|
6 |
|
foreach ($this->parents as $parent) { |
33
|
6 |
|
yield $this->fetch($parent); |
|
|
|
|
34
|
|
|
} |
35
|
6 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $name |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
public function hasParent(string $name): bool |
42
|
|
|
{ |
43
|
|
|
return \in_array($name, $this->parents, true); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $name |
48
|
|
|
* @return null|TypeDefinition |
49
|
|
|
*/ |
50
|
|
|
public function getParent(string $name): ?TypeDefinition |
51
|
|
|
{ |
52
|
|
|
return \in_array($name, $this->parents, true) ? $this->fetch($name) : null; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param TypeDefinition ...$definitions |
57
|
|
|
* @return ProvidesInheritance|$this |
58
|
|
|
* @throws TypeConflictException |
59
|
|
|
*/ |
60
|
40 |
|
public function extends(TypeDefinition ...$definitions): ProvidesInheritance |
61
|
|
|
{ |
62
|
40 |
|
foreach ($definitions as $definition) { |
63
|
40 |
|
$this->verifyExtensionType($definition); |
64
|
|
|
|
65
|
24 |
|
$this->parents[] = $definition->getName(); |
66
|
|
|
} |
67
|
|
|
|
68
|
24 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param TypeDefinition $def |
73
|
|
|
* @throws TypeConflictException |
74
|
|
|
*/ |
75
|
40 |
|
private function verifyExtensionType(TypeDefinition $def): void |
76
|
|
|
{ |
77
|
40 |
|
if (! $this::typeOf($def::getType())) { |
78
|
16 |
|
$error = \sprintf('Type %s can extends only %s types, but %s given.', $this, static::getType(), $def); |
79
|
16 |
|
throw new TypeConflictException($error); |
80
|
|
|
} |
81
|
24 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $name |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function isExtends(string $name): bool |
88
|
|
|
{ |
89
|
|
|
return $this->isExtendsDefinition($this->fetch($name)); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param TypeDefinition $definition |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
6 |
|
public function isExtendsDefinition(TypeDefinition $definition): bool |
97
|
|
|
{ |
98
|
6 |
|
foreach ($this->getParents() as $parent) { |
99
|
6 |
|
if ($parent->instanceOf($definition)) { |
100
|
6 |
|
return true; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
6 |
|
return false; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. 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.