Completed
Push — master ( 8e8e6f...5fb808 )
by Nikola
03:47
created

Configuration::getExclusionsDefinition()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 22
cts 22
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 23
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * This file is part of the  TraitorBundle, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\Traitor\DependencyInjection;
11
12
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
13
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
14
use Symfony\Component\Config\Definition\ConfigurationInterface;
15
16
class Configuration implements ConfigurationInterface
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 7
    public function getConfigTreeBuilder()
22
    {
23 7
        $treeBuilder = new TreeBuilder();
24
25 7
        $rootNode = $treeBuilder->root('runopencode_traitor');
26
27
        $rootNode
28 7
            ->addDefaultsIfNotSet()
29 7
            ->fixXmlConfig('inject')
30 7
            ->children()
31 7
                ->booleanNode('use_common_traits')
32 7
                    ->defaultFalse()
33 7
                    ->info('For sake of productivity, some of the common Symfony and vendor traits, as well as traits from this library, can be automatically added to "inject" definition.')
34 7
                ->end()
35 7
                ->append($this->getInjectionsDefinition())
36 7
                ->append($this->getFiltesDefinition())
37 7
                ->append($this->getExclusionsDefinition())
38 7
            ->end();
39
40 7
        return $treeBuilder;
41
    }
42
43
    /**
44
     * Configure injections
45
     *
46
     * @return ArrayNodeDefinition
47
     */
48 7
    protected function getInjectionsDefinition()
49
    {
50 7
        $node = new ArrayNodeDefinition('injects');
51
52
        $node
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 fixXmlConfig() 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 7
            ->useAttributeAsKey('trait')
54 7
            ->prototype('array')
55 7
                ->fixXmlConfig('call', 'call')
56 7
                ->children()
57 7
                    ->arrayNode('call')
58 7
                        ->prototype('array')
59 7
                        ->fixXmlConfig('argument')
60 7
                            ->children()
61 7
                                ->scalarNode('method')->isRequired()->end()
62 7
                                ->arrayNode('arguments')
63 7
                                    ->prototype('array')
64 7
                                        ->children()
65 7
                                            ->scalarNode('id')->defaultNull()->end()
66 7
                                            ->scalarNode('type')->defaultValue('string')->end()
67 7
                                            ->variableNode('value')->defaultNull()->end()
68 7
                                        ->end()
69 7
                                    ->end()
70 7
                                ->end()
71 7
                            ->end()
72 7
                        ->end()
73 7
                    ->end()
74 7
                ->end()
75 7
            ->end();
76
77 7
        return $node;
78
    }
79
80
    /**
81
     * Configure filters
82
     * 
83
     * @return ArrayNodeDefinition
84
     */
85 7
    protected function getFiltesDefinition()
86
    {
87 7
        $node = new ArrayNodeDefinition('filter');
88
89
        $node
90 7
            ->info('Analyse services for injection which matches filter criteria.')
91 7
            ->fixXmlConfig('tag')
92 7
            ->fixXmlConfig('namespace')
93 7
                ->children()
94 7
                    ->arrayNode('tags')
95 7
                        ->prototype('scalar')->end()
96 7
                    ->end()
97 7
                    ->arrayNode('namespaces')
98 7
                        ->prototype('scalar')->end()
99 7
                    ->end()
100 7
                ->end();
101
102 7
        return $node;
103
    }
104
105
    /**
106
     * Configure exclusions
107
     *
108
     * @return ArrayNodeDefinition
109
     */
110 7
    protected function getExclusionsDefinition()
111
    {
112 7
        $node = new ArrayNodeDefinition('exclude');
113
114
        $node
115 7
            ->info('Exclude services from injection which matches filter criteria.')
116 7
            ->fixXmlConfig('service')
117 7
            ->fixXmlConfig('namespace')
118 7
            ->fixXmlConfig('class', 'classes')
119 7
            ->fixXmlConfig('tag')
120 7
                ->children()
121 7
                    ->arrayNode('services')
122 7
                        ->prototype('scalar')->end()
123 7
                    ->end()
124 7
                    ->arrayNode('namespaces')
125 7
                        ->prototype('scalar')->end()
126 7
                    ->end()
127 7
                    ->arrayNode('classes')
128 7
                        ->prototype('scalar')->end()
129 7
                    ->end()
130 7
                    ->arrayNode('tags')
131 7
                        ->prototype('scalar')->end()
132 7
                    ->end()
133 7
                ->end();
134
135 7
        return $node;
136
    }
137
}
138