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 |
||
| 30 | class XmlSerializationVisitor extends AbstractVisitor |
||
| 31 | { |
||
| 32 | public $document; |
||
| 33 | |||
| 34 | private $navigator; |
||
| 35 | private $defaultRootName = 'result'; |
||
| 36 | private $defaultRootNamespace; |
||
| 37 | private $defaultVersion = '1.0'; |
||
| 38 | private $defaultEncoding = 'UTF-8'; |
||
| 39 | private $stack; |
||
| 40 | private $metadataStack; |
||
| 41 | private $currentNode; |
||
| 42 | private $currentMetadata; |
||
| 43 | private $hasValue; |
||
| 44 | private $nullWasVisited; |
||
| 45 | private $objectMetadataStack; |
||
| 46 | |||
| 47 | /** @var boolean */ |
||
| 48 | private $formatOutput; |
||
| 49 | |||
| 50 | 258 | public function __construct($namingStrategy) |
|
| 51 | { |
||
| 52 | 258 | parent::__construct($namingStrategy); |
|
| 53 | 258 | $this->objectMetadataStack = new \SplStack; |
|
| 54 | 258 | $this->formatOutput = true; |
|
| 55 | 258 | } |
|
| 56 | |||
| 57 | public function setDefaultRootName($name, $namespace = null) |
||
| 58 | { |
||
| 59 | $this->defaultRootName = $name; |
||
| 60 | $this->defaultRootNamespace = $namespace; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return boolean |
||
| 65 | */ |
||
| 66 | public function hasDefaultRootName() |
||
| 70 | |||
| 71 | public function setDefaultVersion($version) |
||
| 75 | |||
| 76 | public function setDefaultEncoding($encoding) |
||
| 80 | |||
| 81 | 82 | public function setNavigator(GraphNavigator $navigator) |
|
| 88 | |||
| 89 | public function getNavigator() |
||
| 93 | |||
| 94 | 9 | public function visitNull($data, array $type, Context $context) |
|
| 113 | |||
| 114 | 51 | public function visitString($data, array $type, Context $context) |
|
| 132 | |||
| 133 | 1 | public function visitSimpleString($data, array $type, Context $context) |
|
| 144 | |||
| 145 | 5 | public function visitBoolean($data, array $type, Context $context) |
|
| 156 | |||
| 157 | 17 | public function visitInteger($data, array $type, Context $context) |
|
| 161 | |||
| 162 | 9 | public function visitDouble($data, array $type, Context $context) |
|
| 166 | |||
| 167 | 26 | public function visitArray($data, array $type, Context $context) |
|
| 168 | { |
||
| 200 | 52 | ||
| 201 | 50 | public function startVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
|
| 202 | 50 | { |
|
| 203 | 16 | $this->objectMetadataStack->push($metadata); |
|
| 204 | 16 | ||
| 205 | 16 | if (null === $this->document) { |
|
| 206 | 34 | $this->document = $this->createDocument(null, null, false); |
|
| 207 | 34 | if ($metadata->xmlRootName) { |
|
| 208 | $rootName = $metadata->xmlRootName; |
||
| 209 | $rootNamespace = $metadata->xmlRootNamespace?:$this->getClassDefaultNamespace($metadata); |
||
| 210 | 50 | } else { |
|
| 211 | 4 | $rootName = $this->defaultRootName; |
|
| 212 | 4 | $rootNamespace = $this->defaultRootNamespace; |
|
| 213 | 46 | } |
|
| 214 | |||
| 215 | if ($rootNamespace) { |
||
| 216 | 50 | $this->currentNode = $this->document->createElementNS($rootNamespace, $rootName); |
|
| 217 | 50 | } else { |
|
| 218 | $this->currentNode = $this->document->createElement($rootName); |
||
| 219 | 52 | } |
|
| 220 | |||
| 221 | 52 | $this->document->appendChild($this->currentNode); |
|
| 222 | 52 | } |
|
| 223 | |||
| 224 | 52 | $this->addNamespaceAttributes($metadata, $this->currentNode); |
|
| 225 | |||
| 226 | 52 | $this->hasValue = false; |
|
| 227 | } |
||
| 228 | 52 | ||
| 229 | 2 | public function visitProperty(PropertyMetadata $metadata, $object, Context $context) |
|
| 230 | { |
||
| 231 | $v = $metadata->getValue($object); |
||
| 232 | 52 | ||
| 233 | 11 | if (null === $v && $context->shouldSerializeNull() !== true) { |
|
| 234 | 11 | return; |
|
| 235 | 11 | } |
|
| 236 | |||
| 237 | 11 | if ($metadata->xmlAttribute) { |
|
| 238 | $this->setCurrentMetadata($metadata); |
||
| 239 | $node = $this->navigator->accept($v, $metadata->type, $context); |
||
| 240 | 11 | $this->revertCurrentMetadata(); |
|
| 241 | 11 | ||
| 242 | if ( ! $node instanceof \DOMCharacterData) { |
||
| 243 | 11 | throw new RuntimeException(sprintf('Unsupported value for XML attribute for %s. Expected character data, but got %s.', $metadata->name, json_encode($v))); |
|
| 244 | } |
||
| 245 | $attributeName = $this->namingStrategy->translateName($metadata); |
||
| 246 | 51 | $this->setAttributeOnNode($this->currentNode, $attributeName, $node->nodeValue, $metadata->xmlNamespace); |
|
| 247 | 51 | ||
| 248 | 1 | return; |
|
| 249 | } |
||
| 250 | |||
| 251 | 51 | if (($metadata->xmlValue && $this->currentNode->childNodes->length > 0) |
|
| 252 | 8 | || ( ! $metadata->xmlValue && $this->hasValue)) { |
|
| 253 | 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)); |
||
| 254 | 8 | } |
|
| 255 | 8 | ||
| 256 | 8 | if ($metadata->xmlValue) { |
|
| 257 | $this->hasValue = true; |
||
| 258 | 8 | ||
| 259 | $this->setCurrentMetadata($metadata); |
||
| 260 | $node = $this->navigator->accept($v, $metadata->type, $context); |
||
| 261 | $this->revertCurrentMetadata(); |
||
| 262 | 8 | ||
| 263 | if ( ! $node instanceof \DOMCharacterData) { |
||
| 264 | 8 | 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))); |
|
| 265 | } |
||
| 266 | |||
| 267 | 47 | $this->currentNode->appendChild($node); |
|
| 268 | 2 | ||
| 269 | 1 | return; |
|
| 270 | } |
||
| 271 | |||
| 272 | 1 | if ($metadata->xmlAttributeMap) { |
|
| 273 | 1 | if ( ! is_array($v)) { |
|
| 274 | 1 | throw new RuntimeException(sprintf('Unsupported value type for XML attribute map. Expected array but got %s.', gettype($v))); |
|
| 275 | 1 | } |
|
| 276 | |||
| 277 | 1 | foreach ($v as $key => $value) { |
|
| 278 | $this->setCurrentMetadata($metadata); |
||
| 279 | $node = $this->navigator->accept($value, null, $context); |
||
| 280 | $this->revertCurrentMetadata(); |
||
| 281 | 1 | ||
| 282 | 1 | if ( ! $node instanceof \DOMCharacterData) { |
|
| 283 | throw new RuntimeException(sprintf('Unsupported value for a XML attribute map value. Expected character data, but got %s.', json_encode($v))); |
||
| 284 | 1 | } |
|
| 285 | |||
| 286 | $this->setAttributeOnNode($this->currentNode, $key, $node->nodeValue, $metadata->xmlNamespace); |
||
| 287 | 45 | } |
|
| 288 | 44 | ||
| 289 | return; |
||
| 290 | 44 | } |
|
| 291 | 5 | ||
| 292 | 5 | if ($addEnclosingElement = ( ! $metadata->xmlCollection || ! $metadata->xmlCollectionInline) && ! $metadata->inline) { |
|
| 293 | 41 | $elementName = $this->namingStrategy->translateName($metadata); |
|
| 294 | 41 | ||
| 295 | if (null !== $metadata->xmlNamespace) { |
||
| 296 | 44 | $element = $this->createElement($elementName, $metadata->xmlNamespace); |
|
| 297 | 44 | } else { |
|
| 298 | $defaultNamespace = $this->getClassDefaultNamespace($this->objectMetadataStack->top()); |
||
| 299 | 45 | $element = $this->createElement($elementName, $defaultNamespace); |
|
| 300 | } |
||
| 301 | 45 | $this->setCurrentNode($element); |
|
| 302 | 35 | } |
|
| 303 | 35 | ||
| 304 | $this->setCurrentMetadata($metadata); |
||
| 305 | 45 | ||
| 306 | if (null !== $node = $this->navigator->accept($v, $metadata->type, $context)) { |
||
| 307 | 45 | $this->currentNode->appendChild($node); |
|
| 308 | 44 | } |
|
| 309 | |||
| 310 | 44 | $this->revertCurrentMetadata(); |
|
| 311 | 44 | ||
| 312 | 44 | if ($addEnclosingElement) { |
|
| 313 | 44 | $this->revertCurrentNode(); |
|
| 314 | |||
| 315 | 45 | if ($this->nodeNotEmpty($element) || ((!$metadata->xmlCollection || !$metadata->xmlCollectionSkipWhenEmpty) && $node === null && $v !== null && !$context->isVisiting($v))) { |
|
| 316 | 45 | $this->currentNode->appendChild($element); |
|
| 317 | } |
||
| 318 | 44 | } |
|
| 319 | |||
| 320 | 44 | $this->hasValue = false; |
|
| 321 | } |
||
| 322 | |||
| 323 | 50 | private function nodeNotEmpty(\DOMElement $element) |
|
| 324 | { |
||
| 325 | 50 | return $element->hasChildNodes() || $element->hasAttributes(); |
|
| 326 | 50 | } |
|
| 327 | |||
| 328 | 79 | public function endVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
|
| 329 | { |
||
| 330 | 79 | $this->objectMetadataStack->pop(); |
|
| 331 | } |
||
| 332 | |||
| 333 | 2 | public function getResult() |
|
| 337 | |||
| 338 | public function getCurrentNode() |
||
| 339 | { |
||
| 340 | return $this->currentNode; |
||
| 341 | } |
||
| 342 | |||
| 343 | public function getCurrentMetadata() |
||
| 347 | |||
| 348 | 51 | public function getDocument() |
|
| 349 | { |
||
| 350 | 51 | return $this->document; |
|
| 351 | 51 | } |
|
| 352 | 51 | ||
| 353 | public function setCurrentMetadata(PropertyMetadata $metadata) |
||
| 354 | 73 | { |
|
| 355 | $this->metadataStack->push($this->currentMetadata); |
||
| 356 | 73 | $this->currentMetadata = $metadata; |
|
| 357 | 73 | } |
|
| 358 | 73 | ||
| 359 | public function setCurrentNode(\DOMNode $node) |
||
| 360 | 51 | { |
|
| 361 | $this->stack->push($this->currentNode); |
||
| 362 | 51 | $this->currentNode = $node; |
|
| 363 | } |
||
| 364 | |||
| 365 | 51 | public function revertCurrentNode() |
|
| 369 | |||
| 370 | 81 | public function revertCurrentMetadata() |
|
| 371 | { |
||
| 372 | 81 | return $this->currentMetadata = $this->metadataStack->pop(); |
|
| 373 | 81 | } |
|
| 374 | |||
| 375 | 81 | public function createDocument($version = null, $encoding = null, $addRoot = true) |
|
| 376 | 27 | { |
|
| 377 | $doc = new \DOMDocument($version ?: $this->defaultVersion, $encoding ?: $this->defaultEncoding); |
||
| 378 | $doc->formatOutput = $this->isFormatOutput(); |
||
| 379 | 27 | ||
| 380 | if ($addRoot) { |
||
| 381 | 27 | if ($this->defaultRootNamespace) { |
|
| 382 | 27 | $rootNode = $doc->createElementNS($this->defaultRootNamespace, $this->defaultRootName); |
|
| 383 | 27 | } else { |
|
| 384 | $rootNode = $doc->createElement($this->defaultRootName); |
||
| 385 | 81 | } |
|
| 386 | $this->setCurrentNode($rootNode); |
||
| 387 | $doc->appendChild($rootNode); |
||
| 388 | 82 | } |
|
| 389 | |||
| 390 | 82 | return $doc; |
|
| 391 | } |
||
| 392 | 82 | ||
| 393 | public function prepare($data) |
||
| 399 | 5 | ||
| 400 | private function visitNumeric($data, array $type) |
||
| 401 | 5 | { |
|
| 402 | if (null === $this->document) { |
||
| 403 | $this->document = $this->createDocument(null, null, true); |
||
| 404 | 21 | $this->currentNode->appendChild($textNode = $this->document->createTextNode((string) $data)); |
|
| 405 | |||
| 406 | return $textNode; |
||
| 407 | } |
||
| 408 | |||
| 409 | return $this->document->createTextNode((string) $data); |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Checks that the name is a valid XML element name. |
||
| 414 | 2 | * |
|
| 415 | * @param string $name |
||
| 416 | 2 | * |
|
| 417 | * @return boolean |
||
| 418 | */ |
||
| 419 | 9 | private function isElementNameValid($name) |
|
| 420 | { |
||
| 421 | 9 | return $name && false === strpos($name, ' ') && preg_match('#^[\pL_][\pL0-9._-]*$#ui', $name); |
|
| 422 | 9 | } |
|
| 423 | 9 | ||
| 424 | 9 | private function attachNullNamespace() |
|
| 425 | { |
||
| 426 | 9 | if ( ! $this->nullWasVisited) { |
|
| 427 | 9 | $this->document->documentElement->setAttributeNS( |
|
| 428 | 9 | 'http://www.w3.org/2000/xmlns/', |
|
| 429 | 9 | 'xmlns:xsi', |
|
| 430 | 'http://www.w3.org/2001/XMLSchema-instance' |
||
| 431 | ); |
||
| 432 | $this->nullWasVisited = true; |
||
| 433 | } |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | 52 | * Adds namespace attributes to the XML root element |
|
| 438 | * |
||
| 439 | 52 | * @param \JMS\Serializer\Metadata\ClassMetadata $metadata |
|
| 440 | 5 | * @param \DOMElement $element |
|
| 441 | 5 | */ |
|
| 442 | 5 | private function addNamespaceAttributes(ClassMetadata $metadata, \DOMElement $element) |
|
| 443 | 5 | { |
|
| 444 | 3 | foreach ($metadata->xmlNamespaces as $prefix => $uri) { |
|
| 445 | $attribute = 'xmlns'; |
||
| 446 | 5 | if ($prefix !== '') { |
|
| 447 | 52 | $attribute .= ':'.$prefix; |
|
| 448 | 52 | } elseif ($element->namespaceURI === $uri) { |
|
| 449 | continue; |
||
| 450 | 51 | } |
|
| 451 | $element->setAttributeNS('http://www.w3.org/2000/xmlns/', $attribute, $uri); |
||
| 452 | 51 | } |
|
| 453 | 48 | } |
|
| 454 | |||
| 455 | 5 | private function createElement($tagName, $namespace = null) |
|
| 469 | 2 | ||
| 470 | 2 | private function setAttributeOnNode(\DOMElement $node, $name, $value, $namespace = null) |
|
| 471 | 4 | { |
|
| 472 | 4 | if (null !== $namespace) { |
|
| 473 | 10 | if (!$prefix = $node->lookupPrefix($namespace)) { |
|
| 474 | $prefix = 'ns-'. substr(sha1($namespace), 0, 8); |
||
| 475 | 12 | } |
|
| 476 | $node->setAttributeNS($namespace, $prefix.':'.$name, $value); |
||
| 477 | 45 | } else { |
|
| 478 | $node->setAttribute($name, $value); |
||
| 479 | 45 | } |
|
| 480 | } |
||
| 481 | |||
| 482 | private function getClassDefaultNamespace(ClassMetadata $metadata) |
||
| 483 | { |
||
| 484 | return (isset($metadata->xmlNamespaces[''])?$metadata->xmlNamespaces['']:null); |
||
| 486 | |||
| 487 | 81 | /** |
|
| 488 | * @return bool |
||
| 489 | */ |
||
| 490 | public function isFormatOutput() |
||
| 494 | |||
| 495 | 1 | /** |
|
| 496 | 1 | * @param bool $formatOutput |
|
| 497 | */ |
||
| 498 | public function setFormatOutput($formatOutput) |
||
| 502 | } |
||
| 503 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.