FlashAlertsExtension::getAlertPublisher()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * FlashAlertsExtension.php
4
 * Definition of class FlashAlertsExtension
5
 *
6
 * Created 16/12/14 19:24
7
 *
8
 * @author Rasanga Perera <[email protected]>
9
 * @copyright (c) 2014, The MIT License (MIT)
10
 */
11
12
namespace Ras\Bundle\FlashAlertBundle\Twig;
13
14
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
17
class FlashAlertsExtension extends \Twig_Extension
18
{
19
    /**
20
     * @var ContainerInterface
21
     */
22
    private $container;
23
24
25
    /**
26
     * @param ContainerInterface $container
27
     */
28
    public function __construct(ContainerInterface $container)
29
    {
30
        $this->container = $container;
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function getName()
37
    {
38
        return 'ras_flash_alert_extension';
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function getFunctions()
45
    {
46
        return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('get_alert_...e' => array('html')))); (array<string,Twig_Function_Method>) is incompatible with the return type declared by the interface Twig\Extension\ExtensionInterface::getFunctions of type Twig\TwigFunction[].

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...
47
            'get_alert_publisher' => new \Twig_Function_Method($this, 'getAlertPublisher', array(
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Function_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...
48
                'is_safe'   =>  array('html')
49
            )),
50
            'render_flash_alerts' => new \Twig_Function_Method($this, 'renderFlashAlerts', array(
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Function_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...
51
                'is_safe'   =>  array('html')
52
            ))
53
        );
54
    }
55
56
    /**
57
     * @return \Ras\Bundle\FlashAlertBundle\Model\AlertPublisher
58
     */
59
    public function getAlertPublisher()
60
    {
61
        return $this->container->get('ras_flash_alert.alert_publisher');
62
    }
63
64
    /**
65
     * Renders the breadcrumbs in a list
66
     *
67
     * @param  array  $options
68
     * @return string
69
     */
70
    public function renderFlashAlerts(array $options = array())
71
    {
72
        return $this->container->get('ras_flash_alert.templating.flash_alerts_helper')
73
            ->renderFlashAlerts($options);
74
    }
75
}