Weather::fetchHistoricalWeather()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 31
c 1
b 0
f 0
nc 12
nop 2
dl 0
loc 51
ccs 0
cts 28
cp 0
crap 42
rs 8.8017

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
namespace Lioo19\Models;
3
4
/**
5
 * Class for retriving weather data
6
 *
7
 */
8
class Weather
9
{
10
    private function getApikey()
11
    {
12
        $apikey = require ANAX_INSTALL_PATH . "/config/apikeys.php";
13
        $apikey = $apikey["openweathermap"];
14
        return $apikey;
15
    }
16
17
    /**
18
    * Method for retriving the current date, and the five previous days
19
    * @return array with the different dates given in unix
20
    *
21
    */
22 1
    public function getDate()
23
    {
24 1
        $days = [];
25 1
        for ($i = 0; $i > -5; $i--) {
26 1
            $days[] = strtotime("$i days");
27
        }
28
29 1
        return $days;
30
    }
31
32
    /**
33
    * Method for retriving the weather info, given coordinates
34
    *
35
    */
36
    public function fetchForecastWeather($lon, $lat, $url = "api.openweathermap.org/data/2.5/onecall?lat=")
37
    {
38
        $curl = curl_init();
39
        $apikey = $this->getApikey();
40
        if (strlen($lon) > 0 && strlen($lat) > 0) {
41
            //sets the url for curl to the correct one
42
            curl_setopt($curl, CURLOPT_URL, "$url" . $lat . "&lon=" . $lon .
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
            curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_URL, "$url" . $lat . "&lon=" . $lon .
Loading history...
43
                        "&exclude=minutely,hourly&lang=se&units=metric&APPID=" . $apikey);
44
            //returns a string
45
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
46
            //execute the started curl-session
47
            $output = curl_exec($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
            $output = curl_exec(/** @scrutinizer ignore-type */ $curl);
Loading history...
48
            $exploded = json_decode($output, true);
49
50
            // $data = $exploded;
51
            if (array_key_exists("message", $exploded)) {
52
                $data = [
53
                    "message" => $exploded["message"]
54
                ];
55
                return $data;
56
            }
57
            $data = $exploded;
58
            //close curl-session to free up space
59
            curl_close($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
            curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
60
61
            return $data;
62
        } else {
63
            return "Not a valid lon/lat";
64
        }
65
    }
66
67
    /**
68
    * Method for retriving the weather info, given coordinates
69
    * @return object With parts of valid JSON-repsonse
70
    * Need to make five different API-calls, one for each day
71
    *
72
    */
73
    public function fetchHistoricalWeather($lon, $lat)
74
    {
75
        $apikey = $this->getApikey();
76
        $url = "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat="
77
                . $lat . "&lon=" . $lon . "&lang=se&units=metric&dt=";
78
        $urlcont = "&APPID=" . $apikey;
79
        $days = $this->getDate();
80
81
        //sets the url for curl to the correct one
82
        $multi = curl_multi_init();
83
        $all = [];
84
        foreach ($days as $day) {
85
            $c = curl_init($url . $day . $urlcont);
86
            curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
0 ignored issues
show
Bug introduced by
It seems like $c can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
            curl_setopt(/** @scrutinizer ignore-type */ $c, CURLOPT_RETURNTRANSFER, 1);
Loading history...
87
            curl_multi_add_handle($multi, $c);
0 ignored issues
show
Bug introduced by
It seems like $c can also be of type false; however, parameter $ch of curl_multi_add_handle() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
            curl_multi_add_handle($multi, /** @scrutinizer ignore-type */ $c);
Loading history...
88
            $all[] = $c;
89
        }
90
91
        $run = null;
92
93
        do {
94
            curl_multi_exec($multi, $run);
95
        } while ($run);
96
97
        //remove handles
98
        foreach ($all as $c) {
99
            curl_multi_remove_handle($multi, $c);
100
        }
101
        //close curl sessions
102
        curl_multi_close($multi);
103
104
        $res = [];
105
        // $res = $days;
106
107
108
        foreach ($all as $c) {
109
            $output = curl_multi_getcontent($c);
110
            $exploded = json_decode($output, true);
111
            if (array_key_exists("message", $exploded)) {
112
                $data = [
113
                    "message" => $exploded["message"]
114
                ];
115
                return $data;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $data returns the type array which is incompatible with the documented return type object.
Loading history...
116
            } else {
117
                $exploded = $exploded["current"];
118
            }
119
120
            $res[] = $exploded;
121
        }
122
123
        return $res;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $res returns the type array which is incompatible with the documented return type object.
Loading history...
124
    }
125
}
126