Conditions | 4 |
Paths | 8 |
Total Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
12 | public static function alter(\DomDocument $originalDom, AnnotatedCommand $command) |
||
13 | { |
||
14 | $dom = new \DOMDocument('1.0', 'UTF-8'); |
||
15 | $dom->appendChild($commandXML = $dom->createElement('command')); |
||
16 | $commandXML->setAttribute('id', $command->getName()); |
||
17 | $commandXML->setAttribute('name', $command->getName()); |
||
18 | |||
19 | // Get the original <command> element and its top-level elements. |
||
20 | $originalCommandXML = static::getSingleElementByTagName($dom, $originalDom, 'command'); |
||
21 | $originalUsagesXML = static::getSingleElementByTagName($dom, $originalCommandXML, 'usages'); |
||
22 | $originalDescriptionXML = static::getSingleElementByTagName($dom, $originalCommandXML, 'description'); |
||
23 | $originalHelpXML = static::getSingleElementByTagName($dom, $originalCommandXML, 'help'); |
||
24 | $originalArgumentsXML = static::getSingleElementByTagName($dom, $originalCommandXML, 'arguments'); |
||
25 | $originalOptionsXML = static::getSingleElementByTagName($dom, $originalCommandXML, 'options'); |
||
26 | |||
27 | // Keep only the first of the <usage> elements |
||
28 | $newUsagesXML = $dom->createElement('usages'); |
||
29 | $firstUsageXML = static::getSingleElementByTagName($dom, $originalUsagesXML, 'usage'); |
||
30 | $newUsagesXML->appendChild($firstUsageXML); |
||
31 | |||
32 | // Create our own <example> elements |
||
33 | $newExamplesXML = $dom->createElement('examples'); |
||
34 | foreach ($command->getExampleUsages() as $usage => $description) { |
||
35 | $newExamplesXML->appendChild($exampleXML = $dom->createElement('example')); |
||
36 | $exampleXML->appendChild($usageXML = $dom->createElement('usage', $usage)); |
||
37 | $exampleXML->appendChild($descriptionXML = $dom->createElement('description', $description)); |
||
38 | } |
||
39 | |||
40 | // Create our own <alias> elements |
||
41 | $newAliasesXML = $dom->createElement('aliases'); |
||
42 | foreach ($command->getAliases() as $alias) { |
||
43 | $newAliasesXML->appendChild($dom->createElement('alias', $alias)); |
||
44 | } |
||
45 | |||
46 | // Create our own <topic> elements |
||
47 | $newTopicsXML = $dom->createElement('topics'); |
||
48 | foreach ($command->getTopics() as $topic) { |
||
49 | $newTopicsXML->appendChild($topicXML = $dom->createElement('topic', $topic)); |
||
50 | } |
||
51 | |||
52 | // Place the different elements into the <command> element in the desired order |
||
53 | $commandXML->appendChild($newUsagesXML); |
||
54 | $commandXML->appendChild($newExamplesXML); |
||
55 | $commandXML->appendChild($originalDescriptionXML); |
||
56 | $commandXML->appendChild($originalArgumentsXML); |
||
57 | $commandXML->appendChild($originalOptionsXML); |
||
58 | $commandXML->appendChild($originalHelpXML); |
||
59 | $commandXML->appendChild($newAliasesXML); |
||
60 | $commandXML->appendChild($newTopicsXML); |
||
61 | |||
62 | return $dom; |
||
63 | } |
||
64 | |||
77 |