| Conditions | 12 |
| Paths | 9 |
| Total Lines | 47 |
| Code Lines | 24 |
| Lines | 8 |
| Ratio | 17.02 % |
| 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 |
||
| 31 | public function computeRoute() |
||
| 32 | { |
||
| 33 | if(! $this->graph instanceof NodeGraph) |
||
| 34 | { |
||
| 35 | throw new \RuntimeException('Invalid Graph'); |
||
| 36 | } |
||
| 37 | if(! $this->existingRoute instanceof NodePath) |
||
| 38 | { |
||
| 39 | throw new \RuntimeException('Invalid ExistingRoute'); |
||
| 40 | } |
||
| 41 | if(count($this->existingRoute) < 3) |
||
| 42 | { |
||
| 43 | return $this->existingRoute; |
||
| 44 | } |
||
| 45 | |||
| 46 | $existingDistance = $this->graph->computeLength($this->existingRoute); |
||
| 47 | |||
| 48 | do |
||
| 49 | { |
||
| 50 | $improvementMade = false; |
||
| 51 | foreach($this->existingRoute->getKeys() as $iKey) |
||
| 52 | { |
||
| 53 | foreach($this->existingRoute->getKeys() as $jKey) |
||
| 54 | { |
||
| 55 | foreach($this->existingRoute->getKeys() as $kKey) |
||
| 56 | { |
||
| 57 | if($iKey == $jKey || $iKey == $kKey || $jKey == $kKey) |
||
| 58 | { |
||
| 59 | continue; |
||
| 60 | } |
||
| 61 | $newRoute = $this->threeOptSwap($iKey, $jKey, $kKey); |
||
| 62 | $newDistance = $this->graph->computeLength($newRoute); |
||
| 63 | View Code Duplication | if($newDistance < $existingDistance) |
|
| 64 | { |
||
| 65 | $this->existingRoute = $newRoute; |
||
| 66 | $existingDistance = $newDistance; |
||
| 67 | $improvementMade = true; |
||
| 68 | |||
| 69 | break 3; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } while($improvementMade); |
||
| 75 | |||
| 76 | return $this->existingRoute; |
||
| 77 | } |
||
| 78 | |||
| 105 |
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.