Completed
Push — upgrade-boilerplate ( 903b21 )
by Kamil
17:46
created

Configuration::addDriversSection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
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\GridBundle\DependencyInjection;
13
14
use Sylius\Bundle\GridBundle\Doctrine\ORM\Driver as DoctrineORMDriver;
15
use Sylius\Bundle\GridBundle\SyliusGridBundle;
16
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
17
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
18
use Symfony\Component\Config\Definition\ConfigurationInterface;
19
20
/**
21
 * @author Paweł Jędrzejewski <[email protected]>
22
 */
23
final class Configuration implements ConfigurationInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function getConfigTreeBuilder()
29
    {
30
        $treeBuilder = new TreeBuilder();
31
        $rootNode = $treeBuilder->root('sylius_grid');
32
33
        $this->addDriversSection($rootNode);
34
        $this->addTemplatesSection($rootNode);
35
        $this->addGridsSection($rootNode);
36
37
        return $treeBuilder;
38
    }
39
40
    /**
41
     * @param ArrayNodeDefinition $node
42
     */
43
    private function addDriversSection(ArrayNodeDefinition $node)
44
    {
45
        $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 prototype() 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...
46
            ->children()
47
                ->arrayNode('drivers')
48
                    ->defaultValue([SyliusGridBundle::DRIVER_DOCTRINE_ORM])
49
                    ->prototype('enum')->values(SyliusGridBundle::getAvailableDrivers())->end()
50
                ->end()
51
            ->end()
52
        ;
53
    }
54
55
    /**
56
     * @param ArrayNodeDefinition $node
57
     */
58
    private function addTemplatesSection(ArrayNodeDefinition $node)
59
    {
60
        $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...
61
            ->children()
62
                ->arrayNode('templates')
63
                    ->addDefaultsIfNotSet()
64
                    ->children()
65
                        ->arrayNode('filter')
66
                            ->useAttributeAsKey('name')
67
                            ->prototype('scalar')->end()
68
                        ->end()
69
                        ->arrayNode('action')
70
                            ->useAttributeAsKey('name')
71
                            ->prototype('scalar')->end()
72
                        ->end()
73
                    ->end()
74
                ->end()
75
            ->end()
76
        ;
77
    }
78
79
    /**
80
     * @param ArrayNodeDefinition $node
81
     */
82
    private function addGridsSection(ArrayNodeDefinition $node)
83
    {
84
        $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...
85
            ->children()
86
                ->arrayNode('grids')
87
                    ->useAttributeAsKey('code')
88
                    ->prototype('array')
89
                        ->children()
90
                            ->scalarNode('extends')->cannotBeEmpty()->end()
91
                            ->arrayNode('driver')
92
                                ->addDefaultsIfNotSet()
93
                                ->children()
94
                                    ->scalarNode('name')->cannotBeEmpty()->defaultValue(DoctrineORMDriver::NAME)->end()
95
                                    ->arrayNode('options')
96
                                        ->prototype('variable')->end()
97
                                        ->defaultValue([])
98
                                    ->end()
99
                                ->end()
100
                            ->end()
101
                            ->arrayNode('sorting')
102
                                ->performNoDeepMerging()
103
                                ->useAttributeAsKey('name')
104
                                ->prototype('enum')->values(['asc', 'desc'])->cannotBeEmpty()->end()
105
                            ->end()
106
                            ->arrayNode('fields')
107
                                ->useAttributeAsKey('name')
108
                                ->prototype('array')
109
                                    ->children()
110
                                        ->scalarNode('type')->isRequired()->cannotBeEmpty()->end()
111
                                        ->scalarNode('label')->cannotBeEmpty()->end()
112
                                        ->scalarNode('path')->cannotBeEmpty()->end()
113
                                        ->scalarNode('sortable')->end()
114
                                        ->scalarNode('enabled')->defaultTrue()->end()
115
                                        ->scalarNode('position')->defaultValue(100)->end()
116
                                        ->arrayNode('options')
117
                                            ->prototype('variable')->end()
118
                                        ->end()
119
                                    ->end()
120
                                ->end()
121
                            ->end()
122
                            ->arrayNode('filters')
123
                                ->useAttributeAsKey('name')
124
                                ->prototype('array')
125
                                    ->children()
126
                                        ->scalarNode('type')->isRequired()->cannotBeEmpty()->end()
127
                                        ->scalarNode('label')->cannotBeEmpty()->end()
128
                                        ->scalarNode('enabled')->defaultTrue()->end()
129
                                        ->scalarNode('template')->end()
130
                                        ->scalarNode('position')->defaultValue(100)->end()
131
                                        ->arrayNode('options')
132
                                            ->prototype('variable')->end()
133
                                        ->end()
134
                                    ->end()
135
                                ->end()
136
                            ->end()
137
                            ->arrayNode('actions')
138
                                ->useAttributeAsKey('name')
139
                                ->prototype('array')
140
                                    ->useAttributeAsKey('name')
141
                                    ->prototype('array')
142
                                        ->children()
143
                                            ->scalarNode('type')->isRequired()->end()
144
                                            ->scalarNode('label')->end()
145
                                            ->scalarNode('icon')->end()
146
                                            ->scalarNode('position')->defaultValue(100)->end()
147
                                            ->arrayNode('options')
148
                                                ->prototype('variable')->end()
149
                                            ->end()
150
                                        ->end()
151
                                    ->end()
152
                                ->end()
153
                            ->end()
154
                        ->end()
155
                    ->end()
156
                ->end()
157
            ->end()
158
        ;
159
    }
160
}
161