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 implements NullAwareVisitorInterface |
||
| 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 | 62 | public function setNavigator(GraphNavigator $navigator) |
|
| 46 | { |
||
| 47 | 62 | $this->navigator = $navigator; |
|
| 48 | 62 | $this->objectStack = new \SplStack; |
|
| 49 | 62 | $this->metadataStack = new \SplStack; |
|
| 50 | 62 | $this->objectMetadataStack = new \SplStack; |
|
| 51 | 62 | $this->result = null; |
|
| 52 | 62 | } |
|
| 53 | |||
| 54 | 1 | public function getNavigator() |
|
| 58 | |||
| 59 | 65 | public function prepare($data) |
|
| 60 | { |
||
| 61 | 65 | $data = $this->emptyStringToSpaceCharacter($data); |
|
| 62 | |||
| 63 | 65 | $previous = libxml_use_internal_errors(true); |
|
| 64 | 65 | libxml_clear_errors(); |
|
| 65 | |||
| 66 | 65 | $previousEntityLoaderState = libxml_disable_entity_loader($this->disableExternalEntities); |
|
| 67 | |||
| 68 | 65 | if (false !== stripos($data, '<!doctype')) { |
|
| 69 | 3 | $internalSubset = $this->getDomDocumentTypeEntitySubset($data); |
|
| 70 | 3 | if (!in_array($internalSubset, $this->doctypeWhitelist, true)) { |
|
| 71 | 2 | 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 | 2 | $internalSubset |
|
| 74 | )); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | 63 | $doc = simplexml_load_string($data); |
|
| 79 | |||
| 80 | 63 | libxml_use_internal_errors($previous); |
|
| 81 | 63 | libxml_disable_entity_loader($previousEntityLoaderState); |
|
| 82 | |||
| 83 | 63 | if (false === $doc) { |
|
| 84 | 1 | throw new XmlErrorException(libxml_get_last_error()); |
|
| 85 | } |
||
| 86 | |||
| 87 | 62 | return $doc; |
|
| 88 | } |
||
| 89 | |||
| 90 | 65 | private function emptyStringToSpaceCharacter($data) |
|
| 94 | |||
| 95 | 7 | public function visitNull($data, array $type, Context $context) |
|
| 99 | |||
| 100 | 24 | public function visitString($data, array $type, Context $context) |
|
| 101 | { |
||
| 102 | 24 | $data = (string)$data; |
|
| 103 | |||
| 104 | 24 | if (null === $this->result) { |
|
| 105 | 2 | $this->result = $data; |
|
| 106 | } |
||
| 107 | |||
| 108 | 24 | return $data; |
|
| 109 | } |
||
| 110 | |||
| 111 | 8 | public function visitBoolean($data, array $type, Context $context) |
|
| 129 | |||
| 130 | 8 | public function visitInteger($data, array $type, Context $context) |
|
| 140 | |||
| 141 | 10 | public function visitDouble($data, array $type, Context $context) |
|
| 142 | { |
||
| 143 | 10 | $data = (double)$data; |
|
| 144 | |||
| 145 | 10 | if (null === $this->result) { |
|
| 146 | 4 | $this->result = $data; |
|
| 147 | } |
||
| 148 | |||
| 149 | 10 | return $data; |
|
| 150 | } |
||
| 151 | |||
| 152 | 18 | public function visitArray($data, array $type, Context $context) |
|
| 153 | { |
||
| 154 | // handle key-value-pairs |
||
| 155 | 18 | if (null !== $this->currentMetadata && $this->currentMetadata->xmlKeyValuePairs) { |
|
| 156 | 2 | if (2 !== count($type['params'])) { |
|
| 157 | throw new RuntimeException('The array type must be specified as "array<K,V>" for Key-Value-Pairs.'); |
||
| 158 | } |
||
| 159 | 2 | $this->revertCurrentMetadata(); |
|
| 160 | |||
| 161 | 2 | list($keyType, $entryType) = $type['params']; |
|
| 162 | |||
| 163 | 2 | $result = []; |
|
| 164 | 2 | foreach ($data as $key => $v) { |
|
| 165 | 2 | $k = $this->navigator->accept($key, $keyType, $context); |
|
| 166 | 2 | $result[$k] = $this->navigator->accept($v, $entryType, $context); |
|
| 167 | } |
||
| 168 | |||
| 169 | 2 | return $result; |
|
| 170 | } |
||
| 171 | |||
| 172 | 18 | $entryName = null !== $this->currentMetadata && $this->currentMetadata->xmlEntryName ? $this->currentMetadata->xmlEntryName : 'entry'; |
|
| 173 | 18 | $namespace = null !== $this->currentMetadata && $this->currentMetadata->xmlEntryNamespace ? $this->currentMetadata->xmlEntryNamespace : null; |
|
| 174 | |||
| 175 | 18 | if ($namespace === null && $this->objectMetadataStack->count()) { |
|
| 176 | 13 | $classMetadata = $this->objectMetadataStack->top(); |
|
| 177 | 13 | $namespace = isset($classMetadata->xmlNamespaces['']) ? $classMetadata->xmlNamespaces[''] : $namespace; |
|
| 178 | 13 | if ($namespace === null) { |
|
| 179 | 10 | $namespaces = $data->getDocNamespaces(); |
|
| 180 | 10 | if (isset($namespaces[''])) { |
|
| 181 | 1 | $namespace = $namespaces['']; |
|
| 182 | } |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | 18 | if (null !== $namespace) { |
|
| 187 | 5 | $prefix = uniqid('ns-'); |
|
| 188 | 5 | $data->registerXPathNamespace($prefix, $namespace); |
|
| 189 | 5 | $nodes = $data->xpath("$prefix:$entryName"); |
|
| 190 | } else { |
||
| 191 | 14 | $nodes = $data->xpath($entryName); |
|
| 192 | } |
||
| 193 | |||
| 194 | 18 | if (!count($nodes)) { |
|
| 195 | 4 | if (null === $this->result) { |
|
| 196 | return $this->result = array(); |
||
| 197 | } |
||
| 198 | |||
| 199 | 4 | return array(); |
|
| 200 | } |
||
| 201 | |||
| 202 | 18 | switch (count($type['params'])) { |
|
| 203 | 18 | case 0: |
|
| 204 | throw new RuntimeException(sprintf('The array type must be specified either as "array<T>", or "array<K,V>".')); |
||
| 205 | |||
| 206 | 18 | case 1: |
|
| 207 | 18 | $result = array(); |
|
| 208 | |||
| 209 | 18 | if (null === $this->result) { |
|
| 210 | 5 | $this->result = &$result; |
|
| 211 | } |
||
| 212 | |||
| 213 | 18 | foreach ($nodes as $v) { |
|
| 214 | 18 | $result[] = $this->navigator->accept($v, $type['params'][0], $context); |
|
| 215 | } |
||
| 216 | |||
| 217 | 18 | return $result; |
|
| 218 | |||
| 219 | 4 | case 2: |
|
| 220 | 4 | if (null === $this->currentMetadata) { |
|
| 221 | throw new RuntimeException('Maps are not supported on top-level without metadata.'); |
||
| 222 | } |
||
| 223 | |||
| 224 | 4 | list($keyType, $entryType) = $type['params']; |
|
| 225 | 4 | $result = array(); |
|
| 226 | 4 | if (null === $this->result) { |
|
| 227 | $this->result = &$result; |
||
| 228 | } |
||
| 229 | |||
| 230 | 4 | $nodes = $data->children($namespace)->$entryName; |
|
| 231 | 4 | foreach ($nodes as $v) { |
|
| 232 | 4 | $attrs = $v->attributes(); |
|
| 233 | 4 | if (!isset($attrs[$this->currentMetadata->xmlKeyAttribute])) { |
|
| 234 | throw new RuntimeException(sprintf('The key attribute "%s" must be set for each entry of the map.', $this->currentMetadata->xmlKeyAttribute)); |
||
| 235 | } |
||
| 236 | |||
| 237 | 4 | $k = $this->navigator->accept($attrs[$this->currentMetadata->xmlKeyAttribute], $keyType, $context); |
|
| 238 | 4 | $result[$k] = $this->navigator->accept($v, $entryType, $context); |
|
| 239 | } |
||
| 240 | |||
| 241 | 4 | return $result; |
|
| 242 | |||
| 243 | default: |
||
| 244 | throw new LogicException(sprintf('The array type does not support more than 2 parameters, but got %s.', json_encode($type['params']))); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | 33 | public function startVisitingObject(ClassMetadata $metadata, $object, array $type, Context $context) |
|
| 249 | { |
||
| 250 | 33 | $this->setCurrentObject($object); |
|
| 251 | 33 | $this->objectMetadataStack->push($metadata); |
|
| 252 | 33 | if (null === $this->result) { |
|
| 253 | 32 | $this->result = $this->currentObject; |
|
| 254 | } |
||
| 255 | 33 | } |
|
| 256 | |||
| 257 | 29 | public function visitProperty(PropertyMetadata $metadata, $data, Context $context) |
|
| 258 | { |
||
| 259 | 29 | $name = $this->namingStrategy->translateName($metadata); |
|
| 260 | |||
| 261 | 29 | if (!$metadata->type) { |
|
| 262 | throw new RuntimeException(sprintf('You must define a type for %s::$%s.', $metadata->reflection->class, $metadata->name)); |
||
| 263 | } |
||
| 264 | |||
| 265 | 29 | if ($metadata->xmlAttribute) { |
|
| 266 | |||
| 267 | 6 | $attributes = $data->attributes($metadata->xmlNamespace); |
|
| 268 | 6 | if (isset($attributes[$name])) { |
|
| 269 | 6 | $v = $this->navigator->accept($attributes[$name], $metadata->type, $context); |
|
| 270 | 6 | $this->accessor->setValue($this->currentObject, $v, $metadata); |
|
| 271 | } |
||
| 272 | |||
| 273 | 6 | return; |
|
| 274 | } |
||
| 275 | |||
| 276 | 29 | if ($metadata->xmlValue) { |
|
| 277 | 7 | $v = $this->navigator->accept($data, $metadata->type, $context); |
|
| 278 | 7 | $this->accessor->setValue($this->currentObject, $v, $metadata); |
|
| 279 | |||
| 280 | 7 | return; |
|
| 281 | } |
||
| 282 | |||
| 283 | 27 | if ($metadata->xmlCollection) { |
|
| 284 | 7 | $enclosingElem = $data; |
|
| 285 | 7 | if (!$metadata->xmlCollectionInline) { |
|
| 286 | 6 | $enclosingElem = $data->children($metadata->xmlNamespace)->$name; |
|
| 287 | } |
||
| 288 | |||
| 289 | 7 | $this->setCurrentMetadata($metadata); |
|
| 290 | 7 | $v = $this->navigator->accept($enclosingElem, $metadata->type, $context); |
|
| 291 | 7 | $this->revertCurrentMetadata(); |
|
| 292 | 7 | $this->accessor->setValue($this->currentObject, $v, $metadata); |
|
| 293 | |||
| 294 | 7 | return; |
|
| 295 | } |
||
| 296 | |||
| 297 | 26 | if ($metadata->xmlNamespace) { |
|
| 298 | 4 | $node = $data->children($metadata->xmlNamespace)->$name; |
|
| 299 | 4 | if (!$node->count()) { |
|
| 300 | 4 | return; |
|
| 301 | } |
||
| 302 | } else { |
||
| 303 | |||
| 304 | 23 | $namespaces = $data->getDocNamespaces(); |
|
| 305 | |||
| 306 | 23 | if (isset($namespaces[''])) { |
|
| 307 | 2 | $prefix = uniqid('ns-'); |
|
| 308 | 2 | $data->registerXPathNamespace($prefix, $namespaces['']); |
|
| 309 | 2 | $nodes = $data->xpath('./' . $prefix . ':' . $name); |
|
| 310 | } else { |
||
| 311 | 21 | $nodes = $data->xpath('./' . $name); |
|
| 312 | } |
||
| 313 | 23 | if (empty($nodes)) { |
|
| 314 | 2 | return; |
|
| 315 | } |
||
| 316 | 23 | $node = reset($nodes); |
|
| 317 | } |
||
| 318 | |||
| 319 | 25 | if ($metadata->xmlKeyValuePairs) { |
|
| 320 | 2 | $this->setCurrentMetadata($metadata); |
|
| 321 | } |
||
| 322 | |||
| 323 | 25 | $v = $this->navigator->accept($node, $metadata->type, $context); |
|
| 324 | |||
| 325 | 25 | $this->accessor->setValue($this->currentObject, $v, $metadata); |
|
| 326 | 25 | } |
|
| 327 | |||
| 328 | 33 | public function endVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
|
| 336 | |||
| 337 | 33 | public function setCurrentObject($object) |
|
| 342 | |||
| 343 | public function getCurrentObject() |
||
| 347 | |||
| 348 | 33 | public function revertCurrentObject() |
|
| 352 | |||
| 353 | 9 | public function setCurrentMetadata(PropertyMetadata $metadata) |
|
| 358 | |||
| 359 | public function getCurrentMetadata() |
||
| 363 | |||
| 364 | 9 | public function revertCurrentMetadata() |
|
| 368 | |||
| 369 | 61 | public function getResult() |
|
| 373 | |||
| 374 | /** |
||
| 375 | * @param array <string> $doctypeWhitelist |
||
| 376 | */ |
||
| 377 | 1 | public function setDoctypeWhitelist(array $doctypeWhitelist) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * @return array<string> |
||
| 384 | */ |
||
| 385 | public function getDoctypeWhitelist() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Retrieves internalSubset even in bugfixed php versions |
||
| 392 | * |
||
| 393 | * @param \DOMDocumentType $child |
||
|
|
|||
| 394 | * @param string $data |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | 3 | private function getDomDocumentTypeEntitySubset($data) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * @param mixed $value |
||
| 421 | * |
||
| 422 | * @return bool |
||
| 423 | */ |
||
| 424 | 63 | public function isNull($value) |
|
| 445 | } |
||
| 446 |
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.