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 ( f85b82...dc3bec )
by Christian
02:12
created

EventListCrawler::getPages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
/*
4
 * (c) Christian Gripp <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Core23\LastFm\Crawler;
11
12
use Core23\LastFm\Model\GeoLocation;
13
use Symfony\Component\DomCrawler\Crawler;
14
15
final class EventListCrawler extends AbstractCrawler implements EventListCrawlerInterface
16
{
17
    private const BASE_URL = 'https://www.last.fm/events';
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function getEvents(GeoLocation $location, $radius = 100, int $page = 1): ?array
23
    {
24
        $node = $this->crawlUrl($location, $radius, $page);
25
26
        if (null === $node) {
27
            return null;
28
        }
29
30
        return $this->crawlEventList($node);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->crawlEventList($node); (array) is incompatible with the return type declared by the interface Core23\LastFm\Crawler\Ev...lerInterface::getEvents of type Core23\LastFm\Model\Event[]|null.

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...
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getPages(GeoLocation $location, $radius = 100): int
37
    {
38
        $node = $this->crawlUrl($location, $radius);
39
40
        if (null === $node) {
41
            return null;
42
        }
43
44
        $lastNode = $node->filter('.pagination .pagination-page')->last();
45
46
        return (int) $lastNode->text();
47
    }
48
49
    /**
50
     * @param GeoLocation $location
51
     * @param int         $radius
52
     * @param int         $page
53
     *
54
     * @return Crawler|null
55
     */
56
    private function crawlUrl(GeoLocation $location, int $radius, int $page = 1): ?Crawler
57
    {
58
        $url = static::BASE_URL;
59
        $url .= '?location_0=Germany';
60
        $url .= '&location_1='.$location->getLongitude();
61
        $url .= '&location_2='.$location->getLatitude();
62
        $url .= '&radius='.($radius*1000);
63
        $url .= '&page='.$page;
64
65
        return $this->crawl($url);
66
    }
67
}
68