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 | 55 | public function setNavigator(GraphNavigator $navigator) |
|
| 46 | { |
||
| 47 | 55 | $this->navigator = $navigator; |
|
| 48 | 55 | $this->objectStack = new \SplStack; |
|
| 49 | 55 | $this->metadataStack = new \SplStack; |
|
| 50 | 55 | $this->objectMetadataStack = new \SplStack; |
|
| 51 | 55 | $this->result = null; |
|
| 52 | 55 | } |
|
| 53 | |||
| 54 | 1 | public function getNavigator() |
|
| 58 | |||
| 59 | 57 | public function prepare($data) |
|
| 60 | { |
||
| 61 | 57 | $previous = libxml_use_internal_errors(true); |
|
| 62 | 57 | $previousEntityLoaderState = libxml_disable_entity_loader($this->disableExternalEntities); |
|
| 63 | |||
| 64 | 57 | if (false !== stripos($data, '<!doctype')) { |
|
| 65 | 3 | $internalSubset = $this->getDomDocumentTypeEntitySubset($data); |
|
| 66 | 3 | if (!in_array($internalSubset, $this->doctypeWhitelist, true)) { |
|
| 67 | 2 | throw new InvalidArgumentException(sprintf( |
|
| 68 | 2 | 'The document type "%s" is not allowed. If it is safe, you may add it to the whitelist configuration.', |
|
| 69 | $internalSubset |
||
| 70 | 2 | )); |
|
| 71 | } |
||
| 72 | 1 | } |
|
| 73 | |||
| 74 | 55 | $doc = simplexml_load_string($data); |
|
| 75 | |||
| 76 | 55 | libxml_use_internal_errors($previous); |
|
| 77 | 55 | libxml_disable_entity_loader($previousEntityLoaderState); |
|
| 78 | |||
| 79 | 55 | if (false === $doc) { |
|
| 80 | throw new XmlErrorException(libxml_get_last_error()); |
||
| 81 | } |
||
| 82 | |||
| 83 | 55 | return $doc; |
|
| 84 | } |
||
| 85 | |||
| 86 | 1 | public function visitNull($data, array $type, Context $context) |
|
| 90 | |||
| 91 | 20 | public function visitString($data, array $type, Context $context) |
|
| 101 | |||
| 102 | 8 | public function visitBoolean($data, array $type, Context $context) |
|
| 103 | { |
||
| 104 | 8 | $data = (string) $data; |
|
| 105 | |||
| 106 | 8 | if ('true' === $data || '1' === $data) { |
|
| 107 | 4 | $data = true; |
|
| 108 | 8 | } elseif ('false' === $data || '0' === $data) { |
|
| 109 | 5 | $data = false; |
|
| 110 | 5 | } else { |
|
| 111 | throw new RuntimeException(sprintf('Could not convert data to boolean. Expected "true", "false", "1" or "0", but got %s.', json_encode($data))); |
||
| 112 | } |
||
| 113 | |||
| 114 | 8 | if (null === $this->result) { |
|
| 115 | 6 | $this->result = $data; |
|
| 116 | 6 | } |
|
| 117 | |||
| 118 | 8 | return $data; |
|
| 119 | } |
||
| 120 | |||
| 121 | 8 | public function visitInteger($data, array $type, Context $context) |
|
| 131 | |||
| 132 | 12 | public function visitDouble($data, array $type, Context $context) |
|
| 142 | |||
| 143 | 15 | public function visitArray($data, array $type, Context $context) |
|
| 144 | { |
||
| 145 | 15 | $entryName = null !== $this->currentMetadata && $this->currentMetadata->xmlEntryName ? $this->currentMetadata->xmlEntryName : 'entry'; |
|
| 214 | |||
| 215 | 27 | public function startVisitingObject(ClassMetadata $metadata, $object, array $type, Context $context) |
|
| 223 | |||
| 224 | 24 | public function visitProperty(PropertyMetadata $metadata, $data, Context $context) |
|
| 290 | |||
| 291 | 27 | public function endVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
|
| 299 | |||
| 300 | 27 | public function setCurrentObject($object) |
|
| 305 | |||
| 306 | public function getCurrentObject() |
||
| 310 | |||
| 311 | 27 | public function revertCurrentObject() |
|
| 315 | |||
| 316 | 6 | public function setCurrentMetadata(PropertyMetadata $metadata) |
|
| 321 | |||
| 322 | public function getCurrentMetadata() |
||
| 326 | |||
| 327 | 6 | public function revertCurrentMetadata() |
|
| 331 | |||
| 332 | 54 | public function getResult() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * @param array<string> $doctypeWhitelist |
||
| 339 | */ |
||
| 340 | 1 | public function setDoctypeWhitelist(array $doctypeWhitelist) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * @return array<string> |
||
| 347 | */ |
||
| 348 | public function getDoctypeWhitelist() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Retrieves internalSubset even in bugfixed php versions |
||
| 355 | * |
||
| 356 | * @param \DOMDocumentType $child |
||
|
|
|||
| 357 | * @param string $data |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | 3 | private function getDomDocumentTypeEntitySubset($data) |
|
| 381 | } |
||
| 382 |
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.