Completed
Pull Request — develop (#43)
by A.
09:18
created

setupAttributeSupportConfiguration()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 43
rs 8.8571
cc 1
eloc 37
nc 1
nop 1
1
<?php
2
3
/**
4
 * Copyright 2015 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace OpenConext\ProfileBundle\DependencyInjection;
20
21
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
22
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
23
use Symfony\Component\Config\Definition\ConfigurationInterface;
24
25
class Configuration implements ConfigurationInterface
26
{
27
    public function getConfigTreeBuilder()
28
    {
29
        $treeBuilder = new TreeBuilder();
30
        $rootNode = $treeBuilder->root('open_conext_profile');
31
32
        $rootNode
33
            ->children()
34
                ->scalarNode('engine_block_entity_id')
35
                    ->info('The EntityID of EngineBlock')
36
                    ->isRequired()
37
                    ->validate()
38
                        ->ifTrue(function ($entityId) {
39
                            return !is_string($entityId);
40
                        })
41
                        ->thenInvalid('EngineBlock EntityID should be a string')
42
                    ->end()
43
                ->end()
44
            ->end();
45
46
        $this->setupLocaleConfiguration($rootNode);
47
        $this->setupAttributeSupportConfiguration($rootNode);
48
49
        return $treeBuilder;
50
    }
51
52
    private function setupLocaleConfiguration(NodeDefinition $rootNode)
53
    {
54
        $rootNode
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...
55
            ->children()
56
                ->arrayNode('locales')
57
                    ->info('The available application locales')
58
                    ->isRequired()
59
                    ->prototype('scalar')
60
                        ->validate()
61
                            ->ifTrue(function ($locale) {
62
                                return !is_string($locale);
63
                            })
64
                            ->thenInvalid('Available application locales should be strings')
65
                        ->end()
66
                    ->end()
67
                ->end()
68
                ->scalarNode('default_locale')
69
                    ->info('The default application locale')
70
                    ->isRequired()
71
                    ->validate()
72
                        ->ifTrue(function ($locale) {
73
                            return !is_string($locale);
74
                        })
75
                        ->thenInvalid('Default application locale should be a string')
76
                    ->end()
77
                ->end()
78
                ->scalarNode('locale_cookie_domain')
79
                    ->info('The domain for which the locale cookie is set')
80
                    ->isRequired()
81
                    ->validate()
82
                        ->ifTrue(function ($domain) {
83
                            return !is_string($domain);
84
                        })
85
                        ->thenInvalid('Locale cookie domain should be a string')
86
                    ->end()
87
                ->end()
88
                ->scalarNode('locale_cookie_key')
89
                    ->info('The key for which the locale cookie value is set')
90
                    ->isRequired()
91
                    ->validate()
92
                        ->ifTrue(function ($key) {
93
                            return !is_string($key);
94
                        })
95
                        ->thenInvalid('Locale cookie key should be a string')
96
                    ->end()
97
                ->end()
98
            ->end();
99
    }
100
101
    private function setupAttributeSupportConfiguration(NodeDefinition $rootNode)
102
    {
103
        $rootNode
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...
104
            ->children()
105
                ->arrayNode('attribute_support')
106
                    ->isRequired()
107
                    ->children()
108
                        ->scalarNode('email_to')
109
                            ->info('Email address to which attributes are sent')
110
                            ->isRequired()
111
                            ->validate()
112
                                ->ifTrue(function ($email) {
113
                                    return !is_string($email);
114
                                })
115
                                ->thenInvalid('Email address to which attributes are sent should be a string')
116
                            ->end()
117
                            ->validate()
118
                                ->ifTrue(function ($email) {
119
                                    return !filter_var($email, FILTER_VALIDATE_EMAIL);
120
                                })
121
                                ->thenInvalid('Email address to which attributes are sent should be valid')
122
                            ->end()
123
                        ->end()
124
                        ->scalarNode('email_from')
125
                            ->info('mail address from which attributes are sent')
126
                            ->isRequired()
127
                            ->validate()
128
                                ->ifTrue(function ($email) {
129
                                    return !is_string($email);
130
                                })
131
                                ->thenInvalid('Email address from which attributes are sent should be a string')
132
                            ->end()
133
                            ->validate()
134
                                ->ifTrue(function ($email) {
135
                                    return !filter_var($email, FILTER_VALIDATE_EMAIL);
136
                                })
137
                                ->thenInvalid('Email address from which attributes are sent should be valid')
138
                            ->end()
139
                        ->end()
140
                    ->end()
141
                ->end()
142
            ->end();
143
    }
144
}
145