GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 1faea8...2295f4 )
by Eric
03:10
created

PositionalElevationRequest::addLocations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Ivory Google Map package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\GoogleMap\Service\Elevation\Request;
13
14
use Ivory\GoogleMap\Service\Base\Location\LocationInterface;
15
16
/**
17
 * @author GeLo <[email protected]>
18
 */
19
class PositionalElevationRequest implements ElevationRequestInterface
20
{
21
    /**
22
     * @var LocationInterface[]
23
     */
24
    private $locations = [];
25
26
    /**
27
     * @param LocationInterface[] $locations
28
     */
29
    public function __construct(array $locations)
30
    {
31
        $this->setLocations($locations);
32
    }
33
34
    /**
35
     * @return bool
36
     */
37
    public function hasLocations()
38
    {
39
        return !empty($this->locations);
40
    }
41
42
    /**
43
     * @return LocationInterface[]
44
     */
45
    public function getLocations()
46
    {
47
        return $this->locations;
48
    }
49
50
    /**
51
     * @param LocationInterface[] $locations
52
     */
53
    public function setLocations(array $locations)
54
    {
55
        $this->locations = [];
56
        $this->addLocations($locations);
57
    }
58
59
    /**
60
     * @param LocationInterface[] $locations
61
     */
62
    public function addLocations(array $locations)
63
    {
64
        foreach ($locations as $location) {
65
            $this->addLocation($location);
66
        }
67
    }
68
69
    /**
70
     * @param LocationInterface $location
71
     *
72
     * @return bool
73
     */
74
    public function hasLocation(LocationInterface $location)
75
    {
76
        return in_array($location, $this->locations, true);
77
    }
78
79
    /**
80
     * @param LocationInterface $location
81
     */
82
    public function addLocation(LocationInterface $location)
83
    {
84
        if (!$this->hasLocation($location)) {
85
            $this->locations[] = $location;
86
        }
87
    }
88
89
    /**
90
     * @param LocationInterface $location
91
     */
92
    public function removeLocation(LocationInterface $location)
93
    {
94
        unset($this->locations[array_search($location, $this->locations, true)]);
95
        $this->locations = array_values($this->locations);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function build()
102
    {
103
        return ['locations' => implode('|', array_map(function (LocationInterface $location) {
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('locations'...}, $this->locations))); (array<string,string>) is incompatible with the return type declared by the interface Ivory\GoogleMap\Service\...RequestInterface::build of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
104
            return $location->build();
105
        }, $this->locations))];
106
    }
107
}
108