Completed
Push — master ( 799620...8a5869 )
by Kamil
116:02 queued 102:30
created

DependencyInjection/Configuration.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\ResourceBundle\DependencyInjection;
15
16
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
17
use Sylius\Bundle\ResourceBundle\Form\Type\DefaultResourceType;
18
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
19
use Sylius\Component\Resource\Factory\Factory;
20
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
21
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
22
use Symfony\Component\Config\Definition\ConfigurationInterface;
23
24
final class Configuration implements ConfigurationInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function getConfigTreeBuilder(): TreeBuilder
30
    {
31
        $treeBuilder = new TreeBuilder();
32
        $rootNode = $treeBuilder->root('sylius_resource');
33
34
        $this->addResourcesSection($rootNode);
35
        $this->addSettingsSection($rootNode);
36
        $this->addTranslationsSection($rootNode);
37
        $this->addDriversSection($rootNode);
38
39
        $rootNode
40
            ->children()
41
                ->scalarNode('authorization_checker')
42
                    ->defaultValue('sylius.resource_controller.authorization_checker.disabled')
43
                    ->cannotBeEmpty()
44
                ->end()
45
            ->end()
46
        ;
47
48
        return $treeBuilder;
49
    }
50
51
    /**
52
     * @param ArrayNodeDefinition $node
53
     */
54
    private function addResourcesSection(ArrayNodeDefinition $node): void
55
    {
56
        $node
57
            ->children()
58
                ->arrayNode('resources')
59
                    ->useAttributeAsKey('name')
60
                    ->prototype('array')
61
                        ->children()
62
                            ->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end()
63
                            ->variableNode('options')->end()
64
                            ->scalarNode('templates')->cannotBeEmpty()->end()
65
                            ->arrayNode('classes')
66
                                ->isRequired()
67
                                ->addDefaultsIfNotSet()
68
                                ->children()
69
                                    ->scalarNode('model')->isRequired()->cannotBeEmpty()->end()
70
                                    ->scalarNode('interface')->cannotBeEmpty()->end()
71
                                    ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
72
                                    ->scalarNode('repository')->cannotBeEmpty()->end()
73
                                    ->scalarNode('factory')->defaultValue(Factory::class)->end()
74
                                    ->scalarNode('form')->defaultValue(DefaultResourceType::class)->cannotBeEmpty()->end()
75
                                ->end()
76
                            ->end()
77
                            ->arrayNode('translation')
78
                                ->children()
79
                                    ->variableNode('options')->end()
80
                                    ->arrayNode('classes')
81
                                        ->isRequired()
82
                                        ->addDefaultsIfNotSet()
83
                                        ->children()
84
                                            ->scalarNode('model')->isRequired()->cannotBeEmpty()->end()
85
                                            ->scalarNode('interface')->cannotBeEmpty()->end()
86
                                            ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
87
                                            ->scalarNode('repository')->cannotBeEmpty()->end()
88
                                            ->scalarNode('factory')->defaultValue(Factory::class)->end()
89
                                            ->scalarNode('form')->defaultValue(DefaultResourceType::class)->cannotBeEmpty()->end()
90
                                        ->end()
91
                                    ->end()
92
                                ->end()
93
                            ->end()
94
                        ->end()
95
                    ->end()
96
                ->end()
97
            ->end()
98
        ;
99
    }
100
101
    /**
102
     * @param ArrayNodeDefinition $node
103
     */
104
    private function addSettingsSection(ArrayNodeDefinition $node): void
105
    {
106
        $node
107
            ->children()
108
                ->arrayNode('settings')
109
                    ->addDefaultsIfNotSet()
110
                    ->children()
111
                        ->variableNode('paginate')->defaultNull()->end()
112
                        ->variableNode('limit')->defaultNull()->end()
113
                        ->arrayNode('allowed_paginate')
114
                            ->prototype('integer')->end()
115
                            ->defaultValue([10, 20, 30])
116
                        ->end()
117
                        ->integerNode('default_page_size')->defaultValue(10)->end()
118
                        ->booleanNode('sortable')->defaultFalse()->end()
119
                        ->variableNode('sorting')->defaultNull()->end()
120
                        ->booleanNode('filterable')->defaultFalse()->end()
121
                        ->variableNode('criteria')->defaultNull()->end()
122
                    ->end()
123
                ->end()
124
            ->end()
125
        ;
126
    }
127
128
    /**
129
     * @param ArrayNodeDefinition $node
130
     */
131
    private function addTranslationsSection(ArrayNodeDefinition $node): void
132
    {
133
        $node
134
            ->children()
135
                ->arrayNode('translation')
136
                    ->canBeDisabled()
137
                    ->children()
138
                        ->scalarNode('locale_provider')->defaultValue('sylius.translation_locale_provider.immutable')->cannotBeEmpty()->end()
139
                ->end()
140
            ->end()
141
        ;
142
    }
143
144
    /**
145
     * @param ArrayNodeDefinition $node
146
     */
147
    private function addDriversSection(ArrayNodeDefinition $node): void
148
    {
149
        $node
0 ignored issues
show
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...
150
            ->children()
151
                ->arrayNode('drivers')
152
                    ->defaultValue([SyliusResourceBundle::DRIVER_DOCTRINE_ORM])
153
                    ->prototype('enum')->values(SyliusResourceBundle::getAvailableDrivers())->end()
154
                ->end()
155
            ->end()
156
        ;
157
    }
158
}
159