GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

FactionRoutine::cmpFactionGeneral()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Asylamba\Modules\Atlas\Routine;
4
5
use Asylamba\Modules\Atlas\Model\Ranking;
6
use Asylamba\Modules\Atlas\Model\FactionRanking;
7
use Asylamba\Modules\Demeter\Model\Color;
8
9
use Asylamba\Classes\Library\Utils;
10
11
class FactionRoutine
12
{
13
	/**
14
	 * Contains results of all alive factions
15
	 *
16
	 * @var array
17
	 */
18
	protected $results = [];
19
	/** @var bool **/
20
	protected $isGameOver = false;
21
	
22
	/**
23
	 * @param Color $faction
24
	 * @param array $playerRankings
25
	 * @param array $routesIncome
26
	 * @param array $sectors
27
	 */
28
	public function execute(Color $faction, $playerRankings, $routesIncome, $sectors)
29
	{
30
		$this->results[$faction->getId()] = array(
31
			'general' => 0, 
32
			'wealth' => 0, 
33
			'territorial' => 0,
34
			'points' => $faction->rankingPoints);
35
		if ($faction->isWinner == 1) {
36
			$this->isGameOver = true;
37
		}
38
		$this->calculateGeneralRanking($playerRankings);
39
		$this->calculateWealthRanking($faction, $routesIncome);
40
		$this->calculateTerritorialRanking($faction, $sectors);
41
	}
42
	
43
	public function processResults(Ranking $ranking, $factions, $factionRankingManager)
44
	{
45
		#---------------- COMPUTING -------------------#
46
47
		# copy the arrays
48
		$listG = $this->results;
49
		$listW = $this->results;
50
		$listT = $this->results;
51
52
		# sort all the copies
53
		uasort($listG, [$this, 'cmpFactionGeneral']);
54
		uasort($listW, [$this, 'cmpWealth']);
55
		uasort($listT, [$this, 'cmpTerritorial']);
56
57
		/*foreach ($list as $key => $value) {
58
			echo $key . ' => ' . $value['general'] . '<br/>';
59
		}*/
60
61
		# put the position in each array
62
		$listG = $this->setPositions($listG, 'general');
63
		$listW = $this->setPositions($listW, 'wealth');
64
		$listT = $this->setPositions($listT, 'territorial');
65
66
		#-------------------------------- POINTS RANKING -----------------------------#
67
68
		# faire ce classement uniquement après x jours de jeu
69
		if (Utils::interval(SERVER_START_TIME, Utils::now(), 'h') > HOURS_BEFORE_START_OF_RANKING) {
70
			# points qu'on gagne en fonction de sa place dans le classement
71
			$pointsToEarn = [40, 30, 20, 10, 0, 0, 0, 0, 0, 0, 0];
72
			$coefG = 0.1; # 4 3 2 1 0 ...
73
			$coefW = 0.4; # 16 12 8 4 0 ...
74
			$coefT = 0.5; # 20 15 10 5 0 ...
75
76
			foreach ($factions as $faction) {
77
				$factionId = $faction->id;
78
				$additionalPoints = 0;
79
80
				# general
81
				$additionalPoints += intval($pointsToEarn[$listG[$factionId]['position'] - 1] * $coefG);
82
83
				# wealth
84
				$additionalPoints += intval($pointsToEarn[$listW[$factionId]['position'] - 1] * $coefW);
85
86
				# territorial
87
				$additionalPoints += intval($pointsToEarn[$listT[$factionId]['position'] - 1] * $coefT);
88
89
				$this->results[$factionId]['points'] += $additionalPoints;
90
			}
91
		}
92
93
94
		#---------------- LAST COMPUTING -------------------#
95
96
		$listP = $this->results;
97
		uasort($listP, [$this, 'cmpPoints']);
98
99
		$position = 1;
100
		foreach ($listP as $key => $value) { $listP[$key]['position'] = $position++;}
101
102
		#---------------- SAVING -------------------#
103
104
		$rankings = [];
105
106
		foreach ($factions as $faction) {
107
			$factionId = $faction->getId();
108
			$fr = new FactionRanking();
109
			$fr->rRanking = $ranking->getId();
110
			$fr->rFaction = $factionId; 
111
112
			$firstRanking = true;
113
			for ($i = 0; $i < $factionRankingManager->size(); $i++) {
114
				if ($factionRankingManager->get($i)->rFaction == $factionId) {
115
					$oldRanking = $factionRankingManager->get($i);
116
					$firstRanking = false;
117
					break;
118
				}
119
			}
120
121
			$fr->general = $listG[$factionId]['general'];
122
			$fr->generalPosition = $listG[$factionId]['position'];
123
			$fr->generalVariation = $firstRanking ? 0 : $oldRanking->generalPosition - $fr->generalPosition;
0 ignored issues
show
Bug introduced by
The variable $oldRanking does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
124
125
			$fr->wealth = $listW[$factionId]['wealth'];
126
			$fr->wealthPosition = $listW[$factionId]['position'];
127
			$fr->wealthVariation = $firstRanking ? 0 : $oldRanking->wealthPosition - $fr->wealthPosition;
128
129
			$fr->territorial = $listT[$factionId]['territorial'];
130
			$fr->territorialPosition = $listT[$factionId]['position'];
131
			$fr->territorialVariation = $firstRanking ? 0 : $oldRanking->territorialPosition - $fr->territorialPosition;
132
133
			if ($this->isGameOver === true) {
134
				$fr->points = $oldRanking->points;
135
				$fr->pointsPosition = $oldRanking->pointsPosition;
136
				$fr->pointsVariation = 0;
137
				$fr->newPoints = 0;
138
			} else {
139
				$fr->points = $listP[$factionId]['points'];
140
				$fr->pointsPosition = $listP[$factionId]['position'];
141
				$fr->pointsVariation = $firstRanking ? 0 : $oldRanking->pointsPosition - $fr->pointsPosition;
142
				$fr->newPoints = $firstRanking ? $fr->points : $fr->points - $oldRanking->points;
143
			}
144
145
			# update faction infos
146
			$faction->rankingPoints = $listP[$factionId]['points'];
147
			$faction->points = $listG[$factionId]['general'];
148
			$faction->sectors = $listT[$factionId]['territorial'];
149
150
			$rankings[] = $fr;
151
			$factionRankingManager->add($fr);
152
		}
153
154
		if ($this->isGameOver === false) {
155
			# check if a faction wins the game
156
			$winRanking = NULL;
157
			foreach ($rankings as $ranking) {
158
				if ($ranking->points >= POINTS_TO_WIN) {
159
					if ($winRanking !== NULL) {
160
						if ($winRanking->points < $ranking->points) {
161
							return $ranking->rFaction;
162
						}
163
					} else {
164
						return $ranking->rFaction;
165
					}
166
				}
167
			}
168
		}
169
		return null;
170
	}
171
	
172
	protected function calculateGeneralRanking($playerRankings)
173
	{
174
		foreach ($playerRankings as $playerRanking) {
175
			$player = $playerRanking->getPlayer();
176
177
			if (isset($player->rColor)) {
178
				$this->results[$player->rColor]['general'] += $playerRanking->general;
179
			}
180
		}
181
	}
182
	
183
	protected function calculateWealthRanking(Color $faction, $routesIncome)
184
	{
185
		if ($routesIncome['income'] == NULL) {
186
			$income = 0;
187
		} else {
188
			$income = $routesIncome['income'];
189
		}
190
		$this->results[$faction->getId()]['wealth'] = $income;
191
	}
192
	
193
	protected function calculateTerritorialRanking($faction, $sectors)
194
	{
195
		foreach ($sectors as $sector) {
196
			if ($sector->rColor === $faction->getId()) {
197
				$this->results[$sector->rColor]['territorial'] += $sector->points;
198
			}
199
		}
200
	}
201
	
202
	protected function cmpFactionGeneral($a, $b) {
203
		if ($a['general'] == $b['general']) {
204
			return 0;
205
		}
206
		return ($a['general'] > $b['general']) ? -1 : 1;
207
	}
208
209
	protected function cmpWealth($a, $b) {
210
		if ($a['wealth'] == $b['wealth']) {
211
			return 0;
212
		}
213
		return ($a['wealth'] > $b['wealth']) ? -1 : 1;
214
	}
215
216
	protected function cmpTerritorial($a, $b) {
217
		if ($a['territorial'] == $b['territorial']) {
218
			return 0;
219
		}
220
		return ($a['territorial'] > $b['territorial']) ? -1 : 1;
221
	}
222
223
	protected function cmpPoints($a, $b) {
224
		if ($a['points'] == $b['points']) {
225
			return 0;
226
		}
227
		return ($a['points'] > $b['points']) ? -1 : 1;
228
	}
229
230
	protected function setPositions($list, $attribute) {
231
		$position = 1;
232
		$index = 1;
233
		$previous = PHP_INT_MAX;
234
		foreach ($list as $key => $value) { 
235
			if ($previous > $list[$key][$attribute]) {
236
				$position = $index;
237
			}
238
			$list[$key]['position'] = $position;
239
			$index++;
240
			$previous = $list[$key][$attribute];
241
		}
242
		return $list;
243
	}
244
	
245
	public function getResults()
246
	{
247
		return $this->results;
248
	}
249
}