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

CombatSimulatorProcessor::build()   F
last analyzed

Complexity

Conditions 21
Paths 160

Size

Total Lines 74
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 21
eloc 48
nc 160
nop 1
dl 0
loc 74
rs 3.6666
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\Admin;
4
5
use DummyShip;
6
use Smr\Page\AccountPageProcessor;
7
use Smr\Request;
8
use SmrAccount;
9
10
/**
11
 * @param array<int, \AbstractSmrPlayer> $realAttackers
12
 * @param array<int, \AbstractSmrPlayer> $realDefenders
13
 * @return array<string, mixed>
14
 */
15
function runAnAttack(array $realAttackers, array $realDefenders): array {
16
	$results = [
17
		'Attackers' => ['Traders' => [], 'TotalDamage' => 0],
18
		'Defenders' => ['Traders' => [], 'TotalDamage' => 0],
19
	];
20
	foreach ($realAttackers as $teamPlayer) {
21
		$playerResults = $teamPlayer->shootPlayers($realDefenders);
22
		$results['Attackers']['Traders'][] = $playerResults;
23
		$results['Attackers']['TotalDamage'] += $playerResults['TotalDamage'];
24
	}
25
	foreach ($realDefenders as $teamPlayer) {
26
		$playerResults = $teamPlayer->shootPlayers($realAttackers);
27
		$results['Defenders']['Traders'][] = $playerResults;
28
		$results['Defenders']['TotalDamage'] += $playerResults['TotalDamage'];
29
	}
30
	return $results;
31
}
32
33
class CombatSimulatorProcessor extends AccountPageProcessor {
34
35
	public function build(SmrAccount $account): never {
36
		$usedNames = [];
37
38
		$i = 1;
39
		$attackers = [];
40
		if (Request::has('attackers')) {
41
			foreach (Request::getArray('attackers') as $attackerName) {
42
				if ($attackerName == 'none') {
43
					continue;
44
				}
45
				if (isset($usedNames[$attackerName])) {
46
					create_error('Duplicate name used: ' . $attackerName);
47
				}
48
				$usedNames[$attackerName] = true;
49
				$attackers[$i] = DummyShip::getCachedDummyShip($attackerName)->getPlayer();
50
				++$i;
51
			}
52
		}
53
54
		$i = 1;
55
		$defenders = [];
56
		foreach (Request::getArray('defenders') as $defenderName) {
57
			if ($defenderName == 'none') {
58
				continue;
59
			}
60
			if (isset($usedNames[$defenderName])) {
61
				create_error('Duplicate name used: ' . $defenderName);
62
			}
63
			$usedNames[$defenderName] = true;
64
			$defenders[$i] = DummyShip::getCachedDummyShip($defenderName)->getPlayer();
65
			++$i;
66
		}
67
68
		if (Request::has('repair')) {
69
			foreach ([...$attackers, ...$defenders] as $player) {
70
				$player->setDead(false);
71
				$player->getShip()->setHardwareToMax();
72
			}
73
		}
74
75
		$results = null;
76
		if (Request::has('run') || Request::has('death_run')) {
77
			if (Request::has('death_run')) {
78
				$maxRounds = 100;
79
			} else {
80
				$maxRounds = 1;
81
			}
82
			$attackersLeft = $attackers;
83
			$defendersLeft = $defenders;
84
			for ($round = 0; $round < $maxRounds; $round++) {
85
				foreach ($attackersLeft as $key => $teamPlayer) {
86
					if ($teamPlayer->isDead()) {
87
						unset($attackersLeft[$key]);
88
					}
89
				}
90
				foreach ($defendersLeft as $key => $teamPlayer) {
91
					if ($teamPlayer->isDead()) {
92
						unset($defendersLeft[$key]);
93
					}
94
				}
95
				if (count($attackersLeft) == 0 || count($defendersLeft) == 0) {
96
					break;
97
				}
98
				$results = runAnAttack($attackersLeft, $defendersLeft);
99
			}
100
		}
101
102
		// Save ships unless we're just updating the dummy list
103
		if (!Request::has('update')) {
104
			DummyShip::saveDummyShips();
105
		}
106
107
		$container = new CombatSimulator($results, $attackers, $defenders);
108
		$container->go();
109
	}
110
111
}
112