Complex classes like Autocomplete 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 Autocomplete, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Autocomplete implements VariableAwareInterface |
||
23 | { |
||
24 | use VariableAwareTrait; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $inputId = 'place_input'; |
||
30 | |||
31 | /** |
||
32 | * @var EventManager |
||
33 | */ |
||
34 | private $eventManager; |
||
35 | |||
36 | /** |
||
37 | * @var Bound|null |
||
38 | */ |
||
39 | private $bound; |
||
40 | |||
41 | /** |
||
42 | * @var string[] |
||
43 | */ |
||
44 | private $types = []; |
||
45 | |||
46 | /** |
||
47 | * @var mixed[] |
||
48 | */ |
||
49 | private $components = []; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $value; |
||
55 | |||
56 | /** |
||
57 | * @var string[] |
||
58 | */ |
||
59 | private $inputAttributes = []; |
||
60 | |||
61 | /** |
||
62 | * @var string[] |
||
63 | */ |
||
64 | private $libraries = []; |
||
65 | |||
66 | public function __construct() |
||
71 | |||
72 | /** |
||
73 | * @return string |
||
74 | */ |
||
75 | public function getHtmlId() |
||
79 | |||
80 | /** |
||
81 | * @param string $inputId |
||
82 | */ |
||
83 | public function setInputId($inputId) |
||
87 | |||
88 | /** |
||
89 | * @return EventManager |
||
90 | */ |
||
91 | public function getEventManager() |
||
95 | |||
96 | /** |
||
97 | * @param EventManager $eventManager |
||
98 | */ |
||
99 | public function setEventManager(EventManager $eventManager) |
||
103 | |||
104 | /** |
||
105 | * @return bool |
||
106 | */ |
||
107 | public function hasBound() |
||
111 | |||
112 | /** |
||
113 | * @return Bound|null |
||
114 | */ |
||
115 | public function getBound() |
||
119 | |||
120 | /** |
||
121 | * @param Bound|null $bound |
||
122 | */ |
||
123 | public function setBound(Bound $bound = null) |
||
127 | |||
128 | /** |
||
129 | * @return bool |
||
130 | */ |
||
131 | public function hasTypes() |
||
135 | |||
136 | /** |
||
137 | * @return string[] |
||
138 | */ |
||
139 | public function getTypes() |
||
143 | |||
144 | /** |
||
145 | * @param string[] $types |
||
146 | */ |
||
147 | public function setTypes(array $types) |
||
152 | |||
153 | /** |
||
154 | * @param string[] $types |
||
155 | */ |
||
156 | public function addTypes(array $types) |
||
162 | |||
163 | /** |
||
164 | * @param string $type |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | public function hasType($type) |
||
172 | |||
173 | /** |
||
174 | * @param string $type |
||
175 | */ |
||
176 | public function addType($type) |
||
182 | |||
183 | /** |
||
184 | * @param string $type |
||
185 | */ |
||
186 | public function removeType($type) |
||
190 | |||
191 | /** |
||
192 | * @return bool |
||
193 | */ |
||
194 | public function hasComponents() |
||
195 | { |
||
196 | return !empty($this->components); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @return mixed[] |
||
201 | */ |
||
202 | public function getComponents() |
||
203 | { |
||
204 | return $this->components; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param mixed[] $components |
||
209 | */ |
||
210 | public function setComponents(array $components) |
||
211 | { |
||
212 | $this->components = []; |
||
213 | $this->addComponents($components); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @param mixed[] $componentRestrictions |
||
218 | */ |
||
219 | public function addComponents(array $componentRestrictions) |
||
220 | { |
||
221 | foreach ($componentRestrictions as $type => $value) { |
||
222 | $this->setComponent($type, $value); |
||
223 | } |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @param string $type |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function hasComponent($type) |
||
232 | { |
||
233 | return isset($this->components[$type]); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param string $type |
||
238 | * |
||
239 | * @return mixed |
||
240 | */ |
||
241 | public function getComponent($type) |
||
242 | { |
||
243 | return $this->hasComponent($type) ? $this->components[$type] : null; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @param string $type |
||
248 | * @param mixed $value |
||
249 | */ |
||
250 | public function setComponent($type, $value) |
||
251 | { |
||
252 | $this->components[$type] = $value; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @param string $type |
||
257 | */ |
||
258 | public function removeComponent($type) |
||
259 | { |
||
260 | unset($this->components[$type]); |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * @return bool |
||
265 | */ |
||
266 | public function hasValue() |
||
270 | |||
271 | /** |
||
272 | * @return string|null |
||
273 | */ |
||
274 | public function getValue() |
||
278 | |||
279 | /** |
||
280 | * @param string|null $value |
||
281 | */ |
||
282 | public function setValue($value = null) |
||
286 | |||
287 | /** |
||
288 | * @return bool |
||
289 | */ |
||
290 | public function hasInputAttributes() |
||
294 | |||
295 | /** |
||
296 | * @return string[] |
||
297 | */ |
||
298 | public function getInputAttributes() |
||
302 | |||
303 | /** |
||
304 | * @param string[] $inputAttributes |
||
305 | */ |
||
306 | public function setInputAttributes(array $inputAttributes) |
||
311 | |||
312 | /** |
||
313 | * @param string[] $inputAttributes |
||
314 | */ |
||
315 | public function addInputAttributes(array $inputAttributes) |
||
321 | |||
322 | /** |
||
323 | * @param string $name |
||
324 | * |
||
325 | * @return bool |
||
326 | */ |
||
327 | public function hasInputAttribute($name) |
||
331 | |||
332 | /** |
||
333 | * @param string $name |
||
334 | * |
||
335 | * @return string|null |
||
336 | */ |
||
337 | public function getInputAttribute($name) |
||
341 | |||
342 | /** |
||
343 | * @param string $name |
||
344 | * @param string $value |
||
345 | */ |
||
346 | public function setInputAttribute($name, $value) |
||
350 | |||
351 | /** |
||
352 | * @param string $name |
||
353 | */ |
||
354 | public function removeInputAttribute($name) |
||
358 | |||
359 | /** |
||
360 | * @return bool |
||
361 | */ |
||
362 | public function hasLibraries() |
||
366 | |||
367 | /** |
||
368 | * @return string[] |
||
369 | */ |
||
370 | public function getLibraries() |
||
374 | |||
375 | /** |
||
376 | * @param string[] $libraries |
||
377 | */ |
||
378 | public function setLibraries(array $libraries) |
||
383 | |||
384 | /** |
||
385 | * @param string[] $libraries |
||
386 | */ |
||
387 | public function addLibraries(array $libraries) |
||
393 | |||
394 | /** |
||
395 | * @param string $library |
||
396 | * |
||
397 | * @return bool |
||
398 | */ |
||
399 | public function hasLibrary($library) |
||
403 | |||
404 | /** |
||
405 | * @param string $library |
||
406 | */ |
||
407 | public function addLibrary($library) |
||
413 | |||
414 | /** |
||
415 | * @param string $library |
||
416 | */ |
||
417 | public function removeLibrary($library) |
||
421 | } |
||
422 |