1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Composite Utils package. |
4
|
|
|
* |
5
|
|
|
* (c) Emily Shepherd <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the |
8
|
|
|
* LICENSE.md file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @package spaark/composite-utils |
11
|
|
|
* @author Emily Shepherd <[email protected]> |
12
|
|
|
* @license MIT |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Spaark\CompositeUtils\Factory\Reflection; |
16
|
|
|
|
17
|
|
|
use Spaark\CompositeUtils\Model\Reflection\ReflectionComposite; |
18
|
|
|
use Spaark\CompositeUtils\Model\Reflection\ReflectionMethod; |
19
|
|
|
use Spaark\CompositeUtils\Model\Reflection\ReflectionParameter; |
20
|
|
|
use Spaark\CompositeUtils\Service\RawPropertyAccessor; |
21
|
|
|
use Spaark\CompositeUtils\Service\TypeComparator; |
22
|
|
|
use Spaark\CompositeUtils\Model\Collection\FixedList; |
23
|
|
|
use Spaark\CompositeUtils\Model\Collection\HashMap; |
24
|
|
|
use \ReflectionMethod as PHPNativeReflectionMethod; |
25
|
|
|
use \ReflectionParameter as PHPNativeReflectionParameter; |
26
|
|
|
use \Reflector as PHPNativeReflector; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Builds a ReflectionMethod for a given method and optionally links |
30
|
|
|
* this to a parent ReflectionComposite |
31
|
|
|
*/ |
32
|
|
|
class ReflectionMethodFactory extends ReflectorFactory |
33
|
|
|
{ |
34
|
|
|
const REFLECTION_OBJECT = ReflectionMethod::class; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var PHPNativeReflectionMethod |
38
|
|
|
*/ |
39
|
|
|
protected $reflector; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var ReflectionMethod |
43
|
|
|
*/ |
44
|
|
|
protected $object; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Hashmap |
48
|
|
|
*/ |
49
|
|
|
protected $parameters; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var TypeParser |
53
|
|
|
*/ |
54
|
|
|
protected $typeParser; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns a new ReflectionMethodFactory using the given class and |
58
|
|
|
* method names |
59
|
|
|
* |
60
|
|
|
* @param string $class The classname of the method |
61
|
|
|
* @param string $method The method to reflect |
62
|
|
|
* @return ReflectionMethodFactory |
63
|
|
|
*/ |
64
|
|
|
public static function fromName(string $class, string $method) |
65
|
|
|
{ |
66
|
|
|
return new static(new PHPNativeReflectionMethod |
67
|
|
|
( |
68
|
|
|
$class, $method |
69
|
|
|
)); |
70
|
|
|
} |
71
|
|
|
|
72
|
20 |
|
public function __construct(PHPNativeReflector $reflector) |
73
|
|
|
{ |
74
|
20 |
|
parent::__construct($reflector); |
75
|
|
|
|
76
|
20 |
|
$this->parameters = new HashMap(); |
|
|
|
|
77
|
20 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Builds the ReflectionMethod from the provided parameters, |
81
|
|
|
* optionally linking to a parent ReflectionComposite |
82
|
|
|
* |
83
|
|
|
* @param ReflectionComposite $parent The reflector for the class |
84
|
|
|
* this method belongs to |
85
|
|
|
* @return ReflectionMethod |
86
|
|
|
*/ |
87
|
20 |
|
public function build(?ReflectionComposite $parent = null) |
88
|
|
|
{ |
89
|
20 |
|
$this->typeParser = new TypeParser($parent); |
90
|
20 |
|
$this->accessor->setRawValue('owner', $parent); |
91
|
20 |
|
$this->accessor->setRawValue |
92
|
|
|
( |
93
|
20 |
|
'name', |
94
|
20 |
|
$this->reflector->getName() |
|
|
|
|
95
|
|
|
); |
96
|
|
|
|
97
|
20 |
|
$this->initParams(); |
98
|
|
|
|
99
|
20 |
|
foreach ($this->reflector->getParameters() as $parameter) |
100
|
|
|
{ |
101
|
20 |
|
$this->addParameter($parameter); |
102
|
|
|
} |
103
|
|
|
|
104
|
20 |
|
$this->parseDocComment(['param' => 'addParamAnnotation']); |
105
|
|
|
|
106
|
20 |
|
return $this->object; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Creates the Method's parameter's property with a fixd list of |
111
|
|
|
* the appropriate size |
112
|
|
|
*/ |
113
|
20 |
|
protected function initParams() |
114
|
|
|
{ |
115
|
20 |
|
$this->accessor->setRawValue |
116
|
|
|
( |
117
|
20 |
|
'parameters', |
118
|
20 |
|
new FixedList(count($this->reflector->getParameters())) |
119
|
|
|
); |
120
|
20 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Processes a param docblock annotation and uses it to decorate |
124
|
|
|
* a method parameter |
125
|
|
|
* |
126
|
|
|
* @param string $name Unused. Should be 'param' |
127
|
|
|
* @param string $value The annotation value |
128
|
|
|
*/ |
129
|
20 |
|
protected function addParamAnnotation($name, $value) : void |
|
|
|
|
130
|
|
|
{ |
131
|
20 |
|
$items = explode(' ', $value); |
132
|
20 |
|
$type = $items[0]; |
133
|
20 |
|
$param = $items[1]; |
134
|
|
|
|
135
|
20 |
|
if (!$this->parameters->containsKey($param)) |
136
|
|
|
{ |
137
|
|
|
throw new \Exception(); |
138
|
|
|
} |
139
|
|
|
|
140
|
20 |
|
$comparator = new TypeComparator(); |
141
|
20 |
|
$type = $this->typeParser->parse($type); |
142
|
20 |
|
$param = $this->parameters[$param]; |
143
|
20 |
|
$nativeType = $param->getRawValue('type'); |
144
|
|
|
|
145
|
20 |
|
if (!$comparator->compatible($nativeType, $type)) |
146
|
|
|
{ |
147
|
|
|
throw new \Exception(); |
148
|
|
|
} |
149
|
|
|
|
150
|
20 |
|
$param->setRawValue('type', $type); |
151
|
20 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Adds a parameter to the method, based on it's native |
155
|
|
|
* ReflectionParameter |
156
|
|
|
* |
157
|
|
|
* @param PHPNativeReflectionParameter $reflect |
158
|
|
|
*/ |
159
|
20 |
|
protected function addParameter |
160
|
|
|
( |
161
|
|
|
PHPNativeReflectionParameter $reflect |
162
|
|
|
) |
163
|
|
|
: void |
164
|
|
|
{ |
165
|
20 |
|
$parameter = new ReflectionParameter(); |
166
|
20 |
|
$accessor = new RawPropertyAccessor($parameter); |
167
|
20 |
|
$this->parameters['$' . $reflect->getName()] = $accessor; |
|
|
|
|
168
|
20 |
|
$this->accessor->rawAddToValue('parameters', $parameter); |
|
|
|
|
169
|
|
|
|
170
|
20 |
|
$accessor->setRawValue('owner', $this->object); |
171
|
20 |
|
$accessor->setRawValue('name', $reflect->getName()); |
|
|
|
|
172
|
20 |
|
$accessor->setRawValue |
173
|
|
|
( |
174
|
20 |
|
'type', |
175
|
20 |
|
$this->typeParser->parse((string)$reflect->getType()) |
176
|
|
|
); |
177
|
20 |
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..