| Conditions | 10 |
| Paths | 60 |
| Total Lines | 62 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 83 | private function getShortestPath($source, $target) |
||
| 84 | { |
||
| 85 | // array of the best estimates of shortest path to each vertex |
||
| 86 | $bestEstimates = []; |
||
| 87 | // array of predecessors for each vertex |
||
| 88 | $predecessors = []; |
||
| 89 | // queue of all unoptimized vertices |
||
| 90 | $queue = new \SplPriorityQueue(); |
||
| 91 | |||
| 92 | foreach ($this->graph as $vertex => $list) { |
||
| 93 | $bestEstimates[$vertex] = \INF; // set initial distance to "infinity" |
||
| 94 | $predecessors[$vertex] = null; // no known predecessors yet |
||
| 95 | foreach ($list as $w => $cost) { |
||
| 96 | // use the edge cost as the priority |
||
| 97 | $queue->insert($w, $cost); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | // initial distance at source is 0 |
||
| 102 | $bestEstimates[$source] = 0; |
||
| 103 | |||
| 104 | while (!$queue->isEmpty()) { |
||
| 105 | // extract min cost |
||
| 106 | $u = $queue->extract(); |
||
| 107 | if (!empty($this->graph[$u])) { |
||
| 108 | // "relax" each adjacent vertex |
||
| 109 | foreach ($this->graph[$u] as $vertex => $cost) { |
||
| 110 | // alternate route length to adjacent neighbor |
||
| 111 | $alt = $bestEstimates[$u] + $cost; |
||
| 112 | /** |
||
| 113 | * if alternate route is shorter: |
||
| 114 | * - update minimum length to vertex |
||
| 115 | * - add neighbor to predecessors for vertex |
||
| 116 | */ |
||
| 117 | if ($alt < $bestEstimates[$vertex]) { |
||
| 118 | $bestEstimates[$vertex] = $alt; |
||
| 119 | $predecessors[$vertex] = $u; |
||
| 120 | |||
| 121 | $queue->insert($vertex, $cost); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | // we can now find the shortest path using reverse iteration |
||
| 128 | $stack = new \SplStack(); |
||
| 129 | $u = $target; |
||
| 130 | while (isset($predecessors[$u]) && $predecessors[$u]) { |
||
| 131 | $stack->push($u); |
||
| 132 | $u = $predecessors[$u]; |
||
| 133 | } |
||
| 134 | |||
| 135 | // there is no route back |
||
| 136 | if ($stack->isEmpty()) { |
||
| 137 | return []; |
||
| 138 | } |
||
| 139 | |||
| 140 | // add the source node |
||
| 141 | $stack->push($source); |
||
| 142 | |||
| 143 | return iterator_to_array($stack, false); |
||
| 144 | } |
||
| 145 | |||
| 176 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.