Passed
Pull Request — master (#16)
by
unknown
13:19 queued 10:34
created

Url::extractUrlPaths()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6.1979

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 20
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 31
ccs 14
cts 17
cp 0.8235
crap 6.1979
rs 8.9777
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