ActionNormalizer::process()   F
last analyzed

Complexity

Conditions 17
Paths 384

Size

Total Lines 91
Code Lines 29

Duplication

Lines 23
Ratio 25.27 %

Importance

Changes 0
Metric Value
cc 17
eloc 29
nc 384
nop 3
dl 23
loc 91
rs 3.6909
c 0
b 0
f 0

How to fix   Long Method    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 namespace Anomaly\Streams\Platform\Ui\Table\Component\Action;
2
3
use Anomaly\Streams\Platform\Ui\Table\TableBuilder;
4
5
/**
6
 * Class ActionNormalizer
7
 *
8
 * @link   http://pyrocms.com/
9
 * @author PyroCMS, Inc. <[email protected]>
10
 * @author Ryan Thompson <[email protected]>
11
 */
12
class ActionNormalizer
13
{
14
15
    /**
16
     * Normalize action input.
17
     *
18
     * @param TableBuilder $builder
19
     */
20
    public function normalize(TableBuilder $builder)
21
    {
22
        $actions = $builder->getActions();
23
        $prefix  = $builder->getTableOption('prefix');
24
25
        foreach ($actions as $slug => &$action) {
0 ignored issues
show
Bug introduced by
The expression $actions 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...
26
            $action = $this->process($prefix, $slug, $action);
27
        }
28
29
        $builder->setActions($actions);
30
    }
31
32
    /**
33
     * Process the action.
34
     *
35
     * @param $prefix
36
     * @param $slug
37
     * @param $action
38
     * @return array
39
     */
40
    protected function process($prefix, $slug, $action)
41
    {
42
        /*
43
         * If the slug is numeric and the action is
44
         * a string then treat the string as both the
45
         * action and the slug. This is OK as long as
46
         * there are not multiple instances of this
47
         * input using the same action which is not likely.
48
         */
49 View Code Duplication
        if (is_numeric($slug) && is_string($action)) {
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...
50
            $action = [
51
                'slug'   => $action,
52
                'action' => $action,
53
            ];
54
        }
55
56
        /*
57
         * If the slug is NOT numeric and the action is a
58
         * string then use the slug as the slug and the
59
         * action as the action.
60
         */
61 View Code Duplication
        if (!is_numeric($slug) && is_string($action)) {
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...
62
            $action = [
63
                'slug'   => $slug,
64
                'action' => $action,
65
            ];
66
        }
67
68
        /*
69
         * If the slug is not numeric and the action is an
70
         * array without a slug then use the slug for
71
         * the slug for the action.
72
         */
73 View Code Duplication
        if (is_array($action) && !isset($action['slug']) && !is_numeric($slug)) {
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...
74
            $action['slug'] = $slug;
75
        }
76
77
        /*
78
         * If the slug is not numeric and the action is an
79
         * array without a action then use the slug for
80
         * the action for the action.
81
         */
82 View Code Duplication
        if (is_array($action) && !isset($action['action']) && !is_numeric($slug)) {
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...
83
            $action['action'] = $slug;
84
        }
85
86
        /*
87
         * Make sure the attributes array is set.
88
         */
89
        $action['attributes'] = array_get($action, 'attributes', []);
90
91
        /*
92
         * Move all data-* keys
93
         * to attributes.
94
         */
95
        foreach ($action as $attribute => $value) {
96
            if (str_is('data-*', $attribute)) {
97
                array_set($action, 'attributes.' . $attribute, array_pull($action, $attribute));
98
            }
99
        }
100
101
        /*
102
         * If the HREF is present outside of the attributes
103
         * then pull it and put it in the attributes array.
104
         */
105
        if (isset($action['url'])) {
106
            $action['attributes']['url'] = array_pull($action, 'url');
107
        }
108
109
        /*
110
         * Set defaults as expected for actions.
111
         */
112
        $action['size']     = array_get($action, 'small', 'sm');
113
        $action['disabled'] = array_get($action, 'disabled', array_get($action, 'toggle', true));
114
115
        $action['attributes']['name']  = $prefix . 'action';
116
        $action['attributes']['value'] = $action['slug'];
117
118
        // If not toggle add the ignore attribute.
119
        if (array_get($action, 'toggle', true) === false) {
120
            $action['attributes']['data-ignore'] = '';
121
        }
122
123 View Code Duplication
        if (isset($action['dropdown'])) {
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...
124
            foreach ($action['dropdown'] as $key => &$dropdown) {
125
                $dropdown = $this->process($prefix, $key, $dropdown);
126
            }
127
        }
128
129
        return $action;
130
    }
131
}
132