Completed
Push — inline_keyboard_pagination ( f737a0 )
by Armando
03:29
created

InlineKeyboard   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 14.12 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 12
loc 85
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C getPagination() 12 55 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file is part of the TelegramBot package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\TelegramBot\Entities;
12
13
/**
14
 * Class InlineKeyboard
15
 *
16
 * @link https://core.telegram.org/bots/api#inlinekeyboardmarkup
17
 */
18
class InlineKeyboard extends Keyboard
19
{
20
    /**
21
     * Get an inline pagination keyboard.
22
     *
23
     * - $callback_data is an ID for the CallbackqueryCommand, to know where the request comes from.
24
     * It must contain a '%d' placeholder for the page number. If no placeholder is found, the ID is automatically appended with '_page_%d'
25
     *
26
     * - $labels allows for custom button labels, using '%d' placeholders.
27
     * Default:
28
     * ```
29
     * [
30
     *     'first'    => '« %d',
31
     *     'previous' => '‹ %d',
32
     *     'current'  => '· %d ·',
33
     *     'next'     => '%d ›',
34
     *     'last'     => '%d »',
35
     * ]
36
     * ``
37
     *`
38
     * initial idea from: https://stackoverflow.com/a/42879866
39
     *
40
     * @param string $callback_data
41
     * @param int    $current_page
42
     * @param int    $max_pages
43
     * @param array  $labels
44
     *
45
     * @return \Longman\TelegramBot\Entities\InlineKeyboard
46
     */
47 1
    public static function getPagination($callback_data, $current_page, $max_pages, array $labels = [])
48
    {
49 1
        if (strpos($callback_data, '%d') === false) {
50 1
            $callback_data .= '_page_%d';
51
        }
52
53
        // Merge labels with defaults.
54 1
        $labels = array_merge([
55 1
            'first'    => '« %d',
56
            'previous' => '‹ %d',
57
            'current'  => '· %d ·',
58
            'next'     => '%d ›',
59
            'last'     => '%d »',
60
        ], $labels);
61
        $pages  = [
62 1
            'first'    => 1,
63 1
            'previous' => $current_page - 1,
64 1
            'current'  => $current_page,
65 1
            'next'     => $current_page + 1,
66 1
            'last'     => $max_pages,
67
        ];
68
69
        // Set labels for keyboard, replacing placeholders with page numbers.
70 1
        foreach ($labels as $key => &$label) {
71 1
            if (strpos($label, '%d') !== false) {
72 1
                $label = sprintf($label, $pages[$key]);
73
            }
74
        }
75 1
        unset($label);
76
77 1
        $callbacks_data = [];
78 1
        foreach ($pages as $key => $page) {
79 1
            $callbacks_data[$key] = sprintf($callback_data, $page);
80
        }
81
82 1
        $buttons = [];
83
84 1 View Code Duplication
        if ($current_page > 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85 1
            $buttons[] = new InlineKeyboardButton(['text' => $labels['first'], 'callback_data' => $callbacks_data['first']]);
86
        }
87 1 View Code Duplication
        if ($current_page > 2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88 1
            $buttons[] = new InlineKeyboardButton(['text' => $labels['previous'], 'callback_data' => $callbacks_data['previous']]);
89
        }
90
91 1
        $buttons[] = new InlineKeyboardButton(['text' => $labels['current'], 'callback_data' => $callbacks_data['current']]);
92
93 1 View Code Duplication
        if ($current_page < $max_pages - 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94 1
            $buttons[] = new InlineKeyboardButton(['text' => $labels['next'], 'callback_data' => $callbacks_data['next']]);
95
        }
96 1 View Code Duplication
        if ($current_page < $max_pages) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97 1
            $buttons[] = new InlineKeyboardButton(['text' => $labels['last'], 'callback_data' => $callbacks_data['last']]);
98
        }
99
100 1
        return new InlineKeyboard($buttons);
101
    }
102
}
103