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\ProvidesInterfaces; |
13
|
|
|
use Railt\Reflection\Contracts\Definition\InterfaceDefinition; |
14
|
|
|
use Railt\Reflection\Contracts\Definition\TypeDefinition; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Trait HasInterfaces |
18
|
|
|
*/ |
19
|
|
|
trait HasInterfaces |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array|string[] |
23
|
|
|
*/ |
24
|
|
|
protected $interfaces = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string|TypeDefinition $type |
28
|
|
|
* @return TypeDefinition |
29
|
|
|
*/ |
30
|
|
|
abstract protected function fetch($type): TypeDefinition; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string|TypeDefinition $interface |
34
|
|
|
* @return bool |
35
|
|
|
*/ |
36
|
2 |
|
public function isImplements($interface): bool |
37
|
|
|
{ |
38
|
2 |
|
$definition = $this->fetch($interface); |
39
|
|
|
|
40
|
2 |
|
foreach ($this->getInterfaces() as $impl) { |
41
|
|
|
if ($impl->hasInterface($this->nameOf($definition))) { |
|
|
|
|
42
|
|
|
return true; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($impl->isImplements($definition)) { |
46
|
|
|
return true; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return iterable|InterfaceDefinition[] |
55
|
|
|
*/ |
56
|
2 |
|
public function getInterfaces(): iterable |
57
|
|
|
{ |
58
|
2 |
|
foreach ($this->interfaces as $interface) { |
59
|
|
|
yield $this->fetch($interface); |
60
|
|
|
} |
61
|
2 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $name |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function hasInterface(string $name): bool |
68
|
|
|
{ |
69
|
|
|
return \in_array($name, $this->interfaces, true); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $name |
74
|
|
|
* @return InterfaceDefinition|TypeDefinition|null |
75
|
|
|
*/ |
76
|
|
|
public function getInterface(string $name): ?InterfaceDefinition |
77
|
|
|
{ |
78
|
|
|
return \in_array($name, $this->interfaces, true) ? $this->fetch($name) : null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string|TypeDefinition ...$interfaces |
83
|
|
|
* @return ProvidesInterfaces|$this |
84
|
|
|
*/ |
85
|
|
|
public function withInterface(...$interfaces): ProvidesInterfaces |
86
|
|
|
{ |
87
|
|
|
foreach ($interfaces as $interface) { |
88
|
|
|
$this->interfaces[] = $this->nameOf($interface); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string|TypeDefinition ...$interfaces |
96
|
|
|
* @return ProvidesInterfaces|$this |
97
|
|
|
*/ |
98
|
|
|
public function withoutInterface(...$interfaces): ProvidesInterfaces |
99
|
|
|
{ |
100
|
|
|
foreach ($interfaces as $interface) { |
101
|
|
|
unset($this->interfaces[$this->nameOf($interface)]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this; |
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.