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