Complex classes like HtmlElement 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 HtmlElement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class HtmlElement implements HtmlElementInterface |
||
| 10 | { |
||
| 11 | /** @var array */ |
||
| 12 | private $map; |
||
| 13 | |||
| 14 | /** @var EscaperInterface */ |
||
| 15 | private $escaper; |
||
| 16 | |||
| 17 | /** @var BranchValidatorInterface */ |
||
| 18 | private $branchValidator; |
||
| 19 | |||
| 20 | /** @var array The already resolved elements */ |
||
| 21 | private $resolved = array(); |
||
| 22 | |||
| 23 | /** @var array The default values of element options */ |
||
| 24 | private $defaults = array( |
||
| 25 | 'parent' => null, |
||
| 26 | 'children' => array(), |
||
| 27 | 'extends' => array(), |
||
| 28 | 'attr' => array(), |
||
| 29 | 'text' => null, |
||
| 30 | 'type' => null, |
||
| 31 | 'class' => Element::class |
||
| 32 | ); |
||
| 33 | |||
| 34 | /** @var array The mergeable attributes */ |
||
| 35 | private $mergeableAttributes = array('class', 'style'); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * HtmlElement constructor. |
||
| 39 | * |
||
| 40 | * @param array $map The elements map |
||
| 41 | * @param BranchValidatorInterface|null $branchValidator The branch validator |
||
| 42 | * @param EscaperInterface|null $escaper The escaper, by default ZendFramework/Escaper is used |
||
| 43 | * @param string $encoding The encoding used for escaping, by default utf-8 is used |
||
| 44 | */ |
||
| 45 | public function __construct( |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Create element on static calls. |
||
| 58 | * |
||
| 59 | * @param string $type The element type |
||
| 60 | * @param array $arguments The arguments array to set: |
||
| 61 | * [0] = text (string) |
||
| 62 | * [1] = attributes (array) |
||
| 63 | * [2] = children (array) |
||
| 64 | * |
||
| 65 | * @return ElementInterface |
||
| 66 | * |
||
| 67 | * @throws InvalidArgumentsNumberException If the arguments length is more than 3 |
||
| 68 | */ |
||
| 69 | public static function __callStatic($type, $arguments) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | */ |
||
| 92 | public static function create($type = null, $text = null, array $attributes = array(), array $children = array()) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | public function getMap() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | */ |
||
| 117 | public function setMap(array $map) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | public function addManyToMap(array $elements) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * {@inheritdoc} |
||
| 138 | */ |
||
| 139 | public function addOneToMap($name, array $element) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * {@inheritdoc} |
||
| 148 | */ |
||
| 149 | public function getBranchValidator() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritdoc} |
||
| 156 | */ |
||
| 157 | public function setBranchValidator(BranchValidatorInterface $branchValidator) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | public function getEscaper() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | public function setEscaper(EscaperInterface $escaper) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * {@inheritdoc} |
||
| 184 | */ |
||
| 185 | public function load($name, array $parameters = array(), array $attributes = array(), array $children = array()) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get the element instance. |
||
| 200 | * |
||
| 201 | * @param string $name The element name |
||
| 202 | * @param array $parameters The parameters to replace in element |
||
| 203 | * @param bool $mainCall Determine if it's the main(first) call of the method |
||
| 204 | * |
||
| 205 | * @return ElementInterface |
||
| 206 | * |
||
| 207 | * @throws InvalidElementException If the current instance doesn't implement ElementInterface |
||
| 208 | */ |
||
| 209 | private function getInstance($name, array $parameters, $mainCall = false) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get the resolved element representation. |
||
| 246 | * |
||
| 247 | * @param string $name The current element name |
||
| 248 | * @param array $parameters The parameters to replace in element |
||
| 249 | * @param bool $mainCall Determine if it's the main(first) call of the method |
||
| 250 | * |
||
| 251 | * @return array |
||
| 252 | */ |
||
| 253 | private function resolveElement($name, array $parameters, $mainCall = false) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Check if an element has been already resolved. |
||
| 287 | * |
||
| 288 | * @param string $name |
||
| 289 | * |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | private function alreadyResolved($name) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * {@inheritdoc} |
||
| 299 | */ |
||
| 300 | public function exists($name) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Get the current element representation. |
||
| 307 | * |
||
| 308 | * @param string $name The element name |
||
| 309 | * |
||
| 310 | * @return array |
||
| 311 | * |
||
| 312 | * @throws InvalidElementException If the current element is defined dynamically and doesn't define a name |
||
| 313 | * @throws UndefinedElementException If the current element doesn't exist |
||
| 314 | */ |
||
| 315 | public function getCurrentElement($name) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Replace the parameters of the element. |
||
| 339 | * |
||
| 340 | * @param array $element The element with the parameters to replace |
||
| 341 | * @param array $parameters The array of parameters values |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | private function replaceParameters(array $element, array $parameters) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Extend element from another one. |
||
| 366 | * |
||
| 367 | * @param array $extend The array of the element to extend |
||
| 368 | * @param array $current The current element which extends |
||
| 369 | * |
||
| 370 | * @return array |
||
| 371 | */ |
||
| 372 | private function extendElement($extend, $current) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Extend attributes from another element. |
||
| 383 | * |
||
| 384 | * @param array $from The array of attributes to extend |
||
| 385 | * @param array $to The array of attributes which extends |
||
| 386 | * |
||
| 387 | * @return array |
||
| 388 | */ |
||
| 389 | private function extendAttributes(array $from, array $to) |
||
| 403 | } |
||
| 404 |