Conditions | 14 |
Paths | 11 |
Total Lines | 41 |
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 |
||
101 | private function removeDotSegments($path) |
||
102 | { |
||
103 | $output = ''; |
||
104 | |||
105 | // Make sure not to be trapped in an infinite loop due to a bug in this |
||
106 | // method |
||
107 | $j = 0; |
||
108 | while ($path && $j++ < 100) { |
||
109 | if (substr($path, 0, 2) === './') { |
||
110 | // Step 2.A |
||
111 | $path = substr($path, 2); |
||
112 | } elseif (substr($path, 0, 3) === '../') { |
||
113 | // Step 2.A |
||
114 | $path = substr($path, 3); |
||
115 | } elseif (substr($path, 0, 3) === '/./' || $path === '/.') { |
||
116 | // Step 2.B |
||
117 | $path = '/' . substr($path, 3); |
||
118 | } elseif (substr($path, 0, 4) === '/../' || $path === '/..') { |
||
119 | // Step 2.C |
||
120 | $path = '/' . substr($path, 4); |
||
121 | $i = strrpos($output, '/'); |
||
122 | $output = $i === false ? '' : substr($output, 0, $i); |
||
123 | } elseif ($path === '.' || $path === '..') { |
||
124 | // Step 2.D |
||
125 | $path = ''; |
||
126 | } else { |
||
127 | // Step 2.E |
||
128 | $i = strpos($path, '/'); |
||
129 | if ($i === 0) { |
||
130 | $i = strpos($path, '/', 1); |
||
131 | } |
||
132 | if ($i === false) { |
||
133 | $i = strlen($path); |
||
134 | } |
||
135 | $output.= substr($path, 0, $i); |
||
136 | $path = substr($path, $i); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | return $output; |
||
141 | } |
||
142 | } |
||
143 |