Completed
Pull Request — master (#8491)
by Stefan
18:30 queued 07:04
created

Configuration::addTemplatesSection()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 21
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
declare(strict_types=1);
13
14
namespace Sylius\Bundle\GridBundle\DependencyInjection;
15
16
use Sylius\Bundle\GridBundle\Doctrine\ORM\Driver as DoctrineORMDriver;
17
use Sylius\Bundle\GridBundle\SyliusGridBundle;
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
final class Configuration implements ConfigurationInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getConfigTreeBuilder(): TreeBuilder
28
    {
29
        $treeBuilder = new TreeBuilder();
30
        $rootNode = $treeBuilder->root('sylius_grid');
31
32
        $this->addDriversSection($rootNode);
33
        $this->addTemplatesSection($rootNode);
34
        $this->addGridsSection($rootNode);
35
36
        return $treeBuilder;
37
    }
38
39
    /**
40
     * @param ArrayNodeDefinition $node
41
     */
42
    private function addDriversSection(ArrayNodeDefinition $node): void
43
    {
44
        $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...
45
            ->children()
46
                ->arrayNode('drivers')
47
                    ->defaultValue([SyliusGridBundle::DRIVER_DOCTRINE_ORM])
48
                    ->prototype('enum')->values(SyliusGridBundle::getAvailableDrivers())->end()
49
                ->end()
50
            ->end()
51
        ;
52
    }
53
54
    /**
55
     * @param ArrayNodeDefinition $node
56
     */
57
    private function addTemplatesSection(ArrayNodeDefinition $node): void
58
    {
59
        $node
0 ignored issues
show
Bug introduced by
The method arrayNode() does not seem to exist on object<Symfony\Component...odeDefinitionInterface>.

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...
60
            ->children()
61
                ->arrayNode('templates')
62
                    ->addDefaultsIfNotSet()
63
                    ->children()
64
                        ->arrayNode('filter')
65
                            ->useAttributeAsKey('name')
66
                            ->prototype('scalar')->end()
67
                        ->end()
68
                        ->arrayNode('action')
69
                            ->useAttributeAsKey('name')
70
                            ->prototype('scalar')->end()
71
                        ->end()
72
                        ->arrayNode('bulk_action')
73
                            ->useAttributeAsKey('name')
74
                            ->prototype('scalar')->end()
75
                        ->end()
76
                    ->end()
77
                ->end()
78
            ->end()
79
        ;
80
    }
81
82
    /**
83
     * @param ArrayNodeDefinition $node
84
     */
85
    private function addGridsSection(ArrayNodeDefinition $node): void
86
    {
87
        $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...
88
            ->children()
89
                ->arrayNode('grids')
90
                    ->useAttributeAsKey('code')
91
                    ->prototype('array')
92
                        ->children()
93
                            ->scalarNode('extends')->cannotBeEmpty()->end()
94
                            ->arrayNode('driver')
95
                                ->addDefaultsIfNotSet()
96
                                ->children()
97
                                    ->scalarNode('name')->cannotBeEmpty()->defaultValue(DoctrineORMDriver::NAME)->end()
98
                                    ->arrayNode('options')
99
                                        ->performNoDeepMerging()
100
                                        ->prototype('variable')->end()
101
                                        ->defaultValue([])
102
                                    ->end()
103
                                ->end()
104
                            ->end()
105
                            ->arrayNode('sorting')
106
                                ->performNoDeepMerging()
107
                                ->useAttributeAsKey('name')
108
                                ->prototype('enum')->values(['asc', 'desc'])->cannotBeEmpty()->end()
109
                            ->end()
110
                            ->arrayNode('limits')
111
                                ->performNoDeepMerging()
112
                                ->prototype('integer')->end()
113
                                ->defaultValue([10, 25, 50])
114
                            ->end()
115
                            ->arrayNode('fields')
116
                                ->useAttributeAsKey('name')
117
                                ->prototype('array')
118
                                    ->children()
119
                                        ->scalarNode('type')->isRequired()->cannotBeEmpty()->end()
120
                                        ->scalarNode('label')->cannotBeEmpty()->end()
121
                                        ->scalarNode('path')->cannotBeEmpty()->end()
122
                                        ->scalarNode('sortable')->end()
123
                                        ->scalarNode('enabled')->defaultTrue()->end()
124
                                        ->scalarNode('position')->defaultValue(100)->end()
125
                                        ->arrayNode('options')
126
                                            ->performNoDeepMerging()
127
                                            ->prototype('variable')->end()
128
                                        ->end()
129
                                    ->end()
130
                                ->end()
131
                            ->end()
132
                            ->arrayNode('filters')
133
                                ->useAttributeAsKey('name')
134
                                ->prototype('array')
135
                                    ->children()
136
                                        ->scalarNode('type')->isRequired()->cannotBeEmpty()->end()
137
                                        ->scalarNode('label')->cannotBeEmpty()->end()
138
                                        ->scalarNode('enabled')->defaultTrue()->end()
139
                                        ->scalarNode('template')->end()
140
                                        ->scalarNode('position')->defaultValue(100)->end()
141
                                        ->arrayNode('options')
142
                                            ->performNoDeepMerging()
143
                                            ->prototype('variable')->end()
144
                                        ->end()
145
                                        ->arrayNode('form_options')
146
                                            ->performNoDeepMerging()
147
                                            ->prototype('variable')->end()
148
                                        ->end()
149
                                        ->variableNode('default_value')->end()
150
                                    ->end()
151
                                ->end()
152
                            ->end()
153
                            ->arrayNode('actions')
154
                                ->useAttributeAsKey('name')
155
                                ->prototype('array')
156
                                    ->useAttributeAsKey('name')
157
                                    ->prototype('array')
158
                                        ->children()
159
                                            ->scalarNode('type')->isRequired()->end()
160
                                            ->scalarNode('label')->end()
161
                                            ->scalarNode('enabled')->defaultTrue()->end()
162
                                            ->scalarNode('icon')->end()
163
                                            ->scalarNode('position')->defaultValue(100)->end()
164
                                            ->arrayNode('options')
165
                                                ->performNoDeepMerging()
166
                                                ->prototype('variable')->end()
167
                                            ->end()
168
                                        ->end()
169
                                    ->end()
170
                                ->end()
171
                            ->end()
172
                        ->end()
173
                    ->end()
174
                ->end()
175
            ->end()
176
        ;
177
    }
178
}
179