Completed
Push — master ( 58a34e...92923b )
by
unknown
14:40
created

CssInline::getStyleBlocksFromMarkup()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 11
cts 11
cp 1
rs 8.9297
c 0
b 0
f 0
cc 6
nc 2
nop 1
crap 6
1
<?php
2
namespace Aoe\Asdis\Content\Scraper\Html;
3
4
use Aoe\Asdis\Content\Scraper\Css\Url;
5
use Aoe\Asdis\Content\Scraper\ScraperInterface;
6
7
/**
8
 * Scrapes assets from inline CSS.
9
 */
10
class CssInline implements ScraperInterface
11
{
12
    /**
13
     * @var \Aoe\Asdis\Content\Scraper\Css\Url
14
     */
15
    private $cssUrlScraper;
16
17
    /**
18
     * @param \Aoe\Asdis\Content\Scraper\Css\Url $cssUrlScraper
19
     */
20 1
    public function injectCssUrlScraper(Url $cssUrlScraper)
21
    {
22 1
        $this->cssUrlScraper = $cssUrlScraper;
23 1
    }
24
25
    /**
26
     * @param $content
27
     * @return \Aoe\Asdis\Domain\Model\Asset\Collection
28
     */
29 1
    public function scrape($content)
30
    {
31 1
        return $this->cssUrlScraper->scrape(implode(PHP_EOL, $this->getStyleBlocksFromMarkup($content)));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->cssUrlScra...FromMarkup($content))); (TYPO3\CMS\Extbase\Object\object) is incompatible with the return type declared by the interface Aoe\Asdis\Content\Scraper\ScraperInterface::scrape of type Aoe\Asdis\Domain\Model\Asset\Collection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
32
    }
33
34
    /**
35
     * Returns the inner content of all <style></style> blocks of the given markup as an array.
36
     *
37
     * @param string $content
38
     * @return array
39
     */
40 1
    private function getStyleBlocksFromMarkup($content)
41
    {
42 1
        $blocks  = [];
43 1
        $matches = [];
44
45 1
        preg_match_all(
46 1
            '~<style[^>]*>(.*?)</style>~is',
47 1
            $content,
48 1
            $matches,
49 1
            PREG_PATTERN_ORDER
50
        );
51
52 1
        if (is_array($matches) && sizeof($matches) > 1 && is_array($matches[1])) {
53 1
            foreach($matches[1] as $match) {
54
                // filter inline svg styles
55
                if (false === strpos($match, 'fill:url')) {
56 1
                    $blocks[] = $match;
57
                }
58
            }
59
        }
60
61
        return $blocks;
62
    }
63
}