Completed
Push — master ( c39a14...8d69cc )
by Lars
02:07
created

example_scraping_imdb.php ➔ scraping_imdb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
require_once '../vendor/autoload.php';
4
5
function scraping_imdb($url)
6
{
7
    // init
8
    $return = [];
9
10
    // create HTML DOM
11
    $dom = \voku\helper\HtmlDomParser::file_get_html($url);
12
13
    // get title
14
    $return['Title'] = $dom->find('title', 0)->innertext;
15
16
    // get rating
17
    $return['Rating'] = $dom->find('.ratingValue strong', 0)->getAttribute('title');
0 ignored issues
show
Bug introduced by
The method getAttribute does only exist in voku\helper\SimpleHtmlDomInterface, but not in voku\helper\SimpleHtmlDomNodeInterface.

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...
18
19
    return $return;
20
}
21
22
// -----------------------------------------------------------------------------
23
24
$data = scraping_imdb('http://imdb.com/title/tt0335266/');
25
26
foreach ($data as $k => $v) {
27
    echo '<strong>' . $k . ' </strong>' . $v . '<br>';
28
}