GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( ee1b66...d8c12e )
by Borut
02:48
created

PaginatorExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 54
rs 9.6716
cc 1
eloc 37
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Application\Twig;
4
5
use Silex\Application;
6
7
/**
8
 * @author Borut Balažek <[email protected]>
9
 */
10
class PaginatorExtension extends \Twig_Extension
11
{
12
    private $app;
13
14
    /**
15
     * @param Application $app
16
     */
17
    public function __construct(Application $app)
18
    {
19
        $this->app = $app;
20
    }
21
    
22
    /**
23
     * @return string
24
     */
25
    public function getName()
26
    {
27
        return 'application/paginator';
28
    }
29
30
    /**
31
     * @return array
32
     */
33
    public function getFunctions()
34
    {
35
        return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('paginator_...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...
36
            'paginator_limit_per_page_render' => new \Twig_Function_Method(
37
                $this,
38
                'paginatorLimitPerPageRender',
39
                array(
40
                    'is_safe' => array('html'),
41
                )
42
            ),
43
            'paginator_search_render' => new \Twig_Function_Method(
44
                $this,
45
                'paginatorSearchRender',
46
                array(
47
                    'is_safe' => array('html'),
48
                )
49
            ),
50
            'paginator_top_render' => new \Twig_Function_Method(
51
                $this,
52
                'paginatorTopRender',
53
                array(
54
                    'is_safe' => array('html'),
55
                )
56
            ),
57
            'paginator_pagination_render' => new \Twig_Function_Method(
58
                $this,
59
                'paginatorPaginationRender',
60
                array(
61
                    'is_safe' => array('html'),
62
                )
63
            ),
64
            'paginator_results_text_render' => new \Twig_Function_Method(
65
                $this,
66
                'paginatorResultsTextRender',
67
                array(
68
                    'is_safe' => array('html'),
69
                )
70
            ),
71
            'paginator_bottom_render' => new \Twig_Function_Method(
72
                $this,
73
                'paginatorBottomRender',
74
                array(
75
                    'is_safe' => array('html'),
76
                )
77
            ),
78
            'paginator_sortable' => new \Twig_Function_Method(
79
                $this,
80
                'paginatorSortable',
81
                array(
82
                    'is_safe' => array('html'),
83
                )
84
            ),
85
        );
86
    }
87
88
    /**
89
     * @param $pagination
90
     *
91
     * @return string
92
     */
93
    public function paginatorPaginationRender($pagination)
94
    {
95
        $output = '';
96
97
        $paginationData = $pagination->getPaginationData();
98
        $maxPageRange = isset($paginationData['pageRangeLimit'])
99
            ? intval($paginationData['pageRangeLimit'])
100
            : 10
101
        ;
102
        $route = $paginationData['route'];
103
        $routeParameters = $this->app['request']->query->all();
104
        if (isset($paginationData['routeParameters'])) {
105
            $routeParameters = array_merge(
106
                $routeParameters,
107
                $paginationData['routeParameters']
108
            );
109
        }
110
        $pageCount = ceil(
111
            intval($paginationData['totalCount']) /
112
            intval($paginationData['numItemsPerPage'])
113
        );
114
        $currentPage = intval($paginationData['current']);
115
116
        if ($pageCount > 1) {
117
            $pageRange = range(1, $pageCount);
118
119
            // Page range by max page numbers
120
            $pageRangeTmp = array();
121
            $rangeFrom = $currentPage - ceil($maxPageRange / 2);
122
            $rangeTo = $currentPage + ceil($maxPageRange / 2);
123
124
            foreach (range($rangeFrom, $rangeTo) as $singleRangePage) {
125
                if (in_array($singleRangePage, $pageRange)) {
126
                    $pageRangeTmp[] = $singleRangePage;
127
                }
128
            }
129
130
            $pageRange = $pageRangeTmp;
131
            // Page range by max page numbers /END
132
133
            // Prev
134 View Code Duplication
            if ($currentPage > 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
135
                $routeParameters = array_merge(
136
                    $routeParameters,
137
                    array(
138
                        $pagination->getPaginatorOption('pageParameterName') => $currentPage - 1,
139
                    )
140
                );
141
142
                $prevUrl = $this->app['url_generator']->generate(
143
                    $route,
144
                    $routeParameters
145
                );
146
            } else {
147
                $prevUrl = '#';
148
            }
149
            // Prev /END
150
151
            // Next
152 View Code Duplication
            if ($currentPage < $pageCount) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
153
                $routeParameters = array_merge(
154
                    $routeParameters,
155
                    array(
156
                        $pagination->getPaginatorOption('pageParameterName') => $currentPage + 1,
157
                    )
158
                );
159
160
                $nextUrl = $this->app['url_generator']->generate(
161
                    $route,
162
                    $routeParameters
163
                );
164
            } else {
165
                $nextUrl = '#';
166
            }
167
            // Next /END
168
169
            $output = $this->app['twig']->render(
170
                'twig/paginator/pagination.html.twig',
171
                array(
172
                    'app' => $this->app,
173
                    'prevUrl' => $prevUrl,
174
                    'nextUrl' => $nextUrl,
175
                    'pageRange' => $pageRange,
176
                    'routeParameters' => $routeParameters,
177
                    'pagination' => $pagination,
178
                    'route' => $route,
179
                    'currentPage' => $currentPage,
180
                    'pageCount' => $pageCount,
181
                )
182
            );
183
        }
184
185
        return $output;
186
    }
187
188
    /**
189
     * @param $pagination
190
     *
191
     * @return string
192
     */
