Completed
Push — master ( 173fc3...d62a34 )
by Felix
02:05
created

Crawler::getHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Felix\Scraper;
4
5
class Crawler
6
{
7
    protected $timeout = 60;
8
    protected $url;
9
    protected $xpath;
10
    protected $html;
11
    protected $content;
12
13 2
    public function __construct($url, $xpath = '//')
14
    {
15 2
        $this->url = $url;
16 2
        $this->xpath = $xpath;
17 2
    }
18
19
    /**
20
     * Obtener el HTML y parsear su contenido.
21
     * 
22
     * @return void
23
     */
24 1
    public function start()
25
    {
26 1
        $html = $this->html($this->url);
0 ignored issues
show
Unused Code introduced by
The call to Crawler::html() has too many arguments starting with $this->url.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
27
        
28 1
        $this->setContent($html, $this->xpath);
29 1
    }
30
31
    /**
32
     * Obtener el contenido HTML de una página.
33
     *
34
     *
35
     * @param string $url Url de la página a scrapear
0 ignored issues
show
Bug introduced by
There is no parameter named $url. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
36
     *
37
     * @return string
38
     */
39 2
    public function html()
40
    {
41 2
        $this->html = @file_get_contents($this->url);
42
43 2
        return $this->html;
44
    }
45
46
    /**
47
     * Obtener el contenido.
48
     * 
49
     * @return string
50
     */
51 1
    public function getContent()
52
    {
53 1
        return $this->content;
54
    }
55
56
    /**
57
     * Almacenar el codigo HTML.
58
     *
59
     * @param string $html
60
     * @param string $xpath
61
     */
62 1
    public function setContent($html, $xpath)
63
    {
64 1
        $crawler = new \Symfony\Component\DomCrawler\Crawler($html);
65 1
        $this->content = $crawler->evaluate($xpath);
66 1
    }
67
68
    /**
69
     * Obtner la URL.
70
     */
71
    public function getUrl()
72
    {
73
        return $this->url;
74
    }
75
76
    /**
77
     * Guardar el tiempo de espera de una página.
78
     */
79
    public function setTimeout(int $timeout)
80
    {
81
        $this->timeout = $timeout;
82
    }
83
}
84