Completed
Push — 1.2 ( d3ea0d...d8c512 )
by David
16:21
created

SonataDoctrinePHPCRAdminExtension::getFilters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of sonata-project.
5
 *
6
 * (c) 2010 Thomas Rabaix
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrinePHPCRAdminBundle\Twig\Extension;
13
14
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
15
use PHPCR\NodeInterface;
16
17
class SonataDoctrinePHPCRAdminExtension extends \Twig_Extension
18
{
19
    /**
20
     * @var \Twig_Environment
21
     */
22
    protected $environment;
23
24
    /**
25
     * {@inheritDoc}
26
     */
27
    public function initRuntime(\Twig_Environment $environment)
28
    {
29
        $this->environment = $environment;
30
    }
31
32
33
    /**
34
     * {@inheritDoc}
35
     */
36
    public function getName()
37
    {
38
        return 'sonata_doctrine_phpcr_admin';
39
    }
40
41
    /**
42
     * render a list element from the FieldDescription
43
     *
44
     * @param object $object
45
     * @param FieldDescriptionInterface $fieldDescription
46
     * @param array $params
47
     *
48
     * @return string
49
     */
50
    public function renderListElement($object, FieldDescriptionInterface $fieldDescription, $params = array())
51
    {
52
        $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_list_field.html.twig');
0 ignored issues
show
Bug introduced by
The method getTemplate() does not seem to exist on object<Sonata\DoctrinePH...inePHPCRAdminExtension>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
54
        return $this->output($fieldDescription, $template, array_merge($params, array(
0 ignored issues
show
Bug introduced by
The method output() does not seem to exist on object<Sonata\DoctrinePH...inePHPCRAdminExtension>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
            'admin'  => $fieldDescription->getAdmin(),
56
            'object' => $object,
57
            'value'  => $this->getValueFromFieldDescription($object, $fieldDescription),
0 ignored issues
show
Bug introduced by
The method getValueFromFieldDescription() does not seem to exist on object<Sonata\DoctrinePH...inePHPCRAdminExtension>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
            'field_description' => $fieldDescription
59
        )));
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function getFilters()
66
    {
67
        return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('render_nod...e' => array('html')))); (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...
68
            'render_node_property'     => new \Twig_Filter_Method($this, 'renderNodeProperty', array('is_safe' => array('html'))),
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...
69
            'render_node_path'     => new \Twig_Filter_Method($this, 'renderNodePath', array('is_safe' => array('html'))),
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...
70
        );
71
    }
72
73
74
    /**
75
     * Renders a property of a node
76
     *
77
     * @param NodeInterface $node
78
     * @param string $property
79
     *
80
     * @return string String representation of the property
81
     */
82
    public function renderNodeProperty(NodeInterface $node, $property)
83
    {
84
        return $node->getProperty($property)->getString();
85
    }
86
87
    /**
88
     * Renders a path of a node
89
     *
90
     * @param NodeInterface $node
91
     *
92
     * @return string Node path
93
     */
94
    public function renderNodePath(NodeInterface $node)
95
    {
96
        return $node->getPath();
97
    }
98
}
99
100