Completed
Push — master ( b4949b...b69366 )
by Kamil
10:51 queued 03:28
created

ReviewBundle/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\ReviewBundle\DependencyInjection;
15
16
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
17
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
18
use Sylius\Bundle\ReviewBundle\Form\Type\ReviewType;
19
use Sylius\Component\Resource\Factory\Factory;
20
use Sylius\Component\Review\Model\ReviewerInterface;
21
use Sylius\Component\Review\Model\ReviewInterface;
22
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
23
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
24
use Symfony\Component\Config\Definition\ConfigurationInterface;
25
26
final class Configuration implements ConfigurationInterface
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getConfigTreeBuilder(): TreeBuilder
32
    {
33
        $treeBuilder = new TreeBuilder();
34
        $rootNode = $treeBuilder->root('sylius_review');
35
36
        $rootNode
37
            ->addDefaultsIfNotSet()
38
            ->children()
39
                ->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end()
40
            ->end()
41
        ;
42
43
        $this->addResourcesSection($rootNode);
44
45
        return $treeBuilder;
46
    }
47
48
    /**
49
     * @param ArrayNodeDefinition $node
50
     */
51
    private function addResourcesSection(ArrayNodeDefinition $node): void
52
    {
53
        $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...
54
            ->children()
55
            ->arrayNode('resources')
56
                ->useAttributeAsKey('name')
57
                ->prototype('array')
58
                    ->children()
59
                        ->scalarNode('subject')->isRequired()->end()
60
                        ->arrayNode('review')
61
                            ->isRequired()
62
                            ->addDefaultsIfNotSet()
63
                            ->children()
64
                                ->variableNode('options')->end()
65
                                ->arrayNode('classes')
66
                                    ->addDefaultsIfNotSet()
67
                                    ->children()
68
                                        ->scalarNode('model')->isRequired()->end()
69
                                        ->scalarNode('interface')->defaultValue(ReviewInterface::class)->cannotBeEmpty()->end()
70
                                        ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
71
                                        ->scalarNode('repository')->cannotBeEmpty()->end()
72
                                        ->scalarNode('factory')->defaultValue(Factory::class)->cannotBeEmpty()->end()
73
                                        ->scalarNode('form')->defaultValue(ReviewType::class)->cannotBeEmpty()->end()
74
                                    ->end()
75
                                ->end()
76
                            ->end()
77
                        ->end()
78
                        ->arrayNode('reviewer')
79
                            ->addDefaultsIfNotSet()
80
                            ->children()
81
                                ->variableNode('options')->end()
82
                                ->arrayNode('classes')
83
                                    ->addDefaultsIfNotSet()
84
                                    ->children()
85
                                        ->scalarNode('model')->isRequired()->end()
86
                                        ->scalarNode('interface')->defaultValue(ReviewerInterface::class)->cannotBeEmpty()->end()
87
                                        ->scalarNode('repository')->cannotBeEmpty()->end()
88
                                        ->scalarNode('factory')->defaultValue(Factory::class)->cannotBeEmpty()->end()
89
                                    ->end()
90
                                ->end()
91
                            ->end()
92
                        ->end()
93
                    ->end()
94
                ->end()
95
            ->end()
96
        ;
97
    }
98
}
99