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\Type; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Trait HasInheritance |
18
|
|
|
*/ |
19
|
|
|
trait HasInheritance |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string|null |
23
|
|
|
*/ |
24
|
|
|
protected $extends; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string[]|array |
28
|
|
|
*/ |
29
|
|
|
protected $extendedBy = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string|TypeDefinition $type |
33
|
|
|
* @return TypeDefinition |
34
|
|
|
*/ |
35
|
|
|
abstract protected function fetch($type): TypeDefinition; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return iterable|TypeDefinition[] |
39
|
|
|
*/ |
40
|
|
|
public function inheritedBy(): iterable |
41
|
|
|
{ |
42
|
|
|
foreach ($this->extendedBy as $parent) { |
43
|
|
|
yield $this->fetch($parent); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return null|TypeDefinition |
49
|
|
|
*/ |
50
|
8 |
|
public function getInheritedParent(): ?TypeDefinition |
51
|
|
|
{ |
52
|
8 |
|
return $this->extends ? $this->fetch($this->extends) : null; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function hasInheritance(): bool |
59
|
|
|
{ |
60
|
|
|
return $this->extends !== null; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param TypeDefinition|string $definition |
65
|
|
|
* @return ProvidesInheritance|$this |
66
|
|
|
*/ |
67
|
25 |
|
public function extends($definition): ProvidesInheritance |
68
|
|
|
{ |
69
|
25 |
|
$this->extends = $this->nameOf($definition); |
|
|
|
|
70
|
|
|
|
71
|
25 |
|
if ($definition instanceof ProvidesInheritance) { |
72
|
|
|
/** @var HasInheritance $definition */ |
73
|
25 |
|
$definition->extendedBy[] = $this->extends; |
74
|
|
|
} |
75
|
|
|
|
76
|
25 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param TypeDefinition|string $definition |
81
|
|
|
* @return ProvidesInheritance|$this |
82
|
|
|
*/ |
83
|
|
|
public function extendsBy($definition): ProvidesInheritance |
84
|
|
|
{ |
85
|
|
|
/** @var HasInheritance $definition */ |
86
|
|
|
$definition = $this->fetch($definition); |
87
|
|
|
|
88
|
|
|
$definition->extends($this); |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param TypeDefinition|string $type |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
8 |
|
public function extendsOf($type): bool |
98
|
|
|
{ |
99
|
8 |
|
return $this->extends === $this->nameOf($type); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param TypeDefinition|string $type |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
8 |
|
public function instanceOf($type): bool |
107
|
|
|
{ |
108
|
|
|
/** |
109
|
|
|
* @var TypeDefinition $type |
110
|
|
|
* @var TypeDefinition $context |
111
|
|
|
*/ |
112
|
8 |
|
[$type, $context] = [$this->fetch($type), $this]; |
|
|
|
|
113
|
|
|
|
114
|
8 |
|
if ($type::getType()->is(Type::ANY)) { |
115
|
8 |
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
8 |
|
if ($type === $context) { |
|
|
|
|
119
|
8 |
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
8 |
|
while ($context) { |
|
|
|
|
123
|
|
|
/** @var TypeDefinition $context */ |
124
|
8 |
|
$context = $this->fetch($context); |
125
|
|
|
|
126
|
8 |
|
if ($context->extendsOf($type)) { |
127
|
8 |
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
8 |
|
$context = $context->getInheritedParent(); |
131
|
|
|
} |
132
|
|
|
|
133
|
8 |
|
return false; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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.