We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 36 |
| Paths | 9 |
| Total Lines | 154 |
| Code Lines | 102 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 declare(strict_types=1); |
||
| 92 | public function build(SmrAccount $account): never { |
||
| 93 | $submit = Request::get('submit'); |
||
| 94 | |||
| 95 | if ($submit == 'Redo Connections') { |
||
| 96 | $galaxy = SmrGalaxy::getGalaxy($this->gameID, $this->galaxyID); |
||
| 97 | $connectivity = Request::getFloat('connect'); |
||
| 98 | if (!$galaxy->setConnectivity($connectivity)) { |
||
| 99 | $message = '<span class="red">Error</span> : Regenerating connections failed.'; |
||
| 100 | } else { |
||
| 101 | $message = '<span class="green">Success</span> : Regenerated connectivity with ' . $connectivity . '% target.'; |
||
| 102 | } |
||
| 103 | SmrSector::saveSectors(); |
||
| 104 | } elseif ($submit == 'Create Locations') { |
||
| 105 | $galSectors = SmrSector::getGalaxySectors($this->gameID, $this->galaxyID); |
||
| 106 | foreach ($galSectors as $galSector) { |
||
| 107 | $galSector->removeAllLocations(); |
||
| 108 | } |
||
| 109 | foreach (SmrLocation::getAllLocations($this->gameID) as $location) { |
||
| 110 | if (Request::has('loc' . $location->getTypeID())) { |
||
| 111 | $numLoc = Request::getInt('loc' . $location->getTypeID()); |
||
| 112 | for ($i = 0; $i < $numLoc; $i++) { |
||
| 113 | //4 per sector max locs and no locations inside fed |
||
| 114 | $randSector = findValidSector( |
||
| 115 | $galSectors, |
||
| 116 | fn(SmrSector $sector): bool => checkSectorAllowedForLoc($sector, $location) |
||
| 117 | ); |
||
| 118 | addLocationToSector($location, $randSector); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | $message = '<span class="green">Success</span> : Succesfully added locations.'; |
||
| 123 | } elseif ($submit == 'Create Warps') { |
||
| 124 | //get all warp info from all gals, some need to be removed, some need to be added |
||
| 125 | $galaxy = SmrGalaxy::getGalaxy($this->gameID, $this->galaxyID); |
||
| 126 | $galSectors = $galaxy->getSectors(); |
||
| 127 | //get totals |
||
| 128 | foreach ($galSectors as $galSector) { |
||
| 129 | if ($galSector->hasWarp()) { |
||
| 130 | $galSector->removeWarp(); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | //iterate over all the galaxies |
||
| 134 | $galaxies = SmrGalaxy::getGameGalaxies($this->gameID); |
||
| 135 | foreach ($galaxies as $eachGalaxy) { |
||
| 136 | //do we have a warp to this gal? |
||
| 137 | if (Request::has('warp' . $eachGalaxy->getGalaxyID())) { |
||
| 138 | // Sanity check the number |
||
| 139 | $numWarps = Request::getInt('warp' . $eachGalaxy->getGalaxyID()); |
||
| 140 | if ($numWarps > 10) { |
||
| 141 | create_error('Specify no more than 10 warps between two galaxies!'); |
||
| 142 | } |
||
| 143 | //iterate for each warp to this gal |
||
| 144 | for ($i = 1; $i <= $numWarps; $i++) { |
||
| 145 | //only 1 warp per sector |
||
| 146 | $galSector = findValidSector( |
||
| 147 | $galSectors, |
||
| 148 | fn(SmrSector $sector): bool => !$sector->hasWarp() && !$sector->offersFederalProtection() |
||
| 149 | ); |
||
| 150 | //get other side |
||
| 151 | //make sure it does not go to itself |
||
| 152 | $otherSector = findValidSector( |
||
| 153 | $eachGalaxy->getSectors(), |
||
| 154 | fn(SmrSector $sector): bool => !$sector->hasWarp() && !$sector->offersFederalProtection() && !$sector->equals($galSector) |
||
| 155 | ); |
||
| 156 | $galSector->setWarp($otherSector); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | SmrSector::saveSectors(); |
||
| 161 | $message = '<span class="green">Success</span> : Succesfully added warps.'; |
||
| 162 | (new CreateWarps($this->gameID, $this->galaxyID, $message))->go(); |
||
| 163 | } elseif ($submit == 'Create Planets') { |
||
| 164 | $galaxy = SmrGalaxy::getGalaxy($this->gameID, $this->galaxyID); |
||
| 165 | $galSectors = $galaxy->getSectors(); |
||
| 166 | foreach ($galSectors as $galSector) { |
||
| 167 | if ($galSector->hasPlanet()) { |
||
| 168 | $galSector->removePlanet(); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | foreach (array_keys(PlanetType::PLANET_TYPES) as $planetTypeID) { |
||
| 173 | $numberOfPlanets = Request::getInt('type' . $planetTypeID); |
||
| 174 | for ($i = 1; $i <= $numberOfPlanets; $i++) { |
||
| 175 | $galSector = findValidSector( |
||
| 176 | $galSectors, |
||
| 177 | fn(SmrSector $sector): bool => !$sector->hasPlanet() // 1 per sector |
||
| 178 | ); |
||
| 179 | $galSector->createPlanet($planetTypeID); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | $message = '<span class="green">Success</span> : Succesfully added planets.'; |
||
| 183 | } elseif ($submit == 'Create Ports') { |
||
| 184 | $numLevelPorts = []; |
||
| 185 | $maxPortLevel = SmrPort::getMaxLevelByGame($this->gameID); |
||
| 186 | for ($i = 1; $i <= $maxPortLevel; $i++) { |
||
| 187 | $numLevelPorts[$i] = Request::getInt('port' . $i); |
||
| 188 | } |
||
| 189 | $totalPorts = array_sum($numLevelPorts); |
||
| 190 | |||
| 191 | $totalRaceDist = 0; |
||
| 192 | $numRacePorts = []; |
||
| 193 | foreach (Race::getAllIDs() as $raceID) { |
||
| 194 | $racePercent = Request::getInt('race' . $raceID); |
||
| 195 | if (!empty($racePercent)) { |
||
| 196 | $totalRaceDist += $racePercent; |
||
| 197 | $numRacePorts[$raceID] = ceil($racePercent / 100 * $totalPorts); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | $assignedPorts = array_sum($numRacePorts); |
||
| 201 | if ($totalRaceDist == 100 || $totalPorts == 0) { |
||
| 202 | $galaxy = SmrGalaxy::getGalaxy($this->gameID, $this->galaxyID); |
||
| 203 | $galSectors = $galaxy->getSectors(); |
||
| 204 | foreach ($galSectors as $galSector) { |
||
| 205 | if ($galSector->hasPort()) { |
||
| 206 | $galSector->removePort(); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | //get race for all ports |
||
| 210 | while ($totalPorts > $assignedPorts) { |
||
| 211 | //this adds extra ports until we reach the requested # |
||
| 212 | $numRacePorts[array_rand($numRacePorts)]++; |
||
| 213 | $assignedPorts++; |
||
| 214 | } |
||
| 215 | //iterate through levels 1-9 port |
||
| 216 | foreach ($numLevelPorts as $portLevel => $numLevel) { |
||
| 217 | //iterate once for each port of this level |
||
| 218 | for ($j = 0; $j < $numLevel; $j++) { |
||
| 219 | //get a sector for this port |
||
| 220 | $galSector = findValidSector( |
||
| 221 | $galSectors, |
||
| 222 | fn(SmrSector $sector): bool => !$sector->hasPort() && !$sector->offersFederalProtection() |
||
| 223 | ); |
||
| 224 | |||
| 225 | $raceID = array_rand($numRacePorts); |
||
| 226 | $numRacePorts[$raceID]--; |
||
| 227 | if ($numRacePorts[$raceID] == 0) { |
||
| 228 | unset($numRacePorts[$raceID]); |
||
| 229 | } |
||
| 230 | $port = $galSector->createPort(); |
||
| 231 | $port->setRaceID($raceID); |
||
| 232 | $port->upgradeToLevel($portLevel); |
||
| 233 | $port->setCreditsToDefault(); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | SmrPort::savePorts(); |
||
| 237 | $message = '<span class="green">Success</span> : Succesfully added ports.'; |
||
| 238 | } else { |
||
| 239 | $message = '<span class="red">Error: Your port race distribution must equal 100!</span>'; |
||
| 240 | } |
||
| 241 | } else { |
||
| 242 | throw new Exception('Unknown submit: ' . $submit); |
||
| 243 | } |
||
| 244 | |||
| 245 | (new EditGalaxy($this->gameID, $this->galaxyID, $message))->go(); |
||
| 246 | } |
||
| 249 |