| Conditions | 27 |
| Paths | 433 |
| Total Lines | 95 |
| Code Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 120 | public static function paginate_links( $args = '' ) { |
||
| 121 | $defaults = array( |
||
| 122 | 'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below) |
||
| 123 | 'format' => '?page=%#%', // ?page=%#% : %#% is replaced by the page number |
||
| 124 | 'total' => 1, |
||
| 125 | 'current' => 0, |
||
| 126 | 'show_all' => false, |
||
| 127 | 'prev_next' => true, |
||
| 128 | 'prev_text' => __( '« Previous' ), |
||
| 129 | 'next_text' => __( 'Next »' ), |
||
| 130 | 'end_size' => 1, |
||
| 131 | 'mid_size' => 2, |
||
| 132 | 'type' => 'array', |
||
| 133 | 'add_args' => false, // array of query args to add |
||
| 134 | 'add_fragment' => '' |
||
| 135 | ); |
||
| 136 | $args = wp_parse_args( $args, $defaults ); |
||
| 137 | // Who knows what else people pass in $args |
||
| 138 | $args['total'] = intval( (int)$args['total'] ); |
||
| 139 | if ( $args['total'] < 2 ) { |
||
| 140 | return array(); |
||
| 141 | } |
||
| 142 | $args['current'] = (int)$args['current']; |
||
| 143 | $args['end_size'] = 0 < (int)$args['end_size'] ? (int)$args['end_size'] : 1; // Out of bounds? Make it the default. |
||
| 144 | $args['mid_size'] = 0 <= (int)$args['mid_size'] ? (int)$args['mid_size'] : 2; |
||
| 145 | $args['add_args'] = is_array( $args['add_args'] ) ? $args['add_args'] : false; |
||
| 146 | $page_links = array(); |
||
| 147 | $dots = false; |
||
| 148 | if ( $args['prev_next'] && $args['current'] && 1 < $args['current'] ) { |
||
| 149 | $link = str_replace( '%_%', 2 == $args['current'] ? '' : $args['format'], $args['base'] ); |
||
| 150 | $link = str_replace( '%#%', $args['current'] - 1, $link ); |
||
| 151 | if ( $args['add_args'] ) { |
||
| 152 | $link = add_query_arg( $args['add_args'], $link ); |
||
| 153 | } |
||
| 154 | $link .= $args['add_fragment']; |
||
| 155 | $link = untrailingslashit( $link ); |
||
| 156 | $page_links[] = array( |
||
| 157 | 'class' => 'prev page-numbers', |
||
| 158 | 'link' => esc_url( apply_filters( 'paginate_links', $link ) ), |
||
| 159 | 'title' => $args['prev_text'] |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | for ( $n = 1; $n <= $args['total']; $n++ ) { |
||
| 163 | $n_display = number_format_i18n( $n ); |
||
| 164 | if ( $n == $args['current'] ) { |
||
| 165 | $page_links[] = array( |
||
| 166 | 'class' => 'page-number page-numbers current', |
||
| 167 | 'title' => $n_display, |
||
| 168 | 'text' => $n_display, |
||
| 169 | 'name' => $n_display, |
||
| 170 | 'current' => true |
||
| 171 | ); |
||
| 172 | $dots = true; |
||
| 173 | } else { |
||
| 174 | if ( $args['show_all'] || ( $n <= $args['end_size'] || ( $args['current'] && $n >= $args['current'] - $args['mid_size'] && $n <= $args['current'] + $args['mid_size'] ) || $n > $args['total'] - $args['end_size'] ) ) { |
||
| 175 | $link = str_replace( '%_%', 1 == $n ? '' : $args['format'], $args['base'] ); |
||
| 176 | $link = str_replace( '%#%', $n, $link ); |
||
| 177 | $link = trailingslashit( $link ) . ltrim( $args['add_fragment'], '/' ); |
||
| 178 | if ( $args['add_args'] ) { |
||
| 179 | $link = rtrim( add_query_arg( $args['add_args'], $link ), '/' ); |
||
| 180 | } |
||
| 181 | $link = str_replace(' ', '+', $link); |
||
| 182 | $link = untrailingslashit( $link ); |
||
| 183 | $page_links[] = array( |
||
| 184 | 'class' => 'page-number page-numbers', |
||
| 185 | 'link' => esc_url( apply_filters( 'paginate_links', $link ) ), |
||
| 186 | 'title' => $n_display, |
||
| 187 | 'name' => $n_display, |
||
| 188 | 'current' => $args['current'] == $n |
||
| 189 | ); |
||
| 190 | $dots = true; |
||
| 191 | } elseif ( $dots && !$args['show_all'] ) { |
||
| 192 | $page_links[] = array( |
||
| 193 | 'class' => 'dots', |
||
| 194 | 'title' => __( '…' ) |
||
| 195 | ); |
||
| 196 | $dots = false; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | if ( $args['prev_next'] && $args['current'] && ( $args['current'] < $args['total'] || -1 == $args['total'] ) ) { |
||
| 201 | $link = str_replace( '%_%', $args['format'], $args['base'] ); |
||
| 202 | $link = str_replace( '%#%', $args['current'] + 1, $link ); |
||
| 203 | if ( $args['add_args'] ) { |
||
| 204 | $link = add_query_arg( $args['add_args'], $link ); |
||
| 205 | } |
||
| 206 | $link = untrailingslashit( trailingslashit( $link ) . $args['add_fragment'] ); |
||
| 207 | $page_links[] = array( |
||
| 208 | 'class' => 'next page-numbers', |
||
| 209 | 'link' => esc_url( apply_filters( 'paginate_links', $link ) ), |
||
| 210 | 'title' => $args['next_text'] |
||
| 211 | ); |
||
| 212 | } |
||
| 213 | return $page_links; |
||
| 214 | } |
||
| 215 | |||
| 304 | } |
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.