Completed
Push — develop ( 7d735f...2159ae )
by Rémi
04:24 queued 02:18
created

SearchResultAdCrawler   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 185
Duplicated Lines 10.81 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 95.16%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 2
cbo 6
dl 20
loc 185
ccs 59
cts 62
cp 0.9516
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUrlParser() 0 4 1
A getId() 0 4 1
A getTitle() 0 4 1
A getPrice() 0 9 2
A getUrl() 0 4 1
A getCreatedAt() 0 16 1
A getThumb() 0 15 2
A getNbImage() 0 8 1
A getPlacement() 8 8 1
A getType() 12 12 2
A getAll() 0 14 1
A getFieldValue() 0 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Lbc\Crawler;
4
5
use Lbc\Filter\DefaultSanitizer;
6
use Lbc\Filter\PrixSanitizer;
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
    /**
39
     * Return the title
40
     *
41
     * @return string
42
     */
43 10
    public function getTitle()
44
    {
45 10
        return DefaultSanitizer::clean($this->node->filter('h2')->text());
46
    }
47
48
    /**
49
     * Return the price
50
     *
51
     * @return int
52
     */
53 10
    public function getPrice()
54
    {
55 10
        if ($this->node->filter('*[itemprop=price]')->count()) {
56 10
            return PrixSanitizer::clean(
57 10
                $this->node->filter('*[itemprop=price]')->text()
58 5
            );
59
        }
60
        return 0;
61
    }
62
63
    /**
64
     * Return the Ad's URL
65
     *
66
     * @return string
67
     */
68 10
    public function getUrl()
69
    {
70 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...
71
    }
72
73
    /**
74
     * Return the data and time the ad was created
75
     *
76
     * @return string
77
     */
78 10
    public function getCreatedAt()
79
    {
80 10
        $node = $this->node
81 10
            ->filter('*[itemprop=availabilityStarts]')
82 10
            ->first();
83
84 10
        $date = $node->attr('content');
85
86
        $time = $this->getFieldValue($node, 0, function ($value) {
87 10
            $value = trim($value);
88
89 10
            return substr($value, strpos($value, ',') + 2);
90 10
        });
91
92 10
        return $date . ' ' . $time;
93
    }
94
95
    /**
96
     * Return the thumb picture url
97
     *
98
     * @return null|string
99
     */
100 10
    public function getThumb()
101
    {
102 10
        $image = $this->node
103 10
            ->filter('.item_imagePic .lazyload[data-imgsrc]')
104 10
            ->first();
105
106 10
        if (0 === $image->count()) {
107
            return null;
108
        }
109
110
        $src = $image
111 10
            ->attr('data-imgsrc');
112
113 10
        return (string)Http::createFromString($src)->withScheme('https');
114
    }
115
116
    /**
117
     * Return the number of picture of the ad
118
     *
119
     * @return int
120
     */
121 10
    public function getNbImage()
122
    {
123 10
        $node = $this->node->filter('.item_imageNumber');
124
125
        return $this->getFieldValue($node, 0, function ($value) {
126 10
            return (int)trim($value);
127 10
        });
128
    }
129
130
    /**
131
     * @return mixed
132
     */
133 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...
134
    {
135 10
        $node = $this->node->filter('*[itemprop=availableAtOrFrom]');
136
137
        return $this->getFieldValue($node, '', function ($value) {
138 10
            return preg_replace('/\s+/', ' ', trim($value));
139 10
        });
140
    }
141
142
    /**
143
     * @return mixed
144
     */
145 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...
146
    {
147 10
        $node = $this->node->filter('*[itemprop=category]');
148
149 10
        return $this->getFieldValue($node, false, function ($value) {
150 10
            if ('pro' === preg_replace('/[\s()]+/', '', $value)) {
151 8
                return 'pro';
152
            }
153
154 10
            return 'part';
155 10
        });
156
    }
157
158
    /**
159
     * @return object
160
     */
161 10
    public function getAll()
162
    {
163
        return (object)[
164 10
            'id'         => $this->getId(),
165 10
            'title'      => $this->getTitle(),
166 10
            'price'      => $this->getPrice(),
167 10
            'url'        => $this->getUrl(),
168 10
            'created_at' => $this->getCreatedAt(),
169 10
            'thumb'      => $this->getThumb(),
170 10
            'nb_image'   => $this->getNbImage(),
171 10
            'placement'  => $this->getPlacement(),
172 10
            'type'       => $this->getType(),
173 5
        ];
174
    }
175
176
    /**
177
     * Return the field's value
178
     *
179
     * @param Crawler $node
180
     * @param mixed $defaultValue
181
     * @param \Closure $callback
182
     * @param string $funcName
183
     * @param string $funcParam
184
     *
185
     * @return mixed
186
     */
187 10
    private function getFieldValue(
188
        Crawler $node,
189
        $defaultValue,
190
        $callback,
191
        $funcName = 'text',
192
        $funcParam = ''
193
    ) {
194 10
        if ($node->count()) {
195 10
            return $callback($node->$funcName($funcParam));
196
        }
197
198
        return $defaultValue;
199
    }
200
}
201