We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 42 |
| Total Lines | 350 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AbstractMenu 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 AbstractMenu, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 54 | class AbstractMenu { |
||
| 55 | |||
| 56 | public static function headquarters(int $locationTypeID): void { |
||
| 57 | $gameID = Smr\Session::getInstance()->getGameID(); |
||
| 58 | |||
| 59 | $links = []; |
||
| 60 | $location = SmrLocation::getLocation($gameID, $locationTypeID); |
||
| 61 | if ($location->isHQ()) { |
||
| 62 | $links[] = [Government::class, 'Government']; |
||
| 63 | $links[] = [MilitaryPaymentClaimProcessor::class, 'Claim Military Payment']; |
||
| 64 | } elseif ($location->isUG()) { |
||
| 65 | $links[] = [Underground::class, 'Underground']; |
||
| 66 | } else { |
||
| 67 | throw new Exception('Location is not HQ or UG: ' . $location->getName()); |
||
| 68 | } |
||
| 69 | |||
| 70 | // No bounties in Semi Wars games |
||
| 71 | if (!SmrGame::getGame($gameID)->isGameType(SmrGame::GAME_TYPE_SEMI_WARS)) { |
||
| 72 | $links[] = [BountyClaimProcessor::class, 'Claim Bounty']; |
||
| 73 | $links[] = [BountyPlace::class, 'Place Bounty']; |
||
| 74 | } |
||
| 75 | |||
| 76 | $menuItems = []; |
||
| 77 | foreach ($links as [$class, $text]) { |
||
| 78 | $container = new $class($locationTypeID); |
||
| 79 | $menuItems[] = [ |
||
| 80 | 'Link' => $container->href(), |
||
| 81 | 'Text' => $text, |
||
| 82 | ]; |
||
| 83 | } |
||
| 84 | $template = Smr\Template::getInstance(); |
||
| 85 | $template->assign('MenuItems', $menuItems); |
||
| 86 | } |
||
| 87 | |||
| 88 | public static function planetList(int $alliance_id, int $selected_index): void { |
||
| 89 | $menuItems = []; |
||
| 90 | $menuItems[] = ['Link' => Globals::getPlanetListHREF($alliance_id), 'Text' => 'Defense']; |
||
| 91 | $menuItems[] = ['Link' => Globals::getPlanetListFinancialHREF($alliance_id), 'Text' => 'Financial']; |
||
| 92 | // make the selected index bold |
||
| 93 | $boldItem =& $menuItems[$selected_index]['Text']; |
||
| 94 | $boldItem = '<span class="bold">' . $boldItem . '</span>'; |
||
| 95 | |||
| 96 | $template = Smr\Template::getInstance(); |
||
| 97 | $template->assign('MenuItems', $menuItems); |
||
| 98 | } |
||
| 99 | |||
| 100 | public static function alliance(int $alliance_id): void { |
||
| 101 | $db = Database::getInstance(); |
||
| 102 | $player = Smr\Session::getInstance()->getPlayer(); |
||
| 103 | |||
| 104 | $in_alliance = ($alliance_id == $player->getAllianceID() || in_array($player->getAccountID(), Globals::getHiddenPlayers())); |
||
| 105 | |||
| 106 | // Some pages are visible to all alliance members |
||
| 107 | $canReadMb = $in_alliance; |
||
| 108 | $canReadMotd = $in_alliance; |
||
| 109 | $canSeePlanetList = $in_alliance; |
||
| 110 | |||
| 111 | // Check if player has permissions through an alliance treaty |
||
| 112 | if (!$in_alliance) { |
||
| 113 | $dbResult = $db->read('SELECT mb_read, mod_read, planet_land FROM alliance_treaties |
||
| 114 | WHERE (alliance_id_1 = ' . $db->escapeNumber($alliance_id) . ' OR alliance_id_1 = ' . $db->escapeNumber($player->getAllianceID()) . ') |
||
| 115 | AND (alliance_id_2 = ' . $db->escapeNumber($alliance_id) . ' OR alliance_id_2 = ' . $db->escapeNumber($player->getAllianceID()) . ') |
||
| 116 | AND game_id = ' . $db->escapeNumber($player->getGameID()) . ' |
||
| 117 | AND (mb_read = 1 OR mod_read = 1 OR planet_land = 1) AND official = \'TRUE\''); |
||
| 118 | if ($dbResult->hasRecord()) { |
||
| 119 | $dbRecord = $dbResult->record(); |
||
| 120 | $canReadMb = $dbRecord->getBoolean('mb_read'); |
||
| 121 | $canReadMotd = $dbRecord->getBoolean('mod_read'); |
||
| 122 | $canSeePlanetList = $dbRecord->getBoolean('planet_land'); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | $role_id = $player->getAllianceRole($alliance_id); |
||
| 127 | $dbResult = $db->read('SELECT send_alliance_msg FROM alliance_has_roles WHERE alliance_id = ' . $db->escapeNumber($alliance_id) . ' AND game_id = ' . $db->escapeNumber($player->getGameID()) . ' AND role_id = ' . $db->escapeNumber($role_id)); |
||
| 128 | if ($dbResult->hasRecord()) { |
||
| 129 | $send_alliance_msg = $dbResult->record()->getBoolean('send_alliance_msg'); |
||
| 130 | } else { |
||
| 131 | $send_alliance_msg = false; |
||
| 132 | } |
||
| 133 | |||
| 134 | $menuItems = []; |
||
| 135 | if ($canReadMotd) { |
||
| 136 | $menuItems[] = ['Link' => Globals::getAllianceMotdHREF($alliance_id), 'Text' => 'Message of the Day']; |
||
| 137 | } |
||
| 138 | $menuItems[] = ['Link' => Globals::getAllianceRosterHREF($alliance_id), 'Text' => 'Roster']; |
||
| 139 | if ($send_alliance_msg) { |
||
| 140 | $menuItems[] = ['Link' => Globals::getAllianceMessageHREF($alliance_id), 'Text' => 'Send Message']; |
||
| 141 | } |
||
| 142 | if ($canReadMb) { |
||
| 143 | $menuItems[] = ['Link' => Globals::getAllianceMessageBoardHREF($alliance_id), 'Text' => 'Message Board']; |
||
| 144 | } |
||
| 145 | if ($canSeePlanetList) { |
||
| 146 | $menuItems[] = ['Link' => Globals::getPlanetListHREF($alliance_id), 'Text' => 'Planets']; |
||
| 147 | } |
||
| 148 | if ($in_alliance) { |
||
| 149 | $menuItems[] = ['Link' => Globals::getAllianceForcesHREF($alliance_id), 'Text' => 'Forces']; |
||
| 150 | $menuItems[] = ['Link' => Globals::getAllianceOptionsHREF(), 'Text' => 'Options']; |
||
| 151 | } |
||
| 152 | $menuItems[] = ['Link' => Globals::getAllianceListHREF(), 'Text' => 'List Alliances']; |
||
| 153 | $menuItems[] = ['Link' => Globals::getAllianceNewsHREF($player->getGameID(), $alliance_id), 'Text' => 'View News']; |
||
| 154 | |||
| 155 | $template = Smr\Template::getInstance(); |
||
| 156 | $template->assign('MenuItems', $menuItems); |
||
| 157 | } |
||
| 158 | |||
| 159 | public static function galacticPost(): void { |
||
| 172 | } |
||
| 173 | |||
| 174 | public static function messages(): void { |
||
| 175 | $player = Smr\Session::getInstance()->getPlayer(); |
||
| 176 | |||
| 177 | $menuItems = []; |
||
| 178 | $menuItems[] = ['Link' => Globals::getViewMessageBoxesHREF(), 'Text' => 'View Messages']; |
||
| 179 | $menuItems[] = ['Link' => Globals::getSendGlobalMessageHREF(), 'Text' => 'Send Global Message']; |
||
| 180 | if ($player->isOnCouncil()) { |
||
| 181 | $menuItems[] = ['Link' => Globals::getSendCouncilMessageHREF($player->getRaceID()), 'Text' => 'Send Council Message']; |
||
| 182 | } |
||
| 183 | $menuItems[] = ['Link' => Globals::getManageBlacklistHREF(), 'Text' => 'Manage Blacklist']; |
||
| 184 | |||
| 185 | $template = Smr\Template::getInstance(); |
||
| 186 | $template->assign('MenuItems', $menuItems); |
||
| 187 | } |
||
| 188 | |||
| 189 | public static function combatLog(): void { |
||
| 190 | $menuItems = []; |
||
| 191 | |||
| 192 | foreach (CombatLogType::cases() as $type) { |
||
| 193 | $container = new CombatLogList($type); |
||
| 194 | $menuItems[] = ['Link' => $container->href(), 'Text' => $type->name]; |
||
|
|
|||
| 195 | } |
||
| 196 | |||
| 197 | $template = Smr\Template::getInstance(); |
||
| 198 | $template->assign('MenuItems', $menuItems); |
||
| 199 | } |
||
| 200 | |||
| 201 | public static function trader(): void { |
||
| 202 | $player = Smr\Session::getInstance()->getPlayer(); |
||
| 203 | |||
| 204 | $template = Smr\Template::getInstance(); |
||
| 205 | $template->assign('MenuItems', [ |
||
| 206 | ['Link' => Globals::getTraderStatusHREF(), 'Text' => 'Trader Status'], |
||
| 207 | ['Link' => Globals::getPlanetListHREF($player->getAllianceID()), 'Text' => 'Planets'], |
||
| 208 | ['Link' => Globals::getAllianceHREF($player->getAllianceID()), 'Text' => 'Alliance'], |
||
| 209 | ['Link' => Globals::getCouncilHREF($player->getRaceID()), 'Text' => 'Politics'], |
||
| 210 | ['Link' => Globals::getTraderRelationsHREF(), 'Text' => 'Relations'], |
||
| 211 | ['Link' => Globals::getTraderBountiesHREF(), 'Text' => 'Bounties']]); |
||
| 212 | } |
||
| 213 | |||
| 214 | public static function planet(SmrPlanet $planet): void { |
||
| 215 | $menu_array = []; |
||
| 216 | $menu_array[] = ['Link' => (new Main())->href(), 'Text' => 'Planet Main']; |
||
| 217 | if ($planet->hasMenuOption('CONSTRUCTION')) { |
||
| 218 | $menu_array[] = ['Link' => (new Construction())->href(), 'Text' => 'Construction']; |
||
| 219 | } |
||
| 220 | if ($planet->hasMenuOption('DEFENSE')) { |
||
| 221 | $menu_array[] = ['Link' => (new Defense())->href(), 'Text' => 'Defense']; |
||
| 222 | } |
||
| 223 | if ($planet->hasMenuOption('OWNERSHIP')) { |
||
| 224 | $menu_array[] = ['Link' => (new Ownership())->href(), 'Text' => 'Ownership']; |
||
| 225 | } |
||
| 226 | if ($planet->hasMenuOption('STOCKPILE')) { |
||
| 227 | $menu_array[] = ['Link' => (new Stockpile())->href(), 'Text' => 'Stockpile']; |
||
| 228 | } |
||
| 229 | if ($planet->hasMenuOption('FINANCE')) { |
||
| 230 | $menu_array[] = ['Link' => (new Financial())->href(), 'Text' => 'Financial']; |
||
| 231 | } |
||
| 232 | |||
| 233 | $template = Smr\Template::getInstance(); |
||
| 234 | $template->assign('MenuItems', $menu_array); |
||
| 235 | } |
||
| 236 | |||
| 237 | /* |
||
| 238 | * $active_level1 - the id of the active menu on the first level |
||
| 239 | * $active_level1 - the id of the active menu on the second level |
||
| 240 | */ |
||
| 241 | public static function rankings(int $active_level1 = 0, int $active_level2 = 0): void { |
||
| 242 | |||
| 243 | $menu = []; |
||
| 244 | |||
| 245 | // player rankings |
||
| 246 | $menu_item = []; |
||
| 247 | $menu_item['entry'] = create_link(new PlayerExperience(), 'Player Rankings', 'nav'); |
||
| 248 | |||
| 249 | $menu_subitem = []; |
||
| 250 | $menu_subitem[] = create_link(new PlayerExperience(), 'Experience', 'nav'); |
||
| 251 | $menu_subitem[] = create_link(new PlayerProfit(), 'Profit', 'nav'); |
||
| 252 | $menu_subitem[] = create_link(new PlayerKills(), 'Kills', 'nav'); |
||
| 253 | $menu_subitem[] = create_link(new PlayerDeaths(), 'Deaths', 'nav'); |
||
| 254 | $menu_subitem[] = create_link(new PlayerAssists(), 'Assists', 'nav'); |
||
| 255 | $menu_subitem[] = create_link(new PlayerNpcKills(), 'NPC Kills', 'nav'); |
||
| 256 | |||
| 257 | $menu_item['submenu'] = $menu_subitem; |
||
| 258 | |||
| 259 | $menu[] = $menu_item; |
||
| 260 | |||
| 261 | // alliance rankings |
||
| 262 | $menu_item = []; |
||
| 263 | $menu_item['entry'] = create_link(new AllianceExperience(), 'Alliance Rankings', 'nav'); |
||
| 264 | |||
| 265 | $menu_subitem = []; |
||
| 266 | $menu_subitem[] = create_link(new AllianceExperience(), 'Experience', 'nav'); |
||
| 267 | $menu_subitem[] = create_link(new AllianceProfit(), 'Profit', 'nav'); |
||
| 268 | $menu_subitem[] = create_link(new AllianceKills(), 'Kills', 'nav'); |
||
| 269 | $menu_subitem[] = create_link(new AllianceDeaths(), 'Deaths', 'nav'); |
||
| 270 | $menu_subitem[] = create_link(new AllianceVsAlliance(), 'Versus', 'nav'); |
||
| 271 | |||
| 272 | $menu_item['submenu'] = $menu_subitem; |
||
| 273 | |||
| 274 | $menu[] = $menu_item; |
||
| 275 | |||
| 276 | // racial rankings |
||
| 277 | $menu_item = []; |
||
| 278 | $menu_item['entry'] = create_link(new RaceExperience(), 'Racial Standings', 'nav'); |
||
| 279 | |||
| 280 | $menu_subitem = []; |
||
| 281 | $menu_subitem[] = create_link(new RaceExperience(), 'Experience', 'nav'); |
||
| 282 | $menu_subitem[] = create_link(new RaceKills(), 'Kills', 'nav'); |
||
| 283 | $menu_subitem[] = create_link(new RaceDeaths(), 'Deaths', 'nav'); |
||
| 284 | |||
| 285 | $menu_item['submenu'] = $menu_subitem; |
||
| 286 | |||
| 287 | $menu[] = $menu_item; |
||
| 288 | |||
| 289 | // sector rankings |
||
| 290 | $menu_item = []; |
||
| 291 | $menu_item['entry'] = create_link(new SectorKills(), 'Sector Kills', 'nav'); |
||
| 292 | $menu[] = $menu_item; |
||
| 293 | |||
| 294 | create_sub_menu($menu, $active_level1, $active_level2); |
||
| 295 | } |
||
| 296 | |||
| 297 | public static function bank(): void { |
||
| 298 | $player = Smr\Session::getInstance()->getPlayer(); |
||
| 299 | |||
| 300 | $links = []; |
||
| 301 | $links[] = [new PersonalBank(), 'Personal Account']; |
||
| 302 | if ($player->hasAlliance()) { |
||
| 303 | $links[] = [new AllianceBank($player->getAllianceID()), 'Alliance Account']; |
||
| 304 | } |
||
| 305 | $links[] = [new AnonBank(), 'Anonymous Account']; |
||
| 306 | |||
| 307 | $menuItems = []; |
||
| 308 | foreach ($links as [$container, $label]) { |
||
| 309 | $menuItems[] = [ |
||
| 310 | 'Link' => $container->href(), |
||
| 311 | 'Text' => $label, |
||
| 312 | ]; |
||
| 313 | } |
||
| 314 | |||
| 315 | $template = Smr\Template::getInstance(); |
||
| 316 | $template->assign('MenuItems', $menuItems); |
||
| 317 | } |
||
| 318 | |||
| 319 | public static function council(int $race_id): void { |
||
| 320 | $player = Smr\Session::getInstance()->getPlayer(); |
||
| 321 | |||
| 322 | $container = new ViewCouncil($race_id); |
||
| 323 | $menu_items = []; |
||
| 324 | $menu_items[] = [ |
||
| 325 | 'Link' => $container->href(), |
||
| 326 | 'Text' => 'View Council', |
||
| 327 | ]; |
||
| 328 | |||
| 329 | $container = new PoliticalStatus($race_id); |
||
| 330 | $menu_items[] = [ |
||
| 331 | 'Link' => $container->href(), |
||
| 332 | 'Text' => 'Political Status', |
||
| 333 | ]; |
||
| 334 | |||
| 335 | $container = new MessageCouncil($race_id); |
||
| 336 | $menu_items[] = [ |
||
| 337 | 'Link' => $container->href(), |
||
| 338 | 'Text' => 'Send Message', |
||
| 339 | ]; |
||
| 340 | |||
| 341 | if ($player->getRaceID() == $race_id) { |
||
| 342 | if ($player->isOnCouncil()) { |
||
| 343 | $container = new VotingCenter(); |
||
| 344 | $menu_items[] = [ |
||
| 345 | 'Link' => $container->href(), |
||
| 346 | 'Text' => 'Voting Center', |
||
| 347 | ]; |
||
| 348 | } |
||
| 349 | if ($player->isPresident()) { |
||
| 350 | $container = new Embassy(); |
||
| 351 | $menu_items[] = [ |
||
| 352 | 'Link' => $container->href(), |
||
| 353 | 'Text' => 'Embassy', |
||
| 354 | ]; |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | $template = Smr\Template::getInstance(); |
||
| 359 | $template->assign('MenuItems', $menu_items); |
||
| 360 | } |
||
| 361 | |||
| 362 | public static function bar(int $locationID): void { |
||
| 363 | $template = Smr\Template::getInstance(); |
||
| 364 | $template->assign('MenuItems', [ |
||
| 365 | ['Link' => (new BarMain($locationID))->href(), 'Text' => 'Bar Main'], |
||
| 366 | ['Link' => (new LottoBuyTicket($locationID))->href(), 'Text' => 'Lotto'], |
||
| 367 | ['Link' => (new PlayBlackjackBet($locationID))->href(), 'Text' => 'BlackJack'], |
||
| 368 | ]); |
||
| 369 | } |
||
| 370 | |||
| 371 | public static function news(int $gameID): void { |
||
| 392 | } |
||
| 393 | |||
| 394 | public static function navigation(AbstractSmrPlayer $player): void { |
||
| 404 | } |
||
| 405 | |||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param array<array<string, mixed>> $menu |
||
| 410 | */ |
||
| 411 | function create_sub_menu(array $menu, int $active_level1, int $active_level2): void { |
||
| 412 | $return = ('<table class="fullwidth center">'); |
||
| 413 | $return .= ('<tr>'); |
||
| 414 | foreach ($menu as $number => $entry) { |
||
| 468 |