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.

PlayerRankingManager::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 32
nc 1
nop 1
dl 0
loc 48
rs 9.125
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * PlayerRankingManager
5
 *
6
 * @author Jacky Casas
7
 * @copyright Asylamba
8
 *
9
 * @package Atlas
10
 * @version 04.06.14
11
 **/
12
namespace Asylamba\Modules\Atlas\Manager;
13
14
use Asylamba\Classes\Worker\Manager;
15
use Asylamba\Classes\Database\Database;
16
use Asylamba\Classes\Library\Utils;
17
use Asylamba\Modules\Atlas\Model\PlayerRanking;
18
19
class PlayerRankingManager extends Manager {
20
	protected $managerType = '_PlayerRanking';
21
22
	/**
23
	 * @param Database $database
24
	 */
25
	public function __construct(Database $database) {
26
		parent::__construct($database);
27
	}
28
	
29
	public function loadLastContext($where = array(), $order = array(), $limit = array()) {
30
		$qr = $this->database->prepare('SELECT * FROM ranking WHERE player = 1 ORDER BY dRanking DESC LIMIT 1');
31
		$qr->execute();
32
		$aw = $qr->fetch();
33
		$rRanking = $aw['id'];
34
35
		# add the rRanking to the WHERE clause
36
		$where['rRanking'] = $rRanking;
37
38
		$formatWhere = Utils::arrayToWhere($where, 'pl.');
39
		$formatOrder = Utils::arrayToOrder($order);
40
		$formatLimit = Utils::arrayToLimit($limit);
41
42
		$qr = $this->database->prepare('SELECT pl.*,
43
				p.rColor AS color,
44
				p.name AS name,
45
				p.avatar AS avatar,
46
				p.status AS status
47
			FROM playerRanking AS pl
48
			LEFT JOIN player AS p 
49
				ON pl.rPlayer = p.id
50
			' . $formatWhere . '
51
			' . $formatOrder . '
52
			' . $formatLimit
53
		);
54
55
		foreach($where AS $v) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
Coding Style introduced by
AS keyword must be lowercase; expected "as" but found "AS"
Loading history...
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected as, but found AS.
Loading history...
56
			if (is_array($v)) {
57
				foreach ($v as $p) {
58
					$valuesArray[] = $p;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$valuesArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $valuesArray = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
59
				}
60
			} else {
61
				$valuesArray[] = $v;
0 ignored issues
show
Bug introduced by
The variable $valuesArray 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...
62
			}
63
		}
64
65
		if(empty($valuesArray)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
66
			$qr->execute();
67
		} else {
68
			$qr->execute($valuesArray);
69
		}
70
71
		while($aw = $qr->fetch()) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after WHILE keyword; 0 found
Loading history...
72
			$pl = new PlayerRanking();
73
74
			$pl->id = $aw['id']; 
75
			$pl->rRanking = $aw['rRanking'];
76
			$pl->rPlayer = $aw['rPlayer']; 
77
			$pl->general = $aw['general'];
78
			$pl->generalPosition = $aw['generalPosition'];
79
			$pl->generalVariation = $aw['generalVariation'];
80
			$pl->experience = $aw['experience'];
81
			$pl->experiencePosition = $aw['experiencePosition'];
82
			$pl->experienceVariation = $aw['experienceVariation'];
83
			$pl->fight = $aw['fight'];
84
			$pl->fightPosition = $aw['fightPosition'];
85
			$pl->fightVariation = $aw['fightVariation'];
86
			$pl->trader = $aw['trader'];
87
			$pl->traderPosition = $aw['traderPosition'];
88
			$pl->traderVariation = $aw['traderVariation'];
89
			$pl->armies = $aw['armies'];
90
			$pl->armiesPosition = $aw['armiesPosition'];
91
			$pl->armiesVariation = $aw['armiesVariation'];
92
			$pl->resources = $aw['resources'];
93
			$pl->resourcesPosition = $aw['resourcesPosition'];
94
			$pl->resourcesVariation = $aw['resourcesVariation'];
95
			$pl->butcher = $aw['butcher'];
96
			$pl->butcherPosition = $aw['butcherPosition'];
97
			$pl->butcherVariation = $aw['butcherVariation'];
98
99
			$pl->color = $aw['color'];
100
			$pl->name = $aw['name'];
101
			$pl->avatar = $aw['avatar'];
102
			$pl->status = $aw['status'];
103
104
			$currentT = $this->_Add($pl);
0 ignored issues
show
Unused Code introduced by
$currentT is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
105
		}