193
    public function paginatorResultsTextRender($pagination)
194
    {
195
        $output = '';
196
197
        $paginationData = $pagination->getPaginationData();
198
        $routeParameters = $this->app['request']->query->all();
199
        if (isset($paginationData['routeParameters'])) {
200
            $routeParameters = array_merge(
0 ignored issues
show
Unused Code introduced by
$routeParameters is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
201
                $routeParameters,
202
                $paginationData['routeParameters']
203
            );
204
        }
205
        $pageCount = ceil(
206
            intval($paginationData['totalCount']) /
207
            intval($paginationData['numItemsPerPage'])
208
        );
209
        $currentPage = intval($paginationData['current']);
210
        $total = $paginationData['totalCount'];
211
212
        if ($total > 0 && $currentPage <= $pageCount) {
213
            $from = (($currentPage -1) * $paginationData['numItemsPerPage']) + 1;
214
            $to = $currentPage * $paginationData['numItemsPerPage'] > $total
215
                ? $total
216
                : $currentPage * $paginationData['numItemsPerPage']
217
            ;
218
219
            $output = $this->app['twig']->render(
220
                'twig/paginator/results-text.html.twig',
221
                array(
222
                    'app' => $this->app,
223
                    'from' => $from,
224
                    'to' => $to,
225
                    'total' => $total,
226
                )
227
            );
228
        }
229
230
        return $output;
231
    }
232
233
    /**
234
     * @param $pagination
235
     *
236
     * @return string
237
     */
238
    public function paginatorBottomRender($pagination)
239
    {
240
        return $this->app['twig']->render(
241
            'twig/paginator/_bottom.html.twig',
242
            array(
243
                'pagination' => $pagination,
244
            )
245
        );
246
    }
247
248
    /**
249
     * @param $pagination
250
     *
251
     * @return string
252
     */
253
    public function paginatorLimitPerPageRender($pagination)
254
    {
255
        $output = '';
256
257
        $paginationData = $pagination->getPaginationData();
258
259
        if ($paginationData['totalCount'] > 0) {
260
            $routeParameters = $this->app['request']->query->all();
261
            if (isset($paginationData['routeParameters'])) {
262
                $routeParameters = array_merge(
0 ignored issues
show
Unused Code introduced by
$routeParameters is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
263
                    $routeParameters,
264
                    $paginationData['routeParameters']
265
                );
266
            }
267
268
            $output = $this->app['twig']->render(
269
                'twig/paginator/limit-per-page.html.twig',
270
                array(
271
                    'app' => $this->app,
272
                    'pagination' => $pagination,
273
                )
274
            );
275
        }
276
277
        return $output;
278
    }
279
280
    /**
281
     * @param $pagination
282
     *
283
     * @return string
284
     */
285
    public function paginatorSearchRender($pagination)
286
    {
287
        return $this->app['twig']->render(
288
            'twig/paginator/search.html.twig',
289
            array(
290
                'app' => $this->app,
291
                'pagination' => $pagination,
292
            )
293
        );
294
    }
295
296
    /**
297
     * @param $pagination
298
     *
299
     * @return string
300
     */
301
    public function paginatorTopRender($pagination)
302
    {
303
        return $this->app['twig']->render(
304
            'twig/paginator/_top.html.twig',
305
            array(
306
                'pagination' => $pagination,
307
            )
308
        );
309
    }
310
311
    /**
312
     * @param $pagination
313
     * @param $text
314
     * @param $key
315
     *
316
     * @return string
317
     */
318
    public function paginatorSortable($pagination, $text = '', $key = '')
319
    {
320
        $output = '';
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
321
322
        $text = $this->app['translator']->trans($text);
323
324
        $sortDirectionParameterName = $pagination->getPaginatorOption('sortDirectionParameterName');
325
        $direction = isset($sortDirectionParameterName)
326
            ? $this->app['request']->query->get($sortDirectionParameterName)
327
            : 'asc'
328
        ;
329
330
        $direction = $direction == 'asc'
331
            ? 'desc'
332
            : 'asc'
333
        ;
334
335
        $paginationData = $pagination->getPaginationData();
336
        $route = $paginationData['route'];
337
        $routeParameters = $this->app['request']->query->all();
338
        if (isset($paginationData['routeParameters'])) {
339
            $routeParameters = array_merge(
340
                $routeParameters,
341
                $paginationData['routeParameters']
342
            );
343
        }
344
        $routeParameters = array_merge(
345
            $routeParameters,
346
            array(
347
                $pagination->getPaginatorOption('pageParameterName') => 1,
348
                $pagination->getPaginatorOption('sortFieldParameterName') => $key,
349
                $pagination->getPaginatorOption('sortDirectionParameterName') => $direction,
350
            )
351
        );
352
353
        $url = $this->app['url_generator']->generate(
354
            $route,
355
            $routeParameters
356
        );
357
358
        $icon = $direction == 'asc'
359
            ? 'down'
360
            : 'up'
361
        ;
362
363
        $showIcon = $this->app['request']->query->get(
364
            $pagination->getPaginatorOption('sortFieldParameterName'),
365
            $paginationData['defaultSortFieldName']
366
        ) == $key
367
            ? true
368
            : false
369
        ;
370
371
        $output = '<a href="'.$url.'">'.
372
            $text.
373
            ($showIcon ? ' <i class="fa fa-chevron-'.$icon.'"></i>' : '').
374
        '</a>';
375
376
        return $output;
377
    }
378
}
379