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