Passed
Push — development ( 7327cc...058d58 )
by Eric
03:29 queued 38s
created

pagination.php ➔ spurs_pagination()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 55
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 37
nc 4
nop 2
dl 0
loc 55
rs 9.078
c 0
b 0
f 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
 * Pagination layout.
4
 *
5
 * @package spurs
6
 */
7
8
if ( ! function_exists( 'spurs_pagination' ) ) {
9
	function spurs_pagination( $args = [], $class = 'pagination' ) {
0 ignored issues
show
Coding Style introduced by
spurs_pagination uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
10
11
		if ( $GLOBALS['wp_query']->max_num_pages <= 1 ) {
12
			return;
13
		}
14
15
		$args = wp_parse_args( $args, [
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $args. This often makes code more readable.
Loading history...
16
			'mid_size'           => 2,
17
			'prev_next'          => false,
18
			'prev_text'          => __( '&laquo;', 'spurs' ),
19
			'next_text'          => __( '&raquo;', 'spurs' ),
20
			'screen_reader_text' => __( 'Posts navigation', 'spurs' ),
21
			'type'               => 'array',
22
			'current'            => max( 1, get_query_var( 'paged' ) ),
23
		] );
24
25
		$links     = paginate_links( $args );
26
		$next_link = get_next_posts_page_link();
27
		$prev_link = get_previous_posts_page_link();
28
29
		?>
30
31
        <nav aria-label="<?php echo $args['screen_reader_text']; ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$args'
Loading history...
32
            <ul class="pagination">
33
                <li class="page-item">
34
                    <a class="page-link" href="<?php echo esc_attr( $prev_link ); ?>"
35
                       aria-label="<?php echo __( 'Previous', 'spurs' ); ?>">
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
36
                        <span aria-hidden="true"><?php echo esc_attr( $args['prev_text'] ); ?></span>
37
                        <span class="sr-only"><?php echo __( 'Previous', 'spurs' ); ?></span>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
38
                    </a>
39
                </li>
40
41
				<?php
42
				$i = 1;
43
				foreach ( $links as $link ) { ?>
44
                    <li class="page-item <?php if ( $i == $args['current'] ) {
45
						echo 'active';
46
					}; ?>">
47
						<?php echo str_replace( 'page-numbers', 'page-link', $link ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'str_replace'
Loading history...
48
                    </li>
49
50
					<?php $i ++;
51
				} ?>
52
53
                <li class="page-item">
54
                    <a class="page-link" href="<?php echo esc_attr( $next_link ); ?>"
55
                       aria-label="<?php echo __( 'Next', 'spurs' ); ?>">
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
56
                        <span aria-hidden="true"><?php echo esc_attr( $args['next_text'] ); ?></span>
57
                        <span class="sr-only"><?php echo __( 'Next', 'spurs' ); ?></span>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
58
                    </a>
59
                </li>
60
            </ul>
61
        </nav>
62
		<?php
63
	}
64
}