Conditions | 11 |
Paths | 9 |
Total Lines | 35 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
42 | private function walkObjectTree($objectOrArray, $query) |
||
43 | { |
||
44 | $node = $objectOrArray; |
||
45 | $objectPath = explode('.', $query); |
||
46 | |||
47 | // walk through the tree |
||
48 | foreach ($objectPath as $pathNode) { |
||
49 | if (is_array($node)) { |
||
50 | if (isset($node[$pathNode])) { |
||
51 | $node = $node[$pathNode]; |
||
52 | continue; |
||
53 | } |
||
54 | } elseif (is_object($node)) { |
||
55 | if (isset($node->$pathNode) || (method_exists($node, '__get') && $node->$pathNode)) { |
||
56 | $node = $node->$pathNode; |
||
57 | continue; |
||
58 | } elseif (method_exists($node, $pathNode)) { |
||
59 | $node = $node->$pathNode(); |
||
60 | continue; |
||
61 | } elseif (method_exists($node, 'get' . $pathNode)) { |
||
62 | $pathNode = 'get' . $pathNode; |
||
63 | $node = $node->$pathNode(); |
||
64 | continue; |
||
65 | } elseif (method_exists($node, 'is' . $pathNode)) { |
||
66 | $pathNode = 'is' . $pathNode; |
||
67 | $node = $node->$pathNode(); |
||
68 | continue; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | return null; |
||
73 | } |
||
74 | |||
75 | return $node; |
||
76 | } |
||
77 | } |
||
78 |