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