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 | 184 | public function __construct() |
|
| 67 | { |
||
| 68 | 184 | $this->setEventManager(new EventManager()); |
|
| 69 | 184 | } |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | 72 | public function getHtmlId() |
|
| 75 | { |
||
| 76 | 72 | return $this->inputId; |
|
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param string $inputId |
||
| 81 | */ |
||
| 82 | 4 | public function setInputId($inputId) |
|
| 83 | { |
||
| 84 | 4 | $this->inputId = $inputId; |
|
| 85 | 4 | } |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @return EventManager |
||
| 89 | */ |
||
| 90 | 72 | public function getEventManager() |
|
| 91 | { |
||
| 92 | 72 | return $this->eventManager; |
|
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param EventManager $eventManager |
||
| 97 | */ |
||
| 98 | 184 | public function setEventManager(EventManager $eventManager) |
|
| 99 | { |
||
| 100 | 184 | $this->eventManager = $eventManager; |
|
| 101 | 184 | } |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @return bool |
||
| 105 | */ |
||
| 106 | 84 | public function hasBound() |
|
| 107 | { |
||
| 108 | 84 | return $this->bound !== null; |
|
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return Bound|null |
||
| 113 | */ |
||
| 114 | 28 | public function getBound() |
|
| 115 | { |
||
| 116 | 28 | return $this->bound; |
|
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param Bound|null $bound |
||
| 121 | */ |
||
| 122 | 24 | public function setBound(Bound $bound = null) |
|
| 123 | { |
||
| 124 | 24 | $this->bound = $bound; |
|
| 125 | 24 | } |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | 76 | public function hasTypes() |
|
| 131 | { |
||
| 132 | 76 | return !empty($this->types); |
|
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return string[] |
||
| 137 | */ |
||
| 138 | 28 | public function getTypes() |
|
| 139 | { |
||
| 140 | 28 | return $this->types; |
|
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param string[] $types |
||
| 145 | */ |
||
| 146 | 16 | public function setTypes(array $types) |
|
| 147 | { |
||
| 148 | 16 | $this->types = []; |
|
| 149 | 16 | $this->addTypes($types); |
|
| 150 | 16 | } |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @param string[] $types |
||
| 154 | */ |
||
| 155 | 16 | public function addTypes(array $types) |
|
| 156 | { |
||
| 157 | 16 | foreach ($types as $type) { |
|
| 158 | 16 | $this->addType($type); |
|
| 159 | 8 | } |
|
| 160 | 16 | } |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @param string $type |
||
| 164 | * |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | 24 | public function hasType($type) |
|
| 168 | { |
||
| 169 | 24 | return in_array($type, $this->types, true); |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param string $type |
||
| 174 | */ |
||
| 175 | 24 | public function addType($type) |
|
| 176 | { |
||
| 177 | 24 | if (!$this->hasType($type)) { |
|
| 178 | 24 | $this->types[] = $type; |
|
| 179 | 12 | } |
|
| 180 | 24 | } |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @param string $type |
||
| 184 | */ |
||
| 185 | 4 | public function removeType($type) |
|
| 186 | { |
||
| 187 | 4 | unset($this->types[array_search($type, $this->types, true)]); |
|
| 188 | 4 | $this->types = empty($this->types) ? [] : array_values($this->types); |
|
| 189 | 4 | } |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | 76 | public function hasComponents() |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @return mixed[] |
||
| 201 | */ |
||
| 202 | 28 | public function getComponents() |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @param mixed[] $components |
||
| 209 | */ |
||
| 210 | 16 | public function setComponents(array $components) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * @param mixed[] $components |
||
| 218 | */ |
||
| 219 | 16 | public function addComponents(array $components) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $type |
||
| 228 | * |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | 12 | public function hasComponent($type) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $type |
||
| 238 | * |
||
| 239 | * @return mixed |
||
| 240 | */ |
||
| 241 | 12 | public function getComponent($type) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @param string $type |
||
| 248 | * @param mixed $value |
||
| 249 | */ |
||
| 250 | 24 | public function setComponent($type, $value) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @param string $type |
||
| 257 | */ |
||
| 258 | 4 | public function removeComponent($type) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @return bool |
||
| 265 | */ |
||
| 266 | 60 | public function hasValue() |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @return string|null |
||
| 273 | */ |
||
| 274 | 52 | public function getValue() |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @param string|null $value |
||
| 281 | */ |
||
| 282 | 8 | public function setValue($value = null) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | 20 | public function hasInputAttributes() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @return string[] |
||
| 297 | */ |
||
| 298 | 76 | public function getInputAttributes() |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @param string[] $inputAttributes |
||
| 305 | */ |
||
| 306 | 8 | public function setInputAttributes(array $inputAttributes) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @param string[] $inputAttributes |
||
| 314 | */ |
||
| 315 | 8 | public function addInputAttributes(array $inputAttributes) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * @param string $name |
||
| 324 | * |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | 12 | public function hasInputAttribute($name) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $name |
||
| 334 | * |
||
| 335 | * @return string|null |
||
| 336 | */ |
||
| 337 | 12 | public function getInputAttribute($name) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * @param string $name |
||
| 344 | * @param string $value |
||
| 345 | */ |
||
| 346 | 20 | public function setInputAttribute($name, $value) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * @param string $name |
||
| 353 | */ |
||
| 354 | 4 | public function removeInputAttribute($name) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @return bool |
||
| 361 | */ |
||
| 362 | 64 | public function hasLibraries() |
|
| 366 | |||
| 367 | /** |
||
| 368 | * @return string[] |
||
| 369 | */ |
||
| 370 | 68 | public function getLibraries() |
|
| 374 | |||
| 375 | /** |
||
| 376 | * @param string[] $libraries |
||
| 377 | */ |
||
| 378 | 8 | public function setLibraries(array $libraries) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * @param string[] $libraries |
||
| 386 | */ |
||
| 387 | 8 | public function addLibraries(array $libraries) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * @param string $library |
||
| 396 | * |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | 20 | public function hasLibrary($library) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * @param string $library |
||
| 406 | */ |
||
| 407 | 20 | public function addLibrary($library) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * @param string $library |
||
| 416 | */ |
||
| 417 | 4 | public function removeLibrary($library) |
|
| 418 | { |
||
| 419 | 4 | unset($this->libraries[array_search($library, $this->libraries, true)]); |
|
| 420 | 4 | $this->libraries = empty($this->libraries) ? [] : array_values($this->libraries); |
|
| 421 | 4 | } |
|
| 422 | } |
||
| 423 |