Conditions | 8 |
Paths | 11 |
Total Lines | 61 |
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 |
||
31 | public function process(\DOMDocument $xml) |
||
32 | { |
||
33 | $ignoreQry = '//tag[@name=\'return\' and @type=\'self\']' |
||
34 | . '|//tag[@name=\'return\' and @type=\'$this\']' |
||
35 | . '|//tag[@name=\'return\']/type[.=\'self\']' |
||
36 | . '|//tag[@name=\'return\']/type[.=\'$this\']'; |
||
37 | |||
38 | $xpath = new \DOMXPath($xml); |
||
39 | $nodes = $xpath->query($ignoreQry); |
||
40 | |||
41 | /** @var \DOMElement $node */ |
||
42 | foreach ($nodes as $node) { |
||
43 | // if a node with name 'type' is selected we need to reach one |
||
44 | // level further. |
||
45 | $docblock = ($node->nodeName === 'type') |
||
46 | ? $node->parentNode->parentNode |
||
47 | : $node->parentNode; |
||
48 | |||
49 | /** @var \DOMElement $method */ |
||
50 | $method = $docblock->parentNode; |
||
51 | |||
52 | // if the method is not a method but a global function: error! |
||
53 | if ($method->nodeName !== 'method') { |
||
54 | continue; |
||
55 | } |
||
56 | |||
57 | /** @var \DOMElement $parentNode */ |
||
58 | $parentNode = $method->parentNode; |
||
59 | $type = $parentNode->getElementsByTagName('full_name') |
||
60 | ->item(0)->nodeValue; |
||
61 | |||
62 | // nodes with name type need to set their content; otherwise we set |
||
63 | // an attribute of the class itself |
||
64 | if ($node->nodeName === 'type') { |
||
65 | $node->nodeValue = $type; |
||
66 | |||
67 | // add a new tag @fluent to indicate that this is a fluent interface |
||
68 | // we only add it here since there should always be a node `type` |
||
69 | $fluent_tag = new \DOMElement('tag'); |
||
70 | $docblock->appendChild($fluent_tag); |
||
71 | $fluent_tag->setAttribute('name', 'fluent'); |
||
72 | $fluent_tag->setAttribute( |
||
73 | 'description', |
||
74 | 'This method is part of a fluent interface and will return ' |
||
75 | . 'the same instance' |
||
76 | ); |
||
77 | } else { |
||
78 | $node->setAttribute('type', $type); |
||
79 | } |
||
80 | |||
81 | // check if an excerpt is set and override that as well |
||
82 | if ($node->hasAttribute('excerpt') |
||
83 | && (($node->getAttribute('excerpt') === 'self') |
||
84 | || ($node->getAttribute('excerpt') === '$this')) |
||
85 | ) { |
||
86 | $node->setAttribute('excerpt', $type); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | return $xml; |
||
91 | } |
||
92 | } |
||
93 |