api_dna::json_data()   F
last analyzed

Complexity

Conditions 10
Paths 257

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 25
rs 3.1304
cc 10
eloc 20
nc 257
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_dna implements apiEndpoint
20
{
21
	public function getDescription()
22
	{
23
		return array("type" => "description", "message" =>
24
				"Outputs fitting DNA for all ships killed."
25
			);
26
	}
27
28
	public function getAcceptedParameters()
29
	{
30
		return array("type" => "parameters", "parameters" =>
31
			array(
32
				"page" => "Pagination.",
33
				"killID" => "Get only data for a single kill."
34
			)
35
		);
36
	}
37
38
	public function execute($parameters)
39
	{
40
		$page = isset($parameters["page"]) ? $parameters["page"] : 1;
41
		if(isset($parameters["killID"]))
42
			$killIDs[] = (int) $parameters["killID"];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$killIDs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $killIDs = 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...
43
44
		if(!isset($killIDs))
45
		{
46
			$kills = Feed::getKills(array("limit" => 1000, "cacheTime" => 3600, "page" => $page));
47
			foreach($kills as $kill)
48
			{
49
				$kill = json_decode($kill, true);
50
				$killIDs[] = (int) $kill["killID"];
0 ignored issues
show
Bug introduced by
The variable $killIDs 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...
51
			}
52
		}
53
		return self::json_data($killIDs);
54
	}
55
56
	private function json_data($killIDs)
57
	{
58
		$dna = array();
59
		foreach($killIDs as $killID)
60
		{
61
			$killdata = Kills::getKillDetails($killID);
62
			$dna[] = array(
63
				"killtime" => $killdata["info"]["dttm"], 
64
				"SolarSystemName" => $killdata["info"]["solarSystemName"],
65
				"solarSystemID" => $killdata["info"]["solarSystemID"],
66
				"regionID" => $killdata["info"]["regionID"],
67
				"regionName" => $killdata["info"]["regionName"],
68
				"victimCharacterID" => isset($killdata["victim"]["characterID"]) ? $killdata["victim"]["characterID"] : null,
69
				"victimCharacterName" => isset($killdata["victim"]["characterName"]) ? $killdata["victim"]["characterName"] : null,
70
				"victimCorporationID" => isset($killdata["victim"]["corporationID"]) ? $killdata["victim"]["corporationID"] : null,
71
				"victimCorporationName" => isset($killdata["victim"]["corporationName"]) ? $killdata["victim"]["corporationName"] : null,
72
				"victimAllianceID" => isset($killdata["victim"]["allianceID"]) ? $killdata["victim"]["allianceID"] : null,
73
				"victimAllianceName" => isset($killdata["victim"]["allianceName"]) ? $killdata["victim"]["allianceName"] : null,
74
				"victimFactionID" => isset($killdata["victim"]["factionID"]) ? $killdata["victim"]["factionID"] : null,
75
				"victimFactionName" => isset($killdata["victim"]["factionName"]) ? $killdata["victim"]["factionName"] : null,
76
				"dna" => Fitting::DNA($killdata["items"], $killdata["victim"]["shipTypeID"])
77
			);
78
		}
79
		return $dna;
80
	}
81
}
82