1 | <?php |
||
26 | class ObjectDefinition extends AbstractDefinition implements |
||
27 | FluentObjectDefinitionInterface, |
||
28 | 8 | ResolverAwareInterface |
|
29 | { |
||
30 | 8 | /** |
|
31 | * @var DefinitionData |
||
32 | */ |
||
33 | protected $definitionData; |
||
34 | |||
35 | /** |
||
36 | * @var ResolverInterface |
||
37 | */ |
||
38 | protected $resolver; |
||
39 | |||
40 | /** |
||
41 | * @var \ReflectionClass |
||
42 | */ |
||
43 | private $reflectionClass; |
||
44 | |||
45 | /** |
||
46 | * @var mixed |
||
47 | */ |
||
48 | private $lastValue; |
||
49 | |||
50 | /** |
||
51 | * Creates an object definition |
||
52 | * |
||
53 | * @param string $className |
||
54 | * |
||
55 | * @throws ClassNotFoundException if the provided class name is from an |
||
56 | * undefined or inaccessible class. |
||
57 | */ |
||
58 | public function __construct($className) |
||
70 | |||
71 | /** |
||
72 | * Creates an object definition |
||
73 | * |
||
74 | * @param string $className |
||
75 | * |
||
76 | * @return FluentObjectDefinitionInterface |
||
77 | * |
||
78 | * @throws ClassNotFoundException if the provided class name is from an |
||
79 | * undefined or inaccessible class. |
||
80 | */ |
||
81 | public static function create($className) |
||
85 | |||
86 | /** |
||
87 | * Resolves the definition into a scalar or object |
||
88 | * |
||
89 | * @return mixed |
||
90 | */ |
||
91 | public function resolve() |
||
97 | |||
98 | /** |
||
99 | * Set the arguments for the last defined method call |
||
100 | * |
||
101 | * If no method call was defined yet it will set the constructor argument list |
||
102 | * |
||
103 | * @param array ...$arguments Arguments passed to object constructor |
||
104 | * |
||
105 | * @return $this|Object|self |
||
106 | */ |
||
107 | public function with(...$arguments) |
||
120 | |||
121 | /** |
||
122 | * Set the arguments used to create the object |
||
123 | * |
||
124 | * @param array ...$arguments |
||
125 | * |
||
126 | * @return self|Object |
||
127 | */ |
||
128 | public function withConstructorArgument(...$arguments) |
||
133 | |||
134 | /** |
||
135 | * Get the object resolver |
||
136 | * |
||
137 | * @return ResolverInterface |
||
138 | */ |
||
139 | public function getResolver() |
||
146 | |||
147 | /** |
||
148 | * Set the object resolver |
||
149 | * |
||
150 | * @param ResolverInterface $resolver |
||
151 | * |
||
152 | * @return self|$this |
||
153 | */ |
||
154 | public function setResolver(ResolverInterface $resolver) |
||
159 | |||
160 | /** |
||
161 | * Get the definition data |
||
162 | * |
||
163 | * @return DefinitionData |
||
164 | */ |
||
165 | public function getDefinitionData() |
||
169 | |||
170 | /** |
||
171 | * Define a method call in the freshly created object |
||
172 | * |
||
173 | * @param string $methodName The method name to call |
||
174 | * |
||
175 | * @return FluentObjectDefinitionInterface|self |
||
176 | * |
||
177 | * @throws MethodNotFoundException |
||
178 | */ |
||
179 | public function call($methodName) |
||
186 | |||
187 | /** |
||
188 | * Define a method call with provide call |
||
189 | * |
||
190 | * @param string $methodName |
||
191 | * @param array ...$arguments |
||
192 | * |
||
193 | * @return self|ObjectDefinitionInterface |
||
194 | * |
||
195 | * @throws MethodNotFoundException |
||
196 | */ |
||
197 | public function callMethod($methodName, ...$arguments) |
||
214 | |||
215 | /** |
||
216 | * Set the value that will be assigned to a property |
||
217 | * |
||
218 | * @param mixed $value |
||
219 | * |
||
220 | * @return self|Object |
||
221 | */ |
||
222 | public function assign($value) |
||
227 | |||
228 | /** |
||
229 | * Assign the last defined value to the provided property |
||
230 | * |
||
231 | * The value will be reset after its assigned. |
||
232 | * |
||
233 | * @param string $property |
||
234 | * |
||
235 | * @return self|Object |
||
236 | */ |
||
237 | public function to($property) |
||
252 | |||
253 | /** |
||
254 | * Assigns a value to the property with provided name |
||
255 | * |
||
256 | * @param string $name |
||
257 | * @param mixed $value |
||
258 | * |
||
259 | * @return self|Object |
||
260 | */ |
||
261 | public function assignProperty($name, $value) |
||
270 | |||
271 | /** |
||
272 | * Get the self reflection |
||
273 | * |
||
274 | * @return \ReflectionClass |
||
275 | */ |
||
276 | protected function getReflectionClass() |
||
283 | } |
||
284 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: