Completed
Push — master ( f1f1bf...2cbedc )
by
unknown
14:06
created

Srcset   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 48
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A injectAssetFactory() 0 4 1
A scrape() 0 28 4
1
<?php
2
namespace Aoe\Asdis\Content\Scraper\Html;
3
4
use Aoe\Asdis\Content\Scraper\Html\AbstractHtmlScraper;
5
use Aoe\Asdis\Content\Scraper\ScraperInterface;
6
use Aoe\Asdis\Domain\Model\Asset\Factory;
7
8
/**
9
 * Scrapes assets from all tags by using data-src attributes.
10
 */
11
class Srcset extends AbstractHtmlScraper implements ScraperInterface
12
{
13
    /**
14
     * @var \Aoe\Asdis\Domain\Model\Asset\Factory
15
     */
16
    private $assetFactory;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
17
18
    /**
19
     * @param \Aoe\Asdis\Domain\Model\Asset\Factory $assetFactory
20
     */
21
    public function injectAssetFactory(Factory $assetFactory)
22
    {
23
        $this->assetFactory = $assetFactory;
24
    }
25
26
    /**
27
     * @param string $content
28
     * @return \Aoe\Asdis\Domain\Model\Asset\Collection
29
     */
30
    public function scrape($content)
31
    {
32
        $paths = [];
33
        $masks = [];
34
        $matches = [];
35
        preg_match_all(
36
            '/srcset=*\s?=\s?([\'"])(.*?)([\'"])/i',
37
            $content,
38
            $matches
39
        );
40
41
        foreach ($matches[2] as $mkey => $path) {
42
            if (strpos($path, ',') === false) {
43
                $paths[] = $path;
44
                $masks[] = $matches[1][$mkey];
45
                continue;
46
            }
47
48
            $expPaths = explode(',', $path);
49
50
            foreach ($expPaths as $singlePath) {
51
                $paths[] = trim(str_replace(' 2x','', $singlePath));
52
                $masks[] = '';
53
            }
54
        }
55
56
        return $this->assetFactory->createAssetsFromPaths($paths, $masks);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->assetFacto...mPaths($paths, $masks); (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...
57
    }
58
}