106
	}
107
108
	public function loadByRequest($request) {
109
		$formatWhere = Utils::arrayToWhere($where, 'pl.');
0 ignored issues
show
Bug introduced by
The variable $where does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Unused Code introduced by
$formatWhere is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
110
		$formatOrder = Utils::arrayToOrder($order);
0 ignored issues
show
Bug introduced by
The variable $order does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Unused Code introduced by
$formatOrder is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
111
		$formatLimit = Utils::arrayToLimit($limit);
0 ignored issues
show
Bug introduced by
The variable $limit does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Unused Code introduced by
$formatLimit is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
112
113
		$qr = $db->prepare($request);
0 ignored issues
show
Bug introduced by
The variable $db does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
114
115
		while($aw = $qr->fetch()) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after WHILE keyword; 0 found
Loading history...
116
			$pl = new playerRanking();
117
118
			if (isset($aw['id'])) { $pl->id = $aw['id']; } 
119
			if (isset($aw['rRanking'])) { $pl->rRanking = $aw['rRanking']; }
120
			if (isset($aw['rPlayer'])) { $pl->rPlayer = $aw['rPlayer']; } 
121
			if (isset($aw['general'])) { $pl->general = $aw['general']; }
122
			if (isset($aw['generalPosition'])) { $pl->generalPosition = $aw['generalPosition']; }
123
			if (isset($aw['generalVariation'])) { $pl->generalVariation = $aw['generalVariation']; }
124
			if (isset($aw['experience'])) { $pl->experience = $aw['experience']; }
125
			if (isset($aw['experiencePosition'])) { $pl->experiencePosition = $aw['experiencePosition']; }
126
			if (isset($aw['experienceVariation'])) { $pl->experienceVariation = $aw['experienceVariation']; }
127
			if (isset($aw['fight'])) { $pl->fight = $aw['fight']; }
128
			if (isset($aw['fightPosition'])) { $pl->fightPosition = $aw['fightPosition']; }
129
			if (isset($aw['fightVariation'])) { $pl->fightVariation = $aw['fightVariation']; }
130
			if (isset($aw['trader'])) { $pl->trader = $aw['trader']; }
131
			if (isset($aw['traderPosition'])) { $pl->traderPosition = $aw['traderPosition']; }
132
			if (isset($aw['traderVariation'])) { $pl->traderVariation = $aw['traderVariation']; }
133
			if (isset($aw['armies'])) { $pl->armies = $aw['armies']; }
134
			if (isset($aw['armiesPosition'])) { $pl->armiesPosition = $aw['armiesPosition']; }
135
			if (isset($aw['armiesVariation'])) { $pl->armiesVariation = $aw['armiesVariation']; }
136
			if (isset($aw['resources'])) { $pl->resources = $aw['resources']; }
137
			if (isset($aw['resourcesPosition'])) { $pl->resourcesPosition = $aw['resourcesPosition']; }
138
			if (isset($aw['resourcesVariation'])) { $pl->resourcesVariation = $aw['resourcesVariation']; }
139
			if (isset($aw['butcher'])) { $pl->butcher = $aw['butcher']; }
140
			if (isset($aw['butcherPosition'])) { $pl->butcherPosition = $aw['butcherPosition']; }
141
			if (isset($aw['butcherVariation'])) { $pl->butcherVariation = $aw['butcherVariation']; }
142
143
			if (isset($aw['color'])) { $pl->color = $aw['color']; }
144
			if (isset($aw['name'])) { $pl->name = $aw['name']; }
145
			if (isset($aw['avatar'])) { $pl->avatar = $aw['avatar']; }
146
			if (isset($aw['status'])) { $pl->status = $aw['status']; }
147
148
			$currentT = $this->_Add($pl);
0 ignored issues
show
Unused Code introduced by
$currentT is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
149
		}
