Complex classes like XmlDeserializationVisitor 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 XmlDeserializationVisitor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class XmlDeserializationVisitor extends AbstractVisitor |
||
| 29 | { |
||
| 30 | private $objectStack; |
||
| 31 | private $metadataStack; |
||
| 32 | private $objectMetadataStack; |
||
| 33 | private $currentObject; |
||
| 34 | private $currentMetadata; |
||
| 35 | private $result; |
||
| 36 | private $navigator; |
||
| 37 | private $disableExternalEntities = true; |
||
| 38 | private $doctypeWhitelist = array(); |
||
| 39 | |||
| 40 | public function enableExternalEntities() |
||
| 44 | |||
| 45 | 56 | public function setNavigator(GraphNavigator $navigator) |
|
| 46 | { |
||
| 47 | 56 | $this->navigator = $navigator; |
|
| 48 | 56 | $this->objectStack = new \SplStack; |
|
| 49 | 56 | $this->metadataStack = new \SplStack; |
|
| 50 | 56 | $this->objectMetadataStack = new \SplStack; |
|
| 51 | 56 | $this->result = null; |
|
| 52 | 56 | } |
|
| 53 | |||
| 54 | 1 | public function getNavigator() |
|
| 58 | |||
| 59 | 58 | public function prepare($data) |
|
| 60 | { |
||
| 61 | 58 | $data = $this->emptyStringToSpaceCharacter($data); |
|
| 62 | |||
| 63 | 58 | $previous = libxml_use_internal_errors(true); |
|
| 64 | 58 | libxml_clear_errors(); |
|
| 65 | |||
| 66 | 58 | $previousEntityLoaderState = libxml_disable_entity_loader($this->disableExternalEntities); |
|
| 67 | 3 | ||
| 68 | 3 | if (false !== stripos($data, '<!doctype')) { |
|
| 69 | 2 | $internalSubset = $this->getDomDocumentTypeEntitySubset($data); |
|
| 70 | 2 | if (!in_array($internalSubset, $this->doctypeWhitelist, true)) { |
|
| 71 | throw new InvalidArgumentException(sprintf( |
||
| 72 | 2 | 'The document type "%s" is not allowed. If it is safe, you may add it to the whitelist configuration.', |
|
| 73 | $internalSubset |
||
| 74 | 1 | )); |
|
| 75 | } |
||
| 76 | 56 | } |
|
| 77 | |||
| 78 | 56 | $doc = simplexml_load_string($data); |
|
| 79 | 56 | ||
| 80 | libxml_use_internal_errors($previous); |
||
| 81 | 56 | libxml_disable_entity_loader($previousEntityLoaderState); |
|
| 82 | |||
| 83 | if (false === $doc) { |
||
| 84 | throw new XmlErrorException(libxml_get_last_error()); |
||
| 85 | 56 | } |
|
| 86 | |||
| 87 | return $doc; |
||
| 88 | 58 | } |
|
| 89 | |||
| 90 | 58 | private function emptyStringToSpaceCharacter($data) |
|
| 94 | |||
| 95 | 1 | public function visitNull($data, array $type, Context $context) |
|
| 99 | |||
| 100 | 20 | public function visitString($data, array $type, Context $context) |
|
| 110 | |||
| 111 | 8 | public function visitBoolean($data, array $type, Context $context) |
|
| 129 | |||
| 130 | 8 | public function visitInteger($data, array $type, Context $context) |
|
| 140 | |||
| 141 | 12 | public function visitDouble($data, array $type, Context $context) |
|
| 151 | |||
| 152 | 15 | public function visitArray($data, array $type, Context $context) |
|
| 223 | |||
| 224 | 27 | public function startVisitingObject(ClassMetadata $metadata, $object, array $type, Context $context) |
|
| 232 | |||
| 233 | 24 | public function visitProperty(PropertyMetadata $metadata, $data, Context $context) |
|
| 299 | |||
| 300 | 27 | public function endVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
|
| 308 | |||
| 309 | 27 | public function setCurrentObject($object) |
|
| 310 | 27 | { |
|
| 311 | 27 | $this->objectStack->push($this->currentObject); |
|
| 312 | $this->currentObject = $object; |
||
| 313 | } |
||
| 314 | |||
| 315 | public function getCurrentObject() |
||
| 316 | { |
||
| 317 | return $this->currentObject; |
||
| 318 | 27 | } |
|
| 319 | |||
| 320 | 27 | public function revertCurrentObject() |
|
| 324 | |||
| 325 | 6 | public function setCurrentMetadata(PropertyMetadata $metadata) |
|
| 326 | 6 | { |
|
| 327 | 6 | $this->metadataStack->push($this->currentMetadata); |
|
| 328 | $this->currentMetadata = $metadata; |
||
| 329 | } |
||
| 330 | |||
| 331 | public function getCurrentMetadata() |
||
| 332 | { |
||
| 333 | return $this->currentMetadata; |
||
| 334 | 6 | } |
|
| 335 | |||
| 336 | 6 | public function revertCurrentMetadata() |
|
| 340 | |||
| 341 | 55 | public function getResult() |
|
| 342 | { |
||
| 343 | return $this->result; |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | 1 | * @param array<string> $doctypeWhitelist |
|
| 348 | */ |
||
| 349 | 1 | public function setDoctypeWhitelist(array $doctypeWhitelist) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * @return array<string> |
||
| 356 | */ |
||
| 357 | public function getDoctypeWhitelist() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Retrieves internalSubset even in bugfixed php versions |
||
| 364 | * |
||
| 365 | * @param \DOMDocumentType $child |
||
|
|
|||
| 366 | * @param string $data |
||
| 367 | 3 | * @return string |
|
| 368 | */ |
||
| 369 | 3 | private function getDomDocumentTypeEntitySubset($data) |
|
| 390 | } |
||
| 391 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.