| Conditions | 15 |
| Paths | 51 |
| Total Lines | 64 |
| Code Lines | 33 |
| 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 |
||
| 22 | public static function remove_class_content($filePath, $cutOffDepth = 1) |
||
| 23 | { |
||
| 24 | |||
| 25 | // Use PHP's built in method to strip comments and whitespace |
||
| 26 | $contents = php_strip_whitespace($filePath); |
||
| 27 | |||
| 28 | if (!trim($contents)) { |
||
| 29 | return $contents; |
||
| 30 | } |
||
| 31 | |||
| 32 | if (!preg_match('/\b(?:class|interface|trait)/i', $contents)) { |
||
| 33 | return ''; |
||
| 34 | } |
||
| 35 | |||
| 36 | // tokenize the file contents |
||
| 37 | $tokens = token_get_all($contents); |
||
| 38 | $cleanContents = ''; |
||
| 39 | $depth = 0; |
||
| 40 | $startCounting = false; |
||
| 41 | // iterate over all the tokens and only store the tokens that are outside $cutOffDepth |
||
| 42 | foreach ($tokens as $token) { |
||
| 43 | // only store the string literal of the token, that's all we need |
||
| 44 | if (!is_array($token)) { |
||
| 45 | $token = [ |
||
| 46 | T_STRING, |
||
| 47 | $token, |
||
| 48 | null |
||
| 49 | ]; |
||
| 50 | } |
||
| 51 | |||
| 52 | // only count if we see a class/interface/trait keyword |
||
| 53 | if (!$startCounting && in_array($token[0], [T_CLASS, T_INTERFACE, T_TRAIT])) { |
||
| 54 | $startCounting = true; |
||
| 55 | } |
||
| 56 | |||
| 57 | // use curly braces as a sign of depth |
||
| 58 | if ($token[1] === '{') { |
||
| 59 | if ($depth < $cutOffDepth) { |
||
| 60 | $cleanContents .= $token[1]; |
||
| 61 | } |
||
| 62 | if ($startCounting) { |
||
| 63 | ++$depth; |
||
| 64 | } |
||
| 65 | } elseif ($token[1] === '}') { |
||
| 66 | if ($startCounting) { |
||
| 67 | --$depth; |
||
| 68 | |||
| 69 | // stop counting if we've just come out of the |
||
| 70 | // class/interface/trait declaration |
||
|
|
|||
| 71 | if ($depth <= 0) { |
||
| 72 | $startCounting = false; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | if ($depth < $cutOffDepth) { |
||
| 76 | $cleanContents .= $token[1]; |
||
| 77 | } |
||
| 78 | } elseif ($depth < $cutOffDepth) { |
||
| 79 | $cleanContents .= $token[1]; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | // return cleaned class |
||
| 84 | return trim($cleanContents); |
||
| 85 | } |
||
| 86 | } |
||
| 87 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.