Conditions | 19 |
Paths | 2 |
Total Lines | 57 |
Code Lines | 38 |
Lines | 35 |
Ratio | 61.4 % |
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 |
||
136 | function getPhpDefinitionsFromFile($filePath) |
||
137 | { |
||
138 | $classes = []; |
||
139 | $traits = []; |
||
140 | $interfaces = []; |
||
141 | $fp = fopen($filePath, 'r'); |
||
142 | $trait = $interface = $class = $namespace = $buffer = ''; |
||
143 | $i = 0; |
||
144 | while (!$class) { |
||
145 | if (feof($fp)) { |
||
146 | break; |
||
147 | } |
||
148 | $buffer .= fread($fp, 512); |
||
149 | $tokens = token_get_all($buffer); |
||
150 | if (strpos($buffer, '{') === false) { |
||
151 | continue; |
||
152 | } |
||
153 | for (; $i < count($tokens); $i++) { |
||
154 | View Code Duplication | if ($tokens[$i][0] === T_NAMESPACE) { |
|
155 | for ($j = $i + 1; $j < count($tokens); $j++) { |
||
156 | if ($tokens[$j][0] === T_STRING) { |
||
157 | $namespace .= '\\' . $tokens[$j][1]; |
||
158 | } else { |
||
159 | if ($tokens[$j] === '{' || $tokens[$j] === ';') { |
||
160 | break; |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | View Code Duplication | if ($tokens[$i][0] === T_CLASS) { |
|
166 | for ($j = $i + 1; $j < count($tokens); $j++) { |
||
167 | if ($tokens[$j] === '{') { |
||
168 | $class = $tokens[$i + 2][1]; |
||
169 | $classes[] = $class; |
||
170 | } |
||
171 | } |
||
172 | } |
||
173 | View Code Duplication | if ($tokens[$i][0] === T_INTERFACE) { |
|
174 | for ($j = $i + 1; $j < count($tokens); $j++) { |
||
175 | if ($tokens[$j] === '{') { |
||
176 | $interface = $tokens[$i + 2][1]; |
||
177 | $interfaces[] = $interface; |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | View Code Duplication | if ($tokens[$i][0] === T_TRAIT) { |
|
182 | for ($j = $i + 1; $j < count($tokens); $j++) { |
||
183 | if ($tokens[$j] === '{') { |
||
184 | $trait = $tokens[$i + 2][1]; |
||
185 | $traits[] = $trait; |
||
186 | } |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | return compact('namespace', 'classes', 'traits', 'interfaces'); |
||
192 | } |
||
193 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: