TwigExtension::dummy()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
10
namespace AnimeDb\Bundle\CatalogBundle\Service;
11
12
/**
13
 * Twig extension.
14
 *
15
 * @author  Peter Gribanov <[email protected]>
16
 */
17
class TwigExtension extends \Twig_Extension
18
{
19
    /**
20
     * @return array
21
     */
22 1
    public function getFilters()
23
    {
24
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('dummy' => ...ethod($this, 'dummy')); (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...
25 1
            'dummy' => new \Twig_Filter_Method($this, 'dummy'),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Filter_Method has been deprecated with message: since 1.12 (to be removed in 2.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
26 1
        ];
27
    }
28
29
    /**
30
     * Dummy for images.
31
     *
32
     * @param string $path
33
     * @param string $filter
34
     *
35
     * @return bool
36
     */
37 2
    public function dummy($path, $filter)
38
    {
39 2
        return $path ?: '/bundles/animedbcatalog/images/dummy/'.$filter.'.jpg';
40
    }
41
42
    /**
43
     * @return string
44
     */
45 1
    public function getName()
46
    {
47 1
        return 'extension';
48
    }
49
}
50