Completed
Pull Request — dev (#6)
by Arnaud
07:28 queued 04:28
created

Configuration::getAdminsConfigurationNode()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 77
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 77
rs 8.9342
cc 1
eloc 72
nc 1
nop 0

How to fix   Long Method   

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
2
3
namespace LAG\AdminBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
7
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8
use Symfony\Component\Config\Definition\ConfigurationInterface;
9
10
class Configuration implements ConfigurationInterface
11
{
12
    public function getConfigTreeBuilder()
13
    {
14
        $treeBuilder = new TreeBuilder();
15
        $rootNode = $treeBuilder->root('lag_admin');
16
17
        $rootNode
18
            ->children()
19
                // application configuration
20
                ->append($this->getApplicationNode())
21
                // admins configuration
22
                ->append($this->getAdminsConfigurationNode())
23
                // menus configurations
24
                ->arrayNode('menus')
25
                    ->prototype('array')
26
                        ->children()
27
                            ->scalarNode('template')->defaultValue('LAGAdminBundle:Menu:main_menu.html.twig')->end()
28
                            ->arrayNode('main_item')
29
                                ->children()
30
                                    ->scalarNode('route')->end()
31
                                    ->scalarNode('title')->end()
32
                                ->end()
33
                            ->end()
34
                            ->arrayNode('items')
35
                                ->prototype('array')
36
                                    ->children()
37
                                        ->scalarNode('admin')->end()
38
                                        ->scalarNode('action')->end()
39
                                        ->scalarNode('title')->end()
40
                                        ->arrayNode('permissions')
41
                                            ->defaultValue(['ROLE_USER'])
42
                                            ->prototype('scalar')
43
                                            ->end()
44
                                        ->end()
45
                                    ->end()
46
                                ->end()
47
                            ->end()
48
                        ->end()
49
                    ->end()
50
                ->end()
51
            ->end()
52
        ->end();
53
54
        return $treeBuilder;
55
    }
56
57
    /**
58
     * @return ArrayNodeDefinition|NodeDefinition
59
     */
60
    public function getAdminsConfigurationNode()
61
    {
62
        $builder = new TreeBuilder();
63
        $node = $builder->root('admins');
64
65
        $node
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method children() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
66
            ->prototype('array')
67
                ->children()
68
                    ->scalarNode('entity')->end()
69
                    ->scalarNode('data_provider')->end()
70
                    ->scalarNode('form')->end()
71
                    ->scalarNode('max_per_page')->end()
72
                    ->scalarNode('controller')->defaultValue('LAGAdminBundle:CRUD')->end()
73
                    // actions configurations
74
                    ->arrayNode('actions')
75
                        ->useAttributeAsKey('name')
76
                        ->prototype('array')
77
                            ->children()
78
                                ->scalarNode('title')->end()
79
                                ->arrayNode('permissions')
80
                                    ->defaultValue(['ROLE_USER'])
81
                                    ->prototype('scalar')->end()
82
                                ->end()
83
                                ->arrayNode('export')
84
                                    ->prototype('scalar')->end()
85
                                ->end()
86
                                ->arrayNode('order')
87
                                    ->prototype('array')
88
                                        ->children()
89
                                            ->scalarNode('field')->end()
90
                                            ->scalarNode('order')->end()
91
                                        ->end()
92
                                    ->end()
93
                                ->end()
94
                                ->arrayNode('fields')
95
                                    ->prototype('array')
96
                                        ->children()
97
                                            ->scalarNode('type')->end()
98
                                            ->arrayNode('options')
99
                                                ->prototype('variable')->end()
100
                                            ->end()
101
                                        ->end()
102
                                    ->end()
103
                                ->end()
104
                                ->arrayNode('filters')
105
                                    ->prototype('scalar')->end()
106
                                ->end()
107
                                ->arrayNode('batch')
108
                                    ->prototype('variable')->end()
109
                                ->end()
110
                                ->arrayNode('actions')
111
                                    ->prototype('array')
112
                                        ->children()
113
                                            ->scalarNode('title')->end()
114
                                            ->scalarNode('route')->end()
115
                                            ->scalarNode('target')->end()
116
                                            ->scalarNode('icon')->end()
117
                                            ->scalarNode('load_strategy')->end()
118
                                            ->arrayNode('criteria')
119
                                                ->prototype('array')
120
                                                ->end()
121
                                            ->end()
122
                                            ->arrayNode('parameters')
123
                                                ->prototype('array')
124
                                                ->end()
125
                                            ->end()
126
                                        ->end()
127
                                    ->end()
128
                                ->end()
129
                            ->end()
130
                        ->end()
131
                    ->end()
132
                ->end()
133
            ->end();
134
135
        return $node;
136
    }
137
138
    /**
139
     * @return ArrayNodeDefinition|NodeDefinition
140
     */
141
    public function getApplicationNode()
142
    {
143
        $builder = new TreeBuilder();
144
        $node = $builder->root('application');
145
146
        $node
147
            ->children()
148
                ->scalarNode('title')->end()
149
                ->scalarNode('description')->end()
150
                ->scalarNode('layout')->end()
151
                ->scalarNode('date_format')->defaultValue('Y-m-d')->end()
152
                ->scalarNode('bootstrap')->end()
153
                ->scalarNode('max_per_page')->end()
154
                ->arrayNode('routing')
155
                    ->children()
156
                        ->scalarNode('name_pattern')->end()
157
                        ->scalarNode('url_pattern')->end()
158
                    ->end()
159
                ->end()
160
                ->arrayNode('translation')
161
                    ->canBeDisabled()
162
                    ->children()
163
                        ->scalarNode('pattern')->end()
164
                    ->end()
165
                ->end()
166
                ->scalarNode('block_template')
167
                ->defaultValue('LAGAdminBundle:Form:fields.html.twig')
168
                ->end()
169
                ->arrayNode('fields_mapping')
170
                ->prototype('scalar')
171
                ->end()
172
                ->end()
173
            ->end()
174
        ->end();
175
176
        return $node;
177
    }
178
}
179