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.

LinkPager::renderPageButtons()   B
last analyzed

Complexity

Conditions 10
Paths 73

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 7.6666
c 0
b 0
f 0
cc 10
nc 73
nop 0

How to fix   Complexity   

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 app\widgets;
4
5
use yii\helpers\Html;
6
7
class LinkPager extends \yii\widgets\LinkPager
8
{
9
    /**
10
     * Renders the page buttons.
11
     * @return string the rendering result
12
     */
13
    protected function renderPageButtons()
14
    {
15
        $pageCount = $this->pagination->getPageCount();
16
        if ($pageCount < 2 && $this->hideOnSinglePage) {
17
            return '';
18
        }
19
20
        $buttons = [];
21
        $currentPage = $this->pagination->getPage();
22
23
        // first page
24
        if ($this->firstPageLabel !== false) {
25
            $buttons[] = $this->renderPageButton($this->firstPageLabel, 0, $this->firstPageCssClass, $currentPage <= 0, false);
0 ignored issues
show
Bug introduced by
It seems like $this->firstPageLabel can also be of type boolean; however, app\widgets\LinkPager::renderPageButton() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
26
        }
27
28
        // prev page
29
        if ($this->prevPageLabel !== false) {
30
            if (($page = $currentPage - 1) < 0) {
31
                $page = 0;
32
            }
33
            $buttons[] = $this->renderPageButton($this->prevPageLabel, $page, $this->prevPageCssClass, $currentPage <= 0, false, 'prev');
0 ignored issues
show
Bug introduced by
It seems like $this->prevPageLabel can also be of type boolean; however, app\widgets\LinkPager::renderPageButton() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
34
        }
35
36
        // internal pages
37
        list($beginPage, $endPage) = $this->getPageRange();
38
        for ($i = $beginPage; $i <= $endPage; ++$i) {
39
            $buttons[] = $this->renderPageButton($i + 1, $i, null, false, $i == $currentPage);
40
        }
41
42
        // next page
43
        if ($this->nextPageLabel !== false) {
44
            if (($page = $currentPage + 1) >= $pageCount - 1) {
45
                $page = $pageCount - 1;
46
            }
47
            $buttons[] = $this->renderPageButton($this->nextPageLabel, $page, $this->nextPageCssClass, $currentPage >= $pageCount - 1, false, 'next');
0 ignored issues
show
Bug introduced by
It seems like $this->nextPageLabel can also be of type boolean; however, app\widgets\LinkPager::renderPageButton() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
48
        }
49
50
        // last page
51
        if ($this->lastPageLabel !== false) {
52
            $buttons[] = $this->renderPageButton($this->lastPageLabel, $pageCount - 1, $this->lastPageCssClass, $currentPage >= $pageCount - 1, false);
0 ignored issues
show
Bug introduced by
It seems like $this->lastPageLabel can also be of type boolean; however, app\widgets\LinkPager::renderPageButton() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
53
        }
54
55
        return Html::tag('ul', implode("\n", $buttons), $this->options);
56
    }
57
58
    /**
59
     * Renders a page button.
60
     * You may override this method to customize the generation of page buttons.
61
     * @param string $label the text label for the button
62
     * @param integer $page the page number
63
     * @param string $class the CSS class for the page button.
64
     * @param boolean $disabled whether this page button is disabled
65
     * @param boolean $active whether this page button is active
66
     * @param string $rel the rel attribute for the button
0 ignored issues
show
Documentation introduced by
Should the type for parameter $rel not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
67
     * @return string the rendering result
68
     */
69
    protected function renderPageButton($label, $page, $class, $disabled, $active, $rel = null)
70
    {
71
        $options = ['class' => $class === '' ? null : $class];
72
        if (!is_null($rel)) {
73
            $options['rel'] = $rel;
74
        }
75
        if ($active) {
76
            Html::addCssClass($options, $this->activePageCssClass);
77
        }
78
        if ($disabled) {
79
            Html::addCssClass($options, $this->disabledPageCssClass);
80
81
            return Html::tag('li', Html::tag('span', $label), $options);
82
        }
83
        $linkOptions = $this->linkOptions;
84
        $linkOptions['data-page'] = $page;
85
86
        return Html::tag('li', Html::a($label, $this->pagination->createUrl($page), $linkOptions), $options);
87
    }
88
}
89