Completed
Push — master ( b4949b...b69366 )
by Kamil
10:51 queued 03:28
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\AttributeBundle\DependencyInjection;
15
16
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
17
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
18
use Sylius\Component\Attribute\Model\Attribute;
19
use Sylius\Component\Attribute\Model\AttributeInterface;
20
use Sylius\Component\Attribute\Model\AttributeTranslation;
21
use Sylius\Component\Attribute\Model\AttributeTranslationInterface;
22
use Sylius\Component\Resource\Factory\Factory;
23
use Sylius\Component\Resource\Factory\TranslatableFactory;
24
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
25
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
26
use Symfony\Component\Config\Definition\ConfigurationInterface;
27
28
final class Configuration implements ConfigurationInterface
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getConfigTreeBuilder(): TreeBuilder
34
    {
35
        $treeBuilder = new TreeBuilder();
36
        $rootNode = $treeBuilder->root('sylius_attribute');
37
38
        $rootNode
39
            ->addDefaultsIfNotSet()
40
            ->children()
41
                ->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end()
42
            ->end()
43
        ;
44
45
        $this->addResourcesSection($rootNode);
46
47
        return $treeBuilder;
48
    }
49
50
    /**
51
     * @param ArrayNodeDefinition $node
52
     */
53
    private function addResourcesSection(ArrayNodeDefinition $node): void
54
    {
55
        $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 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...
56
            ->children()
57
                ->arrayNode('resources')
58
                    ->useAttributeAsKey('name')
59
                    ->prototype('array')
60
                        ->children()
61
                            ->scalarNode('subject')->isRequired()->end()
62
                            ->arrayNode('attribute')
63
                                ->addDefaultsIfNotSet()
64
                                ->children()
65
                                    ->variableNode('options')->end()
66
                                    ->arrayNode('classes')
67
                                        ->addDefaultsIfNotSet()
68
                                        ->children()
69
                                            ->scalarNode('model')->defaultValue(Attribute::class)->cannotBeEmpty()->end()
70
                                            ->scalarNode('interface')->defaultValue(AttributeInterface::class)->cannotBeEmpty()->end()
71
                                            ->scalarNode('controller')->cannotBeEmpty()->end()
72
                                            ->scalarNode('repository')->cannotBeEmpty()->end()
73
                                            ->scalarNode('factory')->defaultValue(TranslatableFactory::class)->end()
74
                                            ->scalarNode('form')->cannotBeEmpty()->end()
75
                                        ->end()
76
                                    ->end()
77
                                    ->arrayNode('translation')
78
                                        ->addDefaultsIfNotSet()
79
                                        ->children()
80
                                            ->variableNode('options')->end()
81
                                            ->arrayNode('classes')
82
                                                ->addDefaultsIfNotSet()
83
                                                ->children()
84
                                                    ->scalarNode('model')->defaultValue(AttributeTranslation::class)->cannotBeEmpty()->end()
85
                                                    ->scalarNode('interface')->defaultValue(AttributeTranslationInterface::class)->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')->cannotBeEmpty()->end()
90
                                                ->end()
91
                                            ->end()
92
                                        ->end()
93
                                    ->end()
94
                                ->end()
95
                            ->end()
96
                            ->arrayNode('attribute_value')
97
                                ->isRequired()
98
                                ->addDefaultsIfNotSet()
99
                                ->children()
100
                                    ->variableNode('options')->end()
101
                                    ->arrayNode('classes')
102
                                        ->addDefaultsIfNotSet()
103
                                        ->children()
104
                                            ->scalarNode('model')->isRequired()->cannotBeEmpty()->end()
105
                                            ->scalarNode('interface')->isRequired()->cannotBeEmpty()->end()
106
                                            ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
107
                                            ->scalarNode('repository')->cannotBeEmpty()->end()
108
                                            ->scalarNode('factory')->defaultValue(Factory::class)->end()
109
                                            ->scalarNode('form')->cannotBeEmpty()->end()
110
                                        ->end()
111
                                    ->end()
112
                                ->end()
113
                            ->end()
114
                        ->end()
115
                    ->end()
116
                ->end()
117
            ->end()
118
        ;
119
    }
120
}
121