| Conditions | 15 |
| Paths | 4 |
| Total Lines | 52 |
| Code Lines | 28 |
| 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 function getClassNameFromFile($file) |
||
| 13 | { |
||
| 14 | $fp = fopen($file, 'r'); |
||
| 15 | |||
| 16 | $class = $namespace = $buffer = ''; |
||
| 17 | $i = 0; |
||
| 18 | |||
| 19 | while (!$class) { |
||
| 20 | if (feof($fp)) { |
||
| 21 | break; |
||
| 22 | } |
||
| 23 | |||
| 24 | // Read entire lines to prevent keyword truncation |
||
| 25 | for ($line = 0; $line <= 20; $line++) { |
||
| 26 | $buffer .= fgets($fp); |
||
| 27 | } |
||
| 28 | $tokens = @token_get_all($buffer); |
||
| 29 | |||
| 30 | if (strpos($buffer, '{') === false) { |
||
| 31 | continue; |
||
| 32 | } |
||
| 33 | |||
| 34 | for (; $i < count($tokens); $i++) { |
||
|
|
|||
| 35 | if ($tokens[$i][0] === \T_NAMESPACE) { |
||
| 36 | for ($j = $i + 1; $j < count($tokens); $j++) { |
||
| 37 | if ($tokens[$j][0] === T_STRING) { |
||
| 38 | $namespace .= '\\' . $tokens[$j][1]; |
||
| 39 | } elseif ($tokens[$j] === '{' || $tokens[$j] === ';') { |
||
| 40 | break; |
||
| 41 | } |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | if ($tokens[$i][0] === \T_CLASS) { |
||
| 46 | for ($j = $i + 1; $j < count($tokens); $j++) { |
||
| 47 | if ($tokens[$j][0] === \T_STRING) { |
||
| 48 | $class = $tokens[$i + 2][1]; |
||
| 49 | break 2; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | } |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | if (!trim($class)) { |
||
| 57 | return; |
||
| 58 | } |
||
| 59 | |||
| 60 | fclose($fp); |
||
| 61 | |||
| 62 | return ltrim($namespace . '\\' . $class, '\\'); |
||
| 63 | } |
||
| 64 | } |
||
| 65 |
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: