We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 20 |
Paths | 40 |
Total Lines | 77 |
Code Lines | 54 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php declare(strict_types=1); |
||
109 | public static function findDistanceToX(mixed $x, SmrSector $sector, bool $useFirst, AbstractSmrPlayer $needsToHaveBeenExploredBy = null, AbstractSmrPlayer $player = null, int $distanceLimit = 10000, int $lowLimit = 0, int $highLimit = 100000): array|Path|false { |
||
110 | $warpAddIndex = TURNS_WARP_SECTOR_EQUIVALENCE - 1; |
||
111 | |||
112 | $checkSector = $sector; |
||
113 | $gameID = $sector->getGameID(); |
||
114 | $distances = []; |
||
115 | $sectorsTravelled = 0; |
||
116 | $visitedSectors = []; |
||
117 | $visitedSectors[$checkSector->getSectorID()] = true; |
||
118 | |||
119 | $distanceQ = []; |
||
120 | for ($i = 0; $i <= TURNS_WARP_SECTOR_EQUIVALENCE; $i++) { |
||
121 | $distanceQ[] = []; |
||
122 | } |
||
123 | //Warps first as a slight optimisation due to how visitedSectors is set. |
||
124 | if ($checkSector->hasWarp() === true) { |
||
125 | $d = new Path($checkSector->getSectorID()); |
||
126 | $d->addWarp($checkSector->getWarp()); |
||
127 | $distanceQ[$warpAddIndex][] = $d; |
||
128 | } |
||
129 | foreach ($checkSector->getLinks() as $nextSector) { |
||
130 | $visitedSectors[$nextSector] = true; |
||
131 | $d = new Path($checkSector->getSectorID()); |
||
132 | $d->addLink($nextSector); |
||
133 | $distanceQ[0][] = $d; |
||
134 | } |
||
135 | $maybeWarps = 0; |
||
136 | while ($maybeWarps <= TURNS_WARP_SECTOR_EQUIVALENCE) { |
||
137 | $sectorsTravelled++; |
||
138 | if ($sectorsTravelled > $distanceLimit) { |
||
139 | break; |
||
140 | } |
||
141 | $distanceQ[] = []; |
||
142 | $q = array_shift($distanceQ); |
||
143 | if (count($q) === 0) { |
||
144 | $maybeWarps++; |
||
145 | continue; |
||
146 | } |
||
147 | $maybeWarps = 0; |
||
148 | while (($distance = array_shift($q)) !== null) { |
||
149 | $checkSectorID = $distance->getEndSectorID(); |
||
150 | $visitedSectors[$checkSectorID] = true; // This is here for warps, because they are delayed visits if we set this before the actual visit we'll get sectors marked as visited long before they are actually visited - causes problems when it's quicker to walk to the warp exit than to warp there. |
||
151 | // We still need to mark walked sectors as visited before we go to each one otherwise we get a huge number of paths being checked twice (up then left, left then up are essentially the same but if we set up-left as visited only when we actually check it then it gets queued up twice - nasty) |
||
152 | if ($checkSectorID >= $lowLimit && $checkSectorID <= $highLimit) { |
||
153 | $checkSector = SmrSector::getSector($gameID, $checkSectorID); |
||
154 | // Does this sector satisfy our criteria? |
||
155 | if ($x == 'Distance' || (($needsToHaveBeenExploredBy === null || $needsToHaveBeenExploredBy->hasVisitedSector($checkSector->getSectorID())) === true |
||
156 | && $checkSector->hasX($x, $player) === true)) { |
||
157 | if ($useFirst === true) { |
||
158 | return $distance; |
||
159 | } |
||
160 | $distances[$checkSector->getSectorID()] = $distance; |
||
161 | } |
||
162 | //Warps first as a slight optimisation due to how visitedSectors is set. |
||
163 | if ($checkSector->hasWarp() === true) { |
||
164 | if (!isset($visitedSectors[$checkSector->getWarp()])) { |
||
165 | $cloneDistance = clone($distance); |
||
166 | $cloneDistance->addWarp($checkSector->getWarp()); |
||
167 | $distanceQ[$warpAddIndex][] = $cloneDistance; |
||
168 | } |
||
169 | } |
||
170 | foreach ($checkSector->getLinks() as $nextSector) { |
||
171 | if (!isset($visitedSectors[$nextSector])) { |
||
172 | $visitedSectors[$nextSector] = true; |
||
173 | |||
174 | $cloneDistance = clone($distance); |
||
175 | $cloneDistance->addLink($nextSector); |
||
176 | $distanceQ[0][] = $cloneDistance; |
||
177 | } |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | if ($useFirst === true) { |
||
183 | return false; |
||
184 | } |
||
185 | return $distances; |
||
186 | } |
||
212 |