Completed
Pull Request — master (#27)
by
unknown
01:10
created

getMixedContentFromLinkedCss()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace Spatie\MixedContentScanner;
4
5
use GuzzleHttp\Exception\RequestException;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\UriInterface;
8
use Spatie\Crawler\CrawlObserver;
9
10
class MixedContentObserver extends CrawlObserver
11
{
12
    private $crawledCss = [];
13
14
    private $shouldExtractLinkedCss = false;
15
16
    public function crawled(UriInterface $url, ResponseInterface $response, ?UriInterface $foundOnUrl = null)
17
    {
18
        [$mixedContent, $linkedCss] = MixedContentExtractor::extract((string) $response->getBody(), $url);
0 ignored issues
show
Bug introduced by
The variable $mixedContent seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Bug introduced by
The variable $linkedCss does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
19
20
        if ($this->shouldExtractLinkedCss) {
21
            foreach ($linkedCss as $css) {
22
                $mixedContent = array_merge($mixedContent, $this->getMixedContentFromLinkedCss($css, $url));
0 ignored issues
show
Bug introduced by
The variable $mixedContent does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
23
            }
24
        }
25
26
        if (! count($mixedContent)) {
27
            $this->noMixedContentFound($url);
28
29
            return;
30
        }
31
32
        foreach ($mixedContent as $mixedContentItem) {
33
            $this->mixedContentFound($mixedContentItem);
34
        }
35
    }
36
37
    public function crawlFailed(
38
        UriInterface $url,
39
        RequestException $requestException,
40
        ?UriInterface $foundOnUrl = null
41
    ) {
42
    }
43
44
    /**
45
     * Will be called when mixed content was found.
46
     *
47
     * @param \Spatie\MixedContentScanner\MixedContent $mixedContent
48
     */
49
    public function mixedContentFound(MixedContent $mixedContent)
0 ignored issues
show
Unused Code introduced by
The parameter $mixedContent 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...
50
    {
51
    }
52
53
    /**
54
     * Will be called when no mixed content was found on the given url.
55
     *
56
     * @param \Psr\Http\Message\UriInterface
57
     */
58
    public function noMixedContentFound(UriInterface $crawledUrl)
0 ignored issues
show
Unused Code introduced by
The parameter $crawledUrl 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...
59
    {
60
    }
61
62
    public function getMixedContentFromLinkedCss(UriInterface $css, UriInterface $linkedByUrl)
63
    {
64
        if (! in_array($css, $this->crawledCss)) {
65
            $this->crawledCss[] = $css;
66
            try {
67
68
                return MixedContentLinkedCssExtractor::extract(Body::get($css), $css, $linkedByUrl);
69
            } catch (RequestException $e) {
70
                $this->crawlFailed($css, $e, $linkedByUrl);
71
            }
72
        }
73
74
        return [];
75
    }
76
77
    public function withLinkedCss()
78
    {
79
        $this->shouldExtractLinkedCss = true;
80
81
        return $this;
82
    }
83
}
84