| Conditions | 11 |
| Paths | 12 |
| Total Lines | 47 |
| Code Lines | 24 |
| Lines | 26 |
| Ratio | 55.32 % |
| 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 |
||
| 54 | private function computeDistanceGraph() |
||
| 55 | { |
||
| 56 | $nbNodes = $this->grid->getNodesNb(); |
||
| 57 | View Code Duplication | for($i = 0; $i < $nbNodes; $i++) |
|
| 58 | { |
||
| 59 | $iNode = $this->grid->getNodeNumber($i); |
||
| 60 | $neighbors = $this->grid->getWalkableNeighbors($iNode); |
||
| 61 | foreach($neighbors as $neighbor) |
||
| 62 | { |
||
| 63 | $alternativeDistance = $this->distance->compute($iNode, $neighbor); |
||
| 64 | if($alternativeDistance < $this->distanceGraph->getEdgeBetween($iNode, $neighbor)) |
||
| 65 | { |
||
| 66 | $this->distanceGraph->createEdgeBetween($iNode, $neighbor, $alternativeDistance); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | for($k = 0; $k < $nbNodes; $k++) |
||
| 72 | { |
||
| 73 | $kNode = $this->grid->getNodeNumber($k); |
||
| 74 | if(! $kNode->isWalkable()) |
||
| 75 | { |
||
| 76 | continue; |
||
| 77 | } |
||
| 78 | for($i = 0; $i < $nbNodes; $i++) |
||
| 79 | { |
||
| 80 | $iNode = $this->grid->getNodeNumber($i); |
||
| 81 | if(! $iNode->isWalkable()) |
||
| 82 | { |
||
| 83 | continue; |
||
| 84 | } |
||
| 85 | View Code Duplication | for($j = 0; $j < $nbNodes; $j++) |
|
| 86 | { |
||
| 87 | $jNode = $this->grid->getNodeNumber($j); |
||
| 88 | if(! $jNode->isWalkable()) |
||
| 89 | { |
||
| 90 | continue; |
||
| 91 | } |
||
| 92 | $alternativeDistance = $this->distanceGraph->getEdgeBetween($iNode, $kNode) + $this->distanceGraph->getEdgeBetween($kNode, $jNode); |
||
| 93 | if($alternativeDistance < $this->distanceGraph->getEdgeBetween($iNode, $jNode)) |
||
| 94 | { |
||
| 95 | $this->distanceGraph->createEdgeBetween($iNode, $jNode, $alternativeDistance); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.