ButtonNormalizer   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 105
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 105
loc 105
rs 10
wmc 17
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 10 10 2
F process() 78 78 15

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 namespace Anomaly\Streams\Platform\Ui\Table\Component\Button;
2
3
use Anomaly\Streams\Platform\Ui\Table\TableBuilder;
4
5
/**
6
 * Class ButtonNormalizer
7
 *
8
 * @link   http://pyrocms.com/
9
 * @author PyroCMS, Inc. <[email protected]>
10
 * @author Ryan Thompson <[email protected]>
11
 */
12 View Code Duplication
class ButtonNormalizer
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
15
    /**
16
     * Normalize button input.
17
     *
18
     * @param TableBuilder $builder
19
     */
20
    public function normalize(TableBuilder $builder)
21
    {
22
        $buttons = $builder->getButtons();
23
24
        foreach ($buttons as $key => &$button) {
0 ignored issues
show
Bug introduced by
The expression $buttons of type array|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
25
            $button = $this->process($key, $button);
26
        }
27
28
        $builder->setButtons($buttons);
29
    }
30
31
    /**
32
     * Process the button.
33
     *
34
     * @param $key
35
     * @param $button
36
     * @return array
37
     */
38
    protected function process($key, $button)
39
    {
40
        /*
41
         * If the button is a string then use
42
         * it as the button parameter.
43
         */
44
        if (is_string($button)) {
45
            $button = [
46
                'button' => $button,
47
            ];
48
        }
49
50
        /*
51
         * If the key is a string and the button
52
         * is an array without a button param then
53
         * move the key into the button as that param.
54
         */
55
        if (!is_integer($key) && !isset($button['button'])) {
56
            $button['button'] = $key;
57
        }
58
59
        /*
60
         * If the key is a string and the button
61
         * is an array without a slug param then
62
         * move the key into the button as that param.
63
         */
64
        if (!is_integer($key) && !isset($button['slug'])) {
65
            $button['slug'] = $key;
66
        }
67
68
        /*
69
         * Move the HREF if any to the attributes.
70
         */
71
        if (isset($button['href'])) {
72
            array_set($button['attributes'], 'href', array_pull($button, 'href'));
73
        }
74
75
        /*
76
         * Move the target if any to the attributes.
77
         */
78
        if (isset($button['target'])) {
79
            array_set($button['attributes'], 'target', array_pull($button, 'target'));
80
        }
81
82
        /*
83
         * Move all data-* keys
84
         * to attributes.
85
         */
86
        foreach ($button as $attribute => $value) {
87
            if (str_is('data-*', $attribute)) {
88
                array_set($button, 'attributes.' . $attribute, array_pull($button, $attribute));
89
            }
90
        }
91
92
        /*
93
         * Make sure the HREF is absolute.
94
         */
95
        if (
96
            isset($button['attributes']['href']) &&
97
            is_string($button['attributes']['href']) &&
98
            !starts_with($button['attributes']['href'], ['http', '{'])
99
        ) {
100
            $button['attributes']['href'] = url($button['attributes']['href']);
101
        }
102
103
        /*
104
         * Use small buttons for tables.
105
         */
106
        $button['size'] = array_get($button, 'size', 'sm');
107
108
        if (isset($button['dropdown'])) {
109
            foreach ($button['dropdown'] as $key => &$dropdown) {
110
                $dropdown = $this->process($key, $dropdown);
111
            }
112
        }
113
114
        return $button;
115
    }
116
}
117