| Conditions | 9 |
| Paths | 3 |
| Total Lines | 56 |
| Code Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 93 | private function resolveGraph(array ...$packages): array |
||
| 94 | { |
||
| 95 | $graph = array_reduce( |
||
| 96 | $packages, |
||
| 97 | function (array $carry, array $package): array { |
||
| 98 | foreach (array_keys($package['require'] ?? []) as $link) { |
||
| 99 | if (!preg_match('/^[^\/]+\/[^\/]+$/', $link)) { |
||
| 100 | // Most likely a platform requirement. |
||
| 101 | // E.g.: php |
||
| 102 | // E.g.: ext-openssl |
||
| 103 | continue; |
||
| 104 | } |
||
| 105 | |||
| 106 | if (!array_key_exists($link, $carry)) { |
||
| 107 | $carry[$link] = []; |
||
| 108 | } |
||
| 109 | |||
| 110 | if (!in_array($package['name'], $carry[$link], true)) { |
||
| 111 | $carry[$link][] = $package['name']; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | return $carry; |
||
| 116 | }, |
||
| 117 | [] |
||
| 118 | ); |
||
| 119 | |||
| 120 | // While the graph keeps receiving updates, keep on resolving. |
||
| 121 | for ($previousGraph = []; $graph !== $previousGraph;) { |
||
| 122 | // Do not update the previous graph before the current iteration |
||
| 123 | // has started. |
||
| 124 | $previousGraph = $graph; |
||
| 125 | |||
| 126 | // For each package and its dependents in the graph ... |
||
| 127 | foreach ($graph as $package => $dependents) { |
||
| 128 | // ... Update the dependents of the package with grandparents. |
||
| 129 | $graph[$package] = array_reduce( |
||
| 130 | // Determine grandparents by looking up the parents of the |
||
| 131 | // available dependents. |
||
| 132 | $dependents, |
||
| 133 | function (array $carry, string $parent) use ($graph): array { |
||
| 134 | foreach ($graph[$parent] ?? [] as $grandparent) { |
||
| 135 | if (!in_array($grandparent, $carry, true)) { |
||
| 136 | $carry[] = $grandparent; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | return $carry; |
||
| 141 | }, |
||
| 142 | // Start out with the current list of dependents. |
||
| 143 | $dependents |
||
| 144 | ); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | return $graph; |
||
| 149 | } |
||
| 151 |