These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace VDB\Spider\Discoverer; |
||
3 | |||
4 | use VDB\Spider\Discoverer\DiscovererInterface; |
||
5 | use VDB\Spider\Discoverer\Discoverer; |
||
6 | use VDB\Spider\Resource; |
||
7 | use VDB\Uri\Exception\UriSyntaxException; |
||
8 | use VDB\Uri\Uri; |
||
9 | use VDB\Spider\Uri\DiscoveredUri; |
||
10 | use VDB\Uri\UriInterface; |
||
11 | |||
12 | /** |
||
13 | * @author Matthijs van den Bos |
||
14 | * @copyright 2013 Matthijs van den Bos |
||
15 | */ |
||
16 | View Code Duplication | class CssSelectorDiscoverer extends Discoverer implements DiscovererInterface |
|
0 ignored issues
–
show
|
|||
17 | { |
||
18 | /** @var string */ |
||
19 | protected $cssSelector; |
||
20 | |||
21 | /** |
||
22 | * @param $cssSelector |
||
23 | */ |
||
24 | public function __construct($cssSelector) |
||
25 | { |
||
26 | $this->cssSelector = $cssSelector; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @param Resource $resource |
||
31 | * @return DiscoveredUri[] |
||
32 | */ |
||
33 | public function discover(Resource $resource) |
||
34 | { |
||
35 | $crawler = $resource->getCrawler()->filter($this->cssSelector); |
||
36 | $uris = array(); |
||
37 | foreach ($crawler as $node) { |
||
38 | try { |
||
39 | $uris[] = new DiscoveredUri(new Uri($node->getAttribute('href'), $resource->getUri()->toString())); |
||
40 | } catch (UriSyntaxException $e) { |
||
41 | // do nothing. We simply ignore invalid URI's |
||
42 | } |
||
43 | } |
||
44 | return $uris; |
||
45 | } |
||
46 | } |
||
47 |
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.