Passed
Pull Request — master (#16)
by
unknown
03:22
created

Url   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 86.96%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 53
ccs 20
cts 23
cp 0.8696
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A scrape() 0 4 1
A injectAssetFactory() 0 3 1
B extractUrlPaths() 0 31 6
1
<?php
2
3
namespace Aoe\Asdis\Content\Scraper\Css;
4
5
use Aoe\Asdis\Content\Scraper\ScraperInterface;
6
use Aoe\Asdis\Domain\Model\Asset\Collection;
7
use Aoe\Asdis\Domain\Model\Asset\Factory;
8
9
/**
10
 * Scrapes paths from "url()" in CSS.
11
 */
12
class Url implements ScraperInterface
13
{
14
    private ?Factory $assetFactory = null;
15
16 3
    public function injectAssetFactory(Factory $assetFactory): void
17
    {
18 3
        $this->assetFactory = $assetFactory;
19 3
    }
20
21 3
    public function scrape(string $content): ?Collection
22
    {
23 3
        $urls = $this->extractUrlPaths($content);
24 3
        return $this->assetFactory->createAssetsFromPaths($urls['paths'], $urls['masks']);
0 ignored issues
show
Bug introduced by
The method createAssetsFromPaths() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        return $this->assetFactory->/** @scrutinizer ignore-call */ createAssetsFromPaths($urls['paths'], $urls['masks']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
    }
26
27
    /**
28
     * Extracts paths to resources in CSS code.
29
     * This means file references which are included in "url(...)".
30
     *
31
     * @param string $cssContent
32
     * @return array
33
     */
34 3
    private function extractUrlPaths($cssContent)
35
    {
36 3
        $paths = [];
37 3
        $masks = [];
38 3
        $matches = [];
39
40 3
        preg_match_all(
41 3
            '~url\((?!#)\s*([\'"]?)(/?(\.\./)?.*?)([\'"]?);?\s*\)~is',
42
            $cssContent,
43
            $matches,
44 3
            PREG_PATTERN_ORDER
45
        );
46
47 3
        if (!(is_array($matches) && count($matches) > 1 && is_array($matches[2]))) {
48
            return [
49
                'paths' => $paths,
50
                'masks' => $masks,
51
            ];
52
        }
53
54 3
        foreach ($matches[2] as $mkey => $path) {
55 2
            if (str_contains($path, ',')) {
56
                continue;
57
            }
58 2
            $paths[] = $path;
59 2
            $masks[] = $matches[1][$mkey];
60
        }
61
62
        return [
63 3
            'paths' => $paths,
64 3
            'masks' => $masks,
65
        ];
66
    }
67
}
68