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->instanceOf($definition)) { |
42
|
|
|
return true; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return iterable|InterfaceDefinition[] |
51
|
|
|
*/ |
52
|
2 |
|
public function getInterfaces(): iterable |
53
|
|
|
{ |
54
|
2 |
|
foreach ($this->interfaces as $interface) { |
55
|
|
|
yield $this->fetch($interface); |
56
|
|
|
} |
57
|
2 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $name |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function hasInterface(string $name): bool |
64
|
|
|
{ |
65
|
|
|
return \in_array($name, $this->interfaces, true); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $name |
70
|
|
|
* @return InterfaceDefinition|TypeDefinition|null |
71
|
|
|
*/ |
72
|
|
|
public function getInterface(string $name): ?InterfaceDefinition |
73
|
|
|
{ |
74
|
|
|
return \in_array($name, $this->interfaces, true) ? $this->fetch($name) : null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string|TypeDefinition ...$interfaces |
79
|
|
|
* @return ProvidesInterfaces|$this |
80
|
|
|
*/ |
81
|
|
|
public function withInterface(...$interfaces): ProvidesInterfaces |
82
|
|
|
{ |
83
|
|
|
foreach ($interfaces as $interface) { |
84
|
|
|
$this->interfaces[] = $this->nameOf($interface); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
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.