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 | 253 | public function __construct($namingStrategy) |
|
| 51 | { |
||
| 52 | 253 | parent::__construct($namingStrategy); |
|
| 53 | 253 | $this->objectMetadataStack = new \SplStack; |
|
| 54 | 253 | $this->formatOutput = true; |
|
| 55 | 253 | } |
|
| 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 | { |
||
| 169 | 26 | if (null === $this->document) { |
|
| 170 | 8 | $this->document = $this->createDocument(null, null, true); |
|
| 171 | 8 | } |
|
| 172 | |||
| 173 | 26 | $entryName = (null !== $this->currentMetadata && null !== $this->currentMetadata->xmlEntryName) ? $this->currentMetadata->xmlEntryName : 'entry'; |
|
| 174 | 26 | $keyAttributeName = (null !== $this->currentMetadata && null !== $this->currentMetadata->xmlKeyAttribute) ? $this->currentMetadata->xmlKeyAttribute : null; |
|
| 175 | 26 | $namespace = (null !== $this->currentMetadata && null !== $this->currentMetadata->xmlEntryNamespace) ? $this->currentMetadata->xmlEntryNamespace : null; |
|
| 176 | |||
| 177 | 26 | foreach ($data as $k => $v) { |
|
| 178 | 25 | $tagName = (null !== $this->currentMetadata && $this->currentMetadata->xmlKeyValuePairs && $this->isElementNameValid($k)) ? $k : $entryName; |
|
| 179 | |||
| 180 | 25 | $entryNode = $this->createElement($tagName, $namespace); |
|
| 181 | 25 | $this->currentNode->appendChild($entryNode); |
|
| 182 | 25 | $this->setCurrentNode($entryNode); |
|
| 183 | |||
| 184 | 25 | if (null !== $keyAttributeName) { |
|
| 185 | 6 | $entryNode->setAttribute($keyAttributeName, (string) $k); |
|
| 186 | 6 | } |
|
| 187 | |||
| 188 | 25 | if (null !== $node = $this->navigator->accept($v, $this->getElementType($type), $context)) { |
|
| 189 | 16 | $this->currentNode->appendChild($node); |
|
| 190 | 16 | } |
|
| 191 | |||
| 192 | 25 | $this->revertCurrentNode(); |
|
| 193 | 26 | } |
|
| 194 | 26 | } |
|
| 195 | |||
| 196 | 52 | public function startVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
|
| 197 | { |
||
| 198 | 52 | $this->objectMetadataStack->push($metadata); |
|
| 199 | |||
| 200 | 52 | if (null === $this->document) { |
|
| 201 | 50 | $this->document = $this->createDocument(null, null, false); |
|
| 202 | 50 | if ($metadata->xmlRootName) { |
|
| 203 | 16 | $rootName = $metadata->xmlRootName; |
|
| 204 | 16 | $rootNamespace = $metadata->xmlRootNamespace?:$this->getClassDefaultNamespace($metadata); |
|
| 205 | 16 | } else { |
|
| 206 | 34 | $rootName = $this->defaultRootName; |
|
| 207 | 34 | $rootNamespace = $this->defaultRootNamespace; |
|
| 208 | } |
||
| 209 | |||
| 210 | 50 | if ($rootNamespace) { |
|
| 211 | 4 | $this->currentNode = $this->document->createElementNS($rootNamespace, $rootName); |
|
| 212 | 4 | } else { |
|
| 213 | 46 | $this->currentNode = $this->document->createElement($rootName); |
|
| 214 | } |
||
| 215 | |||
| 216 | 50 | $this->document->appendChild($this->currentNode); |
|
| 217 | 50 | } |
|
| 218 | |||
| 219 | 52 | $this->addNamespaceAttributes($metadata, $this->currentNode); |
|
| 220 | |||
| 221 | 52 | $this->hasValue = false; |
|
| 222 | 52 | } |
|
| 223 | |||
| 224 | 52 | public function visitProperty(PropertyMetadata $metadata, $object, Context $context) |
|
| 225 | { |
||
| 226 | 52 | $v = $metadata->getValue($object); |
|
| 227 | |||
| 228 | 52 | if (null === $v && $context->shouldSerializeNull() !== true) { |
|
| 229 | 2 | return; |
|
| 230 | } |
||
| 231 | |||
| 232 | 52 | if ($metadata->xmlAttribute) { |
|
| 233 | 11 | $this->setCurrentMetadata($metadata); |
|
| 234 | 11 | $node = $this->navigator->accept($v, $metadata->type, $context); |
|
| 235 | 11 | $this->revertCurrentMetadata(); |
|
| 236 | |||
| 237 | 11 | if ( ! $node instanceof \DOMCharacterData) { |
|
| 238 | throw new RuntimeException(sprintf('Unsupported value for XML attribute for %s. Expected character data, but got %s.', $metadata->name, json_encode($v))); |
||
| 239 | } |
||
| 240 | 11 | $attributeName = $this->namingStrategy->translateName($metadata); |
|
| 241 | 11 | $this->setAttributeOnNode($this->currentNode, $attributeName, $node->nodeValue, $metadata->xmlNamespace); |
|
| 242 | |||
| 243 | 11 | return; |
|
| 244 | } |
||
| 245 | |||
| 246 | 51 | if (($metadata->xmlValue && $this->currentNode->childNodes->length > 0) |
|
| 247 | 51 | || ( ! $metadata->xmlValue && $this->hasValue)) { |
|
| 248 | 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)); |
|
| 249 | } |
||
| 250 | |||
| 251 | 51 | if ($metadata->xmlValue) { |
|
| 252 | 8 | $this->hasValue = true; |
|
| 253 | |||
| 254 | 8 | $this->setCurrentMetadata($metadata); |
|
| 255 | 8 | $node = $this->navigator->accept($v, $metadata->type, $context); |
|
| 256 | 8 | $this->revertCurrentMetadata(); |
|
| 257 | |||
| 258 | 8 | if ( ! $node instanceof \DOMCharacterData) { |
|
| 259 | 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))); |
||
| 260 | } |
||
| 261 | |||
| 262 | 8 | $this->currentNode->appendChild($node); |
|
| 263 | |||
| 264 | 8 | return; |
|
| 265 | } |
||
| 266 | |||
| 267 | 47 | if ($metadata->xmlAttributeMap) { |
|
| 268 | 2 | if ( ! is_array($v)) { |
|
| 269 | 1 | throw new RuntimeException(sprintf('Unsupported value type for XML attribute map. Expected array but got %s.', gettype($v))); |
|
| 270 | } |
||
| 271 | |||
| 272 | 1 | foreach ($v as $key => $value) { |
|
| 273 | 1 | $this->setCurrentMetadata($metadata); |
|
| 274 | 1 | $node = $this->navigator->accept($value, null, $context); |
|
| 275 | 1 | $this->revertCurrentMetadata(); |
|
| 276 | |||
| 277 | 1 | if ( ! $node instanceof \DOMCharacterData) { |
|
| 278 | throw new RuntimeException(sprintf('Unsupported value for a XML attribute map value. Expected character data, but got %s.', json_encode($v))); |
||
| 279 | } |
||
| 280 | |||
| 281 | 1 | $this->setAttributeOnNode($this->currentNode, $key, $node->nodeValue, $metadata->xmlNamespace); |
|
| 282 | 1 | } |
|
| 283 | |||
| 284 | 1 | return; |
|
| 285 | } |
||
| 286 | |||
| 287 | 45 | if ($addEnclosingElement = ( ! $metadata->xmlCollection || ! $metadata->xmlCollectionInline) && ! $metadata->inline) { |
|
| 288 | 44 | $elementName = $this->namingStrategy->translateName($metadata); |
|
| 289 | |||
| 290 | 44 | if (null !== $metadata->xmlNamespace) { |
|
| 291 | 5 | $element = $this->createElement($elementName, $metadata->xmlNamespace); |
|
| 292 | 5 | } else { |
|
| 293 | 41 | $defaultNamespace = $this->getClassDefaultNamespace($this->objectMetadataStack->top()); |
|
| 294 | 41 | $element = $this->createElement($elementName, $defaultNamespace); |
|
| 295 | } |
||
| 296 | 44 | $this->setCurrentNode($element); |
|
| 297 | 44 | } |
|
| 298 | |||
| 299 | 45 | $this->setCurrentMetadata($metadata); |
|
| 300 | |||
| 301 | 45 | if (null !== $node = $this->navigator->accept($v, $metadata->type, $context)) { |
|
| 302 | 35 | $this->currentNode->appendChild($node); |
|
| 303 | 35 | } |
|
| 304 | |||
| 305 | 45 | $this->revertCurrentMetadata(); |
|
| 306 | |||
| 307 | 45 | if ($addEnclosingElement) { |
|
| 308 | 44 | $this->revertCurrentNode(); |
|
| 309 | |||
| 310 | 44 | if ($this->nodeNotEmpty($element) || ((!$metadata->xmlCollection || !$metadata->xmlCollectionSkipWhenEmpty) && $node === null && $v !== null && !$context->isVisiting($v))) { |
|
| 311 | 44 | $this->currentNode->appendChild($element); |
|
| 312 | 44 | } |
|
| 313 | 44 | } |
|
| 314 | |||
| 315 | 45 | $this->hasValue = false; |
|
| 316 | 45 | } |
|
| 317 | |||
| 318 | 44 | private function nodeNotEmpty(\DOMElement $element) |
|
| 319 | { |
||
| 320 | 44 | return $element->hasChildNodes() || $element->hasAttributes(); |
|
| 321 | } |
||
| 322 | |||
| 323 | 50 | public function endVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
|
| 324 | { |
||
| 325 | 50 | $this->objectMetadataStack->pop(); |
|
| 326 | 50 | } |
|
| 327 | |||
| 328 | 79 | public function getResult() |
|
| 332 | |||
| 333 | 2 | public function getCurrentNode() |
|
| 337 | |||
| 338 | public function getCurrentMetadata() |
||
| 342 | |||
| 343 | public function getDocument() |
||
| 347 | |||
| 348 | 51 | public function setCurrentMetadata(PropertyMetadata $metadata) |
|
| 353 | |||
| 354 | 73 | public function setCurrentNode(\DOMNode $node) |
|
| 359 | |||
| 360 | 51 | public function revertCurrentNode() |
|
| 361 | { |
||
| 362 | 51 | return $this->currentNode = $this->stack->pop(); |
|
| 363 | } |
||
| 364 | |||
| 365 | 51 | public function revertCurrentMetadata() |
|
| 366 | { |
||
| 367 | 51 | return $this->currentMetadata = $this->metadataStack->pop(); |
|
| 368 | } |
||
| 369 | |||
| 370 | 81 | public function createDocument($version = null, $encoding = null, $addRoot = true) |
|
| 371 | { |
||
| 372 | 81 | $doc = new \DOMDocument($version ?: $this->defaultVersion, $encoding ?: $this->defaultEncoding); |
|
| 373 | 81 | $doc->formatOutput = $this->isFormatOutput(); |
|
| 374 | |||
| 375 | 81 | if ($addRoot) { |
|
| 376 | 27 | if ($this->defaultRootNamespace) { |
|
| 377 | $rootNode = $doc->createElementNS($this->defaultRootNamespace, $this->defaultRootName); |
||
| 378 | } else { |
||
| 379 | 27 | $rootNode = $doc->createElement($this->defaultRootName); |
|
| 380 | } |
||
| 381 | 27 | $this->setCurrentNode($rootNode); |
|
| 382 | 27 | $doc->appendChild($rootNode); |
|
| 383 | 27 | } |
|
| 384 | |||
| 385 | 81 | return $doc; |
|
| 386 | } |
||
| 387 | |||
| 388 | 82 | public function prepare($data) |
|
| 394 | |||
| 395 | 26 | private function visitNumeric($data, array $type) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Checks that the name is a valid XML element name. |
||
| 409 | * |
||
| 410 | * @param string $name |
||
| 411 | * |
||
| 412 | * @return boolean |
||
| 413 | */ |
||
| 414 | 2 | private function isElementNameValid($name) |
|
| 418 | |||
| 419 | 9 | private function attachNullNamespace() |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Adds namespace attributes to the XML root element |
||
| 433 | * |
||
| 434 | * @param \JMS\Serializer\Metadata\ClassMetadata $metadata |
||
| 435 | * @param \DOMElement $element |
||
| 436 | */ |
||
| 437 | 52 | private function addNamespaceAttributes(ClassMetadata $metadata, \DOMElement $element) |
|
| 438 | { |
||
| 439 | 52 | foreach ($metadata->xmlNamespaces as $prefix => $uri) { |
|
| 440 | 5 | $attribute = 'xmlns'; |
|
| 449 | |||
| 450 | 51 | private function createElement($tagName, $namespace = null) |
|
| 464 | |||
| 465 | 12 | private function setAttributeOnNode(\DOMElement $node, $name, $value, $namespace = null) |
|
| 476 | |||
| 477 | 45 | private function getClassDefaultNamespace(ClassMetadata $metadata) |
|
| 481 | |||
| 482 | /** |
||
| 483 | * @return bool |
||
| 484 | */ |
||
| 485 | 81 | public function isFormatOutput() |
|
| 489 | |||
| 490 | /** |
||
| 491 | * @param bool $formatOutput |
||
| 492 | */ |
||
| 493 | 1 | public function setFormatOutput($formatOutput) |
|
| 497 | } |
||
| 498 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.