150
	}
151
152
	public function add(playerRanking $pl)
153
	{
154
		$qr = $this->database->prepare('INSERT INTO
155
			playerRanking(rRanking, rPlayer, 
156
				general, generalPosition, generalVariation, 
157
				experience, experiencePosition, experienceVariation, 
158
				fight, fightPosition, fightVariation, 
159
				trader, traderPosition, traderVariation, 
160
				armies, armiesPosition, armiesVariation, 
161
				resources, resourcesPosition, resourcesVariation, 
162
				butcher, butcherPosition, butcherVariation, 
163
				butcherDestroyedPEV, butcherLostPEV, victories, defeat)
164
			VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 
165
			?, ?, ?, ?)');
166
		$qr->execute(array(
167
			$pl->rRanking,
168
			$pl->rPlayer, 
169
			$pl->general,
170
			$pl->generalPosition,
171
			$pl->generalVariation,
172
			$pl->experience,
173
			$pl->experiencePosition,
174
			$pl->experienceVariation,
175
			$pl->fight,
176
			$pl->fightPosition,
177
			$pl->fightVariation,
178
			$pl->trader,
179
			$pl->traderPosition,
180
			$pl->traderVariation,
181
			$pl->armies,
182
			$pl->armiesPosition,
183
			$pl->armiesVariation,
184
			$pl->resources,
185
			$pl->resourcesPosition,
186
			$pl->resourcesVariation,
187
			$pl->butcher,
188
			$pl->butcherPosition,
189
			$pl->butcherVariation,
190
			$pl->butcherDestroyedPEV,
191
			$pl->butcherLostPEV,
192
			$pl->victories,
193
			$pl->defeat
194
		));
195
196
		$pl->id = $this->database->lastInsertId();
197
198
		$this->_Add($pl);
199
	}
200
201
	public function save() {
202
		$rankings = $this->_Save();
203
204
		foreach ($rankings AS $pl) {
0 ignored issues
show
Coding Style introduced by
AS keyword must be lowercase; expected "as" but found "AS"
Loading history...
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected as, but found AS.
Loading history...
205
			$qr = $this->database->prepare('UPDATE playerRanking
206
				SET	id = ?,
207
					rRanking = ?,
208
					rPlayer = ?, 
209
					general = ?,
210
					generalPosition = ?,
211
					generalVariation = ?,
212
					experience = ?,
213
					experiencePosition = ?,
214
					experienceVariation = ?,
215
					fight = ?,
216
					fightPosition = ?,
217
					fightVariation = ?,
218
					trader = ?,
219
					traderPosition = ?,
220
					traderVariation = ?,
221
					armies = ?,
222
					armiesPosition = ?,
223
					armiesVariation = ?,
224
					resources = ?,
225
					resourcesPosition = ?,
226
					resourcesVariation = ?,
227
					butcher = ?,
228
					butcherPosition = ?,
229
					butcherVariation = ?
230
				WHERE id = ?');
231
			$qr->execute(array(
232
				$pl->id,
233
				$pl->rRanking,
234
				$pl->rPlayer, 
235
				$pl->general,
236
				$pl->generalPosition,
237
				$pl->generalVariation,
238
				$pl->experience,
239
				$pl->experiencePosition,
240
				$pl->experienceVariation,
241
				$pl->fight,
242
				$pl->fightPosition,
243
				$pl->fightVariation,
244
				$pl->trader,
245
				$pl->traderPosition,
246
				$pl->traderVariation,
247
				$pl->armies,
248
				$pl->armiesPosition,
249
				$pl->armiesVariation,
250
				$pl->resources,
251
				$pl->resourcesPosition,
252
				$pl->resourcesVariation,
253
				$pl->butcher,
254
				$pl->butcherPosition,
255
				$pl->butcherVariation,
256
				$pl->id
257
			));
258
		}
259
	}
260
}