Completed
Push — master ( fe8029...f95222 )
by Michał
432:28 queued 418:44
created

Configuration::addResourcesSection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 65
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 65
rs 9.3571
c 2
b 0
f 0
cc 1
eloc 62
nc 1
nop 1

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
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\UserBundle\DependencyInjection;
13
14
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
15
use Sylius\Bundle\ResourceBundle\Form\Type\ResourceChoiceType;
16
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
17
use Sylius\Bundle\UserBundle\Controller\UserController;
18
use Sylius\Bundle\UserBundle\Form\Type\UserRegistrationType;
19
use Sylius\Bundle\UserBundle\Form\Type\UserType;
20
use Sylius\Component\Resource\Factory\Factory;
21
use Sylius\Component\User\Model\User;
22
use Sylius\Component\User\Model\UserInterface;
23
use Sylius\Component\User\Model\UserOAuth;
24
use Sylius\Component\User\Model\UserOAuthInterface;
25
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
26
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
27
use Symfony\Component\Config\Definition\ConfigurationInterface;
28
29
/**
30
 * This class contains the configuration information for the bundle.
31
 *
32
 * This information is solely responsible for how the different configuration
33
 * sections are normalized, and merged.
34
 *
35
 * @author Bartosz Siejka <[email protected]>
36
 * @author Łukasz Chruściel <[email protected]>
37
 */
38
class Configuration implements ConfigurationInterface
39
{
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getConfigTreeBuilder()
44
    {
45
        $treeBuilder = new TreeBuilder();
46
        $rootNode = $treeBuilder->root('sylius_user');
47
48
        $rootNode
0 ignored issues
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 min() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\FloatNodeDefinition, Symfony\Component\Config...r\IntegerNodeDefinition, Symfony\Component\Config...r\NumericNodeDefinition. 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...
49
            ->addDefaultsIfNotSet()
50
            ->children()
51
                ->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end()
52
                ->arrayNode('resetting')
53
                    ->addDefaultsIfNotSet()
54
                    ->children()
55
                        ->arrayNode('token')
56
                            ->addDefaultsIfNotSet()
57
                            ->children()
58
                                ->scalarNode('ttl')->defaultValue('P1D')->end()
59
                                ->integerNode('length')
60
                                    ->defaultValue(16)
61
                                    ->min(1)->max(40)
62
                                ->end()
63
                            ->end()
64
                        ->end()
65
                        ->arrayNode('pin')
66
                            ->addDefaultsIfNotSet()
67
                            ->children()
68
                                ->integerNode('length')
69
                                    ->defaultValue(4)
70
                                    ->min(1)->max(9)
71
                                ->end()
72
                            ->end()
73
                        ->end()
74
                    ->end()
75
                ->end()
76
                ->arrayNode('verification')
77
                    ->addDefaultsIfNotSet()
78
                    ->children()
79
                        ->arrayNode('token')
80
                            ->addDefaultsIfNotSet()
81
                            ->children()
82
                                ->integerNode('length')
83
                                    ->defaultValue(16)
84
                                    ->min(1)->max(40)
85
                                ->end()
86
                            ->end()
87
                        ->end()
88
                    ->end()
89
                ->end()
90
            ->end()
91
        ;
92
93
        $this->addResourcesSection($rootNode);
94
95
        return $treeBuilder;
96
    }
97
98
    /**
99
     * @param ArrayNodeDefinition $node
100
     */
101
    private function addResourcesSection(ArrayNodeDefinition $node)
102
    {
103
        $node
104
            ->children()
105
                ->arrayNode('resources')
106
                    ->addDefaultsIfNotSet()
107
                    ->children()
108
                        ->arrayNode('user')
109
                            ->addDefaultsIfNotSet()
110
                            ->children()
111
                                ->scalarNode('templates')->defaultValue('SyliusUserBundle:User')->end()
112
                                ->variableNode('options')->end()
113
                                ->arrayNode('classes')
114
                                    ->addDefaultsIfNotSet()
115
                                    ->children()
116
                                        ->scalarNode('model')->defaultValue(User::class)->cannotBeEmpty()->end()
117
                                        ->scalarNode('interface')->defaultValue(UserInterface::class)->cannotBeEmpty()->end()
118
                                        ->scalarNode('controller')->defaultValue(UserController::class)->cannotBeEmpty()->end()
119
                                        ->scalarNode('repository')->cannotBeEmpty()->end()
120
                                        ->scalarNode('factory')->defaultValue(Factory::class)->end()
121
                                        ->arrayNode('form')
122
                                            ->addDefaultsIfNotSet()
123
                                            ->children()
124
                                                ->scalarNode('default')->defaultValue(UserType::class)->cannotBeEmpty()->end()
125
                                                ->scalarNode('registration')->defaultValue(UserRegistrationType::class)->cannotBeEmpty()->end()
126
                                            ->end()
127
                                        ->end()
128
                                    ->end()
129
                                ->end()
130
                                ->arrayNode('validation_groups')
131
                                    ->addDefaultsIfNotSet()
132
                                    ->children()
133
                                        ->arrayNode('default')
134
                                            ->prototype('scalar')->end()
135
                                            ->defaultValue(['sylius'])
136
                                        ->end()
137
                                        ->arrayNode('registration')
138
                                            ->prototype('scalar')->end()
139
                                            ->defaultValue(['sylius', 'sylius_user_registration'])
140
                                        ->end()
141
                                    ->end()
142
                                ->end()
143
                            ->end()
144
                        ->end()
145
                        ->arrayNode('user_oauth')
146
                            ->addDefaultsIfNotSet()
147
                            ->children()
148
                                ->variableNode('options')->end()
149
                                ->arrayNode('classes')
150
                                    ->addDefaultsIfNotSet()
151
                                    ->children()
152
                                        ->scalarNode('model')->defaultValue(UserOAuth::class)->cannotBeEmpty()->end()
153
                                        ->scalarNode('interface')->defaultValue(UserOAuthInterface::class)->cannotBeEmpty()->end()
154
                                        ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
155
                                        ->scalarNode('repository')->cannotBeEmpty()->end()
156
                                        ->scalarNode('factory')->defaultValue(Factory::class)->end()
157
                                    ->end()
158
                                ->end()
159
                            ->end()
160
                        ->end()
161
                    ->end()
162
                ->end()
163
            ->end()
164
        ;
165
    }
166
}
167