We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 86 |
| Total Lines | 351 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SmrGalaxy often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SmrGalaxy, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 2 | class SmrGalaxy { |
||
| 3 | protected static $CACHE_GALAXIES = array(); |
||
| 4 | protected static $CACHE_GAME_GALAXIES = array(); |
||
| 5 | |||
| 6 | const TYPES = ['Racial', 'Neutral', 'Planet']; |
||
| 7 | |||
| 8 | protected $db; |
||
| 9 | protected $SQL; |
||
| 10 | |||
| 11 | protected $gameID; |
||
| 12 | protected $galaxyID; |
||
| 13 | protected $name; |
||
| 14 | protected $width; |
||
| 15 | protected $height; |
||
| 16 | protected $galaxyType; |
||
| 17 | protected $maxForceTime; |
||
| 18 | |||
| 19 | protected $startSector = false; |
||
| 20 | |||
| 21 | protected $hasChanged = false; |
||
| 22 | protected $isNew = false; |
||
| 23 | |||
| 24 | public static function getGameGalaxies($gameID, $forceUpdate = false) { |
||
| 25 | if ($forceUpdate || !isset(self::$CACHE_GAME_GALAXIES[$gameID])) { |
||
| 26 | $db = new SmrMySqlDatabase(); |
||
| 27 | $db->query('SELECT * FROM game_galaxy WHERE game_id = ' . $db->escapeNumber($gameID) . ' ORDER BY galaxy_id ASC'); |
||
| 28 | $galaxies = array(); |
||
| 29 | while ($db->nextRecord()) { |
||
| 30 | $galaxyID = $db->getInt('galaxy_id'); |
||
| 31 | $galaxies[$galaxyID] = self::getGalaxy($gameID, $galaxyID, $forceUpdate, $db); |
||
| 32 | } |
||
| 33 | self::$CACHE_GAME_GALAXIES[$gameID] = $galaxies; |
||
| 34 | } |
||
| 35 | return self::$CACHE_GAME_GALAXIES[$gameID]; |
||
| 36 | } |
||
| 37 | |||
| 38 | public static function getGalaxy($gameID, $galaxyID, $forceUpdate = false, $db = null) { |
||
| 39 | if ($forceUpdate || !isset(self::$CACHE_GALAXIES[$gameID][$galaxyID])) { |
||
| 40 | $g = new SmrGalaxy($gameID, $galaxyID, false, $db); |
||
| 41 | self::$CACHE_GALAXIES[$gameID][$galaxyID] = $g; |
||
| 42 | } |
||
| 43 | return self::$CACHE_GALAXIES[$gameID][$galaxyID]; |
||
| 44 | } |
||
| 45 | |||
| 46 | public static function saveGalaxies() { |
||
| 47 | foreach (self::$CACHE_GALAXIES as $gameGalaxies) { |
||
| 48 | foreach ($gameGalaxies as $galaxy) { |
||
| 49 | $galaxy->save(); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | public static function createGalaxy($gameID, $galaxyID) { |
||
| 55 | if (!isset(self::$CACHE_GALAXIES[$gameID][$galaxyID])) { |
||
| 56 | $g = new SmrGalaxy($gameID, $galaxyID, true); |
||
| 57 | self::$CACHE_GALAXIES[$gameID][$galaxyID] = $g; |
||
| 58 | } |
||
| 59 | return self::$CACHE_GALAXIES[$gameID][$galaxyID]; |
||
| 60 | } |
||
| 61 | |||
| 62 | protected function __construct($gameID, $galaxyID, $create = false, $db = null) { |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | public function save() { |
||
| 89 | if ($this->isNew) { |
||
| 90 | $this->db->query('INSERT INTO game_galaxy (game_id,galaxy_id,galaxy_name,width,height,galaxy_type,max_force_time) |
||
| 91 | values |
||
| 92 | (' . $this->db->escapeNumber($this->getGameID()) . |
||
| 93 | ',' . $this->db->escapeNumber($this->getGalaxyID()) . |
||
| 94 | ',' . $this->db->escapeString($this->getName()) . |
||
| 95 | ',' . $this->db->escapeNumber($this->getWidth()) . |
||
| 96 | ',' . $this->db->escapeNumber($this->getHeight()) . |
||
| 97 | ',' . $this->db->escapeString($this->getGalaxyType()) . |
||
| 98 | ',' . $this->db->escapeNumber($this->getMaxForceTime()) . ')'); |
||
| 99 | } elseif ($this->hasChanged) { |
||
| 100 | $this->db->query('UPDATE game_galaxy SET galaxy_name = ' . $this->db->escapeString($this->getName()) . |
||
| 101 | ', width = ' . $this->db->escapeNumber($this->getWidth()) . |
||
| 102 | ', height = ' . $this->db->escapeNumber($this->getHeight()) . |
||
| 103 | ', galaxy_type = ' . $this->db->escapeString($this->getGalaxyType()) . |
||
| 104 | ', max_force_time = ' . $this->db->escapeNumber($this->getMaxForceTime()) . |
||
| 105 | ' WHERE ' . $this->SQL); |
||
| 106 | } |
||
| 107 | $this->isNew = false; |
||
| 108 | $this->hasChanged = false; |
||
| 109 | } |
||
| 110 | |||
| 111 | public function getGameID() { |
||
| 112 | return $this->gameID; |
||
| 113 | } |
||
| 114 | |||
| 115 | public function getGalaxyID() { |
||
| 116 | return $this->galaxyID; |
||
| 117 | } |
||
| 118 | |||
| 119 | public function getGalaxyMapHREF() { |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns the galaxy name. |
||
| 125 | * Use getDisplayName for an HTML-safe version. |
||
| 126 | */ |
||
| 127 | public function getName() { |
||
| 128 | return $this->name; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns the galaxy name, suitable for HTML display. |
||
| 133 | */ |
||
| 134 | public function getDisplayName() : string { |
||
| 135 | return htmlentities($this->name); |
||
| 136 | } |
||
| 137 | |||
| 138 | public function setName($name) { |
||
| 139 | if ($this->name == $name) { |
||
| 140 | return; |
||
| 141 | } |
||
| 142 | $this->name = $name; |
||
| 143 | $this->hasChanged = true; |
||
| 144 | } |
||
| 145 | |||
| 146 | public function getWidth() { |
||
| 147 | return $this->width; |
||
| 148 | } |
||
| 149 | |||
| 150 | public function setWidth($width) { |
||
| 151 | if ($this->width == $width) { |
||
| 152 | return; |
||
| 153 | } |
||
| 154 | $this->width = $width; |
||
| 155 | $this->hasChanged = true; |
||
| 156 | } |
||
| 157 | |||
| 158 | public function getHeight() { |
||
| 159 | return $this->height; |
||
| 160 | } |
||
| 161 | |||
| 162 | public function setHeight($height) { |
||
| 163 | if ($this->height == $height) { |
||
| 164 | return; |
||
| 165 | } |
||
| 166 | $this->height = $height; |
||
| 167 | $this->hasChanged = true; |
||
| 168 | } |
||
| 169 | |||
| 170 | public function getStartSector() { |
||
| 171 | if ($this->startSector === false) { |
||
| 172 | $this->startSector = 1; |
||
| 173 | if ($this->getGalaxyID() != 1) { |
||
| 174 | $galaxies = SmrGalaxy::getGameGalaxies($this->getGameID()); |
||
| 175 | for ($i = 1; $i < $this->getGalaxyID(); $i++) { |
||
| 176 | $this->startSector += $galaxies[$i]->getSize(); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | return $this->startSector; |
||
| 181 | } |
||
| 182 | |||
| 183 | public function getEndSector() { |
||
| 184 | return $this->getStartSector() + $this->getSize() - 1; |
||
| 185 | } |
||
| 186 | |||
| 187 | public function getSize() { |
||
| 188 | return $this->getHeight() * $this->getWidth(); |
||
| 189 | } |
||
| 190 | |||
| 191 | public function getSectors() { |
||
| 192 | return SmrSector::getGalaxySectors($this->getGameID(), $this->getGalaxyID()); |
||
| 193 | } |
||
| 194 | |||
| 195 | public function getPorts() { |
||
| 196 | return SmrPort::getGalaxyPorts($this->getGameID(), $this->getGalaxyID()); |
||
| 197 | } |
||
| 198 | |||
| 199 | public function getLocations() { |
||
| 201 | } |
||
| 202 | |||
| 203 | public function getPlanets() { |
||
| 204 | return SmrPlanet::getGalaxyPlanets($this->getGameID(), $this->getGalaxyID()); |
||
| 205 | } |
||
| 206 | |||
| 207 | public function getForces() { |
||
| 208 | return SmrForce::getGalaxyForces($this->getGameID(), $this->getGalaxyID()); |
||
| 209 | } |
||
| 210 | |||
| 211 | public function getPlayers() { |
||
| 212 | return SmrPlayer::getGalaxyPlayers($this->getGameID(), $this->getGalaxyID()); |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Returns a 2D array of sectors in the galaxy. |
||
| 217 | * If $centerSectorID is specified, it will be in the center of the array. |
||
| 218 | * If $dist is also specified, only include sectors $dist away from center. |
||
| 219 | * |
||
| 220 | * NOTE: This routine queries sectors inefficiently. You may want to |
||
| 221 | * construct the cache efficiently before calling this. |
||
| 222 | */ |
||
| 223 | public function getMapSectors($centerSectorID = false, $dist = false) { |
||
| 224 | if ($centerSectorID === false) { |
||
| 225 | $topLeft = SmrSector::getSector($this->getGameID(), $this->getStartSector()); |
||
| 226 | } else { |
||
| 227 | $topLeft = SmrSector::getSector($this->getGameID(), $centerSectorID); |
||
| 228 | // go left then up |
||
| 229 | for ($i = 0; ($dist === false || $i < $dist) && $i < floor($this->getWidth() / 2); $i++) { |
||
| 230 | $topLeft = $topLeft->getNeighbourSector('Left'); |
||
| 231 | } |
||
| 232 | for ($i = 0; ($dist === false || $i < $dist) && $i < floor($this->getHeight() / 2); $i++) { |
||
| 233 | $topLeft = $topLeft->getNeighbourSector('Up'); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | $mapSectors = array(); |
||
| 238 | for ($i = 0; ($dist === false || $i < 2 * $dist + 1) && $i < $this->getHeight(); $i++) { |
||
| 239 | $mapSectors[$i] = array(); |
||
| 240 | // get left most sector for this row |
||
| 241 | $rowLeft = $i == 0 ? $topLeft : $rowLeft->getNeighbourSector('Down'); |
||
|
|
|||
| 242 | |||
| 243 | // iterate through the columns |
||
| 244 | for ($j = 0; ($dist === false || $j < 2 * $dist + 1) && $j < $this->getWidth(); $j++) { |
||
| 245 | $nextSec = $j == 0 ? $rowLeft : $nextSec->getNeighbourSector('Right'); |
||
| 246 | $mapSectors[$i][$j] = $nextSec; |
||
| 247 | } |
||
| 248 | } |
||
| 249 | return $mapSectors; |
||
| 250 | } |
||
| 251 | |||
| 252 | public function getGalaxyType() { |
||
| 253 | return $this->galaxyType; |
||
| 254 | } |
||
| 255 | |||
| 256 | public function setGalaxyType($galaxyType) { |
||
| 257 | if ($this->galaxyType == $galaxyType) { |
||
| 258 | return; |
||
| 259 | } |
||
| 260 | $this->galaxyType = $galaxyType; |
||
| 261 | $this->hasChanged = true; |
||
| 262 | } |
||
| 263 | |||
| 264 | public function getMaxForceTime() { |
||
| 265 | return $this->maxForceTime; |
||
| 266 | } |
||
| 267 | |||
| 268 | public function setMaxForceTime($maxForceTime) { |
||
| 269 | if ($this->maxForceTime == $maxForceTime) { |
||
| 270 | return; |
||
| 271 | } |
||
| 272 | $this->maxForceTime = $maxForceTime; |
||
| 273 | $this->hasChanged = true; |
||
| 274 | } |
||
| 275 | |||
| 276 | public function generateSectors() { |
||
| 277 | $sectorID = $this->getStartSector(); |
||
| 278 | for ($i = 0; $i < $this->getSize(); $i++) { |
||
| 279 | $sector = SmrSector::createSector($this->gameID, $sectorID); |
||
| 280 | $sector->setGalaxyID($this->getGalaxyID()); |
||
| 281 | $sector->update(); //Have to save sectors after creating them |
||
| 282 | $sectorID++; |
||
| 283 | } |
||
| 284 | $this->setConnectivity(100); |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Randomly set the connections between all galaxy sectors. |
||
| 289 | * $connectivity = (average) percent of connections to enable. |
||
| 290 | */ |
||
| 291 | public function setConnectivity($connectivity) { |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Returns the sector connectivity of the galaxy as a percent. |
||
| 329 | */ |
||
| 330 | public function getConnectivity() { |
||
| 338 | } |
||
| 339 | |||
| 340 | public function contains($sectorID) { |
||
| 341 | if ($sectorID instanceof SmrSector) { |
||
| 342 | return $sectorID->getGalaxyID() == $this->getGalaxyID(); |
||
| 343 | } |
||
| 344 | return $sectorID >= $this->getStartSector() && $sectorID <= $this->getEndSector(); |
||
| 345 | } |
||
| 346 | |||
| 347 | public static function getGalaxyContaining($gameID, $sectorID) { |
||
| 348 | return SmrSector::getSector($gameID, $sectorID)->getGalaxy(); |
||
| 349 | } |
||
| 350 | |||
| 351 | public function equals(SmrGalaxy $otherGalaxy) { |
||
| 353 | } |
||
| 354 | } |
||
| 355 |