Complex classes like XmlLocation 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 XmlLocation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class XmlLocation extends AbstractLocation |
||
| 14 | { |
||
| 15 | /** @var \XMLWriter XML writer resource */ |
||
| 16 | private $writer; |
||
| 17 | |||
| 18 | /** @var string Content-Type header added when XML is found */ |
||
| 19 | private $contentType; |
||
| 20 | |||
| 21 | /** @var Parameter[] Buffered elements to write */ |
||
| 22 | private $buffered = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param string $locationName Name of the location |
||
| 26 | * @param string $contentType Set to a non-empty string to add a |
||
| 27 | * Content-Type header to a request if any XML content is added to the |
||
| 28 | * body. Pass an empty string to disable the addition of the header. |
||
| 29 | */ |
||
| 30 | 4 | public function __construct($locationName = 'xml', $contentType = 'application/xml') |
|
| 35 | |||
| 36 | /** |
||
| 37 | * @param CommandInterface $command |
||
| 38 | * @param RequestInterface $request |
||
| 39 | * @param Parameter $param |
||
| 40 | * |
||
| 41 | * @return RequestInterface |
||
| 42 | */ |
||
| 43 | 17 | public function visit( |
|
| 59 | |||
| 60 | /** |
||
| 61 | * @param CommandInterface $command |
||
| 62 | * @param RequestInterface $request |
||
| 63 | * @param Operation $operation |
||
| 64 | * |
||
| 65 | * @return RequestInterface |
||
| 66 | */ |
||
| 67 | 18 | public function after( |
|
| 68 | CommandInterface $command, |
||
| 69 | RequestInterface $request, |
||
| 70 | Operation $operation |
||
| 71 | ) { |
||
| 72 | 18 | foreach ($this->buffered as $param) { |
|
| 73 | 17 | $this->visitWithValue( |
|
| 74 | 17 | $command[$param->getName()], |
|
|
|
|||
| 75 | 17 | $param, |
|
| 76 | $operation |
||
| 77 | 17 | ); |
|
| 78 | 18 | } |
|
| 79 | |||
| 80 | 18 | $this->buffered = []; |
|
| 81 | |||
| 82 | 18 | $additional = $operation->getAdditionalParameters(); |
|
| 83 | 18 | if ($additional && $additional->getLocation() == $this->locationName) { |
|
| 84 | 1 | foreach ($command->toArray() as $key => $value) { |
|
| 85 | 1 | if (!$operation->hasParam($key)) { |
|
| 86 | 1 | $additional->setName($key); |
|
| 87 | 1 | $this->visitWithValue($value, $additional, $operation); |
|
| 88 | 1 | } |
|
| 89 | 1 | } |
|
| 90 | 1 | $additional->setName(null); |
|
| 91 | 1 | } |
|
| 92 | |||
| 93 | // If data was found that needs to be serialized, then do so |
||
| 94 | 18 | $xml = ''; |
|
| 95 | 18 | if ($this->writer) { |
|
| 96 | 17 | $xml = $this->finishDocument($this->writer); |
|
| 97 | 18 | } elseif ($operation->getData('xmlAllowEmpty')) { |
|
| 98 | // Check if XML should always be sent for the command |
||
| 99 | 1 | $writer = $this->createRootElement($operation); |
|
| 100 | 1 | $xml = $this->finishDocument($writer); |
|
| 101 | 1 | } |
|
| 102 | |||
| 103 | 18 | if ($xml !== '') { |
|
| 104 | 18 | $request = $request->withBody(Psr7\stream_for($xml)); |
|
| 105 | // Don't overwrite the Content-Type if one is set |
||
| 106 | 18 | if ($this->contentType && !$request->hasHeader('Content-Type')) { |
|
| 107 | 18 | $request = $request->withHeader('Content-Type', $this->contentType); |
|
| 108 | 18 | } |
|
| 109 | 18 | } |
|
| 110 | |||
| 111 | 18 | $this->writer = null; |
|
| 112 | |||
| 113 | 18 | return $request; |
|
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Create the root XML element to use with a request |
||
| 118 | * |
||
| 119 | * @param Operation $operation Operation object |
||
| 120 | * |
||
| 121 | * @return \XMLWriter |
||
| 122 | */ |
||
| 123 | 18 | protected function createRootElement(Operation $operation) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Recursively build the XML body |
||
| 150 | * |
||
| 151 | * @param \XMLWriter $writer XML to modify |
||
| 152 | * @param Parameter $param API Parameter |
||
| 153 | * @param mixed $value Value to add |
||
| 154 | */ |
||
| 155 | 17 | protected function addXml(\XMLWriter $writer, Parameter $param, $value) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Write an attribute with namespace if used |
||
| 193 | * |
||
| 194 | * @param \XMLWriter $writer XMLWriter instance |
||
| 195 | * @param string $prefix Namespace prefix if any |
||
| 196 | * @param string $name Attribute name |
||
| 197 | * @param string $namespace The uri of the namespace |
||
| 198 | * @param string $value The attribute content |
||
| 199 | */ |
||
| 200 | 5 | protected function writeAttribute($writer, $prefix, $name, $namespace, $value) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Write an element with namespace if used |
||
| 211 | * |
||
| 212 | * @param \XMLWriter $writer XML writer resource |
||
| 213 | * @param string $prefix Namespace prefix if any |
||
| 214 | * @param string $name Element name |
||
| 215 | * @param string $namespace The uri of the namespace |
||
| 216 | * @param string $value The element content |
||
| 217 | */ |
||
| 218 | 15 | protected function writeElement(\XMLWriter $writer, $prefix, $name, $namespace, $value) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Create a new xml writer and start a document |
||
| 235 | * |
||
| 236 | * @param string $encoding document encoding |
||
| 237 | * |
||
| 238 | * @return \XMLWriter the writer resource |
||
| 239 | * @throws \RuntimeException if the document cannot be started |
||
| 240 | */ |
||
| 241 | 18 | protected function startDocument($encoding) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * End the document and return the output |
||
| 256 | * |
||
| 257 | * @param \XMLWriter $writer |
||
| 258 | * |
||
| 259 | * @return string the writer resource |
||
| 260 | */ |
||
| 261 | 18 | protected function finishDocument($writer) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Add an array to the XML |
||
| 270 | * |
||
| 271 | * @param \XMLWriter $writer |
||
| 272 | * @param Parameter $param |
||
| 273 | * @param $value |
||
| 274 | */ |
||
| 275 | 4 | protected function addXmlArray(\XMLWriter $writer, Parameter $param, &$value) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Add an object to the XML |
||
| 286 | * |
||
| 287 | * @param \XMLWriter $writer |
||
| 288 | * @param Parameter $param |
||
| 289 | * @param $value |
||
| 290 | */ |
||
| 291 | 8 | protected function addXmlObject(\XMLWriter $writer, Parameter $param, &$value) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @param $value |
||
| 314 | * @param Parameter $param |
||
| 315 | * @param Operation $operation |
||
| 316 | */ |
||
| 317 | 17 | private function visitWithValue( |
|
| 328 | } |
||
| 329 |