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

UserEventCrawler   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 118
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserYears() 0 19 2
A getEvents() 0 10 2
A getYearPages() 0 10 2
A getYearCount() 0 25 4
A countListPages() 0 6 2
A countListEvents() 0 4 1
A crawlUrl() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\LastFm\Crawler;
13
14
use Symfony\Component\DomCrawler\Crawler;
15
16
final class UserEventCrawler extends AbstractCrawler implements UserEventCrawlerInterface
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function getUserYears(string $username): ?array
22
    {
23
        $node = $this->crawlUrl($username);
24
25
        if (null === $node) {
26
            return null;
27
        }
28
29
        $years = $node->filter('.content-top .secondary-nav-item-link')
30
            ->each(static function (Crawler $node) {
31
                return (int) trim($node->text());
32
            })
33
        ;
34
35
        sort($years);
36
        array_shift($years);
37
38
        return $years;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $years; (array) is incompatible with the return type declared by the interface Core23\LastFm\Crawler\Us...Interface::getUserYears of type integer[]|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...
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getEvents(string $username, ?int $year, int $page = 1): ?array
45
    {
46
        $node = $this->crawlUrl($username, $year, $page);
47
48
        if (null === $node) {
49
            return null;
50
        }
51
52
        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\Us...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...
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getYearPages(string $username, ?int $year): ?int
59
    {
60
        $node = $this->crawlUrl($username, $year);
61
62
        if (null === $node) {
63
            return null;
64
        }
65
66
        return $this->countListPages($node);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getYearCount(string $username, ?int $year, int $page = 1): ?int
73
    {
74
        $node = $this->crawlUrl($username, $year, $page);
75
76
        if (null === $node) {
77
            return null;
78
        }
79
80
        $perPage = $this->countListEvents($node);
81
        $pages   = $this->countListPages($node);
82
83
        if ($pages) {
84
            $node = $this->crawlUrl($username, $year, $pages);
85
86
            if (null === $node) {
87
                return $perPage;
88
            }
89
90
            $count = $this->countListEvents($node);
91
92
            return ($pages - 1) * $perPage + $count;
93
        }
94
95
        return $perPage;
96
    }
97
98
    /**
99
     * @param Crawler $node
100
     *
101
     * @return int
102
     */
103
    private function countListPages(Crawler $node): int
104
    {
105
        $pagination = $this->parseString($node->filter('.pagination .pages'));
106
107
        return $pagination ? (int) preg_replace('/.* of /', '', $pagination) : 1;
108
    }
109
110
    /**
111
     * @param Crawler $node
112
     *
113
     * @return int
114
     */
115
    private function countListEvents(Crawler $node): int
116
    {
117
        return $node->filter('.events-list-item')->count();
118
    }
119
120
    /**
121
     * @param string   $username
122
     * @param int|null $year
123
     * @param int      $page
124
     *
125
     * @return Crawler|null
126
     */
127
    private function crawlUrl(string $username, ?int $year = null, int $page = 1): ?Crawler
128
    {
129
        $url = 'http://www.last.fm/user/'.$username.'/events/'.($year ?: '').'?page='.$page;
130
131
        return $this->crawl($url);
132
    }
133
}
134