Complex classes like Entity often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Entity, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Entity extends Listener |
||
24 | { |
||
25 | /** |
||
26 | * Holds this entity (parsed) documentaions. |
||
27 | * @var array|null |
||
28 | */ |
||
29 | protected $docs = null; |
||
30 | |||
31 | /** |
||
32 | * @var Route |
||
33 | */ |
||
34 | protected $route; |
||
35 | |||
36 | /** |
||
37 | * Holds the entity redirect location. |
||
38 | * @var string|null |
||
39 | */ |
||
40 | protected $redirect; |
||
41 | |||
42 | /** |
||
43 | * Holds all the entity available actions. |
||
44 | * @var array|null |
||
45 | */ |
||
46 | protected $actions = null; |
||
47 | |||
48 | /** |
||
49 | * Holds all default actions. |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $defaultActions = array( |
||
53 | 'OPTIONS' => 'help', |
||
54 | 'HEAD' => 'test' |
||
55 | ); |
||
56 | |||
57 | /** |
||
58 | * Holds the array of results of an entity. |
||
59 | * @var array|null |
||
60 | */ |
||
61 | protected $results = null; |
||
62 | |||
63 | /** |
||
64 | * @var Config |
||
65 | */ |
||
66 | protected $config; |
||
67 | |||
68 | /** |
||
69 | * Constructor. |
||
70 | * |
||
71 | * @param Config|null $config A config object |
||
72 | */ |
||
73 | public function __construct(Config $config = null) |
||
77 | |||
78 | /** |
||
79 | * Appends the given array definition and apply generic mappings. |
||
80 | * |
||
81 | * @param array $def An entity array definition. |
||
82 | * @return void |
||
83 | * @see EntityInterface::_append |
||
84 | */ |
||
85 | final public function _append(array $def) |
||
91 | |||
92 | /** |
||
93 | * Call the resource entity and return its results as an array. |
||
94 | * |
||
95 | * @return array |
||
96 | * @see EntityInterface::underlineCall |
||
97 | */ |
||
98 | public function call($direct=false) |
||
116 | |||
117 | /** |
||
118 | * Converts the provided variable to an array. |
||
119 | * |
||
120 | * @param mixed $mix |
||
121 | * @return array |
||
122 | */ |
||
123 | public static function convertToArray($mix) |
||
138 | |||
139 | /** |
||
140 | * Checks wether the current entity holds the specified method. |
||
141 | * |
||
142 | * @param string $method |
||
143 | * @param array $actions=null Use to override local actions. |
||
144 | * @return boolean |
||
145 | */ |
||
146 | public function hasMethod($method) |
||
150 | |||
151 | /** |
||
152 | * Returns all the available actions. |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | public function getAllActions() |
||
166 | |||
167 | /** |
||
168 | * Gets the specified default action. |
||
169 | * |
||
170 | * @return string|null |
||
171 | */ |
||
172 | public function getDefaultAction($method) |
||
178 | |||
179 | /** |
||
180 | * Returns this entity as an associative array. |
||
181 | * |
||
182 | * @return array |
||
183 | */ |
||
184 | public function toArray() |
||
188 | |||
189 | /** |
||
190 | * Returns the full class/group or specified method documentation. |
||
191 | * |
||
192 | * @param string $method |
||
193 | * @return array |
||
194 | */ |
||
195 | public function getDocs($method=null) |
||
213 | |||
214 | /** |
||
215 | * Returns the validated and required parameters. |
||
216 | * |
||
217 | * @param \ReflectionFunctionAbstract $refMethod A reflected method/function to introspect. |
||
218 | * @param string $httpMethod A public method name e.g. GET, POST. |
||
219 | * @param array $routeParams An array of route parameters to check upon. |
||
220 | * @return array The array of validated and required parameters |
||
221 | * @throws \BadMethodCallException 400 |
||
222 | */ |
||
223 | public function getValidatedParams( |
||
259 | |||
260 | /** |
||
261 | * Sets the entity results. |
||
262 | * |
||
263 | * @param array $results |
||
264 | * @return void |
||
265 | */ |
||
266 | public function setResults(array $results=null) |
||
270 | |||
271 | /** |
||
272 | * Sets the entity results. |
||
273 | * |
||
274 | * @return array |
||
275 | */ |
||
276 | public function getResults() |
||
280 | |||
281 | /** |
||
282 | * Sets the route object. |
||
283 | * |
||
284 | * @param Router $route |
||
285 | * @return void |
||
286 | */ |
||
287 | public function setRoute(Router $route) |
||
291 | |||
292 | /** |
||
293 | * Returns the route object. |
||
294 | * |
||
295 | * @return Router |
||
296 | */ |
||
297 | public function getRoute() |
||
301 | |||
302 | /** |
||
303 | * Returns the redirect location. |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | public function hasRedirect() |
||
311 | |||
312 | /** |
||
313 | * Returns the redirect location. |
||
314 | * |
||
315 | * @return string |
||
316 | */ |
||
317 | public function getRedirect() |
||
321 | |||
322 | /** |
||
323 | * Returns an array of method keys and action values. |
||
324 | * |
||
325 | * @param array $array |
||
326 | * @return array |
||
327 | */ |
||
328 | public function getActions() |
||
336 | |||
337 | /** |
||
338 | * Returns the value of an anotation. |
||
339 | * |
||
340 | * @param string $name |
||
341 | * @return mix|null |
||
342 | */ |
||
343 | public function getAnnotationValue($name) |
||
350 | |||
351 | } |
||
352 |
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 sub-classes 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 parent class: