Complex classes like XmlSerializationVisitor 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 XmlSerializationVisitor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class XmlSerializationVisitor extends AbstractVisitor |
||
| 33 | { |
||
| 34 | public $document; |
||
| 35 | |||
| 36 | private $navigator; |
||
| 37 | private $defaultRootName = 'result'; |
||
| 38 | private $defaultRootNamespace; |
||
| 39 | private $defaultVersion = '1.0'; |
||
| 40 | private $defaultEncoding = 'UTF-8'; |
||
| 41 | private $stack; |
||
| 42 | private $metadataStack; |
||
| 43 | private $currentNode; |
||
| 44 | private $currentMetadata; |
||
| 45 | private $hasValue; |
||
| 46 | private $nullWasVisited; |
||
| 47 | private $objectMetadataStack; |
||
| 48 | |||
| 49 | /** @var boolean */ |
||
| 50 | private $formatOutput; |
||
| 51 | |||
| 52 | 379 | public function __construct(PropertyNamingStrategyInterface $namingStrategy, AccessorStrategyInterface $accessorStrategy = null) |
|
| 58 | |||
| 59 | public function setDefaultRootName($name, $namespace = null) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return boolean |
||
| 67 | */ |
||
| 68 | public function hasDefaultRootName() |
||
| 72 | |||
| 73 | public function setDefaultVersion($version) |
||
| 77 | |||
| 78 | public function setDefaultEncoding($encoding) |
||
| 82 | |||
| 83 | 107 | public function setNavigator(GraphNavigator $navigator) |
|
| 90 | |||
| 91 | public function getNavigator() |
||
| 95 | |||
| 96 | 9 | public function visitNull($data, array $type, Context $context) |
|
| 115 | |||
| 116 | 73 | public function visitString($data, array $type, Context $context) |
|
| 134 | |||
| 135 | 2 | public function visitSimpleString($data, array $type, Context $context) |
|
| 146 | |||
| 147 | 5 | public function visitBoolean($data, array $type, Context $context) |
|
| 158 | |||
| 159 | 19 | public function visitInteger($data, array $type, Context $context) |
|
| 163 | |||
| 164 | 9 | public function visitDouble($data, array $type, Context $context) |
|
| 168 | |||
| 169 | 28 | public function visitArray($data, array $type, Context $context) |
|
| 202 | |||
| 203 | 73 | public function startVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
|
| 230 | |||
| 231 | 73 | public function visitProperty(PropertyMetadata $metadata, $object, Context $context) |
|
| 232 | { |
||
| 233 | 73 | $v = $this->accessor->getValue($object, $metadata); |
|
| 234 | |||
| 235 | 72 | if (null === $v && $context->shouldSerializeNull() !== true) { |
|
| 236 | 8 | return; |
|
| 237 | } |
||
| 238 | |||
| 239 | 72 | if ($metadata->xmlAttribute) { |
|
| 240 | 12 | $this->setCurrentMetadata($metadata); |
|
| 241 | 12 | $node = $this->navigator->accept($v, $metadata->type, $context); |
|
| 242 | 12 | $this->revertCurrentMetadata(); |
|
| 243 | |||
| 244 | 12 | if ( ! $node instanceof \DOMCharacterData) { |
|
| 245 | throw new RuntimeException(sprintf('Unsupported value for XML attribute for %s. Expected character data, but got %s.', $metadata->name, json_encode($v))); |
||
| 246 | } |
||
| 247 | 12 | $attributeName = $this->namingStrategy->translateName($metadata); |
|
| 248 | 12 | $this->setAttributeOnNode($this->currentNode, $attributeName, $node->nodeValue, $metadata->xmlNamespace); |
|
| 249 | |||
| 250 | 12 | return; |
|
| 251 | } |
||
| 252 | |||
| 253 | 70 | if (($metadata->xmlValue && $this->currentNode->childNodes->length > 0) |
|
| 254 | 70 | || ( ! $metadata->xmlValue && $this->hasValue)) { |
|
| 255 | 1 | throw new RuntimeException(sprintf('If you make use of @XmlValue, all other properties in the class must have the @XmlAttribute annotation. Invalid usage detected in class %s.', $metadata->class)); |
|
| 256 | } |
||
| 257 | |||
| 258 | 70 | if ($metadata->xmlValue) { |
|
| 259 | 8 | $this->hasValue = true; |
|
| 260 | |||
| 261 | 8 | $this->setCurrentMetadata($metadata); |
|
| 262 | 8 | $node = $this->navigator->accept($v, $metadata->type, $context); |
|
| 263 | 8 | $this->revertCurrentMetadata(); |
|
| 264 | |||
| 265 | 8 | if ( ! $node instanceof \DOMCharacterData) { |
|
| 266 | throw new RuntimeException(sprintf('Unsupported value for property %s::$%s. Expected character data, but got %s.', $metadata->reflection->class, $metadata->reflection->name, is_object($node) ? get_class($node) : gettype($node))); |
||
| 267 | } |
||
| 268 | |||
| 269 | 8 | $this->currentNode->appendChild($node); |
|
| 270 | |||
| 271 | 8 | return; |
|
| 272 | } |
||
| 273 | |||
| 274 | 66 | if ($metadata->xmlAttributeMap) { |
|
| 275 | 2 | if ( ! is_array($v)) { |
|
| 276 | 1 | throw new RuntimeException(sprintf('Unsupported value type for XML attribute map. Expected array but got %s.', gettype($v))); |
|
| 277 | } |
||
| 278 | |||
| 279 | 1 | foreach ($v as $key => $value) { |
|
| 280 | 1 | $this->setCurrentMetadata($metadata); |
|
| 281 | 1 | $node = $this->navigator->accept($value, null, $context); |
|
| 282 | 1 | $this->revertCurrentMetadata(); |
|
| 283 | |||
| 284 | 1 | if ( ! $node instanceof \DOMCharacterData) { |
|
| 285 | throw new RuntimeException(sprintf('Unsupported value for a XML attribute map value. Expected character data, but got %s.', json_encode($v))); |
||
| 286 | } |
||
| 287 | |||
| 288 | 1 | $this->setAttributeOnNode($this->currentNode, $key, $node->nodeValue, $metadata->xmlNamespace); |
|
| 289 | 1 | } |
|
| 290 | |||
| 291 | 1 | return; |
|
| 292 | } |
||
| 293 | |||
| 294 | 64 | if ($addEnclosingElement = !$this->isInLineCollection($metadata) && ! $metadata->inline) { |
|
| 295 | 63 | $elementName = $this->namingStrategy->translateName($metadata); |
|
| 296 | |||
| 297 | 63 | $namespace = null !== $metadata->xmlNamespace |
|
| 298 | 63 | ? $metadata->xmlNamespace |
|
| 299 | 63 | : $this->getClassDefaultNamespace($this->objectMetadataStack->top()); |
|
| 300 | |||
| 301 | 63 | $element = $this->createElement($elementName, $namespace); |
|
| 302 | 63 | $this->currentNode->appendChild($element); |
|
| 303 | 63 | $this->setCurrentNode($element); |
|
| 304 | 63 | } |
|
| 305 | |||
| 306 | 64 | $this->setCurrentMetadata($metadata); |
|
| 307 | |||
| 308 | 64 | if (null !== $node = $this->navigator->accept($v, $metadata->type, $context)) { |
|
| 309 | 53 | $this->currentNode->appendChild($node); |
|
| 310 | 53 | } |
|
| 311 | |||
| 312 | 64 | $this->revertCurrentMetadata(); |
|
| 313 | |||
| 314 | 64 | if ($addEnclosingElement) { |
|
| 315 | 63 | $this->revertCurrentNode(); |
|
| 316 | |||
| 317 | 63 | if ($this->isElementEmpty($element) && ($v === null || $this->isSkippableCollection($metadata) || $this->isSkippableEmptyObject($node, $metadata) || $this->isCircularRef($context, $v))) { |
|
| 318 | 5 | $this->currentNode->removeChild($element); |
|
| 319 | 5 | } |
|
| 320 | 63 | } |
|
| 321 | |||
| 322 | 64 | $this->hasValue = false; |
|
| 323 | 64 | } |
|
| 324 | |||
| 325 | 64 | private function isInLineCollection(PropertyMetadata $metadata) |
|
| 329 | |||
| 330 | 4 | private function isCircularRef(SerializationContext $context, $v) |
|
| 334 | |||
| 335 | 6 | private function isSkippableEmptyObject($node, PropertyMetadata $metadata) |
|
| 336 | { |
||
| 337 | 6 | return $node === null && !$metadata->xmlCollection && $metadata->skipWhenEmpty; |
|
| 338 | } |
||
| 339 | |||
| 340 | 7 | private function isSkippableCollection(PropertyMetadata $metadata) |
|
| 344 | |||
| 345 | 63 | private function isElementEmpty(\DOMElement $element) |
|
| 349 | |||
| 350 | 70 | public function endVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
|
| 354 | |||
| 355 | 102 | public function getResult() |
|
| 359 | |||
| 360 | 2 | public function getCurrentNode() |
|
| 364 | |||
| 365 | public function getCurrentMetadata() |
||
| 369 | |||
| 370 | public function getDocument() |
||
| 374 | |||
| 375 | 71 | public function setCurrentMetadata(PropertyMetadata $metadata) |
|
| 380 | |||
| 381 | 95 | public function setCurrentNode(\DOMNode $node) |
|
| 386 | |||
| 387 | 71 | public function revertCurrentNode() |
|
| 391 | |||
| 392 | 71 | public function revertCurrentMetadata() |
|
| 396 | |||
| 397 | 105 | public function createDocument($version = null, $encoding = null, $addRoot = true) |
|
| 414 | |||
| 415 | 107 | public function prepare($data) |
|
| 421 | |||
| 422 | 28 | private function visitNumeric($data, array $type) |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Checks that the name is a valid XML element name. |
||
| 436 | * |
||
| 437 | * @param string $name |
||
| 438 | * |
||
| 439 | * @return boolean |
||
| 440 | */ |
||
| 441 | 2 | private function isElementNameValid($name) |
|
| 445 | |||
| 446 | 9 | private function attachNullNamespace() |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Adds namespace attributes to the XML root element |
||
| 460 | * |
||
| 461 | * @param \JMS\Serializer\Metadata\ClassMetadata $metadata |
||
| 462 | * @param \DOMElement $element |
||
| 463 | */ |
||
| 464 | 73 | private function addNamespaceAttributes(ClassMetadata $metadata, \DOMElement $element) |
|
| 476 | |||
| 477 | 71 | private function createElement($tagName, $namespace = null) |
|
| 490 | |||
| 491 | 13 | private function setAttributeOnNode(\DOMElement $node, $name, $value, $namespace = null) |
|
| 502 | |||
| 503 | 62 | private function getClassDefaultNamespace(ClassMetadata $metadata) |
|
| 507 | |||
| 508 | /** |
||
| 509 | * @return bool |
||
| 510 | */ |
||
| 511 | 105 | public function isFormatOutput() |
|
| 515 | |||
| 516 | /** |
||
| 517 | * @param bool $formatOutput |
||
| 518 | */ |
||
| 519 | 1 | public function setFormatOutput($formatOutput) |
|
| 523 | } |
||
| 524 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.