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 | 59 | public function setNavigator(GraphNavigator $navigator) |
|
| 46 | { |
||
| 47 | 59 | $this->navigator = $navigator; |
|
| 48 | 59 | $this->objectStack = new \SplStack; |
|
| 49 | 59 | $this->metadataStack = new \SplStack; |
|
| 50 | 59 | $this->objectMetadataStack = new \SplStack; |
|
| 51 | 59 | $this->result = null; |
|
| 52 | 59 | } |
|
| 53 | |||
| 54 | 1 | public function getNavigator() |
|
| 58 | |||
| 59 | 62 | public function prepare($data) |
|
| 89 | |||
| 90 | 62 | private function emptyStringToSpaceCharacter($data) |
|
| 94 | |||
| 95 | 7 | public function visitNull($data, array $type, Context $context) |
|
| 99 | |||
| 100 | 21 | public function visitString($data, array $type, Context $context) |
|
| 110 | |||
| 111 | 8 | public function visitBoolean($data, array $type, Context $context) |
|
| 129 | |||
| 130 | 7 | public function visitInteger($data, array $type, Context $context) |
|
| 140 | |||
| 141 | 10 | public function visitDouble($data, array $type, Context $context) |
|
| 151 | |||
| 152 | 15 | public function visitArray($data, array $type, Context $context) |
|
| 244 | 5 | ||
| 245 | 5 | public function startVisitingObject(ClassMetadata $metadata, $object, array $type, Context $context) |
|
| 246 | 5 | { |
|
| 247 | 5 | $this->setCurrentObject($object); |
|
| 248 | $this->objectMetadataStack->push($metadata); |
||
| 249 | 5 | if (null === $this->result) { |
|
| 250 | $this->result = $this->currentObject; |
||
| 251 | } |
||
| 252 | 26 | } |
|
| 253 | 6 | ||
| 254 | 6 | public function visitProperty(PropertyMetadata $metadata, $data, Context $context) |
|
| 255 | { |
||
| 256 | 6 | $name = $this->namingStrategy->translateName($metadata); |
|
| 257 | |||
| 258 | if (!$metadata->type) { |
||
| 259 | 24 | throw new RuntimeException(sprintf('You must define a type for %s::$%s.', $metadata->reflection->class, $metadata->name)); |
|
| 260 | 6 | } |
|
| 261 | 6 | ||
| 262 | 6 | if ($metadata->xmlAttribute) { |
|
| 263 | 6 | ||
| 264 | $attributes = $data->attributes($metadata->xmlNamespace); |
||
| 265 | 6 | if (isset($attributes[$name])) { |
|
| 266 | 6 | $v = $this->navigator->accept($attributes[$name], $metadata->type, $context); |
|
| 267 | 6 | $this->accessor->setValue($this->currentObject, $v, $metadata); |
|
| 268 | 6 | } |
|
| 269 | |||
| 270 | 6 | return; |
|
| 271 | } |
||
| 272 | |||
| 273 | 23 | if ($metadata->xmlValue) { |
|
| 274 | 4 | $v = $this->navigator->accept($data, $metadata->type, $context); |
|
| 275 | 4 | $this->accessor->setValue($this->currentObject, $v, $metadata); |
|
| 276 | 1 | ||
| 277 | return; |
||
| 278 | 3 | } |
|
| 279 | |||
| 280 | 20 | if ($metadata->xmlCollection) { |
|
| 281 | $enclosingElem = $data; |
||
| 282 | 20 | if (!$metadata->xmlCollectionInline) { |
|
| 283 | 1 | $enclosingElem = $data->children($metadata->xmlNamespace)->$name; |
|
| 284 | 1 | } |
|
| 285 | 1 | ||
| 286 | 1 | $this->setCurrentMetadata($metadata); |
|
| 287 | 19 | $v = $this->navigator->accept($enclosingElem, $metadata->type, $context); |
|
| 288 | $this->revertCurrentMetadata(); |
||
| 289 | 20 | $this->accessor->setValue($this->currentObject, $v, $metadata); |
|
| 290 | 1 | ||
| 291 | return; |
||
| 292 | 20 | } |
|
| 293 | |||
| 294 | if ($metadata->xmlNamespace) { |
||
| 295 | 22 | $node = $data->children($metadata->xmlNamespace)->$name; |
|
| 296 | if (!$node->count()) { |
||
| 297 | 22 | return; |
|
| 298 | 22 | } |
|
| 299 | } else { |
||
| 300 | 30 | ||
| 301 | $namespaces = $data->getDocNamespaces(); |
||
| 302 | 30 | ||
| 303 | 30 | if (isset($namespaces[''])) { |
|
| 304 | 30 | $prefix = uniqid('ns-'); |
|
| 305 | $data->registerXPathNamespace($prefix, $namespaces['']); |
||
| 306 | 30 | $nodes = $data->xpath('./' . $prefix . ':' . $name); |
|
| 307 | } else { |
||
| 308 | $nodes = $data->xpath('./' . $name); |
||
| 309 | 30 | } |
|
| 310 | if (empty($nodes)) { |
||
| 311 | 30 | return; |
|
| 312 | 30 | } |
|
| 313 | 30 | $node = reset($nodes); |
|
| 314 | } |
||
| 315 | |||
| 316 | $v = $this->navigator->accept($node, $metadata->type, $context); |
||
| 317 | |||
| 318 | $this->accessor->setValue($this->currentObject, $v, $metadata); |
||
| 319 | } |
||
| 320 | 30 | ||
| 321 | public function endVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
||
| 322 | 30 | { |
|
| 323 | $rs = $this->currentObject; |
||
| 324 | $this->objectMetadataStack->pop(); |
||
| 325 | 6 | $this->revertCurrentObject(); |
|
| 326 | |||
| 327 | 6 | return $rs; |
|
| 328 | 6 | } |
|
| 329 | 6 | ||
| 330 | public function setCurrentObject($object) |
||
| 331 | { |
||
| 332 | $this->objectStack->push($this->currentObject); |
||
| 333 | $this->currentObject = $object; |
||
| 334 | } |
||
| 335 | |||
| 336 | 6 | public function getCurrentObject() |
|
| 337 | { |
||
| 338 | 6 | return $this->currentObject; |
|
| 339 | } |
||
| 340 | |||
| 341 | 58 | public function revertCurrentObject() |
|
| 345 | |||
| 346 | public function setCurrentMetadata(PropertyMetadata $metadata) |
||
| 347 | { |
||
| 348 | $this->metadataStack->push($this->currentMetadata); |
||
| 349 | 1 | $this->currentMetadata = $metadata; |
|
| 350 | } |
||
| 351 | 1 | ||
| 352 | 1 | public function getCurrentMetadata() |
|
| 356 | |||
| 357 | public function revertCurrentMetadata() |
||
| 361 | |||
| 362 | public function getResult() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param array <string> $doctypeWhitelist |
||
| 369 | 3 | */ |
|
| 370 | public function setDoctypeWhitelist(array $doctypeWhitelist) |
||
| 374 | 3 | ||
| 375 | 3 | /** |
|
| 376 | 3 | * @return array<string> |
|
| 377 | 3 | */ |
|
| 378 | 3 | public function getDoctypeWhitelist() |
|
| 382 | |||
| 383 | 3 | /** |
|
| 384 | 3 | * Retrieves internalSubset even in bugfixed php versions |
|
| 385 | 3 | * |
|
| 386 | 3 | * @param \DOMDocumentType $child |
|
|
|
|||
| 387 | * @param string $data |
||
| 388 | 3 | * @return string |
|
| 389 | */ |
||
| 390 | private function getDomDocumentTypeEntitySubset($data) |
||
| 411 | 8 | ||
| 412 | /** |
||
| 413 | 53 | * @param mixed $value |
|
| 414 | * |
||
| 415 | 54 | * @return bool |
|
| 416 | */ |
||
| 417 | public function isNull($value) |
||
| 438 | } |
||
| 439 |
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.