Completed
Push — master ( f77ca5...2d9d37 )
by Ryan
07:29
created

SectionNormalizer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 12.82 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 1
dl 5
loc 39
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B normalize() 5 30 5

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\Form\Component\Section;
2
3
use Anomaly\Streams\Platform\Ui\Form\FormBuilder;
4
5
/**
6
 * Class SectionNormalizer
7
 *
8
 * @link          http://pyrocms.com/
9
 * @author        PyroCMS, Inc. <[email protected]>
10
 * @author        Ryan Thompson <[email protected]>
11
 * @package       Anomaly\Streams\Platform\Ui\Form\Component\Section
12
 */
13
class SectionNormalizer
14
{
15
16
    /**
17
     * Normalize the sections.
18
     *
19
     * @param FormBuilder $builder
20
     */
21
    public function normalize(FormBuilder $builder)
22
    {
23
        $sections = $builder->getSections();
24
25
        foreach ($sections as $slug => &$section) {
26
27
            if (is_string($section)) {
28
                $section = [
29
                    'view' => $section
30
                ];
31
            }
32
33
            /**
34
             * Make sure some default parameters exist.
35
             */
36
            $section['attributes'] = array_get($section, 'attributes', []);
37
38
            /**
39
             * Move all data-* keys
40
             * to attributes.
41
             */
42 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...
43
                if (str_is('data-*', $attribute)) {
44
                    array_set($section, 'attributes.' . $attribute, array_pull($section, $attribute));
45
                }
46
            }
47
        }
48
49
        $builder->setSections($sections);
50
    }
51
}
52