ActionNormalizer::process()   F
last analyzed

Complexity

Conditions 20
Paths 768

Size

Total Lines 98
Code Lines 32

Duplication

Lines 30
Ratio 30.61 %

Importance

Changes 0
Metric Value
cc 20
eloc 32
nc 768
nop 3
dl 30
loc 98
rs 2.3199
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\Form\Component\Action;
2
3
use Anomaly\Streams\Platform\Ui\Form\FormBuilder;
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 FormBuilder $builder
19
     */
20
    public function normalize(FormBuilder $builder)
21
    {
22
        $form    = $builder->getForm();
23
        $actions = $builder->getActions();
24
25
        $prefix = $form->getOption('prefix');
26
27
        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...
28
            $action = $this->process($prefix, $slug, $action);
29
        }
30
31
        $builder->setActions($actions);
32
    }
33
34
    /**
35
     * Process the action.
36
     *
37
     * @param $prefix
38
     * @param $slug
39
     * @param $action
40
     * @return array
41
     */
42
    protected function process($prefix, $slug, $action)
43
    {
44
        /*
45
         * If the slug is numeric and the action is
46
         * a string then treat the string as both the
47
         * action and the slug. This is OK as long as
48
         * there are not multiple instances of this
49
         * input using the same action which is not likely.
50
         */
51 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...
52
            $action = [
53
                'slug'   => $action,
54
                'action' => $action,
55
            ];
56
        }
57
58
        /*
59
         * If the slug is NOT numeric and the action is a
60
         * string then use the slug as the slug and the
61
         * action as the action.
62
         */
63 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...
64
            $action = [
65
                'slug'   => $slug,
66
                'action' => $action,
67
            ];
68
        }
69
70
        /*
71
         * If the slug is not numeric and the action is an
72
         * array without a slug then use the slug for
73
         * the slug for the action.
74
         */
75 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...
76
            $action['slug'] = $slug;
77
        }
78
79
        /*
80
         * If the slug is not numeric and the action is an
81
         * array without a action then use the slug for
82
         * the action for the action.
83
         */
84 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...
85
            $action['action'] = $slug;
86
        }
87
88
        /**
89
         * Default to size "sm"
90
         */
91
        if (!isset($action['size'])) {
92
            $action['size'] = 'sm';
93
        }
94
95
        /*
96
         * Make sure the attributes array is set.
97
         */
98
        $action['attributes'] = array_get($action, 'attributes', []);
99
100
        /*
101
         * Move all data-* keys
102
         * to attributes.
103
         */
104
        foreach ($action as $attribute => $value) {
105
            if (str_is('data-*', $attribute)) {
106
                array_set($action, 'attributes.' . $attribute, array_pull($action, $attribute));
107
            }
108
        }
109
110
        /*
111
         * If the HREF is present outside of the attributes
112
         * then pull it and put it in the attributes array.
113
         */
114
        if (isset($action['url'])) {
115
            $action['attributes']['url'] = array_pull($action, 'url');
116
        }
117
118
        /*
119
         * Make sure the HREF is absolute.
120
         */
121 View Code Duplication
        if (
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...
122
            isset($action['redirect']) &&
123
            is_string($action['redirect']) &&
124
            !starts_with($action['redirect'], ['http', '{url.'])
125
        ) {
126
            $action['redirect'] = url($action['redirect']);
127
        }
128
129
        $action['attributes']['name']  = $prefix . 'action';
130
        $action['attributes']['value'] = $action['slug'];
131
132 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...
133
            foreach ($action['dropdown'] as $key => &$dropdown) {
0 ignored issues
show
Bug introduced by
The expression $action['dropdown'] of type object<Illuminate\Contra...ng\UrlGenerator>|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...
134
                $dropdown = $this->process($prefix, $key, $dropdown);
135
            }
136
        }
137
138
        return $action;
139
    }
140
}
141