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

AdCrawler::getPictures()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 40
ccs 29
cts 29
cp 1
rs 8.439
cc 5
eloc 26
nc 2
nop 1
crap 5
1
<?php
2
3
namespace Lbc\Crawler;
4
5
use Lbc\Filter\CitySanitizer;
6
use Lbc\Filter\CpSanitizer;
7
use Lbc\Filter\DefaultSanitizer;
8
use Lbc\Filter\KeySanitizer;
9
use Lbc\Filter\PriceSanitizer;
10
use Lbc\Parser\AdUrlParser;
11
use League\Uri\Schemes\Http;
12
use Symfony\Component\DomCrawler\Crawler;
13
14
/**
15
 * Class AdCrawler
16
 * @package Lbc\Crawler
17
 */
18
class AdCrawler extends CrawlerAbstract
19
{
20
    /**
21
     * @param $url
22
     * @return AdUrlParser
23
     */
24 14
    protected function setUrlParser($url)
25
    {
26 14
        $this->url = new AdUrlParser($url);
27 14
    }
28
29
    /**
30
     * Return a full ad information
31
     *
32
     * @return array
33
     */
34 14
    public function getAll()
35
    {
36 14
        return array_merge(
37
            [
38 6
                'id'       => $this->getUrlParser()->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...
39 6
                'category' => $this->getUrlParser()->getCategory(),
40 6
            ],
41 6
            $this->getPictures(),
42 6
            $this->getProperties(),
43 6
            $this->getDescription()
44 6
        );
45
    }
46
47
    /**
48
     * Return an array with the Thumbs pictures url
49
     *
50
     * @param Crawler $node
51
     * @return array
52
     */
53 8
    public function getPictures(Crawler $node = null)
54
    {
55 8
        $node = $node ?: $this->node;
56
57 8
        $images = [];
58 8
        $images_thumbs = [];
59
60
        $node
61 8
            ->filter('.adview_main script')
62
            ->each(function (Crawler $crawler) use (&$images, &$images_thumbs) {
63 8
                preg_match_all(
64 8
                    '#//img.+.leboncoin.fr/.*\.jpg#',
65 8
                    $crawler->html(),
66
                    $matches
67 8
                );
68
69 8
                if (count($matches[0]) > 0) {
70 8
                    foreach ($matches[0] as $image) {
71 8
                        if (preg_match('/thumb/', $image)) {
72 8
                            array_push(
73 8
                                $images_thumbs,
74 8
                                (string)Http::createFromString($image)
75 8
                                    ->withScheme($this->sheme)
76 8
                            );
77 8
                        } else {
78 8
                            array_push(
79 8
                                $images,
80 8
                                (string)Http::createFromString($image)
81 8
                                    ->withScheme($this->sheme)
82 8
                            );
83
                        }
84 8
                    }
85 8
                }
86 8
            });
87
88
        return [
89 8
            'images'        => $images,
90 8
            'images_thumbs' => $images_thumbs,
91 8
        ];
92
    }
93
94
    /**
95
     * Return the common information (price, cp, city)
96
     *
97
     * @param Crawler $node
98
     *
99
     * @return array
100
     */
101 8
    public function getProperties(Crawler $node = null)
102
    {
103 8
        $node = $node ?: $this->node;
104
105 8
        $properties = [];
106
107 8
        $properties['title'] = DefaultSanitizer::clean(
108 8
            $this->node->filter('h1')->text()
109 8
        );
110
111 8
        $node->filter('h2')
112 8
            ->each(function (Crawler $crawler) use (&$properties) {
113 8
                $properties = array_merge(
114 8
                    $properties,
115 8
                    $this->sanitize(
116 8
                        $crawler->filter('.property')->text(),
117 8
                        $crawler->filter('.value')->text()
118 8
                    )
119 8
                );
120 8
            });
121
122 8
        return ['properties' => $properties];
123
    }
124
125
    /**
126
     * Return the description
127
     *
128
     * @param Crawler $node
129
     * @return string
130
     */
131 8
    public function getDescription(Crawler $node = null)
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
132
    {
133 8
        return ['description' => $this->node->filter("p#description")->text()];
134
    }
135
136
    /**
137
     * Transform the properties name into a snake_case string
138
     *
139
     * @param string $value
140
     * @return string
141
     */
142 8
    private function sanitize($key, $value)
143
    {
144 8
        $key = KeySanitizer::clean($key);
145
146
        switch ($key) {
147 8
            case 'prix':
148 8
                return ['price' => PriceSanitizer::clean($value)];
149
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
150 8
            case 'ville':
151
                return [
152 8
                    'city' => CitySanitizer::clean($value),
153 8
                    'cp'   => CpSanitizer::clean($value),
154 8
                ];
155
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
156 8
            default:
157 8
                return [$key => DefaultSanitizer::clean($value)];
158 8
        }
159
    }
160
}
161