Completed
Push — master ( cbe7c9...6a9cd3 )
by Ryan
08:04
created

SectionNormalizer::normalize()   F

Complexity

Conditions 17
Paths 289

Size

Total Lines 81
Code Lines 28

Duplication

Lines 19
Ratio 23.46 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 19
loc 81
rs 3.7915
cc 17
eloc 28
nc 289
nop 1

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\ControlPanel\Component\Section;
2
3
use Anomaly\Streams\Platform\Addon\Module\ModuleCollection;
4
use Anomaly\Streams\Platform\Ui\ControlPanel\ControlPanelBuilder;
5
6
/**
7
 * Class SectionNormalizer
8
 *
9
 * @link          http://anomaly.is/streams-platform
10
 * @author        AnomalyLabs, Inc. <[email protected]>
11
 * @author        Ryan Thompson <[email protected]>
12
 * @package       Anomaly\Streams\Platform\Ui\ControlPanel\Component\Section
13
 */
14
class SectionNormalizer
15
{
16
17
    /**
18
     * The module collection.
19
     *
20
     * @var ModuleCollection
21
     */
22
    protected $modules;
23
24
    /**
25
     * Create a new SectionNormalizer instance.
26
     *
27
     * @param ModuleCollection $modules
28
     */
29
    public function __construct(ModuleCollection $modules)
30
    {
31
        $this->modules = $modules;
32
    }
33
34
    /**
35
     * Normalize the section input.
36
     *
37
     * @param ControlPanelBuilder $builder
38
     */
39
    public function normalize(ControlPanelBuilder $builder)
40
    {
41
        $sections = $builder->getSections();
42
43
        /**
44
         * Loop over each section and make sense of the input
45
         * provided for the given module.
46
         */
47
        foreach ($sections as $slug => &$section) {
48
49
            /**
50
             * If the slug is not valid and the section
51
             * is a string then use the section as the slug.
52
             */
53
            if (is_numeric($slug) && is_string($section)) {
54
                $section = [
55
                    'slug' => $section
56
                ];
57
            }
58
59
            /**
60
             * If the slug is a string and the title is not
61
             * set then use the slug as the slug.
62
             */
63
            if (is_string($slug) && !isset($section['slug'])) {
64
                $section['slug'] = $slug;
65
            }
66
67
            /**
68
             * Make sure we have attributes.
69
             */
70
            $section['attributes'] = array_get($section, 'attributes', []);
71
72
            /**
73
             * Move the HREF into attributes.
74
             */
75
            if (isset($section['href'])) {
76
                $section['attributes']['href'] = array_pull($section, 'href');
77
            }
78
79
            /**
80
             * Move all data-* keys
81
             * to attributes.
82
             */
83 View Code Duplication
            foreach ($section as $attribute => $value) {
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...
84
                if (str_is('data-*', $attribute)) {
85
                    array_set($section, 'attributes.' . $attribute, array_pull($section, $attribute));
86
                }
87
            }
88
89
            /**
90
             * Make sure the HREF and data-HREF are absolute.
91
             */
92 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...
93
                isset($section['attributes']['href']) &&
94
                is_string($section['attributes']['href']) &&
95
                !starts_with($section['attributes']['href'], 'http')
96
            ) {
97
                $section['attributes']['href'] = url($section['attributes']['href']);
98
            }
99
100 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...
101
                isset($section['attributes']['data-href']) &&
102
                is_string($section['attributes']['data-href']) &&
103
                !starts_with($section['attributes']['data-href'], 'http')
104
            ) {
105
                $section['attributes']['data-href'] = url($section['attributes']['data-href']);
106
            }
107
108
            /**
109
             * Move child sections into main array.
110
             */
111
            if (isset($section['sections'])) {
112
                foreach ($section['sections'] as $key => $child) {
113
                    $sections[$key] = $child;
114
                }
115
            }
116
        }
117
118
        $builder->setSections(array_values($sections));
119
    }
120
}
121