| Conditions | 11 |
| Paths | 35 |
| Total Lines | 36 |
| Code Lines | 21 |
| 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 |
||
| 46 | public function parse(Method $method) { |
||
| 47 | |||
| 48 | $start = $this->searcher->getNext($method->getTokens(), 0, '{') + 1; |
||
| 49 | $len = sizeof($method->getTokens()); |
||
| 50 | $tokens = array_slice($method->getTokens(), $start, ($len - $start) - 1); |
||
| 51 | |||
| 52 | foreach($tokens as $n => $token) { |
||
| 53 | // replace $this->aaa by "class_attribute |
||
| 54 | if(preg_match('!^\$this\->\w+$!', $token)) { |
||
| 55 | $tokens[$n] = 'class_attribute'; |
||
| 56 | } |
||
| 57 | |||
| 58 | // replace vars by "var" |
||
| 59 | if(preg_match('!^\$\w+$!', $token) && $token != '$this') { |
||
| 60 | $tokens[$n] = 'var'; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | switch($tokens) { |
||
| 64 | // getters |
||
| 65 | case array('return', 'cast', 'class_attribute'): |
||
| 66 | case array('return','class_attribute'): |
||
| 67 | return MethodUsage::USAGE_GETTER; |
||
| 68 | break; |
||
|
|
|||
| 69 | |||
| 70 | // setters |
||
| 71 | case array('class_attribute', '=', 'var'): |
||
| 72 | case array('class_attribute', '=', 'cast', 'var'): |
||
| 73 | case array('class_attribute', '=', 'var', 'return', '$this'): |
||
| 74 | case array('class_attribute', '=', 'cast', 'var', 'return', '$this'): |
||
| 75 | return MethodUsage::USAGE_SETTER; |
||
| 76 | break; |
||
| 77 | } |
||
| 78 | |||
| 79 | |||
| 80 | return MethodUsage::USAGE_UNKNWON; |
||
| 81 | } |
||
| 82 | } |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.