| Conditions | 26 |
| Paths | 94 |
| Total Lines | 108 |
| Code Lines | 71 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 104 | function calculate() { |
||
| 105 | $this->pagination = ""; |
||
| 106 | $this->calculate = true; |
||
| 107 | $this->errors = false; |
||
| 108 | |||
| 109 | if ( $this->urlF and $this->urlF != '%' and strpos( $this->target, $this->urlF ) === false ) { |
||
| 110 | //Es necesario especificar el comodin para sustituir |
||
| 111 | $this->errors = "Especificaste un wildcard para sustituir, pero no existe en el target"; |
||
| 112 | return; |
||
| 113 | } elseif ( $this->urlF and $this->urlF == '%' and strpos( $this->target, $this->urlF ) === false ) { |
||
| 114 | $this->errors = "Es necesario especificar en el target el comodin % para sustituir el n�mero de p�gina"; |
||
| 115 | return; |
||
| 116 | } |
||
| 117 | |||
| 118 | $n = $this->nextI; |
||
| 119 | $p = $this->prevI; |
||
| 120 | |||
| 121 | /* Setup vars for query. */ |
||
| 122 | if ( $this->page ) { |
||
| 123 | $this->start = ( $this->page - 1 ) * $this->limit; //first item to display on this page |
||
| 124 | $this->end = $this->start + $this->limit - 1; |
||
| 125 | } else { |
||
| 126 | $this->start = 0; //if no page var is given, set start to 0 |
||
| 127 | $this->end = $this->limit - 1; |
||
| 128 | } |
||
| 129 | |||
| 130 | /* Setup page vars for display. */ |
||
| 131 | $counter = 1; |
||
| 132 | $prev = $this->page - 1; //previous page is page - 1 |
||
| 133 | $next = $this->page + 1; //next page is page + 1 |
||
| 134 | $this->page_count = ceil( $this->item_count / $this->limit ); //lastpage is = total pages / items per page, rounded up. |
||
| 135 | $lpm1 = $this->page_count - 1; //last page minus 1 |
||
| 136 | |||
| 137 | /* |
||
| 138 | Now we apply our rules and draw the pagination object. |
||
| 139 | We're actually saving the code to a variable in case we want to draw it more than once. |
||
| 140 | */ |
||
| 141 | |||
| 142 | if ( $this->page_count > 1 ) { |
||
| 143 | if ( $this->page ) { |
||
| 144 | //anterior button |
||
| 145 | if ( $this->page > 1 ) { |
||
| 146 | $this->pagination .= "<a href=\"" . $this->get_page_number_url( $prev ) . "\" class=\"prev\">$p</a>"; |
||
| 147 | } else { |
||
| 148 | $this->pagination .= "<span class=\"disabled\">$p</span>"; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | //pages |
||
| 152 | if ( $this->page_count < 7 + ( $this->adjacents * 2 ) ) {//not enough pages to bother breaking it up |
||
| 153 | for ( $counter = 1; $counter <= $this->page_count; $counter ++ ) { |
||
| 154 | if ( $counter == $this->page ) { |
||
| 155 | $this->pagination .= "<span class=\"selected-page\">$counter</span>"; |
||
| 156 | } else { |
||
| 157 | $this->pagination .= "<a href=\"" . $this->get_page_number_url( $counter ) . "\">$counter</a>"; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } elseif ( $this->page_count > 5 + ( $this->adjacents * 2 ) ) {//enough pages to hide some |
||
| 161 | //close to beginning; only hide later pages |
||
| 162 | if ( $this->page < 1 + ( $this->adjacents * 2 ) ) { |
||
| 163 | for ( $counter = 1; $counter < 4 + ( $this->adjacents * 2 ); $counter ++ ) { |
||
| 164 | if ( $counter == $this->page ) { |
||
| 165 | $this->pagination .= "<span class=\"selected-page\">$counter</span>"; |
||
| 166 | } else { |
||
| 167 | $this->pagination .= "<a href=\"" . $this->get_page_number_url( $counter ) . "\">$counter</a>"; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | $this->pagination .= "..."; |
||
| 171 | $this->pagination .= "<a href=\"" . $this->get_page_number_url( $lpm1 ) . "\">$lpm1</a>"; |
||
| 172 | $this->pagination .= "<a href=\"" . $this->get_page_number_url( $this->page_count ) . "\">$this->page_count</a>"; |
||
| 173 | } //in middle; hide some front and some back |
||
| 174 | elseif ( $this->page_count - ( $this->adjacents * 2 ) > $this->page && $this->page > ( $this->adjacents * 2 ) ) { |
||
| 175 | $this->pagination .= "<a href=\"" . $this->get_page_number_url( 1 ) . "\">1</a>"; |
||
| 176 | $this->pagination .= "<a href=\"" . $this->get_page_number_url( 2 ) . "\">2</a>"; |
||
| 177 | $this->pagination .= "..."; |
||
| 178 | for ( $counter = $this->page - $this->adjacents; $counter <= $this->page + $this->adjacents; $counter ++ ) { |
||
| 179 | if ( $counter == $this->page ) { |
||
| 180 | $this->pagination .= "<span class=\"selected-page\">$counter</span>"; |
||
| 181 | } else { |
||
| 182 | $this->pagination .= "<a href=\"" . $this->get_page_number_url( $counter ) . "\">$counter</a>"; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | $this->pagination .= "..."; |
||
| 186 | $this->pagination .= "<a href=\"" . $this->get_page_number_url( $lpm1 ) . "\">$lpm1</a>"; |
||
| 187 | $this->pagination .= "<a href=\"" . $this->get_page_number_url( $this->page_count ) . "\">$this->page_count</a>"; |
||
| 188 | } //close to end; only hide early pages |
||
| 189 | else { |
||
| 190 | $this->pagination .= "<a href=\"" . $this->get_page_number_url( 1 ) . "\">1</a>"; |
||
| 191 | $this->pagination .= "<a href=\"" . $this->get_page_number_url( 2 ) . "\">2</a>"; |
||
| 192 | $this->pagination .= "..."; |
||
| 193 | for ( $counter = $this->page_count - ( 2 + ( $this->adjacents * 2 ) ); $counter <= $this->page_count; $counter ++ ) { |
||
| 194 | if ( $counter == $this->page ) { |
||
| 195 | $this->pagination .= "<span class=\"selected-page\">$counter</span>"; |
||
| 196 | } else { |
||
| 197 | $this->pagination .= "<a href=\"" . $this->get_page_number_url( $counter ) . "\">$counter</a>"; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | } |
||
| 202 | if ( $this->page ) { |
||
| 203 | //siguiente button |
||
| 204 | if ( $this->page < $counter - 1 ) { |
||
| 205 | $this->pagination .= "<a href=\"" . $this->get_page_number_url( $next ) . "\" class=\"next\">$n</a>"; |
||
| 206 | } else { |
||
| 207 | $this->pagination .= "<span class=\"disabled\">$n</span>"; |
||
| 208 | } |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.