Completed
Pull Request — develop (#438)
by Armando
03:24
created

InlineKeyboard   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 99
Duplicated Lines 15.15 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 2
dl 15
loc 99
ccs 30
cts 32
cp 0.9375
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
D getPagination() 15 57 14
A getPageFromCallbackData() 0 4 1

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
     * The ID is automatically appended with '_page_%d' to filter out the selected page number.
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
        $callback_data .= '_page_%d';
50
51
        // Merge labels with defaults.
52 1
        $labels = array_merge([
53 1
            'first'    => '« %d',
54
            'previous' => '‹ %d',
55
            'current'  => '· %d ·',
56
            'next'     => '%d ›',
57
            'last'     => '%d »',
58
        ], $labels);
59
        $pages  = [
60 1
            'first'    => 1,
61 1
            'previous' => $current_page - 1,
62 1
            'current'  => $current_page,
63 1
            'next'     => $current_page + 1,
64 1
            'last'     => $max_pages,
65
        ];
66
67
        // Set labels for keyboard, replacing placeholders with page numbers.
68 1
        foreach ($labels as $key => &$label) {
69 1
            if (strpos($label, '%d') !== false) {
70 1
                $label = sprintf($label, $pages[$key]);
71 1
            } elseif ($label === '') {
72 1
                $label = null;
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 && $labels['first'] !== null) {
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 && $labels['previous'] !== null) {
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 View Code Duplication
        if ($labels['current'] !== null) {
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...
92 1
            $buttons[] = new InlineKeyboardButton(['text' => $labels['current'], 'callback_data' => $callbacks_data['current']]);
93
        }
94
95 1 View Code Duplication
        if ($current_page < $max_pages - 1 && $labels['next'] !== null) {
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...
96 1
            $buttons[] = new InlineKeyboardButton(['text' => $labels['next'], 'callback_data' => $callbacks_data['next']]);
97
        }
98 1 View Code Duplication
        if ($current_page < $max_pages && $labels['last'] !== null) {
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...
99 1
            $buttons[] = new InlineKeyboardButton(['text' => $labels['last'], 'callback_data' => $callbacks_data['last']]);
100
        }
101
102 1
        return new InlineKeyboard($buttons);
103
    }
104
105
    /**
106
     * Extract the page number from the passed callback data.
107
     *
108
     * @param string $callback_data
109
     *
110
     * @return int
111
     */
112
    public static function getPageFromCallbackData($callback_data)
113
    {
114
        return (int) preg_replace('/.*_page_(\d+)$/', '$1', $callback_data);
115
    }
116
}
117