Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FleetValidator 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 FleetValidator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class FleetValidator { |
||
| 7 | /** |
||
| 8 | * @var Fleet $fleet |
||
| 9 | */ |
||
| 10 | protected $fleet; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * FleetValidator constructor. |
||
| 14 | * |
||
| 15 | * @param Fleet $fleet |
||
| 16 | */ |
||
| 17 | public function __construct($fleet) { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * |
||
| 23 | */ |
||
| 24 | public function validate() { |
||
| 25 | $checklist = sn_get_groups('mission_checks'); |
||
| 26 | |||
| 27 | $this->checkMissionRestrictions($checklist); |
||
| 28 | |||
| 29 | // try { |
||
|
|
|||
| 30 | // // TODO - Do the restrictMission checks |
||
| 31 | // |
||
| 32 | // // TODO - Кое-какие проверки дают FLIGHT_ALLOWED - ЧТО НЕПРАВДА В ДАННОМ СЛУЧАЕ!!! |
||
| 33 | // // На странице 1 некоторые проверки ДОЛЖНЫ БЫТЬ опущены - иначе будет некрасиво |
||
| 34 | // // А вот здесь надо проверять много дополнительной хуйни |
||
| 35 | // $this->checkMissionRestrictions($checklist); |
||
| 36 | ////pdump('passed'); |
||
| 37 | // |
||
| 38 | // // 2nd level restrictions |
||
| 39 | // // Still cheap |
||
| 40 | //// $this->restrict2ToAllowedMissions(); |
||
| 41 | //// $this->restrict2ToAllowedPlanetTypes(); |
||
| 42 | // } catch (ExceptionFleetInvalid $e) { |
||
| 43 | ////pdump($e->getCode(), '$e->getCode()'); |
||
| 44 | ////pdump($e->getMessage(), '$e->getMessage()'); |
||
| 45 | // if ($e->getCode() != FLIGHT_ALLOWED) { |
||
| 46 | // pdie(classLocale::$lang['fl_attack_error'][$e->getCode()]); |
||
| 47 | // } else { |
||
| 48 | // pdump('FLIGHT_ALLOWED', FLIGHT_ALLOWED); |
||
| 49 | // } |
||
| 50 | // } |
||
| 51 | pdump('// TODO - Сделать flletvalidator DI - внутре контейнер для методов, а методы - анонимные функции, вызывающие другие методы же', FLIGHT_ALLOWED); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param array $checklist |
||
| 56 | * |
||
| 57 | * @throws Exception |
||
| 58 | */ |
||
| 59 | public function checkMissionRestrictions($checklist) { |
||
| 60 | foreach ($checklist as $condition => $action) { |
||
| 61 | |||
| 62 | $checkResult = call_user_func(array($this, $condition)); |
||
| 63 | pdump($action, $condition . ' ' . ($checkResult ? 'TRUE' : 'FALSE')); |
||
| 64 | |||
| 65 | // // Simple action on failed check |
||
| 66 | // if(!is_array($action)) { |
||
| 67 | // if(!$checkResult) { |
||
| 68 | // throw new ExceptionFleetInvalid($action, $action); |
||
| 69 | // } else { |
||
| 70 | // continue; |
||
| 71 | // } |
||
| 72 | // } |
||
| 73 | // If check failed and there no alternative actions - throw exception |
||
| 74 | // Shortcut ACTION => FAIL_STATUS instead of ACTION => array(false => FAIL_STATUS) |
||
| 75 | if (!$checkResult && !is_array($action)) { |
||
| 76 | throw new ExceptionFleetInvalid($action, $action); |
||
| 77 | } |
||
| 78 | |||
| 79 | // If no actions on current result - just skipping to next condition |
||
| 80 | if (!isset($action[$checkResult])) { |
||
| 81 | // If not - just continuing |
||
| 82 | continue; |
||
| 83 | } |
||
| 84 | |||
| 85 | // Otherwise - we got some action for current result |
||
| 86 | $action = $action[$checkResult]; |
||
| 87 | |||
| 88 | // Is it a list of conditions? |
||
| 89 | if (is_array($action)) { |
||
| 90 | // Yes - performing condition check |
||
| 91 | $this->checkMissionRestrictions($action); |
||
| 92 | } else { |
||
| 93 | // No - then just performing action |
||
| 94 | throw new ExceptionFleetInvalid($action, $action); |
||
| 95 | } |
||
| 96 | |||
| 97 | |||
| 98 | // // Is there some alternatives? |
||
| 99 | // if (is_array($action)) { |
||
| 100 | // if (!empty($action[$checkResult])) { |
||
| 101 | // // Now action is selected alternative |
||
| 102 | // $action = $action[$checkResult]; |
||
| 103 | // } else { |
||
| 104 | // continue; |
||
| 105 | // } |
||
| 106 | // // No alternatives - just action |
||
| 107 | // } elseif (!$checkResult) { |
||
| 108 | // // Action launched if check failed |
||
| 109 | // |
||
| 110 | // throw new ExceptionFleetInvalid($action, $action); |
||
| 111 | // } |
||
| 112 | // |
||
| 113 | // // Here we got action that should be processed |
||
| 114 | // |
||
| 115 | ////pdump($action, $condition); |
||
| 116 | // |
||
| 117 | ////var_dump($checkResult); |
||
| 118 | // // Is new action an array - i.e. list of other checks? |
||
| 119 | // if (is_array($action)) { |
||
| 120 | // // Yes - performing check |
||
| 121 | // $this->checkMissionRestrictions($action); |
||
| 122 | // } else { |
||
| 123 | //// if (!$checkResult) |
||
| 124 | // { |
||
| 125 | // throw new ExceptionFleetInvalid($action, $action); |
||
| 126 | // } |
||
| 127 | // } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * @throws Exception |
||
| 134 | */ |
||
| 135 | protected function restrict2ToAllowedMissions() { |
||
| 136 | if (empty($this->fleet->allowed_missions[$this->fleet->mission_type])) { |
||
| 137 | throw new Exception('FLIGHT_MISSION_IMPOSSIBLE', FLIGHT_MISSION_IMPOSSIBLE); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @throws Exception |
||
| 143 | */ |
||
| 144 | protected function restrict2ToAllowedPlanetTypes() { |
||
| 145 | if (empty($this->fleet->allowed_planet_types[$this->fleet->targetVector->type])) { |
||
| 146 | throw new Exception('FLIGHT_MISSION_IMPOSSIBLE', FLIGHT_MISSION_IMPOSSIBLE); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | protected function checkSpeedPercentOld() { |
||
| 155 | return in_array($this->fleet->oldSpeedInTens, array(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return bool |
||
| 160 | */ |
||
| 161 | protected function checkSenderNoVacation() { |
||
| 162 | return empty($this->fleet->dbOwnerRow['vacation']) || $this->fleet->dbOwnerRow['vacation'] <= SN_TIME_NOW; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | protected function checkTargetNoVacation() { |
||
| 169 | return empty($this->fleet->dbTargetOwnerRow['vacation']) || $this->fleet->dbTargetOwnerRow['vacation'] >= SN_TIME_NOW; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | protected function checkMultiAccountNot() { |
||
| 176 | return !sys_is_multiaccount($this->fleet->dbOwnerRow, $this->fleet->dbTargetOwnerRow); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | protected function checkTargetNotSource() { |
||
| 183 | return !$this->fleet->targetVector->isSameLocation($this->fleet->dbSourcePlanetRow); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return bool |
||
| 188 | */ |
||
| 189 | protected function checkTargetInUniverse() { |
||
| 190 | return $this->fleet->targetVector->isInUniverse(); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | protected function checkUnitsPositive() { |
||
| 197 | return $this->fleet->shipsAllPositive(); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | protected function checkOnlyFleetUnits() { |
||
| 204 | return $this->fleet->shipsAllFlying(); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return bool |
||
| 209 | */ |
||
| 210 | protected function checkOnlyFlyingUnits() { |
||
| 211 | return $this->fleet->shipsAllMovable(); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | protected function checkEnoughFleetSlots() { |
||
| 218 | return FleetList::fleet_count_flying($this->fleet->getPlayerOwnerId()) < GetMaxFleets($this->fleet->dbOwnerRow); |
||
| 219 | } |
||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * @return bool |
||
| 224 | */ |
||
| 225 | protected function checkEnoughCapacity($includeResources = true) { |
||
| 226 | $checkVia = $this->fleet->travelData['consumption']; |
||
| 227 | $checkVia = ceil(($includeResources ? array_sum($this->fleet->resource_list) : 0) + $checkVia); |
||
| 228 | |||
| 229 | return |
||
| 230 | !empty($this->fleet->travelData) && |
||
| 231 | is_array($this->fleet->travelData) && |
||
| 232 | floor($this->fleet->travelData['capacity']) >= $checkVia; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | protected function checkNotTooFar() { |
||
| 239 | return $this->checkEnoughCapacity(false); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | protected function checkDebrisExists() { |
||
| 246 | return is_array($this->fleet->dbTargetRow) && ($this->fleet->dbTargetRow['debris_metal'] + $this->fleet->dbTargetRow['debris_crystal'] > 0); |
||
| 247 | } |
||
| 248 | |||
| 249 | |||
| 250 | |||
| 251 | |||
| 252 | |||
| 253 | |||
| 254 | |||
| 255 | |||
| 256 | |||
| 257 | |||
| 258 | |||
| 259 | |||
| 260 | // Resources checks |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return bool |
||
| 264 | */ |
||
| 265 | protected function checkResourcesPositive() { |
||
| 266 | foreach ($this->fleet->resource_list as $resourceId => $resourceAmount) { |
||
| 267 | if ($resourceAmount < 0) { |
||
| 268 | return false; |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | return true; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | protected function checkCargo() { |
||
| 279 | return array_sum($this->fleet->resource_list) >= 1; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | protected function checkSourceEnoughFuel() { |
||
| 286 | $deuteriumOnPlanet = mrc_get_level($this->fleet->dbOwnerRow, $this->fleet->dbSourcePlanetRow, RES_DEUTERIUM); |
||
| 287 | |||
| 288 | return $deuteriumOnPlanet > ceil($this->fleet->travelData['consumption']); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @return bool |
||
| 293 | */ |
||
| 294 | protected function checkSourceEnoughResources() { |
||
| 295 | $fleetResources = $this->fleet->resource_list; |
||
| 296 | $fleetResources[RES_DEUTERIUM] = ceil($fleetResources[RES_DEUTERIUM] + $this->fleet->travelData['consumption']); |
||
| 297 | foreach ($fleetResources as $resourceId => $resourceAmount) { |
||
| 298 | if (mrc_get_level($this->fleet->dbOwnerRow, $this->fleet->dbSourcePlanetRow, $resourceId) < ceil($fleetResources[$resourceId])) { |
||
| 299 | return false; |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | return true; |
||
| 304 | } |
||
| 305 | |||
| 306 | |||
| 307 | |||
| 308 | |||
| 309 | |||
| 310 | |||
| 311 | |||
| 312 | |||
| 313 | |||
| 314 | |||
| 315 | |||
| 316 | |||
| 317 | |||
| 318 | |||
| 319 | |||
| 320 | |||
| 321 | |||
| 322 | |||
| 323 | |||
| 324 | |||
| 325 | |||
| 326 | |||
| 327 | |||
| 328 | |||
| 329 | |||
| 330 | // Target vector checks (????????) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return bool |
||
| 334 | */ |
||
| 335 | protected function checkKnownSpace() { |
||
| 336 | return $this->fleet->targetVector->isInKnownSpace(); |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | protected function checkTargetExists() { |
||
| 343 | return !empty($this->fleet->dbTargetRow['id']); |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @return bool |
||
| 348 | */ |
||
| 349 | protected function checkTargetIsPlanet() { |
||
| 350 | return $this->fleet->targetVector->type == PT_PLANET; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | protected function checkTargetIsDebris() { |
||
| 357 | return $this->fleet->targetVector->type == PT_DEBRIS; |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @return bool |
||
| 362 | */ |
||
| 363 | protected function checkTargetIsMoon() { |
||
| 364 | return $this->fleet->targetVector->type == PT_MOON; |
||
| 365 | } |
||
| 366 | |||
| 367 | |||
| 368 | |||
| 369 | |||
| 370 | |||
| 371 | |||
| 372 | // Ships checks |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @return bool |
||
| 376 | */ |
||
| 377 | protected function checkFleetNotEmpty() { |
||
| 378 | return $this->fleet->shipsGetTotal() >= 1; |
||
| 379 | } |
||
| 380 | |||
| 381 | |||
| 382 | /** |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | protected function checkSourceEnoughShips() { |
||
| 386 | return $this->fleet->shipsIsEnoughOnPlanet(); |
||
| 387 | } |
||
| 388 | |||
| 389 | |||
| 390 | /** |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | protected function checkHaveColonizer() { |
||
| 394 | // Colonization fleet should have at least one colonizer |
||
| 395 | return $this->fleet->shipsGetTotalById(SHIP_COLONIZER) >= 1; |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @return bool |
||
| 400 | */ |
||
| 401 | protected function checkHaveRecyclers() { |
||
| 402 | $recyclers = 0; |
||
| 403 | foreach (Fleet::$snGroupRecyclers as $recycler_id) { |
||
| 404 | $recyclers += $this->fleet->shipsGetTotalById($recycler_id); |
||
| 405 | } |
||
| 406 | |||
| 407 | return $recyclers >= 1; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | // TODO - used only as callable. Redo inversion |
||
| 414 | protected function checkSpiesOnly() { |
||
| 415 | return $this->fleet->shipsGetTotalById(SHIP_SPY) == $this->fleet->shipsGetTotal(); |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @return bool |
||
| 420 | */ |
||
| 421 | protected function checkNotOnlySpies() { |
||
| 422 | return !$this->checkSpiesOnly(); |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @return bool |
||
| 427 | */ |
||
| 428 | protected function checkNoMissiles() { |
||
| 429 | return |
||
| 430 | $this->fleet->shipsGetTotalById(UNIT_DEF_MISSILE_INTERPLANET) == 0 |
||
| 431 | && |
||
| 432 | $this->fleet->shipsGetTotalById(UNIT_DEF_MISSILE_INTERCEPTOR) == 0; |
||
| 433 | } |
||
| 434 | |||
| 435 | |||
| 436 | /** |
||
| 437 | * @return bool |
||
| 438 | */ |
||
| 439 | protected function checkTargetOwn() { |
||
| 440 | return $this->fleet->dbTargetRow['id_owner'] == $this->fleet->dbSourcePlanetRow['id_owner']; |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @return bool |
||
| 445 | */ |
||
| 446 | protected function forceTargetOwn() { |
||
| 447 | if ($result = $this->checkTargetOwn()) { |
||
| 448 | unset($this->fleet->allowed_missions[MT_EXPLORE]); |
||
| 449 | unset($this->fleet->allowed_missions[MT_COLONIZE]); |
||
| 450 | unset($this->fleet->allowed_missions[MT_RECYCLE]); |
||
| 451 | unset($this->fleet->allowed_missions[MT_MISSILE]); |
||
| 452 | |||
| 453 | unset($this->fleet->allowed_missions[MT_SPY]); |
||
| 454 | |||
| 455 | unset($this->fleet->allowed_missions[MT_ATTACK]); |
||
| 456 | unset($this->fleet->allowed_missions[MT_ACS]); |
||
| 457 | unset($this->fleet->allowed_missions[MT_DESTROY]); |
||
| 458 | } else { |
||
| 459 | unset($this->fleet->allowed_missions[MT_RELOCATE]); |
||
| 460 | } |
||
| 461 | |||
| 462 | return $result; |
||
| 463 | } |
||
| 464 | |||
| 465 | protected function checkMissionPeaceful() { |
||
| 466 | return |
||
| 467 | !$this->fleet->mission_type |
||
| 468 | || |
||
| 469 | in_array($this->fleet->mission_type, array( |
||
| 470 | MT_HOLD, |
||
| 471 | MT_RELOCATE, |
||
| 472 | MT_TRANSPORT, |
||
| 473 | )); |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @return bool |
||
| 478 | */ |
||
| 479 | protected function checkTargetOther() { |
||
| 480 | return !$this->checkTargetOwn(); |
||
| 481 | } |
||
| 482 | |||
| 483 | |||
| 484 | /** |
||
| 485 | * @return bool |
||
| 486 | */ |
||
| 487 | protected function alwaysFalse() { |
||
| 488 | return false; |
||
| 489 | } |
||
| 490 | |||
| 491 | |||
| 492 | /** |
||
| 493 | * @return bool |
||
| 494 | */ |
||
| 495 | protected function checkTargetAllyDeposit() { |
||
| 496 | $result = mrc_get_level($this->fleet->dbTargetOwnerRow, $this->fleet->dbTargetRow, STRUC_ALLY_DEPOSIT) >= 1; |
||
| 497 | if (!$result) { |
||
| 498 | unset($this->fleet->allowed_missions[MT_HOLD]); |
||
| 499 | } |
||
| 500 | |||
| 501 | return $result; |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @param int $missionType |
||
| 506 | * @param bool $exact |
||
| 507 | * |
||
| 508 | * @return bool |
||
| 509 | */ |
||
| 510 | protected function checkMission($missionType, $exact = false) { |
||
| 511 | return $this->fleet->mission_type == $missionType || (!$exact && $this->fleet->mission_type == MT_NONE); |
||
| 512 | } |
||
| 513 | |||
| 514 | |||
| 515 | /** |
||
| 516 | * Check mission type OR no mission - and limits available missions to this type if positive |
||
| 517 | * |
||
| 518 | * @param int $missionType |
||
| 519 | * |
||
| 520 | * @return bool |
||
| 521 | */ |
||
| 522 | protected function forceMission($missionType, $exact = false) { |
||
| 523 | return $this->unsetMission($missionType, $this->checkMission($missionType, $exact), true); |
||
| 524 | } |
||
| 525 | |||
| 526 | protected function unsetMission($missionType, $result, $forceMission = false) { |
||
| 527 | if (!$result) { |
||
| 528 | unset($this->fleet->allowed_missions[$missionType]); |
||
| 529 | } elseif ($forceMission) { |
||
| 530 | $this->fleet->allowed_missions = array( |
||
| 531 | $missionType => $this->fleet->exists_missions[$missionType], |
||
| 532 | ); |
||
| 533 | } |
||
| 534 | |||
| 535 | return $result; |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @param string $name |
||
| 540 | * @param string $prefix |
||
| 541 | * |
||
| 542 | * @return int|false |
||
| 543 | * @throws Exception |
||
| 544 | */ |
||
| 545 | protected function checkMissionPrefix($name, $prefix) { |
||
| 546 | $result = false; |
||
| 547 | if (strpos($name, $prefix) === 0) { |
||
| 548 | $mission = 'MT_' . strtoupper(substr($name, strlen($prefix))); |
||
| 549 | if (!defined($mission)) { |
||
| 550 | // TODO - Ну, как-то получше это обделать |
||
| 551 | throw new Exception('Mission type "' . $mission . '" is not defined'); |
||
| 552 | } |
||
| 553 | |||
| 554 | $result = constant($mission); |
||
| 555 | } |
||
| 556 | |||
| 557 | return $result; |
||
| 558 | } |
||
| 559 | |||
| 560 | public function __call($name, $arguments) { |
||
| 561 | $result = null; |
||
| 562 | if (($missionType = $this->checkMissionPrefix($name, 'unsetMission')) !== false) { |
||
| 563 | $result = $this->unsetMission($missionType, false); |
||
| 564 | } elseif (($missionType = $this->checkMissionPrefix($name, 'forceMissionExact')) !== false) { |
||
| 565 | $result = $this->forceMission($missionType, true); |
||
| 566 | } elseif (($missionType = $this->checkMissionPrefix($name, 'forceMission')) !== false) { |
||
| 567 | $result = $this->forceMission($missionType, false); |
||
| 568 | } elseif (($missionType = $this->checkMissionPrefix($name, 'checkMissionExact')) !== false) { |
||
| 569 | $result = $this->checkMission($missionType, true); |
||
| 570 | } elseif (($missionType = $this->checkMissionPrefix($name, 'checkMission')) !== false) { |
||
| 571 | $result = $this->checkMission($missionType, false); |
||
| 572 | } |
||
| 573 | |||
| 574 | return $result; |
||
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Just checks mission type |
||
| 579 | * |
||
| 580 | * @param int $missionType |
||
| 581 | * |
||
| 582 | * @return bool |
||
| 583 | */ |
||
| 584 | // TODO - obsolete ?? |
||
| 585 | protected function checkMissionExact($missionType) { |
||
| 586 | return $this->checkMission($missionType, true); |
||
| 587 | } |
||
| 588 | |||
| 589 | |||
| 590 | /** |
||
| 591 | * @return bool |
||
| 592 | */ |
||
| 593 | protected function checkNotEmptyMission() { |
||
| 594 | return !empty($this->fleet->mission_type); |
||
| 595 | } |
||
| 596 | |||
| 597 | /** |
||
| 598 | * @return bool |
||
| 599 | */ |
||
| 600 | protected function checkRealFlight() { |
||
| 601 | return $this->fleet->isRealFlight; |
||
| 602 | } |
||
| 603 | |||
| 604 | |||
| 605 | /** |
||
| 606 | * @return bool |
||
| 607 | */ |
||
| 608 | protected function unsetMissionSpyComplex() { |
||
| 619 | |||
| 620 | |||
| 621 | /** |
||
| 622 | * @return bool |
||
| 623 | */ |
||
| 624 | protected function checkMissionExists() { |
||
| 627 | |||
| 628 | /** |
||
| 629 | * @return bool |
||
| 630 | */ |
||
| 631 | protected function checkMissionAllowed() { |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @return bool |
||
| 637 | */ |
||
| 638 | protected function checkPlayerInactiveOrNotNoob() { |
||
| 644 | |||
| 645 | /** |
||
| 646 | * @return bool |
||
| 647 | */ |
||
| 648 | protected function checkTargetActive() { |
||
| 654 | |||
| 655 | /** |
||
| 656 | * @return bool |
||
| 657 | */ |
||
| 658 | // TODO - REDO MAIN FUNCTION |
||
| 659 | protected function checkTargetNotActive() { |
||
| 662 | |||
| 663 | |||
| 664 | /** |
||
| 665 | * @return bool |
||
| 666 | */ |
||
| 667 | protected function checkSameAlly() { |
||
| 670 | |||
| 671 | /** |
||
| 672 | * @return bool |
||
| 673 | */ |
||
| 674 | protected function checkTargetNoob() { |
||
| 692 | |||
| 693 | /** |
||
| 694 | * @return bool |
||
| 695 | */ |
||
| 696 | // TODO - REDO MAIN FUNCTION |
||
| 697 | protected function checkTargetNotNoob() { |
||
| 700 | |||
| 701 | |||
| 702 | /** |
||
| 703 | * @return bool |
||
| 704 | */ |
||
| 705 | protected function checkMissionHoldReal() { |
||
| 706 | return |
||
| 707 | $this->checkRealFlight() |
||
| 708 | && |
||
| 709 | $this->checkMissionExact(MT_HOLD); |
||
| 710 | } |
||
| 711 | |||
| 712 | /** |
||
| 713 | * @return bool |
||
| 714 | */ |
||
| 715 | protected function checkMissionHoldOnNotNoob() { |
||
| 723 | |||
| 724 | |||
| 725 | // Missiles |
||
| 726 | |||
| 727 | /** |
||
| 728 | * @return bool |
||
| 729 | */ |
||
| 730 | protected function checkOnlyAttackMissiles() { |
||
| 735 | |||
| 736 | /** |
||
| 737 | * @return bool |
||
| 738 | */ |
||
| 739 | protected function checkSiloLevel() { |
||
| 744 | |||
| 745 | /** |
||
| 746 | * @return bool |
||
| 747 | */ |
||
| 748 | protected function checkSameGalaxy() { |
||
| 751 | |||
| 752 | /** |
||
| 753 | * @return bool |
||
| 754 | */ |
||
| 755 | protected function checkMissileDistance() { |
||
| 758 | |||
| 759 | /** |
||
| 760 | * @return bool |
||
| 761 | */ |
||
| 762 | protected function checkMissileTarget() { |
||
| 765 | |||
| 766 | |||
| 767 | /** |
||
| 768 | * @return int |
||
| 769 | */ |
||
| 770 | protected function checkExpeditionsMax() { |
||
| 773 | |||
| 774 | /** |
||
| 775 | * @return bool |
||
| 776 | */ |
||
| 777 | protected function checkExpeditionsFree() { |
||
| 780 | |||
| 781 | /** |
||
| 782 | * @return bool |
||
| 783 | */ |
||
| 784 | protected function checkCaptainSent() { |
||
| 787 | |||
| 788 | /** |
||
| 789 | * @return bool |
||
| 790 | */ |
||
| 791 | protected function checkCaptainExists() { |
||
| 794 | |||
| 795 | /** |
||
| 796 | * @return bool |
||
| 797 | */ |
||
| 798 | protected function checkCaptainOnPlanetType() { |
||
| 799 | return $this->fleet->captain['unit_location_type'] == $this->fleet->dbSourcePlanetRow['planet_type']; |
||
| 800 | } |
||
| 801 | |||
| 802 | /** |
||
| 803 | * @return bool |
||
| 804 | */ |
||
| 805 | protected function checkCaptainOnPlanetSource() { |
||
| 806 | return $this->checkCaptainOnPlanetType() && $this->fleet->captain['unit_location_id'] == $this->fleet->dbSourcePlanetRow['id']; |
||
| 807 | } |
||
| 808 | |||
| 809 | /** |
||
| 810 | * @return bool |
||
| 811 | */ |
||
| 812 | protected function checkCaptainNotRelocating() { |
||
| 813 | if ($this->fleet->mission_type == MT_RELOCATE) { |
||
| 814 | $arriving_captain = mrc_get_level($this->fleet->dbOwnerRow, $this->fleet->dbTargetRow, UNIT_CAPTAIN, true); |
||
| 815 | } else { |
||
| 816 | $arriving_captain = false; |
||
| 817 | } |
||
| 818 | |||
| 819 | return empty($arriving_captain) || !is_array($arriving_captain); |
||
| 820 | } |
||
| 821 | |||
| 822 | |||
| 823 | /** |
||
| 824 | * @return bool |
||
| 825 | */ |
||
| 826 | protected function checkMissionDestroyReal() { |
||
| 827 | return |
||
| 828 | $this->checkRealFlight() |
||
| 829 | && |
||
| 830 | $this->checkMissionExact(MT_DESTROY); |
||
| 831 | } |
||
| 832 | |||
| 833 | /** |
||
| 834 | * @return bool |
||
| 835 | */ |
||
| 836 | protected function checkHaveReapers() { |
||
| 837 | $unitsTyped = 0; |
||
| 838 | foreach (sn_get_groups('flt_reapers') as $unit_id) { |
||
| 839 | $unitsTyped += $this->fleet->shipsGetTotalById($unit_id); |
||
| 840 | } |
||
| 841 | |||
| 842 | return $unitsTyped >= 1; |
||
| 843 | } |
||
| 844 | |||
| 845 | |||
| 846 | /** |
||
| 847 | * @return bool |
||
| 848 | */ |
||
| 849 | protected function checkMissionACSReal() { |
||
| 855 | |||
| 856 | protected function checkACSInTime() { |
||
| 859 | |||
| 860 | |||
| 861 | protected function checkMissionRealAndSelected($missionType) { |
||
| 867 | |||
| 868 | protected function checkMissionResultAndUnset($missionType, $result, $forceMission = false) { |
||
| 873 | |||
| 874 | |||
| 875 | /** |
||
| 876 | * @return bool |
||
| 877 | */ |
||
| 878 | protected function checkMissionSpyPossibleAndReal() { |
||
| 885 | |||
| 886 | /** |
||
| 887 | * @return bool |
||
| 888 | */ |
||
| 889 | protected function checkMissionDestroyAndReal() { |
||
| 895 | |||
| 896 | /** |
||
| 897 | * @return bool |
||
| 898 | */ |
||
| 899 | protected function checkMissionHoldPossibleAndReal() { |
||
| 905 | |||
| 906 | /** |
||
| 907 | * @return bool |
||
| 908 | */ |
||
| 909 | protected function checkSpiesOnlyFriendlyRestrictsToRelocate() { |
||
| 918 | |||
| 919 | |||
| 920 | protected function checkFleetGroupACS() { |
||
| 931 | |||
| 932 | protected function checkACSNotEmpty() { |
||
| 935 | |||
| 936 | /** |
||
| 937 | * @return bool |
||
| 938 | */ |
||
| 939 | protected function checkACSInvited() { |
||
| 949 | |||
| 950 | /** |
||
| 951 | * @return bool |
||
| 952 | */ |
||
| 953 | protected function checkMissionACSPossibleAndReal() { |
||
| 960 | |||
| 961 | /** |
||
| 962 | * @return bool |
||
| 963 | */ |
||
| 964 | protected function checkMissionTransportPossibleAndReal() { |
||
| 970 | |||
| 971 | /** |
||
| 972 | * @return bool |
||
| 973 | */ |
||
| 974 | protected function checkMissionTransportReal() { |
||
| 980 | |||
| 981 | |||
| 982 | protected function checkBashingNotRestricted() { |
||
| 985 | |||
| 986 | protected function checkBashingBothAllies() { |
||
| 989 | |||
| 990 | protected function checkBashingAlliesHaveRelationWar() { |
||
| 993 | |||
| 994 | protected function checkBashingBothAlliesAndRelationWar() { |
||
| 997 | |||
| 998 | protected function checkBashingAlliesWarNoDelay() { |
||
| 1006 | |||
| 1007 | |||
| 1008 | protected function checkBashingNone() { |
||
| 1056 | |||
| 1057 | } |
||
| 1058 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.