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 | * Load element on dynamic calls. |
||
| 58 | * |
||
| 59 | * @param string $name The element name |
||
| 60 | * @param array $arguments The arguments array to set: |
||
| 61 | * [0] = parameters (array) |
||
| 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 function __call($name, $arguments) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | */ |
||
| 92 | public function getMap() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritdoc} |
||
| 99 | */ |
||
| 100 | public function setMap(array $map) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | public function addManyToMap(array $elements) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritdoc} |
||
| 121 | */ |
||
| 122 | public function addOneToMap($name, array $element) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * {@inheritdoc} |
||
| 131 | */ |
||
| 132 | public function getBranchValidator() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritdoc} |
||
| 139 | */ |
||
| 140 | public function setBranchValidator(BranchValidatorInterface $branchValidator) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | */ |
||
| 150 | public function getEscaper() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritdoc} |
||
| 157 | */ |
||
| 158 | public function setEscaper(EscaperInterface $escaper) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | public function load($name, array $parameters = array(), array $attributes = array(), array $children = array()) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get the element instance. |
||
| 183 | * |
||
| 184 | * @param string $name The element name |
||
| 185 | * @param array $parameters The parameters to replace in element |
||
| 186 | * @param bool $mainCall Determine if it's the main(first) call of the method |
||
| 187 | * |
||
| 188 | * @return ElementInterface |
||
| 189 | * |
||
| 190 | * @throws InvalidElementException If the current instance doesn't implement ElementInterface |
||
| 191 | */ |
||
| 192 | private function getInstance($name, array $parameters, $mainCall = false) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get the resolved element representation. |
||
| 229 | * |
||
| 230 | * @param string $name The current element name |
||
| 231 | * @param array $parameters The parameters to replace in element |
||
| 232 | * @param bool $mainCall Determine if it's the main(first) call of the method |
||
| 233 | * |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | private function resolveElement($name, array $parameters, $mainCall = false) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Check if an element has been already resolved. |
||
| 270 | * |
||
| 271 | * @param string $name |
||
| 272 | * |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | private function alreadyResolved($name) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * {@inheritdoc} |
||
| 282 | */ |
||
| 283 | public function exists($name) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get the current element representation. |
||
| 290 | * |
||
| 291 | * @param string $name The element name |
||
| 292 | * |
||
| 293 | * @return array |
||
| 294 | * |
||
| 295 | * @throws InvalidElementException If the current element is defined dynamically and doesn't define a name |
||
| 296 | * @throws UndefinedElementException If the current element doesn't exist |
||
| 297 | */ |
||
| 298 | public function getCurrentElement($name) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Replace parameters in text and attr. |
||
| 322 | * |
||
| 323 | * @param array $element The element with the parameters to replace |
||
| 324 | * @param array $parameters The array of parameters values |
||
| 325 | * |
||
| 326 | * @return array |
||
| 327 | */ |
||
| 328 | private function replaceParameters(array $element, array $parameters) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Replace parameters in attr. |
||
| 341 | * |
||
| 342 | * @param array $attributes The attributes |
||
| 343 | * @param array $parameters The parameters |
||
| 344 | * |
||
| 345 | * @return array |
||
| 346 | */ |
||
| 347 | private function replaceParametersInAttributes(array $attributes, array $parameters) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Extend element from another one. |
||
| 370 | * |
||
| 371 | * @param array $extend The array of the element to extend |
||
| 372 | * @param array $current The current element which extends |
||
| 373 | * |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | private function extendElement($extend, $current) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Extend attributes from another element. |
||
| 395 | * |
||
| 396 | * @param array $from The array of attributes to extend |
||
| 397 | * @param array $to The array of attributes which extends |
||
| 398 | * |
||
| 399 | * @return array |
||
| 400 | */ |
||
| 401 | private function extendAttributes(array $from, array $to) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Extend mergeable attributes from another element. |
||
| 418 | * |
||
| 419 | * @param string|array $from The attribute to extend |
||
| 420 | * @param string|array $to The attribute which extends |
||
| 421 | * @param string $attr The attribute name |
||
| 422 | * |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | private function extendMergeableAttributes($from, $to, $attr) |
||
| 436 | } |
||
| 437 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: