| Conditions | 17 |
| Paths | 26 |
| Total Lines | 93 |
| Code Lines | 50 |
| 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 |
||
| 123 | private function getUpgradeChains($packages, $buildVersion, $upgradeTo) |
||
| 124 | { |
||
| 125 | $versionMatrix = $this->getVersionMatrix($packages); |
||
| 126 | $allVersions = array_keys($versionMatrix); |
||
| 127 | |||
| 128 | $getExistingSubversions = function ($version) use ($allVersions) { |
||
| 129 | $existingVersions = []; |
||
| 130 | |||
| 131 | $fromParts = explode('.', $version); |
||
| 132 | foreach (range(2, count($fromParts)) as $length) { |
||
| 133 | $subversion = implode('.', array_slice($fromParts, 0, $length)); |
||
| 134 | if (in_array($subversion, $allVersions)) { |
||
| 135 | $existingVersions[] = $subversion; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | return $existingVersions; |
||
| 140 | }; |
||
| 141 | |||
| 142 | // init chains with starting versions |
||
| 143 | $chains = array_map(function ($version) use ($buildVersion) { |
||
| 144 | return [$version => $buildVersion]; |
||
| 145 | }, $getExistingSubversions($buildVersion)); |
||
| 146 | |||
| 147 | // finish early if starting / ending version doesn't exist |
||
| 148 | if (!$chains || !in_array($upgradeTo, $allVersions)) { |
||
| 149 | return []; |
||
| 150 | } |
||
| 151 | |||
| 152 | // gets last key of assoc array |
||
| 153 | $getLastKey = function ($array) { |
||
| 154 | end($array); |
||
| 155 | |||
| 156 | return key($array); |
||
| 157 | }; |
||
| 158 | |||
| 159 | // find all chains |
||
| 160 | while (true) { |
||
| 161 | $fullChains = []; |
||
| 162 | foreach ($chains as $index => $chain) { |
||
| 163 | $fromVersion = $getLastKey($chain); |
||
| 164 | |||
| 165 | // skip not interesting chains |
||
| 166 | if (version_compare($fromVersion, $upgradeTo, '>=')) { |
||
| 167 | continue; |
||
| 168 | } |
||
| 169 | |||
| 170 | $validChain = false; |
||
| 171 | foreach ($allVersions as $version) { |
||
| 172 | if (!empty($versionMatrix[$fromVersion][$version])) { |
||
| 173 | $to = $getExistingSubversions($version); |
||
| 174 | foreach ($to as $toVersion) { |
||
| 175 | |||
| 176 | if ($toVersion === $fromVersion |
||
| 177 | || version_compare($toVersion, $upgradeTo, '>') |
||
| 178 | || $chain[$getLastKey($chain)] === $version |
||
| 179 | ) { |
||
| 180 | continue; |
||
| 181 | } |
||
| 182 | |||
| 183 | $validChain = true; |
||
| 184 | $fullChains[] = array_merge($chain, [$toVersion => $version]); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | // remove invalid chain |
||
| 190 | if (!$validChain) { |
||
| 191 | unset($chains[$index]); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | if (!$fullChains) { |
||
| 196 | break; |
||
| 197 | } |
||
| 198 | |||
| 199 | $chains = $fullChains; |
||
| 200 | } |
||
| 201 | |||
| 202 | $chains = array_map(function ($chain) use ($versionMatrix) { |
||
| 203 | $keys = array_keys($chain); |
||
| 204 | $values = array_values($chain); |
||
| 205 | |||
| 206 | $packages = []; |
||
| 207 | foreach (range(1, count($keys) - 1) as $index) { |
||
| 208 | $packages[] = $versionMatrix[$keys[$index - 1]][$values[$index]]; |
||
| 209 | } |
||
| 210 | |||
| 211 | return $packages; |
||
| 212 | }, array_values($chains)); |
||
| 213 | |||
| 214 | return $chains; |
||
| 215 | } |
||
| 216 | |||
| 254 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.