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 XmlDumper 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 XmlDumper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class XmlDumper extends Dumper |
||
31 | { |
||
32 | /** |
||
33 | * @var \DOMDocument |
||
34 | */ |
||
35 | private $document; |
||
36 | |||
37 | /** |
||
38 | * Dumps the service container as an XML string. |
||
39 | * |
||
40 | * @param array $options An array of options |
||
41 | * |
||
42 | * @return string An xml string representing of the service container |
||
43 | * |
||
44 | * @api |
||
45 | */ |
||
46 | public function dump(array $options = array()) |
||
47 | { |
||
48 | $this->document = new \DOMDocument('1.0', 'utf-8'); |
||
49 | $this->document->formatOutput = true; |
||
50 | |||
51 | $container = $this->document->createElementNS('http://symfony.com/schema/dic/services', 'container'); |
||
52 | $container->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); |
||
53 | $container->setAttribute('xsi:schemaLocation', 'http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd'); |
||
54 | |||
55 | $this->addParameters($container); |
||
56 | $this->addServices($container); |
||
57 | |||
58 | $this->document->appendChild($container); |
||
59 | $xml = $this->document->saveXML(); |
||
60 | $this->document = null; |
||
61 | |||
62 | return $xml; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Adds parameters. |
||
67 | * |
||
68 | * @param \DOMElement $parent |
||
69 | */ |
||
70 | private function addParameters(\DOMElement $parent) |
||
71 | { |
||
72 | $data = $this->container->getParameterBag()->all(); |
||
73 | if (!$data) { |
||
|
|||
74 | return; |
||
75 | } |
||
76 | |||
77 | if ($this->container->isFrozen()) { |
||
78 | $data = $this->escape($data); |
||
79 | } |
||
80 | |||
81 | $parameters = $this->document->createElement('parameters'); |
||
82 | $parent->appendChild($parameters); |
||
83 | $this->convertParameters($data, 'parameter', $parameters); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Adds method calls. |
||
88 | * |
||
89 | * @param array $methodcalls |
||
90 | * @param \DOMElement $parent |
||
91 | */ |
||
92 | private function addMethodCalls(array $methodcalls, \DOMElement $parent) |
||
93 | { |
||
94 | foreach ($methodcalls as $methodcall) { |
||
95 | $call = $this->document->createElement('call'); |
||
96 | $call->setAttribute('method', $methodcall[0]); |
||
97 | if (count($methodcall[1])) { |
||
98 | $this->convertParameters($methodcall[1], 'argument', $call); |
||
99 | } |
||
100 | $parent->appendChild($call); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Adds a service. |
||
106 | * |
||
107 | * @param Definition $definition |
||
108 | * @param string $id |
||
109 | * @param \DOMElement $parent |
||
110 | */ |
||
111 | private function addService($definition, $id, \DOMElement $parent) |
||
211 | |||
212 | /** |
||
213 | * Adds a service alias. |
||
214 | * |
||
215 | * @param string $alias |
||
216 | * @param Alias $id |
||
217 | * @param \DOMElement $parent |
||
218 | */ |
||
219 | private function addServiceAlias($alias, Alias $id, \DOMElement $parent) |
||
220 | { |
||
221 | $service = $this->document->createElement('service'); |
||
222 | $service->setAttribute('id', $alias); |
||
223 | $service->setAttribute('alias', $id); |
||
224 | if (!$id->isPublic()) { |
||
225 | $service->setAttribute('public', 'false'); |
||
226 | } |
||
227 | $parent->appendChild($service); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Adds services. |
||
232 | * |
||
233 | * @param \DOMElement $parent |
||
234 | */ |
||
235 | private function addServices(\DOMElement $parent) |
||
236 | { |
||
237 | $definitions = $this->container->getDefinitions(); |
||
238 | if (!$definitions) { |
||
239 | return; |
||
240 | } |
||
241 | |||
242 | $services = $this->document->createElement('services'); |
||
243 | foreach ($definitions as $id => $definition) { |
||
244 | $this->addService($definition, $id, $services); |
||
245 | } |
||
246 | |||
247 | $aliases = $this->container->getAliases(); |
||
248 | View Code Duplication | foreach ($aliases as $alias => $id) { |
|
249 | while (isset($aliases[(string) $id])) { |
||
250 | $id = $aliases[(string) $id]; |
||
251 | } |
||
252 | $this->addServiceAlias($alias, $id, $services); |
||
253 | } |
||
254 | $parent->appendChild($services); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Converts parameters. |
||
259 | * |
||
260 | * @param array $parameters |
||
261 | * @param string $type |
||
262 | * @param \DOMElement $parent |
||
263 | * @param string $keyAttribute |
||
264 | */ |
||
265 | private function convertParameters($parameters, $type, \DOMElement $parent, $keyAttribute = 'key') |
||
266 | { |
||
267 | $withKeys = array_keys($parameters) !== range(0, count($parameters) - 1); |
||
268 | foreach ($parameters as $key => $value) { |
||
269 | $element = $this->document->createElement($type); |
||
270 | if ($withKeys) { |
||
271 | $element->setAttribute($keyAttribute, $key); |
||
272 | } |
||
273 | |||
274 | if (is_array($value)) { |
||
275 | $element->setAttribute('type', 'collection'); |
||
276 | $this->convertParameters($value, $type, $element, 'key'); |
||
277 | } elseif ($value instanceof Reference) { |
||
278 | $element->setAttribute('type', 'service'); |
||
279 | $element->setAttribute('id', (string) $value); |
||
280 | $behaviour = $value->getInvalidBehavior(); |
||
281 | if ($behaviour == ContainerInterface::NULL_ON_INVALID_REFERENCE) { |
||
282 | $element->setAttribute('on-invalid', 'null'); |
||
283 | } elseif ($behaviour == ContainerInterface::IGNORE_ON_INVALID_REFERENCE) { |
||
284 | $element->setAttribute('on-invalid', 'ignore'); |
||
285 | } |
||
286 | if (!$value->isStrict()) { |
||
287 | $element->setAttribute('strict', 'false'); |
||
288 | } |
||
289 | } elseif ($value instanceof Definition) { |
||
290 | $element->setAttribute('type', 'service'); |
||
291 | $this->addService($value, null, $element); |
||
292 | } elseif ($value instanceof Expression) { |
||
293 | $element->setAttribute('type', 'expression'); |
||
294 | $text = $this->document->createTextNode(self::phpToXml((string) $value)); |
||
295 | $element->appendChild($text); |
||
296 | } else { |
||
297 | if (in_array($value, array('null', 'true', 'false'), true)) { |
||
298 | $element->setAttribute('type', 'string'); |
||
299 | } |
||
300 | $text = $this->document->createTextNode(self::phpToXml($value)); |
||
301 | $element->appendChild($text); |
||
302 | } |
||
303 | $parent->appendChild($element); |
||
304 | } |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Escapes arguments. |
||
309 | * |
||
310 | * @param array $arguments |
||
311 | * |
||
312 | * @return array |
||
313 | */ |
||
314 | View Code Duplication | private function escape($arguments) |
|
329 | |||
330 | /** |
||
331 | * Converts php types to xml types. |
||
332 | * |
||
333 | * @param mixed $value Value to convert |
||
334 | * |
||
335 | * @return string |
||
336 | * |
||
337 | * @throws RuntimeException When trying to dump object or resource |
||
338 | */ |
||
339 | public static function phpToXml($value) |
||
356 | } |
||
357 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.