L10nExtension   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 50%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
c 3
b 0
f 0
lcom 1
cbo 3
dl 0
loc 45
ccs 5
cts 10
cp 0.5
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFilters() 0 6 1
A getName() 0 4 1
A getL10n() 0 4 1
1
<?php
2
3
namespace L10nBundle\Twig\Extension;
4
5
use L10nBundle\Business\L10nProvider;
6
7
/**
8
 * @author Olivier Versane
9
 */
10
class L10nExtension extends \Twig_Extension
11
{
12
    /**
13
     * @var \L10nBundle\Business\L10nProvider
14
     */
15
    private $l10nProvider;
16
17
    /**
18
     * @param L10nProvider $l10nProvider
19
     */
20 2
    public function __construct(L10nProvider $l10nProvider)
21
    {
22 2
        $this->l10nProvider = $l10nProvider;
23 2
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function getFilters()
29
    {
30
        return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('l10n' => n...ay($this, 'getL10n'))); (array<string,Twig_SimpleFilter>) 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...
31
            'l10n' => new \Twig_SimpleFilter('l10n', array($this, 'getL10n'))
32
        );
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    public function getName()
39
    {
40
        return 'l10n';
41
    }
42
43
    /**
44
     * @param string $key
45
     * @param string|null $idLocalization
46
     * @param string|null $locale
47
     *
48
     * @return string
49
     */
50 2
    public function getL10n($key, $idLocalization = null, $locale = null)
51
    {
52 2
        return $this->l10nProvider->getL10n($key, $idLocalization, $locale);
53
    }
54
}
55