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 ( c9bd98...b01a3a )
by Christian
06:56
created

AbstractCrawler::parseImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ni-ju-san CMS.
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 Core23\LastFm\Connection\ConnectionInterface;
15
use Symfony\Component\DomCrawler\Crawler;
16
17
abstract class AbstractCrawler
18
{
19
    const URL_PREFIX = 'http://last.fm';
20
21
    /**
22
     * @var ConnectionInterface
23
     */
24
    protected $connection;
25
26
    /**
27
     * AbstractService constructor.
28
     *
29
     * @param ConnectionInterface $connection
30
     */
31
    public function __construct(ConnectionInterface $connection)
32
    {
33
        $this->connection = $connection;
34
    }
35
36
    /**
37
     * Crawles a url.
38
     *
39
     * @param string $url
40
     *
41
     * @return Crawler
42
     */
43
    final protected function crawl($url)
44
    {
45
        $content = $this->connection->loadPage($url);
46
47
        return new Crawler($content);
48
    }
49
50
    /**
51
     * Parses a url node.
52
     *
53
     * @param Crawler $node
54
     * @param string  $attr
55
     *
56
     * @return null|string
57
     */
58
    final protected function parseUrl(Crawler $node, $attr = 'href')
59
    {
60
        if ($node == null || $node->count() === 0) {
61
            return;
62
        }
63
64
        if ($url = $node->attr($attr)) {
65
            return preg_replace('/^\//', static::URL_PREFIX.'/', $url);
66
        }
67
68
        return;
69
    }
70
71
    /**
72
     * Parses an image node.
73
     *
74
     * @param Crawler $node
75
     *
76
     * @return null|string
77
     */
78
    final protected function parseImage(Crawler $node)
79
    {
80
        return $this->parseUrl($node, 'src');
81
    }
82
83
    /**
84
     * Parses a string node.
85
     *
86
     * @param Crawler $node
87
     *
88
     * @param bool    $multiline
89
     *
90
     * @return null|string
91
     */
92
    final protected function parseString(Crawler $node, $multiline = false)
93
    {
94
        if ($node == null || $node->count() === 0) {
95
            return;
96
        }
97
98
        $content = $node->attr('content');
99
100
        if (!$content) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $content of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
101
            if ($multiline) {
102
                $content = strip_tags($node->html());
103
            } else {
104
                $content = $node->text();
105
            }
106
        }
107
108
        return trim($content);
109
    }
110
111
    /**
112
     * Parses a date note.
113
     *
114
     * @param Crawler $node
115
     *
116
     * @return \DateTime|null
117
     */
118
    final protected function parseDate(Crawler $node)
119
    {
120
        $content = $this->parseString($node);
121
122
        if ($content) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $content of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
123
            return new \DateTime($content);
124
        }
125
126
        return;
127
    }
128
}
129