Image::getFilterDimension()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace DoS\ResourceBundle\Twig\Extension;
4
5
use DoS\ResourceBundle\Model\ImageInterface;
6
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
7
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
8
9
class Image extends \Twig_Extension
10
{
11
    /**
12
     * @var CacheManager
13
     */
14
    private $cacheManager;
15
16
    /**
17
     * @var FilterConfiguration
18
     */
19
    private $filter;
20
21
    private $defaultFilterDimension = '32x32';
22
23
    /**
24
     * @var string
25
     */
26
    private $imageHolderPath = 'https://placehold.it';
27
28
    public function __construct(
29
        CacheManager $cacheManager,
30
        FilterConfiguration $filter,
31
        $imageHolderPath = null,
32
        $defaultFilterDimension = null
33
    ) {
34
        $this->cacheManager = $cacheManager;
35
        $this->filter = $filter;
36
37
        if ($imageHolderPath) {
38
            $this->imageHolderPath = $imageHolderPath;
39
        }
40
41
        if ($defaultFilterDimension) {
42
            $this->defaultFilterDimension = $defaultFilterDimension;
43
        }
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    public function getFilters()
50
    {
51
        return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('ui_image_f...thod($this, 'filter')); (array<string,Twig_Filter_Method>) is incompatible with the return type declared by the interface Twig_ExtensionInterface::getFilters of type Twig_SimpleFilter[].

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...
52
            'ui_image_filter' => new \Twig_Filter_Method($this, 'filter'), // deprecated
53
            'ui_image' => new \Twig_Filter_Method($this, 'filter'),
54
        );
55
    }
56
57
    /**
58
     * Gets the browser path for the image and filter to apply.
59
     *
60
     * @param string $path
61
     * @param string $filter
62
     * @param array  $runtimeConfig
63
     *
64
     * @return \Twig_Markup
65
     */
66
    public function filter($path, $filter, array $runtimeConfig = array())
67
    {
68
        if ($path instanceof ImageInterface) {
69
            $path = $path->getPath();
70
        }
71
72
        if (empty($path)) {
73
            return $this->imageHolderPath.'/'.$this->getFilterDimension($filter);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->imageHolde...lterDimension($filter); (string) is incompatible with the return type documented by DoS\ResourceBundle\Twig\Extension\Image::filter of type Twig_Markup.

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...
74
        }
75
76
        return new \Twig_Markup(
77
            $this->cacheManager->getBrowserPath($path, $filter, $runtimeConfig),
78
            'utf8'
79
        );
80
    }
81
82
    private function getFilterDimension($name)
83
    {
84
        $filter = $this->filter->get($name);
85
86
        if (empty($filter)) {
87
            return $this->defaultFilterDimension;
88
        }
89
90
        $size = $filter['filters']['thumbnail']['size'];
91
92
        return vsprintf('%sx%s', $size);
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98
    public function getName()
99
    {
100
        return 'ui_image';
101
    }
102
}
103