Completed
Push — master ( affb41...f007be )
by Rafał
09:54
created

Configuration   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 3
dl 0
loc 98
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigTreeBuilder() 0 54 1
A addRegistrationSection() 0 37 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher User Bundle.
7
 *
8
 * Copyright 2021 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @Copyright 2021 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\UserBundle\DependencyInjection;
18
19
use SWP\Bundle\StorageBundle\Doctrine\ORM\EntityRepository;
20
use SWP\Bundle\UserBundle\Form\RegistrationFormType;
21
use SWP\Bundle\UserBundle\Model\ResetPasswordRequest;
22
use SWP\Bundle\UserBundle\Model\User;
23
use SWP\Bundle\UserBundle\Model\UserInterface;
24
use SWP\Component\Storage\Factory\Factory;
25
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
26
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
27
use Symfony\Component\Config\Definition\ConfigurationInterface;
28
29
/**
30
 * This is the class that validates and merges configuration from your app/config files.
31
 *
32
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
33
 */
34
class Configuration implements ConfigurationInterface
35
{
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getConfigTreeBuilder()
40
    {
41
        $treeBuilder = new TreeBuilder('swp_user');
42
        $treeBuilder->getRootNode()
0 ignored issues
show
Bug introduced by
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...
43
            ->children()
44
                ->arrayNode('from_email')
45
                    ->isRequired()
46
                    ->children()
47
                        ->scalarNode('address')->isRequired()->cannotBeEmpty()->end()
48
                        ->scalarNode('sender_name')->isRequired()->cannotBeEmpty()->end()
49
                    ->end()
50
                ->end()
51
                ->arrayNode('persistence')
52
                    ->addDefaultsIfNotSet()
53
                    ->children()
54
                        ->arrayNode('orm')
55
                            ->addDefaultsIfNotSet()
56
                            ->canBeEnabled()
57
                            ->children()
58
                                ->arrayNode('classes')
59
                                ->addDefaultsIfNotSet()
60
                                ->children()
61
                                    ->arrayNode('user')
62
                                        ->addDefaultsIfNotSet()
63
                                        ->children()
64
                                            ->scalarNode('model')->cannotBeEmpty()->defaultValue(User::class)->end()
65
                                            ->scalarNode('repository')->defaultValue(EntityRepository::class)->end()
66
                                            ->scalarNode('factory')->defaultValue(Factory::class)->end()
67
                                            ->scalarNode('interface')->defaultValue(UserInterface::class)->end()
68
                                            ->scalarNode('object_manager_name')->defaultValue(null)->end()
69
                                        ->end()
70
                                    ->end()
71
                                    ->arrayNode('reset_password_request')
72
                                        ->addDefaultsIfNotSet()
73
                                        ->children()
74
                                            ->scalarNode('model')->cannotBeEmpty()->defaultValue(ResetPasswordRequest::class)->end()
75
                                            ->scalarNode('repository')->defaultValue(EntityRepository::class)->end()
76
                                            ->scalarNode('factory')->defaultValue(Factory::class)->end()
77
                                            ->scalarNode('interface')->defaultValue(UserInterface::class)->end()
78
                                            ->scalarNode('object_manager_name')->defaultValue(null)->end()
79
                                        ->end()
80
                                    ->end()
81
                                ->end() // classes
82
                            ->end()
83
                        ->end()
84
                    ->end()
85
                ->end()
86
            ->end()
87
        ;
88
89
        $this->addRegistrationSection($treeBuilder->getRootNode());
0 ignored issues
show
Compatibility introduced by
$treeBuilder->getRootNode() of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
90
91
        return $treeBuilder;
92
    }
93
94
    private function addRegistrationSection(ArrayNodeDefinition $node)
95
    {
96
        $node
97
            ->children()
98
                ->arrayNode('registration')
99
                    ->addDefaultsIfNotSet()
100
                    ->canBeUnset()
101
                    ->children()
102
                        ->arrayNode('confirmation')
103
                            ->addDefaultsIfNotSet()
104
                            ->children()
105
                                ->booleanNode('enabled')->defaultFalse()->end()
106
                                ->scalarNode('template')->defaultValue('@SWPUser/Registration/confirmation_email.html.twig')->end()
107
                                ->arrayNode('from_email')
108
                                    ->canBeUnset()
109
                                    ->children()
110
                                        ->scalarNode('address')->isRequired()->cannotBeEmpty()->end()
111
                                        ->scalarNode('sender_name')->isRequired()->cannotBeEmpty()->end()
112
                                    ->end()
113
                                ->end()
114
                        ->end()
115
                    ->end()
116
                    ->arrayNode('form')
117
                        ->addDefaultsIfNotSet()
118
                        ->children()
119
                            ->scalarNode('type')->defaultValue(RegistrationFormType::class)->end()
120
                            ->scalarNode('name')->defaultValue('swp_user_registration_form')->end()
121
                            ->arrayNode('validation_groups')
122
                                ->prototype('scalar')->end()
123
                                ->defaultValue(['Registration', 'Default'])
124
                            ->end()
125
                        ->end()
126
                    ->end()
127
                ->end()
128
            ->end()
129
            ->end();
130
    }
131
}
132