Test Failed
Push — develop ( 08e897...bec489 )
by Paul
10:43
created

Paginate::links()   B

Complexity

Conditions 10
Paths 6

Size

Total Lines 30
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 24
c 1
b 1
f 0
dl 0
loc 30
ccs 0
cts 23
cp 0
rs 7.6666
cc 10
nc 6
nop 0
crap 110

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 GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Defaults\PaginationDefaults;
6
use GeminiLabs\SiteReviews\Modules\Html\Builder;
7
8
class Paginate
9
{
10
    public $args;
11
    public $style;
12
13
    public function __construct(array $args = [])
14
    {
15
        $base = wp_specialchars_decode(get_pagenum_link());
16
        $args = wp_parse_args($args, compact('base'));
17
        $args = glsr(PaginationDefaults::class)->restrict($args);
18
        $parts = explode('?', $base);
19
        if ($parts[1] ?? false) {
20
            $format = explode('?', str_replace('%_%', $args['format'], $args['base']));
21
            $formatQuery = $format[1] ?? '';
22
            wp_parse_str($formatQuery, $formatArgs);
23
            wp_parse_str($parts[1], $urlQueryArgs);
24
            foreach ($formatArgs as $arg => $value) {
25
                unset($urlQueryArgs[$arg]);
26
            }
27
            $verificationKeys = ['review_id', 'verified']; // remove verification keys from pagination URLs
28
            if (empty(array_diff_key(array_fill_keys($verificationKeys, ''), $urlQueryArgs))) {
29
                foreach ($verificationKeys as $key) {
30
                    unset($urlQueryArgs[$key]);
31
                }
32
            }
33
            $args['add_args'] = array_merge($args['add_args'], (array) urlencode_deep($urlQueryArgs));
34
        }
35
        $this->args = glsr()->args($args);
36
        $this->style = glsr_get_option('general.style');
37
    }
38
39
    public function linkPage(int $page): array
40
    {
41
        $format = 1 == $page ? '' : $this->args->format;
42
        return $this->link('page', [
43
            'class' => 'page-numbers',
44
            'data-page' => $page,
45
            'href' => $this->href($page, $format),
46
            'text' => trim($this->args->before_page_number.number_format_i18n($page)),
47
        ]);
48
    }
49
50
    public function linkCurrent(int $page): array
51
    {
52
        return $this->link('current', [
53
            'aria-current' => 'page',
54
            'class' => 'page-numbers current',
55
            'data-page' => $page,
56
            'href' => $this->href($page, $this->args->format),
57
            'text' => trim($this->args->before_page_number.number_format_i18n($page)),
58
        ], 'span');
59
    }
60
61
    public function linkDots(): array
62
    {
63
        return $this->link('dots', [
64
            'class' => 'page-numbers dots',
65
            'text' => __('&hellip;', 'site-reviews'),
66
        ], 'span');
67
    }
68
69
    public function linkNext(int $page): array
70
    {
71
        return $this->link('next', [
72
            'class' => 'page-numbers next',
73
            'data-page' => $page,
74
            'href' => $this->href($page, $this->args->format),
75
            'text' => $this->args->next_text,
76
        ]);
77
    }
78
79
    public function linkPrevious(int $page): array
80
    {
81
        $format = 2 == $this->args->current ? '' : $this->args->format;
82
        return $this->link('prev', [
83
            'class' => 'page-numbers prev',
84
            'data-page' => $page,
85
            'href' => $this->href($page, $format),
86
            'text' => $this->args->prev_text,
87
        ]);
88
    }
89
90
    public function links(): array
91
    {
92
        $args = $this->args;
93
        $dots = false;
94
        $minimum = max(0, $args->mid_size * 2);
95
        $firstPage = min($args->total - $minimum, max(1, $args->current - $args->mid_size));
96
        $lastPage = min($args->total, max($minimum + 1, $args->current + $args->mid_size));
97
        $links = [];
98
        if ($args->total < 2) {
99
            return $links;
100
        }
101
        $links[] = $this->linkPrevious($this->args->current - 1);
102
        for ($num = 1; $num <= $args->total; ++$num) {
103
            if ($num === $args->current) {
104
                $dots = true;
105
                $links[] = $this->linkCurrent($num);
106
            } else {
107
                $hasFirst = $num <= $args->end_size;
108
                $hasLast = $num > $args->total - $args->end_size;
109
                if ($hasFirst || ($num >= $firstPage && $num <= $lastPage) || $hasLast) {
110
                    $dots = true;
111
                    $links[] = $this->linkPage($num);
112
                } elseif ($dots && $args->end_size > 0) {
113
                    $dots = false;
114
                    $links[] = $this->linkDots();
115
                }
116
            }
117
        }
118
        $links[] = $this->linkNext($this->args->current + 1);
119
        return $links;
120
    }
121
122
    protected function href(int $page, string $format): string
123
    {
124
        $href = str_replace('%_%', $format, $this->args->base);
125
        $href = str_replace('%#%', (string) $page, $href);
126
        $href = add_query_arg($this->args->add_args, $href);
127
        return esc_url(apply_filters('paginate_links', $href));
128
    }
129
130
    protected function link(string $type, array $args, string $tag = 'a'): array
131
    {
132
        $builder = glsr(Builder::class);
133
        if ('prev' === $type && 1 >= $this->args->current) {
134
            $link = '';
135
        } elseif ('next' === $type && $this->args->current >= $this->args->total) {
136
            $link = '';
137
        } else {
138
            $link = $builder->build($tag, $args);
139
        }
140
        $link = compact('link', 'type');
141
        return glsr()->filterArray('paginate_link', $link, $args, $builder, $this);
142
    }
143
}
144