PaginationExtension   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 10
c 3
b 1
f 0
lcom 1
cbo 4
dl 0
loc 110
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A initRuntime() 0 4 1
A getFunctions() 0 8 1
A render() 0 7 2
A sortable() 0 7 2
A filter() 0 7 2
A getName() 0 4 1
1
<?php
2
3
namespace Saxulum\PaginationProvider\Twig;
4
5
use Saxulum\PaginationProvider\Helper\Processor;
6
7
class PaginationExtension extends \Twig_Extension
8
{
9
    /**
10
     * @var \Twig_Environment
11
     */
12
    protected $environment;
13
14
    /**
15
     * @var Processor
16
     */
17
    protected $processor;
18
19
    public function __construct(Processor $processor)
20
    {
21
        $this->processor = $processor;
22
    }
23
24
    /**
25
     * {@inheritDoc}
26
     */
27
    public function initRuntime(\Twig_Environment $environment)
28
    {
29
        $this->environment = $environment;
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
    public function getFunctions()
36
    {
37
        return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('knp_pagina...e' => array('html')))); (array<string,Twig_Function_Method>) is incompatible with the return type declared by the interface Twig_ExtensionInterface::getFunctions of type Twig_SimpleFunction[].

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...
38
            'knp_pagination_render' => new \Twig_Function_Method($this, 'render', array('is_safe' => array('html'))),
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...
39
            'knp_pagination_sortable' => new \Twig_Function_Method($this, 'sortable', array('is_safe' => array('html'))),
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...
40
            'knp_pagination_filter' => new \Twig_Function_Method($this, 'filter', array('is_safe' => array('html'))),
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...
41
        );
42
    }
43
44
    /**
45
     * Renders the pagination template
46
     *
47
     * @param string $template
48
     * @param array  $queryParams
49
     * @param array  $viewParams
50
     *
51
     * @return string
52
     */
53
    public function render($pagination, $template = null, array $queryParams = array(), array $viewParams = array())
54
    {
55
        return $this->environment->render(
56
            $template ?: $pagination->getTemplate(),
57
            $this->processor->render($pagination, $queryParams, $viewParams)
58
        );
59
    }
60
61
    /**
62
     * Create a sort url for the field named $title
63
     * and identified by $key which consists of
64
     * alias and field. $options holds all link
65
     * parameters like "alt, class" and so on.
66
     *
67
     * $key example: "article.title"
68
     *
69
     * @param  string $title
70
     * @param  string $key
71
     * @param  array  $options
72
     * @param  array  $params
73
     * @param  string $template
74
     * @return string
75
     */
76
    public function sortable($pagination, $title, $key, $options = array(), $params = array(), $template = null)
77
    {
78
        return $this->environment->render(
79
            $template ?: $pagination->getSortableTemplate(),
80
            $this->processor->sortable($pagination, $title, $key, $options, $params)
81
        );
82
    }
83
84
    /**
85
     * Create a filter url for the field named $title
86
     * and identified by $key which consists of
87
     * alias and field. $options holds all link
88
     * parameters like "alt, class" and so on.
89
     *
90
     * $key example: "article.title"
91
     *
92
     * @param  string $title
0 ignored issues
show
Bug introduced by
There is no parameter named $title. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
93
     * @param  string $key
0 ignored issues
show
Bug introduced by
There is no parameter named $key. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
94
     * @param  array  $options
95
     * @param  array  $params
96
     * @param  string $template
97
     * @return string
98
     */
99
    public function filter($pagination, array $fields, $options = array(), $params = array(), $template = null)
100
    {
101
        return $this->environment->render(
102
            $template ?: $pagination->getFiltrationTemplate(),
103
            $this->processor->filter($pagination, $fields, $options, $params)
104
        );
105
    }
106
107
    /**
108
     * Get name
109
     *
110
     * @return string
111
     */
112
    public function getName()
113
    {
114
        return 'knp_pagination';
115
    }
116
}
117