| Conditions | 12 |
| Paths | 68 |
| Total Lines | 58 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 28 | public function calculate(Klass $class) { |
||
| 29 | |||
| 30 | $graph = new Graph(); |
||
| 31 | |||
| 32 | // attributes in graph are prefixed with '_attr_' string |
||
| 33 | foreach($class->getMethods() as $method) { |
||
| 34 | |||
| 35 | // avoid getters and setters |
||
| 36 | if($method->isGetter() ||$method->isSetter()) { |
||
| 37 | continue; |
||
| 38 | } |
||
| 39 | |||
| 40 | if(null === ($from = $graph->get($method->getName()))) { |
||
|
|
|||
| 41 | $from = new Node($method->getName()); |
||
| 42 | $graph->insert($from); |
||
| 43 | } |
||
| 44 | |||
| 45 | // calls |
||
| 46 | foreach($method->getCalls() as $call) { |
||
| 47 | |||
| 48 | if(!$call->isItself()) { |
||
| 49 | continue; |
||
| 50 | |||
| 51 | } |
||
| 52 | |||
| 53 | if(null === ($to = $graph->get($call->getMethodName()))) { |
||
| 54 | $to = new Node($call->getMethodName()); |
||
| 55 | $graph->insert($to); |
||
| 56 | } |
||
| 57 | $graph->addEdge($from, $to); |
||
| 58 | } |
||
| 59 | |||
| 60 | // attributes |
||
| 61 | foreach($method->getTokens() as $token) { |
||
| 62 | if(preg_match('!\$this\->(\w+)$!', $token, $matches)) { |
||
| 63 | list(, $attribute) = $matches; |
||
| 64 | |||
| 65 | if(null === ($to = $graph->get('_attr_' . $attribute))) { |
||
| 66 | $to = new Node('_attr_' . $attribute); |
||
| 67 | $graph->insert($to); |
||
| 68 | } |
||
| 69 | $graph->addEdge($from, $to); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | } |
||
| 74 | |||
| 75 | // iterate over nodes, and count paths |
||
| 76 | $paths = 0; |
||
| 77 | foreach($graph->all() as $node) { |
||
| 78 | $paths += $this->traverse($node); |
||
| 79 | } |
||
| 80 | |||
| 81 | $result = new Result; |
||
| 82 | $result->setLcom($paths); |
||
| 83 | return $result; |
||
| 84 | |||
| 85 | } |
||
| 86 | |||
| 113 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.