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 | 136 | public function __construct() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | 24 | public function getHtmlId() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @param string $inputId |
||
| 81 | */ |
||
| 82 | 4 | public function setInputId($inputId) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @return EventManager |
||
| 89 | */ |
||
| 90 | 24 | public function getEventManager() |
|
| 94 | |||
| 95 | 136 | public function setEventManager(EventManager $eventManager) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @return bool |
||
| 102 | */ |
||
| 103 | 36 | public function hasBound() |
|
| 107 | |||
| 108 | /** |
||
| 109 | * @return Bound|null |
||
| 110 | */ |
||
| 111 | 24 | public function getBound() |
|
| 115 | |||
| 116 | 20 | public function setBound(Bound $bound = null) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | 28 | public function hasTypes() |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @return string[] |
||
| 131 | */ |
||
| 132 | 24 | public function getTypes() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @param string[] $types |
||
| 139 | */ |
||
| 140 | 12 | public function setTypes(array $types) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @param string[] $types |
||
| 148 | */ |
||
| 149 | 12 | public function addTypes(array $types) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @param string $type |
||
| 158 | * |
||
| 159 | * @return bool |
||
| 160 | */ |
||
| 161 | 20 | public function hasType($type) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $type |
||
| 168 | */ |
||
| 169 | 20 | public function addType($type) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @param string $type |
||
| 178 | */ |
||
| 179 | 4 | public function removeType($type) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | 28 | public function hasComponents() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @return mixed[] |
||
| 195 | */ |
||
| 196 | 24 | public function getComponents() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @param mixed[] $components |
||
| 203 | */ |
||
| 204 | 12 | public function setComponents(array $components) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @param mixed[] $components |
||
| 212 | */ |
||
| 213 | 12 | public function addComponents(array $components) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $type |
||
| 222 | * |
||
| 223 | * @return bool |
||
| 224 | */ |
||
| 225 | 12 | public function hasComponent($type) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @param string $type |
||
| 232 | * |
||
| 233 | * @return mixed |
||
| 234 | */ |
||
| 235 | 12 | public function getComponent($type) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $type |
||
| 242 | * @param mixed $value |
||
| 243 | */ |
||
| 244 | 20 | public function setComponent($type, $value) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $type |
||
| 251 | */ |
||
| 252 | 4 | public function removeComponent($type) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | 12 | public function hasValue() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @return string|null |
||
| 267 | */ |
||
| 268 | 8 | public function getValue() |
|
| 272 | |||
| 273 | /** |
||
| 274 | * @param string|null $value |
||
| 275 | */ |
||
| 276 | 4 | public function setValue($value = null) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | 20 | public function hasInputAttributes() |
|
| 288 | |||
| 289 | /** |
||
| 290 | * @return string[] |
||
| 291 | */ |
||
| 292 | 28 | public function getInputAttributes() |
|
| 296 | |||
| 297 | /** |
||
| 298 | * @param string[] $inputAttributes |
||
| 299 | */ |
||
| 300 | 8 | public function setInputAttributes(array $inputAttributes) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * @param string[] $inputAttributes |
||
| 308 | */ |
||
| 309 | 8 | public function addInputAttributes(array $inputAttributes) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * @param string $name |
||
| 318 | * |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | 12 | public function hasInputAttribute($name) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @param string $name |
||
| 328 | * |
||
| 329 | * @return string|null |
||
| 330 | */ |
||
| 331 | 12 | public function getInputAttribute($name) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * @param string $name |
||
| 338 | * @param string $value |
||
| 339 | */ |
||
| 340 | 16 | public function setInputAttribute($name, $value) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * @param string $name |
||
| 347 | */ |
||
| 348 | 4 | public function removeInputAttribute($name) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | 20 | public function hasLibraries() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * @return string[] |
||
| 363 | */ |
||
| 364 | 20 | public function getLibraries() |
|
| 368 | |||
| 369 | /** |
||
| 370 | * @param string[] $libraries |
||
| 371 | */ |
||
| 372 | 8 | public function setLibraries(array $libraries) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * @param string[] $libraries |
||
| 380 | */ |
||
| 381 | 8 | public function addLibraries(array $libraries) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * @param string $library |
||
| 390 | * |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | 16 | public function hasLibrary($library) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * @param string $library |
||
| 400 | */ |
||
| 401 | 16 | public function addLibrary($library) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * @param string $library |
||
| 410 | */ |
||
| 411 | 4 | public function removeLibrary($library) |
|
| 416 | } |
||
| 417 |