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

Failed Conditions
Pull Request — main (#1494)
by Dan
08:06 queued 03:15
created

AllianceVsAlliance::build()   F

Complexity

Conditions 26
Paths > 20000

Size

Total Lines 135
Code Lines 101

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 26
eloc 101
nc 32256
nop 2
dl 0
loc 135
rs 0
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\Rankings;
4
5
use AbstractSmrPlayer;
6
use Exception;
7
use Menu;
8
use Smr\Database;
9
use Smr\Page\PlayerPage;
10
use Smr\Page\ReusableTrait;
11
use Smr\Request;
12
use Smr\Template;
13
use SmrAlliance;
14
15
class AllianceVsAlliance extends PlayerPage {
16
17
	use ReusableTrait;
18
19
	public string $file = 'rankings_alliance_vs_alliance.php';
20
21
	/**
22
	 * @param array<int> $versusAllianceIDs
23
	 */
24
	public function __construct(
25
		private ?int $detailsAllianceID = null,
26
		private ?array $versusAllianceIDs = null
27
	) {}
28
29
	public function build(AbstractSmrPlayer $player, Template $template): void {
30
		$template->assign('PageTopic', 'Alliance VS Alliance Rankings');
31
32
		Menu::rankings(1, 4);
33
		$db = Database::getInstance();
34
		$container = new self($this->detailsAllianceID);
35
		$template->assign('SubmitHREF', $container->href());
36
37
		$this->versusAllianceIDs ??= Request::getIntArray('alliancer', []);
38
		$this->detailsAllianceID ??= Request::getInt('alliance_id', $player->getAllianceID());
39
40
		// Get list of alliances that have kills or deaths
41
		$activeAlliances = [];
42
		$dbResult = $db->read('SELECT * FROM alliance WHERE game_id = ' . $db->escapeNumber($player->getGameID()) . ' AND (alliance_deaths > 0 OR alliance_kills > 0) ORDER BY alliance_kills DESC, alliance_name');
43
		foreach ($dbResult->records() as $dbRecord) {
44
			$allianceID = $dbRecord->getInt('alliance_id');
45
			$activeAlliances[$allianceID] = SmrAlliance::getAlliance($allianceID, $player->getGameID(), false, $dbRecord);
46
		}
47
		$template->assign('ActiveAlliances', $activeAlliances);
48
49
		// Get list of alliances to display (max of 5)
50
		// These must be a subset of the active alliances
51
		if (count($this->versusAllianceIDs) === 0) {
0 ignored issues
show
Bug introduced by
It seems like $this->versusAllianceIDs can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
		if (count(/** @scrutinizer ignore-type */ $this->versusAllianceIDs) === 0) {
Loading history...
52
			$alliance_vs_ids = array_slice(array_keys($activeAlliances), 0, 4);
53
			$alliance_vs_ids[] = 0;
54
		} else {
55
			$alliance_vs_ids = $this->versusAllianceIDs;
56
		}
57
58
		$alliance_vs = [];
59
		foreach ($alliance_vs_ids as $curr_id) {
60
			$curr_alliance = SmrAlliance::getAlliance($curr_id, $player->getGameID());
61
			$container = new self($curr_id, $this->versusAllianceIDs);
62
			$style = '';
63
			if (!$curr_alliance->isNone() && $curr_alliance->hasDisbanded()) {
64
				$style = 'class="red"';
65
			}
66
			if ($player->getAllianceID() == $curr_id) {
67
				$style = 'class="bold"';
68
			}
69
			$alliance_vs[] = [
70
				'ID' => $curr_id,
71
				'DetailsHREF' => $container->href(),
72
				'Name' => $curr_alliance->isNone() ? 'No Alliance' : $curr_alliance->getAllianceDisplayName(),
73
				'Style' => $style,
74
			];
75
		}
76
		$template->assign('AllianceVs', $alliance_vs);
77
78
		$alliance_vs_table = [];
79
		foreach ($alliance_vs_ids as $curr_id) {
80
			$curr_alliance = SmrAlliance::getAlliance($curr_id, $player->getGameID());
81
			foreach ($alliance_vs_ids as $id) {
82
				$row_alliance = SmrAlliance::getAlliance($id, $player->getGameID());
83
				$showRed = (!$curr_alliance->isNone() && $curr_alliance->hasDisbanded()) ||
84
				           (!$row_alliance->isNone() && $row_alliance->hasDisbanded());
85
				$showBold = $curr_id == $player->getAllianceID() || $id == $player->getAllianceID();
86
				$style = '';
87
				if ($curr_id == $id && !$row_alliance->isNone()) {
88
					$value = '--';
89
					if ($showRed) {
90
						$style = 'class="red"';
91
					} elseif ($showBold) {
92
						$style = 'class="bold"';
93
					}
94
				} else {
95
					$dbResult = $db->read('SELECT kills FROM alliance_vs_alliance
96
								WHERE alliance_id_2 = ' . $db->escapeNumber($curr_id) . '
97
									AND alliance_id_1 = ' . $db->escapeNumber($id) . '
98
									AND game_id = ' . $db->escapeNumber($player->getGameID()));
99
					$value = $dbResult->hasRecord() ? $dbResult->record()->getInt('kills') : 0;
100
					if ($showRed && $showBold) {
101
						$style = 'class="bold red"';
102
					} elseif ($showRed) {
103
						$style = 'class="red"';
104
					} elseif ($showBold) {
105
						$style = 'class="bold"';
106
					}
107
				}
108
				$alliance_vs_table[$curr_id][$id] = [
109
					'Value' => $value,
110
					'Style' => $style,
111
				];
112
			}
113
		}
114
		$template->assign('AllianceVsTable', $alliance_vs_table);
115
116
		// Show details for a specific alliance
117
		$main_alliance = SmrAlliance::getAlliance($this->detailsAllianceID, $player->getGameID());
118
		$mainName = $main_alliance->isNone() ? 'No Alliance' : $main_alliance->getAllianceDisplayName();
119
		$template->assign('DetailsName', $mainName);
120
121
		$kills = [];
122
		$dbResult = $db->read('SELECT * FROM alliance_vs_alliance
123
					WHERE alliance_id_1 = ' . $db->escapeNumber($this->detailsAllianceID) . '
124
						AND game_id = ' . $db->escapeNumber($player->getGameID()) . ' ORDER BY kills DESC');
125
		foreach ($dbResult->records() as $dbRecord) {
126
			$id = $dbRecord->getInt('alliance_id_2');
127
			$alliance_name = match (true) {
128
				$id > 0 => SmrAlliance::getAlliance($id, $player->getGameID())->getAllianceDisplayName(),
129
				$id == 0 => 'No Alliance',
130
				$id == ALLIANCE_VS_FORCES => '<span class="yellow">Forces</span>',
131
				$id == ALLIANCE_VS_PLANETS => '<span class="yellow">Planets</span>',
132
				$id == ALLIANCE_VS_PORTS => '<span class="yellow">Ports</span>',
133
				default => throw new Exception('Unknown alliance ID: ' . $id),
134
			};
135
136
			$kills[] = [
137
				'Name' => $alliance_name,
138
				'Kills' => $dbRecord->getInt('kills'),
139
			];
140
		}
141
		$template->assign('Kills', $kills);
142
143
		$deaths = [];
144
		$dbResult = $db->read('SELECT * FROM alliance_vs_alliance
145
					WHERE alliance_id_2 = ' . $db->escapeNumber($this->detailsAllianceID) . '
146
						AND game_id = ' . $db->escapeNumber($player->getGameID()) . ' ORDER BY kills DESC');
147
		foreach ($dbResult->records() as $dbRecord) {
148
			$id = $dbRecord->getInt('alliance_id_1');
149
			$alliance_name = match (true) {
150
				$id > 0 => SmrAlliance::getAlliance($id, $player->getGameID())->getAllianceDisplayName(),
151
				$id == 0 => 'No Alliance',
152
				$id == ALLIANCE_VS_FORCES => '<span class="yellow">Forces</span>',
153
				$id == ALLIANCE_VS_PLANETS => '<span class="yellow">Planets</span>',
154
				$id == ALLIANCE_VS_PORTS => '<span class="yellow">Ports</span>',
155
				default => throw new Exception('Unknown alliance ID: ' . $id),
156
			};
157
158
			$deaths[] = [
159
				'Name' => $alliance_name,
160
				'Deaths' => $dbRecord->getInt('kills'),
161
			];
162
		}
163
		$template->assign('Deaths', $deaths);
164
	}
165
166
}
167