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\Behaviour\ProvidesInterfaces; |
14
|
|
|
use Railt\Reflection\Contracts\Definition\Behaviour\ProvidesTypeDefinitions; |
15
|
|
|
use Railt\Reflection\Contracts\Definition\TypeDefinition; |
16
|
|
|
use Railt\Reflection\Type; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Trait HasInheritance |
20
|
|
|
*/ |
21
|
|
|
trait HasInheritance |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string|null |
25
|
|
|
*/ |
26
|
|
|
protected $extends; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string[]|array |
30
|
|
|
*/ |
31
|
|
|
protected $extendedBy = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string|TypeDefinition $type |
35
|
|
|
* @return TypeDefinition |
36
|
|
|
*/ |
37
|
|
|
abstract protected function fetch($type): TypeDefinition; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return iterable|TypeDefinition[] |
41
|
|
|
*/ |
42
|
|
|
public function inheritedBy(): iterable |
43
|
|
|
{ |
44
|
|
|
foreach ($this->extendedBy as $parent) { |
45
|
|
|
yield $this->fetch($parent); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return null|TypeDefinition |
51
|
|
|
*/ |
52
|
8 |
|
public function getInheritedParent(): ?TypeDefinition |
53
|
|
|
{ |
54
|
8 |
|
return $this->extends ? $this->fetch($this->extends) : null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function hasInheritance(): bool |
61
|
|
|
{ |
62
|
|
|
return $this->extends !== null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param TypeDefinition|string $definition |
67
|
|
|
* @return ProvidesInheritance|$this |
68
|
|
|
*/ |
69
|
25 |
|
public function extends($definition): ProvidesInheritance |
70
|
|
|
{ |
71
|
25 |
|
$this->extends = $this->nameOf($definition); |
|
|
|
|
72
|
|
|
|
73
|
25 |
|
if ($definition instanceof ProvidesInheritance) { |
74
|
|
|
/** @var HasInheritance $definition */ |
75
|
25 |
|
$definition->extendedBy[] = $this->getName(); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
25 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param TypeDefinition|string $definition |
83
|
|
|
* @return ProvidesInheritance|$this |
84
|
|
|
*/ |
85
|
|
|
public function extendsBy($definition): ProvidesInheritance |
86
|
|
|
{ |
87
|
|
|
/** @var HasInheritance $definition */ |
88
|
|
|
$definition = $this->fetch($definition); |
89
|
|
|
|
90
|
|
|
$definition->extends($this); |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param TypeDefinition|string $type |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
8 |
|
public function extendsOf($type): bool |
100
|
|
|
{ |
101
|
8 |
|
return $this->extends === $this->nameOf($type); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param TypeDefinition|string $type |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
8 |
|
public function instanceOf($type): bool |
109
|
|
|
{ |
110
|
|
|
/** |
111
|
|
|
* @var TypeDefinition $type |
112
|
|
|
* @var TypeDefinition $context |
113
|
|
|
*/ |
114
|
8 |
|
[$type, $context] = [$this->fetch($type), $this]; |
|
|
|
|
115
|
|
|
|
116
|
|
|
// Return a positive response if the child is an Any type implementation. |
117
|
8 |
|
if ($type::getType()->is(Type::ANY)) { |
118
|
8 |
|
return true; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// Return a positive response if the desired child is the same type from |
122
|
|
|
// which the search is performed. |
123
|
8 |
|
if ($type === $context) { |
|
|
|
|
124
|
8 |
|
return true; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// Return a positive response if the parent type (like Object or Interface) |
128
|
|
|
// can implement the desired type. |
129
|
8 |
|
if ($this instanceof ProvidesInterfaces && $this->isImplements($type->getName())) { |
130
|
|
|
return true; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// Return a positive response if the parent type (like Union) contains a |
134
|
|
|
// reference to the desired child type. |
135
|
8 |
|
if ($this instanceof ProvidesTypeDefinitions && $this->hasDefinition($type->getName())) { |
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// Return a positive response if the parent type contains a reference |
140
|
|
|
// to the desired child when using inheritance. |
141
|
8 |
|
while ($context) { |
|
|
|
|
142
|
|
|
/** @var TypeDefinition $context */ |
143
|
8 |
|
$context = $this->fetch($context); |
144
|
|
|
|
145
|
8 |
|
if ($context->extendsOf($type)) { |
146
|
8 |
|
return true; |
147
|
|
|
} |
148
|
|
|
|
149
|
8 |
|
$context = $context->getInheritedParent(); |
150
|
|
|
} |
151
|
|
|
|
152
|
8 |
|
return false; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
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.