api_battles::execute()   C
last analyzed

Complexity

Conditions 12
Paths 44

Size

Total Lines 49
Code Lines 32

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 49
rs 5.1474
cc 12
eloc 32
nc 44
nop 1

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
/* zKillboard
3
 * Copyright (C) 2012-2015 EVE-KILL Team and EVSCO.
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
class api_battles implements apiEndpoint
20
{
21
	public function getDescription()
22
	{
23
		return array("type" => "description", "message" =>
24
			"Battles lists the battles that has been sorted by users. You can list all the battles that are available, and you can call up data for a single battle."
25
		);
26
	}
27
28
	public function getAcceptedParameters()
29
	{
30
		return array("type" => "parameters", "parameters" =>
31
			array(
32
				"list" => "Lists all the battles available to peruse.",
33
				"battle/#/" => "Lists data for a single battle. Replace # with a battleID."
34
			)
35
		);
36
	}
37
	public function execute($parameters)
38
	{
39
		$command = isset($parameters["battles"]) ? $parameters["battles"] : NULL;
40
		$battleID = isset($parameters["battle"]) ? $parameters["battle"] : 0;
41
42
		if($command == "list")
43
		{
44
			$data = array();
0 ignored issues
show
Unused Code introduced by
$data 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...
45
			$battles = Db::query("SELECT * FROM zz_battles", array(), 360);
46
			foreach($battles as $key => $value)
47
			{
48
				unset($battles[$key]["teamAJson"]);
49
				unset($battles[$key]["teamBJson"]);
50
			}
51
			return $battles;
52
		}
53
		elseif($command == "battle")
54
		{
55
			$getData = Db::queryRow("SELECT * FROM zz_battles WHERE battleID = :battleID", array(":battleID" => $battleID), 360);
56
57
			foreach($getData as $key => $value)
58
			{
59
				switch($key)
60
				{
61
					case "teamAinvolved":
62
					case "teamBinvolved":
63
						$data[$key] = json_decode($value, true);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = 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...
64
					break;
65
66
					case "teamAJson":
67
					case "teamBJson":
68
						$subJson = json_decode($value, true);
69
						foreach($subJson as $d)
70
							$data[$key][] = json_decode($d, true);
0 ignored issues
show
Bug introduced by
The variable $data 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...
71
					break;
72
73
					default:
74
							$data[$key] = $value;
75
					break;
76
				}
77
			}
78
			return $data;
79
		}
80
		else
81
			return array(
82
				"type" => "error",
83
				"message" => "No valid parameter passed."
84
			);
85
	}
86
}
87