Complex classes like Addon 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 Addon, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Anomaly\Streams\Platform\Addon; |
||
16 | class Addon implements PresentableInterface, Arrayable |
||
17 | { |
||
18 | |||
19 | use FiresCallbacks; |
||
20 | use DispatchesJobs; |
||
21 | |||
22 | /** |
||
23 | * Runtime cache. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $cache = []; |
||
28 | |||
29 | /** |
||
30 | * The addon path. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $path = null; |
||
35 | |||
36 | /** |
||
37 | * The addon type. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $type = null; |
||
42 | |||
43 | /** |
||
44 | * The addon slug. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $slug = null; |
||
49 | |||
50 | /** |
||
51 | * The addon vendor. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $vendor = null; |
||
56 | |||
57 | /** |
||
58 | * The addon namespace. |
||
59 | * |
||
60 | * @var null|string |
||
61 | */ |
||
62 | protected $namespace = null; |
||
63 | |||
64 | /** |
||
65 | * Get the addon's presenter. |
||
66 | * |
||
67 | * @return Presenter |
||
68 | */ |
||
69 | public function getPresenter() |
||
73 | |||
74 | /** |
||
75 | * Return a new service provider. |
||
76 | * |
||
77 | * @return AddonServiceProvider |
||
78 | */ |
||
79 | public function newServiceProvider() |
||
83 | |||
84 | /** |
||
85 | * Get the service provider class. |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | public function getServiceProvider() |
||
93 | |||
94 | /** |
||
95 | * Return whether the addon is core or not. |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | public function isCore() |
||
103 | |||
104 | /** |
||
105 | * Return whether the addon is shared or not. |
||
106 | * |
||
107 | * @return bool |
||
108 | */ |
||
109 | public function isShared() |
||
113 | |||
114 | /** |
||
115 | * Return whether the addon is for testing or not. |
||
116 | * |
||
117 | * @return bool |
||
118 | */ |
||
119 | public function isTesting() |
||
123 | |||
124 | /** |
||
125 | * Get the addon name string. |
||
126 | * |
||
127 | * @return string |
||
128 | */ |
||
129 | public function getName() |
||
133 | |||
134 | /** |
||
135 | * Get the addon title string. |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | public function getTitle() |
||
143 | |||
144 | /** |
||
145 | * Get the addon description string. |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | public function getDescription() |
||
153 | |||
154 | /** |
||
155 | * Get a namespaced key string. |
||
156 | * |
||
157 | * @param null $key |
||
158 | * @return string |
||
159 | */ |
||
160 | public function getNamespace($key = null) |
||
168 | |||
169 | /** |
||
170 | * Get the transformed |
||
171 | * class to another suffix. |
||
172 | * |
||
173 | * @param null $suffix |
||
174 | * @return string |
||
175 | */ |
||
176 | public function getTransformedClass($suffix = null) |
||
182 | |||
183 | /** |
||
184 | * Return the ID representation (namespace). |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | public function getId() |
||
192 | |||
193 | /** |
||
194 | * Return whether an addon has |
||
195 | * config matching the key. |
||
196 | * |
||
197 | * @param string $key |
||
198 | * @return boolean |
||
199 | */ |
||
200 | public function hasConfig($key = '*') |
||
204 | |||
205 | /** |
||
206 | * Return whether an addon has |
||
207 | * config matching any key. |
||
208 | * |
||
209 | * @param array $keys |
||
210 | * @return bool |
||
211 | */ |
||
212 | public function hasAnyConfig(array $keys = ['*']) |
||
222 | |||
223 | /** |
||
224 | * Get the composer json contents. |
||
225 | * |
||
226 | * @return mixed|null |
||
227 | */ |
||
228 | public function getComposerJson() |
||
238 | |||
239 | /** |
||
240 | * @param $path |
||
241 | * @return $this |
||
242 | */ |
||
243 | public function setPath($path) |
||
249 | |||
250 | /** |
||
251 | * Get the addon path. |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | public function getPath($path = null) |
||
259 | |||
260 | /** |
||
261 | * Return the app path. |
||
262 | * |
||
263 | * @param null $path |
||
264 | */ |
||
265 | public function getAppPath($path = null) |
||
269 | |||
270 | /** |
||
271 | * Set the addon slug. |
||
272 | * |
||
273 | * @param $slug |
||
274 | * @return $this |
||
275 | */ |
||
276 | public function setSlug($slug) |
||
282 | |||
283 | /** |
||
284 | * Get the addon slug. |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | public function getSlug() |
||
292 | |||
293 | /** |
||
294 | * Set the addon type. |
||
295 | * |
||
296 | * @param $type |
||
297 | * @return $this |
||
298 | */ |
||
299 | public function setType($type) |
||
305 | |||
306 | /** |
||
307 | * Get the addon type. |
||
308 | * |
||
309 | * @return string |
||
310 | */ |
||
311 | public function getType() |
||
315 | |||
316 | /** |
||
317 | * Set the vendor. |
||
318 | * |
||
319 | * @param $vendor |
||
320 | * @return $this |
||
321 | */ |
||
322 | public function setVendor($vendor) |
||
328 | |||
329 | /** |
||
330 | * Get the vendor. |
||
331 | * |
||
332 | * @return string |
||
333 | */ |
||
334 | public function getVendor() |
||
338 | |||
339 | /** |
||
340 | * Make the addon namespace. |
||
341 | */ |
||
342 | protected function makeNamespace() |
||
346 | |||
347 | /** |
||
348 | * Get a property value from the object. |
||
349 | * |
||
350 | * @param $name |
||
351 | * @return mixed |
||
352 | */ |
||
353 | public function __get($name) |
||
369 | |||
370 | /** |
||
371 | * Return whether a property is set or not. |
||
372 | * |
||
373 | * @param $name |
||
374 | * @return bool |
||
375 | */ |
||
376 | public function __isset($name) |
||
392 | |||
393 | /** |
||
394 | * Return the addon as a string. |
||
395 | * |
||
396 | * @return string |
||
397 | */ |
||
398 | public function __toString() |
||
402 | |||
403 | /** |
||
404 | * Get the instance as an array. |
||
405 | * |
||
406 | * @return array |
||
407 | */ |
||
408 | public function toArray() |
||
417 | } |
||
418 |
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: