WeatherJsonController::jsonAction()   B
last analyzed

Complexity

Conditions 8
Paths 10

Size

Total Lines 61
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 13.1383

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 51
c 1
b 0
f 0
nc 10
nop 0
dl 0
loc 61
ccs 29
cts 51
cp 0.5686
crap 13.1383
rs 7.8246

How to fix   Long Method   

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
namespace Anax\Controller;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
use Anax\Controller\IpModel;
8
9
// use Anax\Route\Exception\ForbiddenException;
10
// use Anax\Route\Exception\NotFoundException;
11
// use Anax\Route\Exception\InternalErrorException;
12
13
/**
14
* A sample controller to show how a controller class can be implemented.
15
* The controller will be injected with $di if implementing the interface
16
* ContainerInjectableInterface, like this sample class does.
17
* The controller is mounted on a particular route and can then handle all
18
* requests for that mount point.
19
*
20
@SuppressWarnings(PHPMD.TooManyPublicMethods)
21
*/
22
class WeatherJsonController implements ContainerInjectableInterface
23
{
24
    use ContainerInjectableTrait;
25
26
27
28
    /**
29
    * @var string $db a sample member variable that gets initialised
30
    */
31
    private $model;
32
33
34
35
    /**
36
    * The initialize method is optional and will always be called before the
37
    * target method/action. This is a convienient method where you could
38
    * setup internal properties that are commonly used by several methods.
39
    *
40
    * @return void
41
    */
42 2
    public function initialize() : void
43
    {
44
        // Use to initialise member variables.
45 2
        $this->model = new IpModel();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Anax\Controller\IpModel() of type Anax\Controller\IpModel is incompatible with the declared type string of property $model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46 2
    }
47
48
49
50
    /**
51
     * This is the index method action, it handles:
52
     * ANY METHOD mountpoint
53
     * ANY METHOD mountpoint/
54
     * ANY METHOD mountpoint/index
55
     *
56
     * @return string
57
     */
58
59 1
    public function indexAction() : object
60
    {
61 1
        $title = "IP JSON";
62 1
        $page = $this->di->get("page");
63
64 1
        $page->add("weather/json_docs");
65 1
        return $page->render([
66 1
            "title" => $title,
67
        ]);
68
    }
69
70
    /**
71
    * Return array
72
    *@SuppressWarnings(PHPMD.UnusedLocalVariable)
73
    */
74 1
    public function jsonAction() : array
75
    {
76 1
        $weather = $this->di->get("weather");
77 1
        $title = "ip validator with map";
0 ignored issues
show
Unused Code introduced by
The assignment to $title is dead and can be removed.
Loading history...
78 1
        $request = $this->di->get("request");
79 1
        $ipAddress = $request->getGet("ipMap") ?? null;
80 1
        $version = null;
81 1
        $localIP = $this->model->local();
0 ignored issues
show
Unused Code introduced by
The assignment to $localIP is dead and can be removed.
Loading history...
82 1
        $res = null;
83 1
        $lat = $res;//["latitude"];
84 1
        $long = $res;//["longitude"];
85
        // $t = $weather->histWeather($lat, $long);
86 1
        $country = $res;//["country_name"];
87 1
        $region = $res;//["region_name"];
88 1
        $hostname = null;
89 1
        $check = null;
90 1
        $answer = null;
91 1
        $summaries = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $summaries is dead and can be removed.
Loading history...
92 1
        $dates = null;
93 1
        $type = $request->getGet("type") ?? null;
94 1
        $json = null;
95 1
        if ($ipAddress !== null && $type !== null) {
96
            if (strpos($ipAddress, ":") || strpos($ipAddress, ",") == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($ipAddress, ',') of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
97
                $response = $this->model->ipValidate($ipAddress);
98
                $check = $response[0];
99
                $hostname = $response[1];
100
                $version = $response[2];
101
                $res = $this->model->ipStack($ipAddress);
102
                $lat = $res["latitude"];
103
                $long = $res["longitude"];
104
            } elseif (strpos($ipAddress, ",")) {
105
                $latlong = explode(",", $ipAddress);
106
                $lat = $latlong[0];
107
                $long = $latlong[1];
108
            }
109
110
            if ($type == "Historik") {
111
                $answer = $weather->histWeather($lat, $long);
112
                $dates = $weather->dates('-31 day', 30);
113
                $json = $weather->json($answer, $dates, "h");
114
            } elseif ($type == "Kommande") {
115
                $answer = $weather->newWeather($lat, $long);
116
                $dates = $weather->dates(' +0 day', 7);
117
                $json = $weather->json($answer, $dates, "k");
118
            }
119
            $country = $res["country_name"];
120
            $region = $res["region_name"];
121
        }
122
123
        $data = [
124 1
            "check" => $check,
125 1
            "hostname" => $hostname,
126 1
            "type" => $version,
127 1
            "lat" => $lat,
128 1
            "long" => $long,
129 1
            "country" => $country,
130 1
            "region" => $region,
131 1
            "weather" => $json,
132
        ];
133
134 1
        return [$data];
135
    }
136
}
137