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.
Completed
Push — master ( 11c0a1...856a6b )
by Jacky
34s
created

FactionRoutine   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 237
rs 8.3157
c 1
b 0
f 0
wmc 43
lcom 1
cbo 5

10 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 14 2
F processResults() 0 130 18
A calculateGeneralRanking() 0 10 3
A calculateWealthRanking() 0 10 2
A calculateTerritorialRanking() 0 8 3
A cmpFactionGeneral() 0 6 3
A cmpWealth() 0 6 3
A cmpTerritorial() 0 6 3
A cmpPoints() 0 6 3
A setPositions() 0 14 3

How to fix   Complexity   

Complex Class

Complex classes like FactionRoutine 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 FactionRoutine, and based on these observations, apply Extract Interface, too.

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 extends AbstractRoutine
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
			//$this->colorManager->updateInfos($faction);
151
152
			$rankings[] = $fr;
153
			$factionRankingManager->add($fr);
154
		}
155
156
		if ($this->isGameOver === false) {
157
			# check if a faction wins the game
158
			$winRanking = NULL;
159
			foreach ($rankings as $ranking) {
160
				if ($ranking->points >= POINTS_TO_WIN) {
161
					if ($winRanking !== NULL) {
162
						if ($winRanking->points < $ranking->points) {
163
							return $ranking->rFaction;
164
						}
165
					} else {
166
						return $ranking->rFaction;
167
					}
168
				}
169
			}
170
		}
171
		return null;
172
	}
173
	
174
	protected function calculateGeneralRanking($playerRankings)
175
	{
176
		foreach ($playerRankings as $playerRanking) {
177
			$player = $playerRanking->getPlayer();
178
179
			if (isset($player->rColor)) {
180
				$this->results[$player->rColor]['general'] += $playerRanking->general;
181
			}
182
		}
183
	}
184
	
185
	protected function calculateWealthRanking(Color $faction, $routesIncome)
186
	{
187
		
188
		if ($routesIncome['income'] == NULL) {
189
			$income = 0;
190
		} else {
191
			$income = $routesIncome['income'];
192
		}
193
		$this->results[$faction->getId()]['wealth'] = $income;
194
	}
195
	
196
	protected function calculateTerritorialRanking($faction, $sectors)
197
	{
198
		foreach ($sectors as $sector) {
199
			if ($sector->rColor === $faction->getId()) {
200
				$this->results[$sector->rColor]['territorial'] += $sector->points;
201
			}
202
		}
203
	}
204
	
205
	protected function cmpFactionGeneral($a, $b) {
206
		if ($a['general'] == $b['general']) {
207
			return 0;
208
		}
209
		return ($a['general'] > $b['general']) ? -1 : 1;
210
	}
211
212
	protected function cmpWealth($a, $b) {
213
		if ($a['wealth'] == $b['wealth']) {
214
			return 0;
215
		}
216
		return ($a['wealth'] > $b['wealth']) ? -1 : 1;
217
	}
218
219
	protected function cmpTerritorial($a, $b) {
220
		if ($a['territorial'] == $b['territorial']) {
221
			return 0;
222
		}
223
		return ($a['territorial'] > $b['territorial']) ? -1 : 1;
224
	}
225
226
	protected function cmpPoints($a, $b) {
227
		if ($a['points'] == $b['points']) {
228
			return 0;
229
		}
230
		return ($a['points'] > $b['points']) ? -1 : 1;
231
	}
232
233
	protected function setPositions($list, $attribute) {
234
		$position = 1;
235
		$index = 1;
236
		$previous = PHP_INT_MAX;
237
		foreach ($list as $key => $value) { 
238
			if ($previous > $list[$key][$attribute]) {
239
				$position = $index;
240
			}
241
			$list[$key]['position'] = $position;
242
			$index++;
243
			$previous = $list[$key][$attribute];
244
		}
245
		return $list;
246
	}
247
}