Passed
Pull Request — master (#57)
by Thierry
02:40
created

Renderer   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 191
Duplicated Lines 10.47 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 20
loc 191
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setPreviousText() 0 4 1
A setNextText() 0 4 1
A setRequest() 0 9 3
A getPageCall() 0 4 1
A getPrevLink() 10 10 2
A getNextLink() 10 10 2
A getLink() 0 18 3
A render() 0 17 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Renderer.php - Paginator renderer
5
 *
6
 * Render pagination links.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2016 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
14
15
namespace Jaxon\Utils\Pagination;
16
17
use Jaxon\Contracts\Template\Renderer as TemplateRenderer;
18
19
use Jaxon\Request\Factory\Request;
20
use Jaxon\Request\Factory\Parameter;
21
22
class Renderer
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Renderer
Loading history...
23
{
24
    /**
25
     * The template renderer.
26
     *
27
     * Will be used to render HTML code for links.
28
     *
29
     * @var TemplateRenderer
30
     */
31
    protected $xRenderer = null;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
32
33
    /**
34
     * The Jaxon request to be paginated
35
     *
36
     * @var Request
37
     */
38
    protected $xRequest = null;
39
40
    /**
41
     * @var string
42
     */
43
    protected $previousText = '&laquo;';
44
45
    /**
46
     * @var string
47
     */
48
    protected $nextText = '&raquo;';
49
50
    /**
51
     * @var string
52
     */
53
    protected $ellipsysText = '...';
54
55
    /**
56
     * @var integer
57
     */
58
    protected $totalPages = 0;
59
60
    /**
61
     * @var integer
62
     */
63
    protected $currentPage = 0;
64
65
    /**
66
     * The class contructor
67
     *
68
     * @param TemplateRenderer          $xRenderer
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 10 found
Loading history...
69
     */
70
    public function __construct(TemplateRenderer $xRenderer)
71
    {
72
        $this->xRenderer = $xRenderer;
73
    }
74
75
    /**
76
     * Set the text for the previous page link
77
     *
78
     * @param string $text The text for the previous page link
79
     *
80
     * @return void
81
     */
82
    public function setPreviousText($text)
83
    {
84
        $this->previousText = $text;
85
    }
86
87
    /**
88
     * Set the text for the next page link
89
     *
90
     * @param string $text The text for the previous page link
91
     *
92
     * @return void
93
     */
94
    public function setNextText($text)
95
    {
96
        $this->nextText = $text;
97
    }
98
99
    /**
100
     * Set the request to be paginated
101
     *
102
     * @param Request $xRequest The request to be paginated
103
     *
104
     * @return void
105
     */
106
    public function setRequest(Request $xRequest)
107
    {
108
        $this->xRequest = $xRequest;
109
        // Append the page number to the parameter list, if not yet given.
110
        if(($this->xRequest) && !$this->xRequest->hasPageNumber())
111
        {
112
            $this->xRequest->addParameter(Parameter::PAGE_NUMBER, 0);
113
        }
114
    }
115
116
    /**
117
     * Get the js call to a given page
118
     *
119
     * @param int $pageNum The page number
120
     *
121
     * @return string
122
     */
123
    protected function getPageCall($pageNum)
124
    {
125
        return $this->xRequest->setPageNumber($pageNum)->getScript();
126
    }
127
128
    /**
129
     * Render the previous link.
130
     *
131
     * @return string
132
     */
133 View Code Duplication
    protected function getPrevLink()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
    {
135
        $aVars = ['text' => $this->previousText];
136
        if($this->currentPage > 1)
137
        {
138
            $aVars['call'] = $this->getPageCall($this->currentPage - 1);
139
            return $this->xRenderer->render('pagination::links/prev', $aVars);
140
        }
141
        return $this->xRenderer->render('pagination::links/disabled', $aVars);
142
    }
143
144
    /**
145
     * Render the next link.
146
     *
147
     * @return string
148
     */
149 View Code Duplication
    protected function getNextLink()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151
        $aVars = ['text' => $this->nextText];
152
        if($this->currentPage < $this->totalPages)
153
        {
154
            $aVars['call'] = $this->getPageCall($this->currentPage + 1);
155
            return $this->xRenderer->render('pagination::links/next', $aVars);
156
        }
157
        return $this->xRenderer->render('pagination::links/disabled', $aVars);
158
    }
159
160
    /**
161
     * Render the pagination links.
162
     *
163
     * @param integer        $nNumber         The page number
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 9 found
Loading history...
164
     *
165
     * @return string
166
     */
167
    protected function getLink($nNumber)
168
    {
169
        if($nNumber <= 0)
170
        {
171
            // Disabled page
172
            return $this->xRenderer->render('pagination::links/disabled', ['text' => $this->ellipsysText]);
173
        }
174
        if($nNumber == $this->currentPage)
175
        {
176
            // Current page
177
            return $this->xRenderer->render('pagination::links/current', ['text' => $nNumber]);
178
        }
179
        // Enabled page
180
        return $this->xRenderer->render('pagination::links/enabled',[
181
            'text' => $nNumber,
182
            'call' => $this->getPageCall($nNumber)
183
        ]);
184
    }
185
186
    /**
187
     * Render an HTML pagination control.
188
     *
189
     * @param array     $aPageNumbers       The page numbers to be rendered
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 5 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 7 found
Loading history...
190
     * @param integer   $currentPage        The current page number
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 3 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 8 found
Loading history...
191
     * @param integer   $totalPages         The total number of pages
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 3 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 9 found
Loading history...
192
     *
193
     * @return string
194
     */
195
    public function render(array $aPageNumbers, $currentPage, $totalPages)
196
    {
197
        $this->currentPage = $currentPage;
198
        $this->totalPages = $totalPages;
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
199
200
        $sLinks = '';
201
        foreach($aPageNumbers as $nNumber)
202
        {
203
            $sLinks .= $this->getLink($nNumber);
204
        }
205
206
        return $this->xRenderer->render('pagination::wrapper', [
207
            'links' => $sLinks,
208
            'prev' => $this->getPrevLink(),
209
            'next' => $this->getNextLink(),
210
        ]);
211
    }
212
}
213