Completed
Push — master ( 00c88c...56f5c3 )
by Rémi
13s
created

SearchResultAdCrawler::setUrlParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Lbc\Crawler;
4
5
use Lbc\Filter\DefaultSanitizer;
6
use Lbc\Filter\PriceSanitizer;
7
use Lbc\Parser\AdUrlParser;
8
use Lbc\Parser\SearchResultUrlParser;
9
use League\Uri\Schemes\Http;
10
use Symfony\Component\DomCrawler\Crawler;
11
12
/**
13
 * Class SearchResultAdCrawler
14
 * @package Lbc\Crawler
15
 */
16
class SearchResultAdCrawler extends CrawlerAbstract
17
{
18
    /**
19
     * @param $url
20
     * @return SearchResultUrlParser
21
     */
22 10
    protected function setUrlParser($url)
23
    {
24 10
        $this->url = new AdUrlParser($url);
25 10
    }
26
27
    /**
28
     * Return the Ad's ID
29
     *
30
     * @return string
31
     */
32 10
    public function getId()
33
    {
34 10
        return $this->url->getId();
0 ignored issues
show
Bug introduced by
The method getId does only exist in Lbc\Parser\AdUrlParser, but not in Lbc\Parser\SearchResultUrlParser.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
35
    }
36
37
    /**
38
     * Return the title
39
     *
40
     * @return mixed
41
     */
42 10
    public function getTitle()
43
    {
44 10
        return DefaultSanitizer::clean($this->node->filter('h2')->text());
45
    }
46
47
    /**
48
     * Return the price
49
     *
50
     * @return int
51
     */
52 10
    public function getPrice()
53
    {
54 10
        if ($this->node->filter('*[itemprop=price]')->count()) {
55 10
            return PriceSanitizer::clean(
56 10
                $this->node->filter('*[itemprop=price]')->text()
57 10
            );
58
        }
59
        return 0;
60
    }
61
62
    /**
63
     * Return the Ad's URL
64
     *
65
     * @return string
66
     */
67 10
    public function getUrl()
68
    {
69 10
        return (string)Http::createFromString($this->url)->withScheme('https');
0 ignored issues
show
Documentation introduced by
$this->url is of type object<Lbc\Parser\AdUrlP...\SearchResultUrlParser>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
    }
71
72
    /**
73
     * Return the data and time the ad was created
74
     *
75
     * @return string
76
     */
77 10
    public function getCreatedAt()
78
    {
79 10
        $node = $this->node
80 10
            ->filter('*[itemprop=availabilityStarts]')
81 10
            ->first();
82
83 10
        $date = $node->attr('content');
84
85
        $time = $this->getFieldValue($node, 0, function ($value) {
86 10
            $value = trim($value);
87
88 10
            return substr($value, strpos($value, ',') + 2);
89 10
        });
90
91 10
        return $date . ' ' . $time;
92
    }
93
94
    /**
95
     * Return the thumb picture url
96
     *
97
     * @return null|string
98
     */
99 10
    public function getThumb()
100
    {
101 10
        $image = $this->node
102 10
            ->filter('.item_imagePic .lazyload[data-imgsrc]')
103 10
            ->first();
104
105 10
        if (0 === $image->count()) {
106
            return null;
107
        }
108
109
        $src = $image
110 10
            ->attr('data-imgsrc');
111
112 10
        return (string)Http::createFromString($src)->withScheme('https');
113
    }
114
115
    /**
116
     * Return the number of picture of the ad
117
     *
118
     * @return int
119
     */
120 10
    public function getNbImage()
121
    {
122 10
        $node = $this->node->filter('.item_imageNumber');
123
124
        return $this->getFieldValue($node, 0, function ($value) {
125 10
            return (int)trim($value);
126 10
        });
127
    }
128
129
    /**
130
     * @return mixed
131
     */
132 10 View Code Duplication
    public function getPlacement()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
    {
134 10
        $node = $this->node->filter('*[itemprop=availableAtOrFrom]');
135
136
        return $this->getFieldValue($node, '', function ($value) {
137 10
            return preg_replace('/\s+/', ' ', trim($value));
138 10
        });
139
    }
140
141
    /**
142
     * @return mixed
143
     */
144 10 View Code Duplication
    public function getType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
    {
146 10
        $node = $this->node->filter('*[itemprop=category]');
147
148 10
        return $this->getFieldValue($node, false, function ($value) {
149 10
            if ('pro' === preg_replace('/[\s()]+/', '', $value)) {
150 8
                return 'pro';
151
            }
152
153 10
            return 'part';
154 10
        });
155
    }
156
157
    /**
158
     * @return object
159
     */
160 10
    public function getAll()
161
    {
162
        return (object)[
163 10
            'id'         => $this->getId(),
164 10
            'title'      => $this->getTitle(),
165 10
            'price'      => $this->getPrice(),
166 10
            'url'        => $this->getUrl(),
167 10
            'created_at' => $this->getCreatedAt(),
168 10
            'thumb'      => $this->getThumb(),
169 10
            'nb_image'   => $this->getNbImage(),
170 10
            'placement'  => $this->getPlacement(),
171 10
            'type'       => $this->getType(),
172 10
        ];
173
    }
174
175
    /**
176
     * Return the field's value
177
     *
178
     * @param $node
179
     * @param $defaultValue
180
     * @param $callback
181
     * @param string $funcName
182
     * @param string $funcParam
183
     *
184
     * @return mixed
185
     */
186 10
    private function getFieldValue(
187
        Crawler $node,
188
        $defaultValue,
189
        $callback,
190
        $funcName = 'text',
191
        $funcParam = ''
192
    ) {
193 10
        if ($node->count()) {
194 10
            return $callback($node->$funcName($funcParam));
195
        }
196
197
        return $defaultValue;
198
    }
199
}
200