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); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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.