Complex classes like Choice 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 Choice, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Choice extends Field |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Renders the checkables as inline |
||
| 17 | * |
||
| 18 | * @var boolean |
||
| 19 | */ |
||
| 20 | protected $inline = false; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The choice's placeholder |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $placeholder = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The choice's options |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $options = [ |
||
| 35 | 'isMultiple' => false, |
||
| 36 | 'isExpanded' => false, |
||
| 37 | ]; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The choice's choices |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $choices = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The choice's element |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $element = 'choice'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The choice's self-closing state |
||
| 55 | * |
||
| 56 | * @var boolean |
||
| 57 | */ |
||
| 58 | protected $isSelfClosing = false; |
||
| 59 | |||
| 60 | //////////////////////////////////////////////////////////////////// |
||
| 61 | /////////////////////////// CORE METHODS /////////////////////////// |
||
| 62 | //////////////////////////////////////////////////////////////////// |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Easier arguments order for selects |
||
| 66 | * |
||
| 67 | * @param Container $app The Container instance |
||
| 68 | * @param string $type select |
||
| 69 | * @param string $name Field name |
||
| 70 | * @param string $label Its label |
||
| 71 | * @param array $choices The choice's choices |
||
| 72 | * @param string $selected The selected choice(s) |
||
| 73 | * @param array $attributes Attributes |
||
| 74 | */ |
||
| 75 | public function __construct(Container $app, $type, $name, $label, $choices, $selected, $attributes) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Renders the choice |
||
| 97 | * |
||
| 98 | * @return string A <select> tag |
||
| 99 | */ |
||
| 100 | public function render() |
||
| 122 | |||
| 123 | public function getSelect() |
||
| 139 | |||
| 140 | public function getOptions() |
||
| 158 | |||
| 159 | public function getOptGroup($label, $options) |
||
| 171 | |||
| 172 | public function getOption($key, $value) |
||
| 190 | |||
| 191 | public function getCheckables($choiceType) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get the choice's placeholder |
||
| 275 | * |
||
| 276 | * @return Element |
||
| 277 | */ |
||
| 278 | protected function getPlaceholder() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Sets the element's type based on options |
||
| 294 | * |
||
| 295 | * @return this |
||
| 296 | */ |
||
| 297 | protected function setChoiceType() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Select a value in the field's children |
||
| 311 | * |
||
| 312 | * @param mixed $value |
||
| 313 | * |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | protected function hasValue($choiceValue) |
||
| 328 | |||
| 329 | //////////////////////////////////////////////////////////////////// |
||
| 330 | ////////////////////////// FIELD METHODS /////////////////////////// |
||
| 331 | //////////////////////////////////////////////////////////////////// |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Set the choices |
||
| 335 | * |
||
| 336 | * @param array $_choices The choices as an array |
||
| 337 | * @param mixed $selected Facultative selected entry |
||
| 338 | * @param boolean $valuesAsKeys Whether the array's values should be used as |
||
| 339 | * the option's values instead of the array's keys |
||
| 340 | */ |
||
| 341 | public function addChoice($value, $key = null) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Set the choices |
||
| 350 | * |
||
| 351 | * @param array $_choices The choices as an array |
||
| 352 | * @param mixed $selected Facultative selected entry |
||
| 353 | * @param boolean $valuesAsKeys Whether the array's values should be used as |
||
| 354 | * the option's values instead of the array's keys |
||
| 355 | */ |
||
| 356 | public function choices($_choices, $valuesAsKeys = false) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Creates a list of choices from a range |
||
| 376 | * |
||
| 377 | * @param integer $from |
||
| 378 | * @param integer $to |
||
| 379 | * @param integer $step |
||
| 380 | */ |
||
| 381 | public function range($from, $to, $step = 1) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Use the results from a Fluent/Eloquent query as choices |
||
| 391 | * |
||
| 392 | * @param array $results An array of Eloquent models |
||
| 393 | * @param string|function $text The value to use as text |
||
| 394 | * @param string|array $attributes The data to use as attributes |
||
| 395 | * @param string $selected The default selected item |
||
| 396 | */ |
||
| 397 | public function fromQuery($results, $text = null, $attributes = null, $selected = null) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Add a placeholder to the current select |
||
| 406 | * |
||
| 407 | * @param string $placeholder The placeholder text |
||
| 408 | */ |
||
| 409 | public function placeholder($placeholder) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Set isMultiple |
||
| 418 | * |
||
| 419 | * @param boolean $isMultiple |
||
| 420 | * @return $this |
||
| 421 | */ |
||
| 422 | public function multiple($isMultiple = true) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Set isExpanded |
||
| 432 | * |
||
| 433 | * @param boolean $isExpanded |
||
| 434 | * @return $this |
||
| 435 | */ |
||
| 436 | public function expanded($isExpanded = true) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Set the choices as inline (for expanded items) |
||
| 446 | */ |
||
| 447 | public function inline($isInline = true) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Set the choices as stacked (for expanded items) |
||
| 456 | */ |
||
| 457 | public function stacked($isStacked = true) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Check if field is a checkbox or a radio |
||
| 466 | * |
||
| 467 | * @return boolean |
||
| 468 | */ |
||
| 469 | public function isCheckable() |
||
| 473 | |||
| 474 | //////////////////////////////////////////////////////////////////// |
||
| 475 | ////////////////////////////// HELPERS ///////////////////////////// |
||
| 476 | //////////////////////////////////////////////////////////////////// |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Returns the current choices in memory for manipulations |
||
| 480 | * |
||
| 481 | * @return array The current choices array |
||
| 482 | */ |
||
| 483 | public function getChoices() |
||
| 487 | |||
| 488 | } |
||
| 489 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: