Completed
Pull Request — dev (#19)
by Arnaud
14:27 queued 06:29
created

MenuConfiguration   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 60 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 0
cbo 3
dl 30
loc 50
ccs 20
cts 22
cp 0.9091
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B configureOptions() 30 42 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
2
3
namespace LAG\AdminBundle\Menu\Configuration;
4
5
use LAG\AdminBundle\Configuration\Configuration;
6
use Symfony\Component\OptionsResolver\Options;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
9
class MenuConfiguration extends Configuration
10
{
11
    /**
12
     * Define allowed parameters and values for this configuration, using optionsResolver component.
13
     *
14
     * @param OptionsResolver $resolver
15
     */
16 2
    public function configureOptions(OptionsResolver $resolver)
17
    {
18
        // menu html attributes
19
        $resolver
20 2
            ->setDefault('attr', [])
21 View Code Duplication
            ->setNormalizer('attr', function(Options $options, $attr) {
22
23 2
                if (!is_array($attr)) {
24
                    $attr = [];
25
                }
26
27 2
                if (empty($attr['id'])) {
28 2
                    $attr['id'] = uniqid('admin-menu-');
29
                }
30
31 2
                return $attr;
32 2
            })
33
        ;
34
35
        // menu item
36
        $resolver
37 2
            ->setDefault('items', [])
38 2 View Code Duplication
            ->setNormalizer('items', function(Options $options, $items) {
39
40 2
                if (!is_array($items)) {
41
                    $items = [];
42
                }
43 2
                $resolver = new OptionsResolver();
44 2
                $resolvedItems = [];
45
46 2
                foreach ($items as $name => $item) {
47 1
                    $itemConfiguration = new MenuItemConfiguration();
48 1
                    $itemConfiguration->configureOptions($resolver);
49 1
                    $itemConfiguration->setParameters($resolver->resolve($item));
50
51 1
                    $resolvedItems[$name] = $itemConfiguration;
52
                }
53
54 2
                return $resolvedItems;
55 2
            })
56
        ;
57 2
    }
58
}
59