Completed
Pull Request — master (#90)
by Arnaud
07:17 queued 28s
created

ActionCollectionConfiguration   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 81
Duplicated Lines 37.04 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 30
loc 81
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configureOptions() 0 21 2
A normalizeIcon() 15 15 3
A normalizeClass() 15 15 3
A normalizeRoute() 0 17 4
A normalizeText() 0 6 2

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\Field\Configuration;
4
5
use JK\Sam\Configuration\Configuration;
6
use LogicException;
7
use Symfony\Component\OptionsResolver\Options;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
class ActionCollectionConfiguration extends Configuration
11
{
12
    public function configureOptions(OptionsResolver $resolver)
13
    {
14
        $resolver
15
            ->setRequired('actions')
16
            ->setAllowedTypes('actions', 'array')
17
            ->setNormalizer('actions', function (Options $options, $actions) {
18
    
19
                $normalizedActions = [];
20
                
21
                foreach ($actions as $action => $options) {
22
                    $this->normalizeIcon($options, $action);
23
                    $this->normalizeClass($options, $action);
24
                    $this->normalizeText($options, $action);
25
                    
26
                    $normalizedActions[$action] = $options;
27
                }
28
    
29
                return $normalizedActions;
30
            })
31
        ;
32
    }
33
    
34 View Code Duplication
    protected function normalizeIcon(array &$options, $action)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
35
    {
36
        $iconMapping = [
37
            'delete' => 'fa fa-times',
38
            'edit' => 'fa fa-pencil',
39
        ];
40
    
41
        if (!array_key_exists('icon', $options)) {
42
            $options['icon'] = '';
43
        
44
            if (array_key_exists($action, $iconMapping)) {
45
                $options['icon'] = $iconMapping[$action];
46
            }
47
        }
48
    }
49
    
50 View Code Duplication
    protected function normalizeClass(array &$options, $action)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
51
    {
52
        $classMapping = [
53
            'delete' => 'btn btn-danger',
54
            'edit' => 'btn btn-default',
55
        ];
56
    
57
        if (!array_key_exists('class', $options)) {
58
            $options['class'] = '';
59
        
60
            if (array_key_exists($action, $classMapping)) {
61
                $options['class'] = $classMapping[$action];
62
            }
63
        }
64
    }
65
    
66
    protected function normalizeRoute(array &$options, $action)
67
    {
68
        if (!array_key_exists('route', $options)) {
69
            throw new LogicException(
70
                'You should provide a route for the Action "'.$action.'" in ActionCollection Field'
71
            );
72
        }
73
    
74
        if (array_key_exists('parameters', $options)) {
75
    
76
            if (!is_array($options['parameters'])) {
77
                throw new LogicException(
78
                    'You should provide an array of parameters for route configuration in action "'.$action.'"'
79
                );
80
            }
81
        }
82
    }
83
    
84
    protected function normalizeText(array &$options, $action)
85
    {
86
        if (!array_key_exists('text', $options)) {
87
            $options['text'] = ucfirst($action);
88
        }
89
    }
90
}
91