Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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( |
|
| 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 |