Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

AttackPortProcessor::build()   F
last analyzed

Complexity

Conditions 17
Paths 12288

Size

Total Lines 108
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 62
nc 12288
nop 1
dl 0
loc 108
rs 1.0499
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php declare(strict_types=1);
2
3
namespace Smr\Pages\Player;
4
5
use AbstractSmrPlayer;
6
use Smr\Database;
7
use Smr\Epoch;
8
use Smr\Page\PlayerPageProcessor;
9
use Smr\SectorLock;
10
11
class AttackPortProcessor extends PlayerPageProcessor {
12
13
	public function build(AbstractSmrPlayer $player): never {
14
		$account = $player->getAccount();
15
		$ship = $player->getShip();
16
		$sector = $player->getSector();
17
18
		if ($player->hasNewbieTurns()) {
19
			create_error('You are under newbie protection!');
20
		}
21
		if ($player->hasFederalProtection()) {
22
			create_error('You are under federal protection!');
23
		}
24
		if ($player->isLandedOnPlanet()) {
25
			create_error('You cannot attack ports whilst on a planet!');
26
		}
27
		if ($player->getTurns() < TURNS_TO_SHOOT_PORT) {
28
			create_error('You do not have enough turns to attack this port!');
29
		}
30
		if (!$ship->hasWeapons() && !$ship->hasCDs()) {
31
			create_error('What are you going to do? Insult it to death?');
32
		}
33
		if (!$player->canFight()) {
34
			create_error('You are not allowed to fight!');
35
		}
36
37
		$port = $sector->getPort();
38
39
		if (!$port->exists()) {
40
			create_error('This port does not exist.');
41
		}
42
43
		if ($port->isDestroyed()) {
44
			(new AttackPort())->go();
45
		}
46
47
		// ********************************
48
		// *
49
		// * P o r t   a t t a c k
50
		// *
51
		// ********************************
52
53
		$results = ['Attackers' => ['TotalDamage' => 0]];
54
55
		$attackers = $sector->getFightingTradersAgainstPort($player, $port);
56
57
		$port->attackedBy($player, $attackers);
58
59
		// take the turns and decloak all attackers
60
		foreach ($attackers as $attacker) {
61
			$attacker->takeTurns(TURNS_TO_SHOOT_PORT);
62
			$attacker->getShip()->decloak();
63
		}
64
65
		$totalShieldDamage = 0;
66
		foreach ($attackers as $attacker) {
67
			$playerResults = $attacker->shootPort($port);
68
			$results['Attackers']['Traders'][$attacker->getAccountID()] = $playerResults;
69
			$results['Attackers']['TotalDamage'] += $playerResults['TotalDamage'];
70
			foreach ($playerResults['Weapons'] as $weapon) {
71
				if (isset($weapon['ActualDamage'])) { // Only set if the weapon hits
72
					$totalShieldDamage += $weapon['ActualDamage']['Shield'];
73
				}
74
			}
75
		}
76
77
		// Planet downgrades only occur on non-shield damage
78
		$downgradeDamage = $results['Attackers']['TotalDamage'] - $totalShieldDamage;
79
		$results['Attackers']['Downgrades'] = $port->checkForDowngrade($downgradeDamage);
80
81
		$results['Port'] = $port->shootPlayers($attackers);
82
83
		$account->log(LOG_TYPE_PORT_RAIDING, 'Player attacks port, the port does ' . $results['Port']['TotalDamage'] . ', their team does ' . $results['Attackers']['TotalDamage'] . ' and downgrades ' . $results['Attackers']['Downgrades'] . ' levels.', $port->getSectorID());
84
85
		$port->update();
86
87
		$db = Database::getInstance();
88
		$logId = $db->insert('combat_logs', [
89
			'game_id' => $db->escapeNumber($player->getGameID()),
90
			'type' => $db->escapeString('PORT'),
91
			'sector_id' => $db->escapeNumber($port->getSectorID()),
92
			'timestamp' => $db->escapeNumber(Epoch::time()),
93
			'attacker_id' => $db->escapeNumber($player->getAccountID()),
94
			'attacker_alliance_id' => $db->escapeNumber($player->getAllianceID()),
95
			'defender_id' => $db->escapeNumber(ACCOUNT_ID_PORT),
96
			'defender_alliance_id' => $db->escapeNumber(PORT_ALLIANCE_ID),
97
			'result' => $db->escapeObject($results, true),
98
		]);
99
100
		$sectorMessage = '[ATTACK_RESULTS]' . $logId;
101
		foreach ($attackers as $attacker) {
102
			if (!$player->equals($attacker)) {
103
				$db->replace('sector_message', [
104
					'account_id' => $db->escapeNumber($attacker->getAccountID()),
105
					'game_id' => $db->escapeNumber($attacker->getGameID()),
106
					'message' => $db->escapeString($sectorMessage),
107
				]);
108
			}
109
		}
110
111
		// If player died they are now in another sector, and thus locks need reset
112
		if ($player->isDead()) {
113
			saveAllAndReleaseLock(updateSession: false);
114
			// Grab the lock in the new sector to avoid reloading session
115
			SectorLock::getInstance()->acquireForPlayer($player);
116
		}
117
118
		// If they died on the shot they get to see the results
119
		$container = new AttackPort($results, $player->isDead());
120
		$container->go();
121
	}
122
123
}
124