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

UserBundle/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\UserBundle\DependencyInjection;
15
16
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
17
use Sylius\Bundle\UserBundle\Controller\UserController;
18
use Sylius\Component\Resource\Factory\Factory;
19
use Sylius\Component\User\Model\User;
20
use Sylius\Component\User\Model\UserInterface;
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
final class Configuration implements ConfigurationInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getConfigTreeBuilder(): TreeBuilder
31
    {
32
        $treeBuilder = new TreeBuilder();
33
        $rootNode = $treeBuilder->root('sylius_user');
34
35
        $rootNode
36
            ->addDefaultsIfNotSet()
37
            ->children()
38
                ->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end()
39
            ->end()
40
        ;
41
42
        $this->addResourcesSection($rootNode);
43
44
        return $treeBuilder;
45
    }
46
47
    /**
48
     * @param ArrayNodeDefinition $node
49
     */
50
    private function addResourcesSection(ArrayNodeDefinition $node): void
51
    {
52
        $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...
53
            ->children()
54
                ->arrayNode('resources')
55
                    ->useAttributeAsKey('name')
56
                    ->prototype('array')
57
                        ->children()
58
                            ->arrayNode('user')
59
                                ->addDefaultsIfNotSet()
60
                                ->children()
61
                                    ->scalarNode('templates')->defaultValue('SyliusUserBundle:User')->end()
62
                                    ->variableNode('options')->end()
63
                                    ->arrayNode('resetting')
64
                                        ->addDefaultsIfNotSet()
65
                                        ->children()
66
                                            ->arrayNode('token')
67
                                                ->addDefaultsIfNotSet()
68
                                                ->children()
69
                                                    ->scalarNode('ttl')->defaultValue('P1D')->end()
70
                                                    ->integerNode('length')
71
                                                        ->defaultValue(16)
72
                                                        ->min(1)->max(40)
73
                                                    ->end()
74
                                                    ->scalarNode('field_name')
75
                                                        ->defaultValue('passwordResetToken')
76
                                                        ->validate()
77
                                                        ->ifTrue(function ($tokenFieldName) {
78
                                                            return !is_string($tokenFieldName);
79
                                                        })
80
                                                            ->thenInvalid('Invalid resetting token field "%s"')
81
                                                        ->end()
82
                                                    ->end()
83
                                                ->end()
84
                                            ->end()
85
                                            ->arrayNode('pin')
86
                                                ->addDefaultsIfNotSet()
87
                                                ->children()
88
                                                    ->integerNode('length')
89
                                                        ->defaultValue(4)
90
                                                        ->min(1)->max(9)
91
                                                    ->end()
92
                                                    ->scalarNode('field_name')
93
                                                        ->defaultValue('passwordResetToken')
94
                                                        ->validate()
95
                                                        ->ifTrue(function ($passwordResetToken) {
96
                                                            return !is_string($passwordResetToken);
97
                                                        })
98
                                                            ->thenInvalid('Invalid resetting pin field "%s"')
99
                                                        ->end()
100
                                                    ->end()
101
                                                ->end()
102
                                            ->end()
103
                                        ->end()
104
                                    ->end()
105
                                    ->arrayNode('verification')
106
                                        ->addDefaultsIfNotSet()
107
                                        ->children()
108
                                            ->arrayNode('token')
109
                                                ->addDefaultsIfNotSet()
110
                                                ->children()
111
                                                    ->integerNode('length')
112
                                                        ->defaultValue(16)
113
                                                        ->min(1)->max(40)
114
                                                    ->end()
115
                                                    ->scalarNode('field_name')
116
                                                        ->defaultValue('emailVerificationToken')
117
                                                        ->validate()
118
                                                        ->ifTrue(function ($emailVerificationToken) {
119
                                                            return !is_string($emailVerificationToken);
120
                                                        })
121
                                                            ->thenInvalid('Invalid verification token field "%s"')
122
                                                        ->end()
123
                                                    ->end()
124
                                                ->end()
125
                                            ->end()
126
                                        ->end()
127
                                    ->end()
128
                                    ->arrayNode('classes')
129
                                        ->addDefaultsIfNotSet()
130
                                        ->children()
131
                                            ->scalarNode('model')->defaultValue(User::class)->cannotBeEmpty()->end()
132
                                            ->scalarNode('interface')->defaultValue(UserInterface::class)->cannotBeEmpty()->end()
133
                                            ->scalarNode('controller')->defaultValue(UserController::class)->cannotBeEmpty()->end()
134
                                            ->scalarNode('repository')->cannotBeEmpty()->end()
135
                                            ->scalarNode('factory')->defaultValue(Factory::class)->end()
136
                                            ->scalarNode('form')->cannotBeEmpty()->end()
137
                                        ->end()
138
                                    ->end()
139
                                ->end()
140
                            ->end()
141
                        ->end()
142
                    ->end()
143
                ->end()
144
            ->end()
145
        ;
146
    }
147
}
148