GeoTag::getWeatherMultiCurl()   B
last analyzed

Complexity

Conditions 8
Paths 32

Size

Total Lines 56
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 56
ccs 30
cts 30
cp 1
rs 8.1635
cc 8
nc 32
nop 3
crap 8

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 Anax\Model;
3
4
class GeoTag
5
{
6
7
    private $config;
8
9 1
    public function __construct($config)
10
    {
11 1
        $this->config = $config;
12 1
    }
13
14 1
    public function setConfig($config)
15
    {
16 1
        $this->config = $config;
17 1
    }
18
19
    /**
20
     *
21
     * @param $when -> 7 days in future, or 30 days in the past, $lat -> Latitude, $long -> Longitude
0 ignored issues
show
Documentation Bug introduced by
The doc comment -> at position 0 could not be parsed: Unknown type name '-' at position 0 in ->.
Loading history...
22
     * @return array of weather using file_get_contents()
23
     *
24
     */
25
    public function getWeather($when, $lat, $long) : array
26
    {
27
        $now = time();
28
        $dates = array();
29
        $weather = array();
30
        $temp = array();
31
        $time = array();
32
33
        if ($when == "past") {
34
            for ($i = 0; $i < 30; $i++) {
35
                // 24h = 86400 unix time
36
                $now -= 86400;
37
                $dates[] = $now;
38
            }
39
        } else {
40
            for ($i = 0; $i < 7; $i++) {
41
                // 24h = 86400 unix time
42
                $now += 86400;
43
                $dates[] = $now;
44
            }
45
        }
46
47
        for ($i = 0; $i < count($dates); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
48
            $details =      json_decode(file_get_contents("https://api.darksky.net/forecast/{$this->config}/{$lat},{$long},{$dates[$i]}?lang=sv&units=si"));
49
50
            $time[] = $details->currently->time;
51
            $weather[] = $details->currently->summary;
52
            $temp[] = $details->currently->temperature;
53
        }
54
55
        return [
56
            "time" => $time,
57
            "weather" => $weather,
58
            "temp" => $temp,
59
        ];
60
    }
61
62
63
    /**
64
     *
65
     * @param $id -> time id of weather 7 days in future,
0 ignored issues
show
Documentation Bug introduced by
The doc comment -> at position 0 could not be parsed: Unknown type name '-' at position 0 in ->.
Loading history...
66
     * @param $lat -> Latitude,
67
     * @param $long -> Longitude,
68
     * @return array of weather using multicurl
69
     *
70
     */
71 1
    public function getWeatherMultiCurl($when, $lat, $long) : array
72
    {
73 1
        $dates = [];
74 1
        $now = time();
75
76 1
        if ($when == "past") {
77 1
            for ($i = 0; $i < 30; $i++) {
78
                // 24h = 86400 unix time
79 1
                $now -= 86400;
80 1
                $dates[] = $now;
81
            }
82
        } else {
83 1
            for ($i = 0; $i < 7; $i++) {
84
                // 24h = 86400 unix time
85 1
                $now += 86400;
86 1
                $dates[] = $now;
87
            }
88
        }
89
90 1
        $url = "https://api.darksky.net/forecast/{$this->config->config}/{$lat},{$long}";
91
92
        $options = [
93 1
            CURLOPT_RETURNTRANSFER => true,
94
        ];
95
96 1
        $mh = curl_multi_init();
97 1
        $chAll = [];
98 1
        foreach ($dates as $day) {
99 1
            $ch = curl_init("$url,{$day}?lang=sv&units=si");
100 1
            curl_setopt_array($ch, $options);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() 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

100
            curl_setopt_array(/** @scrutinizer ignore-type */ $ch, $options);
Loading history...
101 1
            curl_multi_add_handle($mh, $ch);
0 ignored issues
show
Bug introduced by
It seems like $ch 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

101
            curl_multi_add_handle($mh, /** @scrutinizer ignore-type */ $ch);
Loading history...
102 1
            $chAll[] = $ch;
103
        }
104
105
        // execute all queries simultaneously and countinue when all are complete
106 1
        $running = null;
107
        do {
108 1
            curl_multi_exec($mh, $running);
109 1
        } while ($running);
110
111
        // Close handle
112 1
        foreach ($chAll as $ch) {
113 1
            curl_multi_remove_handle($mh, $ch);
114
        }
115 1
        curl_multi_close($mh);
116
117
        //req are done, access results
118 1
        $response = [];
119 1
        foreach ($chAll as $ch) {
120 1
            $data = curl_multi_getcontent($ch);
121 1
            $response[] = json_decode($data, true);
122
123
            // $weather[] = $response->currently->summary;
124
            // $temp[] = $response->currently->temperature;
125
        }
126 1
        return $response;
127
    }
128
}
129