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.

FactionRankingManager::loadByRequest()   F
last analyzed

Complexity

Conditions 18
Paths > 20000

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 23
nc 65537
nop 2
dl 0
loc 31
rs 2.7087
c 0
b 0
f 0

How to fix   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
2
3
/**
4
 * FactionRankingManager
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\FactionRanking;
18
19
class FactionRankingManager extends Manager {
20
	protected $managerType = '_FactionRanking';
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 faction = 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);
39
		$formatOrder = Utils::arrayToOrder($order);
40
		$formatLimit = Utils::arrayToLimit($limit);
41
42
		$qr = $this->database->prepare('SELECT *
43
			FROM factionRanking AS fr
44
			' . $formatWhere . '
45
			' . $formatOrder . '
46
			' . $formatLimit
47
		);
48
49
		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...
50
			if (is_array($v)) {
51
				foreach ($v as $p) {
52
					$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...
53
				}
54
			} else {
55
				$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...
56
			}
57
		}
58
59
		if(empty($valuesArray)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
60
			$qr->execute();
61
		} else {
62
			$qr->execute($valuesArray);
63
		}
64
65
		while($aw = $qr->fetch()) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after WHILE keyword; 0 found
Loading history...
66
			$fr = new FactionRanking();
67
68
			$fr->id = $aw['id']; 
69
			$fr->rRanking = $aw['rRanking'];
70
			$fr->rFaction = $aw['rFaction']; 
71
			$fr->points = $aw['points'];
72
			$fr->pointsPosition = $aw['pointsPosition'];
73
			$fr->pointsVariation = $aw['pointsVariation'];
74
			$fr->newPoints = $aw['newPoints'];
75
			$fr->general = $aw['general'];
76
			$fr->generalPosition = $aw['generalPosition'];
77
			$fr->generalVariation = $aw['generalVariation'];
78
			$fr->wealth = $aw['wealth'];
79
			$fr->wealthPosition = $aw['wealthPosition'];
80
			$fr->wealthVariation = $aw['wealthVariation'];
81
			$fr->territorial = $aw['territorial'];
82
			$fr->territorialPosition = $aw['territorialPosition'];
83
			$fr->territorialVariation = $aw['territorialVariation'];
84
85
			$currentT = $this->_Add($fr);
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...
86
		}
87
	}
88
89
	public function loadByRequest($request, $args = array()) {
90
		$qr = $this->database->prepare('SELECT *
91
			FROM factionRanking AS fr
92
			' . $request
93
		);
94
95
		$qr->execute($args);
96
97
		while ($aw = $qr->fetch()) {
98
			$fr = new FactionRanking();
99
100
			$fr->id = isset($aw['id']) ? $aw['id'] : NULL;
101
			$fr->rRanking = isset($aw['rRanking']) ? $aw['rRanking'] : NULL;
102
			$fr->rFaction = isset($aw['rFaction']) ? $aw['rFaction'] : NULL;
103
			$fr->points = isset($aw['points']) ? $aw['points'] : NULL;
104
			$fr->pointsPosition = isset($aw['pointsPosition']) ? $aw['pointsPosition'] : NULL;
105
			$fr->pointsVariation = isset($aw['pointsVariation']) ? $aw['pointsVariation'] : NULL;
106
			$fr->newPoints = isset($aw['newPoints']) ? $aw['newPoints'] : NULL;
107
			$fr->general = isset($aw['general']) ? $aw['general'] : NULL;
108
			$fr->generalPosition = isset($aw['generalPosition']) ? $aw['generalPosition'] : NULL;
109
			$fr->generalVariation = isset($aw['generalVariation']) ? $aw['generalVariation'] : NULL;
110
			$fr->wealth = isset($aw['wealth']) ? $aw['wealth'] : NULL;
111
			$fr->wealthPosition = isset($aw['wealthPosition']) ? $aw['wealthPosition'] : NULL;
112
			$fr->wealthVariation = isset($aw['wealthVariation']) ? $aw['wealthVariation'] : NULL;
113
			$fr->territorial = isset($aw['territorial']) ? $aw['territorial'] : NULL;
114
			$fr->territorialPosition = isset($aw['territorialPosition']) ? $aw['territorialPosition'] : NULL;
115
			$fr->territorialVariation = isset($aw['territorialVariation']) ? $aw['territorialVariation'] : NULL;
116
117
			$currentT = $this->_Add($fr);
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...
118
		}
119
	}
120
121
	public function add(FactionRanking $fr) {
122
		$qr = $this->database->prepare('INSERT INTO
123
			factionRanking(rRanking, rFaction, points, pointsPosition, pointsVariation, newPoints, general, generalPosition, generalVariation, 
124
				wealth, wealthPosition, wealthVariation, territorial, territorialPosition, territorialVariation)
125
			VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
126
		$qr->execute(array(
127
			$fr->rRanking,
128
			$fr->rFaction, 
129
			$fr->points,
130
			$fr->pointsPosition,
131
			$fr->pointsVariation,
132
			$fr->newPoints,
133
			$fr->general,
134
			$fr->generalPosition,
135
			$fr->generalVariation,
136
			$fr->wealth,
137
			$fr->wealthPosition,
138
			$fr->wealthVariation,
139
			$fr->territorial,
140
			$fr->territorialPosition,
141
			$fr->territorialVariation
142
		));
143
144
		$fr->id = $this->database->lastInsertId();
145
146
		$this->_Add($fr);
147
	}
148
149
	public function save() {
150
		$rankings = $this->_Save();
151
152
		foreach ($rankings AS $fr) {
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...
153
			$qr = $this->database->prepare('UPDATE factionRanking
154
				SET	id = ?,
155
					rRanking = ?,
156
					rFaction = ?,
157
					points = ?,
158
					pointsPosition = ?,
159
					pointsVariation = ?, 
160
					newPoints = ?,
161
					general = ?,
162
					generalPosition = ?,
163
					generalVariation = ?,
164
					wealth = ?,
165
					wealthPosition = ?,
166
					wealthVariation = ?,
167
					territorial = ?,
168
					territorialPosition = ?,
169
					territorialVariation = ?
170
				WHERE id = ?');
171
			$qr->execute(array(
172
				$fr->id,
173
				$fr->rRanking,
174
				$fr->rFaction, 
175
				$fr->points,
176
				$fr->pointsPosition,
177
				$fr->pointsVariation,
178
				$fr->newPoints,
179
				$fr->general,
180
				$fr->generalPosition,
181
				$fr->generalVariation,
182
				$fr->wealth,
183
				$fr->wealthPosition,
184
				$fr->wealthVariation,
185
				$fr->territorial,
186
				$fr->territorialPosition,
187
				$fr->territorialVariation,
188
				$fr->id
189
			));
190
		}
191
	}
192
}