Completed
Push — bring-back-validation-groups ( e1fc79...5a8a4f )
by Kamil
20:58
created

Configuration::addResourcesSection()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 46
rs 8.9411
cc 1
eloc 43
nc 1
nop 1
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\ResourceBundle\DependencyInjection;
13
14
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
15
use Sylius\Bundle\ResourceBundle\Form\Type\DefaultResourceType;
16
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
17
use Sylius\Component\Resource\Factory\Factory;
18
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
19
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
20
use Symfony\Component\Config\Definition\ConfigurationInterface;
21
22
/**
23
 * @author Paweł Jędrzejewski <[email protected]>
24
 */
25
final class Configuration implements ConfigurationInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getConfigTreeBuilder()
31
    {
32
        $treeBuilder = new TreeBuilder();
33
        $rootNode = $treeBuilder->root('sylius_resource');
34
35
        $this->addResourcesSection($rootNode);
36
        $this->addSettingsSection($rootNode);
37
        $this->addTranslationsSection($rootNode);
38
        $this->addDriversSection($rootNode);
39
40
        $rootNode
41
            ->children()
42
                ->scalarNode('authorization_checker')
43
                    ->defaultValue('sylius.resource_controller.authorization_checker.disabled')
44
                    ->cannotBeEmpty()
45
                ->end()
46
            ->end()
47
        ;
48
49
        return $treeBuilder;
50
    }
51
52
    /**
53
     * @param ArrayNodeDefinition $node
54
     */
55
    private function addResourcesSection(ArrayNodeDefinition $node)
56
    {
57
        $node
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 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...
58
            ->children()
59
                ->arrayNode('resources')
60
                    ->useAttributeAsKey('name')
61
                    ->prototype('array')
62
                        ->children()
63
                            ->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end()
64
                            ->variableNode('options')->end()
65
                            ->scalarNode('templates')->cannotBeEmpty()->end()
66
                            ->arrayNode('classes')
67
                                ->isRequired()
68
                                ->addDefaultsIfNotSet()
69
                                ->children()
70
                                    ->scalarNode('model')->isRequired()->cannotBeEmpty()->end()
71
                                    ->scalarNode('interface')->cannotBeEmpty()->end()
72
                                    ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
73
                                    ->scalarNode('repository')->cannotBeEmpty()->end()
74
                                    ->scalarNode('factory')->defaultValue(Factory::class)->end()
75
                                    ->scalarNode('form')->defaultValue(DefaultResourceType::class)->cannotBeEmpty()->end()
76
                                ->end()
77
                            ->end()
78
                            ->arrayNode('translation')
79
                                ->children()
80
                                    ->variableNode('options')->end()
81
                                    ->arrayNode('classes')
82
                                        ->isRequired()
83
                                        ->addDefaultsIfNotSet()
84
                                        ->children()
85
                                            ->scalarNode('model')->isRequired()->cannotBeEmpty()->end()
86
                                            ->scalarNode('interface')->cannotBeEmpty()->end()
87
                                            ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
88
                                            ->scalarNode('repository')->cannotBeEmpty()->end()
89
                                            ->scalarNode('factory')->defaultValue(Factory::class)->end()
90
                                            ->scalarNode('form')->defaultValue(DefaultResourceType::class)->cannotBeEmpty()->end()
91
                                        ->end()
92
                                    ->end()
93
                                ->end()
94
                            ->end()
95
                        ->end()
96
                    ->end()
97
                ->end()
98
            ->end()
99
        ;
100
    }
101
102
    /**
103
     * @param ArrayNodeDefinition $node
104
     */
105
    private function addSettingsSection(ArrayNodeDefinition $node)
106
    {
107
        $node
0 ignored issues
show
Bug introduced by
The method defaultValue() does not seem to exist on object<Symfony\Component...on\Builder\NodeBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
            ->children()
109
                ->arrayNode('settings')
110
                    ->addDefaultsIfNotSet()
111
                    ->children()
112
                        ->variableNode('paginate')->defaultNull()->end()
113
                        ->variableNode('limit')->defaultNull()->end()
114
                        ->arrayNode('allowed_paginate')
115
                            ->prototype('integer')->end()
116
                            ->defaultValue([10, 20, 30])
117
                        ->end()
118
                        ->integerNode('default_page_size')->defaultValue(10)->end()
119
                        ->booleanNode('sortable')->defaultFalse()->end()
120
                        ->variableNode('sorting')->defaultNull()->end()
121
                        ->booleanNode('filterable')->defaultFalse()->end()
122
                        ->variableNode('criteria')->defaultNull()->end()
123
                    ->end()
124
                ->end()
125
            ->end()
126
        ;
127
    }
128
129
    /**
130
     * @param ArrayNodeDefinition $node
131
     */
132
    private function addTranslationsSection(ArrayNodeDefinition $node)
133
    {
134
        $node
135
            ->children()
136
                ->arrayNode('translation')
137
                    ->canBeDisabled()
138
                    ->children()
139
                        ->scalarNode('locale_provider')->defaultValue('sylius.translation_locale_provider.immutable')->cannotBeEmpty()->end()
140
                ->end()
141
            ->end()
142
        ;
143
    }
144
145
    /**
146
     * @param ArrayNodeDefinition $node
147
     */
148
    private function addDriversSection(ArrayNodeDefinition $node)
149
    {
150
        $node
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 values() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...lder\EnumNodeDefinition. 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...
151
            ->children()
152
                ->arrayNode('drivers')
153
                    ->defaultValue([SyliusResourceBundle::DRIVER_DOCTRINE_ORM])
154
                    ->prototype('enum')->values(SyliusResourceBundle::getAvailableDrivers())->end()
155
                ->end()
156
            ->end()
157
        ;
158
    }
159
}
160