Completed
Push — master ( 9a19e6...44bc09 )
by Mikołaj
06:44
created

Paging::isAttribute()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 2
dl 11
loc 11
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Rudolf\Component\Html;
4
5
class Paging
6
{
7
    /**
8
     * @var array
9
     */
10
    private $info;
11
12
    /**
13
     * @var string
14
     */
15
    private $path;
16
17
    /**
18
     * @var array
19
     */
20
    private $classes;
21
22
    /**
23
     * @var int
24
     */
25
    private $nesting;
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param array  $info     with data for loop
31
     *                        <page> - current page
32
     *                        <forstart> -
33
     *                        <forend> -
34
     *                        <allpages> - all pages
35
     *                        <prev> - prev page
36
     *                        <next> - next page
37
     * @param string $path    path with a slash at the beginning and at the end without him, like: '/kg'
38
     * @param array  $classes Specifies a pagination appearance
39
     *                        <ul> - main ul class
40
     *                        <current> - current active li class
41
     * @param int    $nesting
42
     */
43
    public function __construct(array $info = [], $path = '', array $classes = [], $nesting = 0)
44
    {
45
        $this->setInfo($info);
46
        $this->setPath($path);
47
        $this->setClasses($classes);
48
        $this->setNesting($nesting);
49
    }
50
51
    public function setInfo(array $info)
52
    {
53
        $this->info = $info;
54
    }
55
56
    public function getInfo()
57
    {
58
        return $this->info;
59
    }
60
61
    public function setPath($path)
62
    {
63
        $this->path = $path;
64
    }
65
66
    public function getPath()
67
    {
68
        return DIR.$this->path;
69
    }
70
71
    public function setClasses(array $classes)
72
    {
73
        $this->classes = array_merge([
74
            'ul' => '',
75
            'li_current' => '',
76
            'a' => '',
77
            'a_current' => '',
78
        ], $classes);
79
    }
80
81
    public function getClasses()
82
    {
83
        return $this->classes;
84
    }
85
86
    public function setNesting($nesting)
87
    {
88
        $this->nesting = $nesting;
89
    }
90
91
    public function getNesting()
92
    {
93
        return $this->nesting;
94
    }
95
96
    /**
97
     * Put value is not empty.
98
     *
99
     * @param string       $attribute
100
     * @param string|array $value
101
     *
102
     * @return string
103
     */
104 View Code Duplication
    private function isAttribute($attribute, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
105
    {
106
        if (is_array($value)) {
107
            array_filter($value);
108
            $value = trim(implode(' ', $value));
109
110
            return !empty($value) ? ' '.$attribute.'="'.$value.'"' : '';
111
        }
112
113
        return (isset($value) and !empty($value)) ? ' '.$attribute.'="'.trim($value).'"' : '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
114
    }
115
116
    public function create()
117
    {
118
        $nav = $this->getInfo();
119
        $path = $this->getPath();
120
        $classes = $this->getClasses();
121
        $nesting = $this->getNesting();
122
123
        $html[] = sprintf('<ul%1$s>', $this->isAttribute('class', $classes['ul']));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$html was never initialized. Although not strictly required by PHP, it is generally a good practice to add $html = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
124
125
        $nest = str_repeat("\t", $nesting);
126
        $tab = str_repeat("\t", 1 + $nesting);
127
128
        if ($nav['page'] > 1) {
129
            $html[] = sprintf(
130
                '%1$s<li%2$s><a%3$s href="%4$s">«</a></li>',
131
                $tab,
132
                $this->isAttribute('class', $classes['li']),
133
                $this->isAttribute('class', $classes['a']),
134
                $path.'/page/'.$nav['prev']
135
            );
136
        }
137 View Code Duplication
        if ($nav['forstart'] > 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...
138
            $html[] = sprintf(
139
                '%1$s<li%2$s><a%3$s href="%4$s">1</a></li>',
140
                $tab,
141
                $this->isAttribute('class', $classes['li']),
142
                $this->isAttribute('class', $classes['a']),
143
                $path.'/page/1'
144
            );
145
        }
146 View Code Duplication
        if ($nav['forstart'] > 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...
147
            $html[] = sprintf(
148
                '%1$s<li%2$s><a%3$s>...</a></li>',
149
                $tab,
150
                $this->isAttribute('class', $classes['li']),
151
                $this->isAttribute('class', $classes['a'])
152
            );
153
        }
154
        for ($nav['forstart']; $nav['forstart'] < $nav['forend']; ++$nav['forstart']) {
155
            if ((int) $nav['forstart'] === $nav['page']) {
156
                $html[] = sprintf(
157
                    '%1$s<li%2$s><a%3$s href="%4$s">%5$s</a></li>',
158
                    $tab,
159
                    $this->isAttribute('class', [$classes['li'], $classes['li_current']]),
160
                    $this->isAttribute('class', [$classes['a'], $classes['a_current']]),
161
                    $path.'/page/'.$nav['forstart'],
162
                    $nav['forstart']
163
                );
164
            }
165 View Code Duplication
            if ((int) $nav['forstart'] !== $nav['page']) {
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...
166
                $html[] = sprintf(
167
                    '%1$s<li%2$s><a%3$s href="%4$s">%5$s</a></li>',
168
                    $tab,
169
                    $this->isAttribute('class', $classes['li']),
170
                    $this->isAttribute('class', $classes['a']),
171
                    $path.'/page/'.$nav['forstart'],
172
                    $nav['forstart']
173
                );
174
            }
175
        }
176 View Code Duplication
        if ($nav['forstart'] < $nav['allpages']) {
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...
177
            $html[] = sprintf(
178
                '%1$s<li%2$s><a%3$s>...</a></li>',
179
                $tab,
180
                $this->isAttribute('class', $classes['li']),
181
                $this->isAttribute('class', $classes['a'])
182
            );
183
        }
184 View Code Duplication
        if ($nav['forstart'] - 1 < $nav['allpages']) {
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...
185
            $html[] = sprintf(
186
                '%1$s<li%2$s><a%3$s href="%4$s">%5$s</a></li>',
187
                $tab,
188
                $this->isAttribute('class', $classes['li']),
189
                $this->isAttribute('class', $classes['a']),
190
                $path.'/page/'.$nav['allpages'],
191
                $nav['allpages']
192
            );
193
        }
194 View Code Duplication
        if ($nav['page'] < $nav['allpages']) {
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...
195
            $html[] = sprintf(
196
                '%1$s<li%2$s><a%3$s href="%4$s">»</a></li>',
197
                $tab,
198
                $this->isAttribute('class', $classes['li']),
199
                $this->isAttribute('class', $classes['a']),
200
                $path.'/page/'.$nav['next']
201
            );
202
        }
203
        $html[] = $nest.'</ul>'."\n";
204
205
        return implode("\n", $html);
206
    }
207
}
208