Test Failed
Push — master ( f5edd0...a5936b )
by Daniel
02:25
created

DomPaginationByDanielGP::setPagination()   D

Complexity

Conditions 14
Paths 240

Size

Total Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
nc 240
nop 4
dl 0
loc 82
rs 4.3393
c 0
b 0
f 0

How to fix   Long Method    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
/*
4
 * The MIT License
5
 *
6
 * Copyright 2018 Daniel Popiniuc
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace danielgp\common_lib;
28
29
/**
30
 * DOM component functions
31
 *
32
 * @author Daniel Popiniuc
33
 */
34
trait DomPaginationByDanielGP
35
{
36
37
    use DomBasicComponentsByDanielGP;
38
39
    /**
40
     * Returns a pagination bar
41
     *
42
     * @param int $iCrtPgNo
43
     * @param int $inRecPrPg
44
     * @param int $iAllRec
45
     * @param boolean $bKpFlPg
46
     * returns string
47
     */
48
    protected function setPagination($iCrtPgNo, $inRecPrPg, $iAllRec, $bKpFlPg = true)
0 ignored issues
show
Unused Code introduced by
The parameter $inRecPrPg is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

48
    protected function setPagination($iCrtPgNo, /** @scrutinizer ignore-unused */ $inRecPrPg, $iAllRec, $bKpFlPg = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
        $sReturn             = null;
51
        $iRecPrPg            = min($iRecPrPg, $iAllRec);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $iRecPrPg seems to be never defined.
Loading history...
52
        $iStartingPageRecord = $this->setStartingPageRecord($iCrtPgNo, $iRecPrPg, $iAllRec, $bKpFlPg);
53
        $sReturn             .= '<span style="float:left;font-size:smaller;margin-top:1px; margin-right:1px;">'
54
            . $this->setStringIntoTag($iAllRec, 'b')
55
            . $this->lclMsgCmn('i18n_RecordsAvailableNowDisplaying')
56
            . $this->setStringIntoTag(($iStartingPageRecord + 1), 'b')
57
            . ' - ' . $this->setStringIntoTag(min($iAllRec, ($iStartingPageRecord + $iRecPrPg)), 'b')
58
            . ' </span>';
59
        switch ($iCrtPgNo) {
60
            case 'first':
61
                $iCrtPgNo = ceil(($iStartingPageRecord + 1 ) / $iRecPrPg);
62
                break;
63
            case 'last':
64
                $iCrtPgNo = ceil($iAllRec / $iRecPrPg);
65
                break;
66
        }
67
        $sReturn              .= '<span style="float:right;font-size:smaller;margin-top:1px; margin-right:1px;">';
68
        $iNumberOfPages       = ceil($iAllRec / $iRecPrPg);
69
        $sAdditionalArguments = '';
70
        if (isset($_GET)) {
71
            if ($_GET != ['page' => @$_GET['page']]) {
72
                $sAdditionalArguments = '&amp;'
73
                    . $this->setArrayToStringForUrl('&amp;', $_GET, ['page', 'action', 'server_action']);
0 ignored issues
show
Bug introduced by
It seems like setArrayToStringForUrl() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
                    . $this->/** @scrutinizer ignore-call */ setArrayToStringForUrl('&amp;', $_GET, ['page', 'action', 'server_action']);
Loading history...
74
            }
75
            if (isset($_GET['page'])) {
76
                $iCrtPgNo = $_GET['page'];
77
            }
78
        }
79
        if ($iCrtPgNo != 1) {
80
            $sReturn .= $this->setStringIntoTag($this->lclMsgCmn('i18n_Previous'), 'a', [
81
                'href'  => ('?page=' . ($iCrtPgNo - 1 ) . $sAdditionalArguments ),
82
                'class' => 'pagination'
83
            ]);
84
        } else {
85
            $sReturn .= $this->setStringIntoTag($this->lclMsgCmn('i18n_Previous'), 'span', [
86
                'class' => 'pagination_inactive'
87
            ]);
88
        }
89
        $pages2display = [];
90
        for ($counter = 1; $counter <= $iNumberOfPages; $counter++) {
91
            $pages2display[$counter] = $counter;
92
        }
93
        $sReturn .= '<span class="pagination"><form method="get" action="' . $_SERVER['SCRIPT_NAME'] . '">';
94
        $sReturn .= $this->setArrayToSelect($pages2display, @$_REQUEST['page']
0 ignored issues
show
Bug introduced by
It seems like setArrayToSelect() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
        $sReturn .= $this->/** @scrutinizer ignore-call */ setArrayToSelect($pages2display, @$_REQUEST['page']
Loading history...
95
            , 'page', ['size' => 1, 'autosubmit', 'id_no' => mt_rand()]);
96
        if (isset($_GET)) {
97
            foreach ($_GET as $key => $value) {
98
                if ($key != 'page') {
99
                    if (is_array($value)) {
100
                        foreach ($value as $value2) {
101
                            $sReturn .= $this->setStringIntoShortTag('input', [
102
                                'type'  => 'hidden',
103
                                'name'  => $key . '[]',
104
                                'value' => $value2,
105
                            ]);
106
                        }
107
                    } else {
108
                        $sReturn .= $this->setStringIntoShortTag('input', [
109
                            'type'  => 'hidden',
110
                            'name'  => $key,
111
                            'value' => $value,
112
                        ]);
113
                    }
114
                }
115
            }
116
        }
117
        $sReturn .= '</form></span>';
118
        if ($iCrtPgNo != $iNumberOfPages) {
119
            $sReturn .= $this->setStringIntoTag($this->lclMsgCmn('i18n_Next'), 'a', [
120
                'href'  => ('?page=' . ($iCrtPgNo + 1 ) . $sAdditionalArguments ),
121
                'class' => 'pagination',
122
            ]);
123
        } else {
124
            $sReturn .= $this->setStringIntoTag($this->lclMsgCmn('i18n_Next'), 'span', [
125
                'class' => 'pagination_inactive',
126
            ]);
127
        }
128
        $sReturn .= '</span>';
129
        return $sReturn;
130
    }
131
132
    /**
133
     * Returns starting records for LIMIT clause on SQL interrogation
134
     *
135
     * @version 20080521
136
     * @param string $sDefaultPageNo
137
     * @param int $iRecordsPerPage
138
     * @param int $iAllRecords
139
     * @param boolean $bKeepFullPage
140
     * @return int
141
     */
142
    private function setStartingPageRecord($sDefaultPageNo, $iRecordsPerPage
143
        , $iAllRecords, $bKeepFullPage = true)
144
    {
145
        if (isset($_REQUEST['page'])) {
146
            $iStartingPageRecord = ($_REQUEST['page'] - 1 ) * $iRecordsPerPage;
147
        } else {
148
            switch ($sDefaultPageNo) {
149
                case 'last':
150
                    $iStartingPageRecord = $iAllRecords - $iRecordsPerPage;
151
                    break;
152
                case 'first':
153
                default:
154
                    $iStartingPageRecord = 0;
155
                    break;
156
            }
157
        }
158
        if (($bKeepFullPage ) && (($iStartingPageRecord + $iRecordsPerPage ) > $iAllRecords)) {
159
            $iStartingPageRecord = $iAllRecords - $iRecordsPerPage;
160
        }
161
        return max(0, $iStartingPageRecord);
162
    }
163
164
}
165