Test Setup Failed
Push — master ( f21594...860cfe )
by Lydia
02:48
created

Coordinates::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
namespace Anax\Model;
3
4
class Coordinates
5
{
6
    private $config;
7
    /**
8
     * Constructor, allow for $di to be injected.
9
     *
10
     * @param Array $config for api key 
11
     */
12
    public function __construct($config)
13
    {
14
        $this->config = $config;
15
    }
16
17
    public function getCoordinates(String $search) : array
18
    {
19
        $apiKeyOpenCage = $config["coordinates"];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $config seems to be never defined.
Loading history...
20
21
        if (is_string($search)) {
0 ignored issues
show
introduced by
The condition is_string($search) is always true.
Loading history...
22
            $details = json_decode(file_get_contents("https://api.opencagedata.com/geocode/v1/json?q=$search&key={$apiKeyOpenCage["key"]}"));
23
            $results = $details->results;
24
            if (isset($results[0]->geometry)) {
25
                $valid = "Valid";
26
                $lat = $results[0]->geometry->lat;
27
                $long = $results[0]->geometry->lng;
28
            } else {
29
                $valid = "Not valid, showing Gold Coast instead";
30
                $lat = -28.016666;
31
                $long = 153.399994;
32
            }
33
            return [
34
                "valid" => $valid,
35
                "lat" => $lat,
36
                "long" => $long
37
            ];
38
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 21 is false. This is incompatible with the type-hinted return array. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
39
    }
40
}
41