Completed
Pull Request — master (#37)
by Yann
01:36
created

Configuration   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 3

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
wmc 4
cbo 3
dl 0
loc 92
ccs 56
cts 58
cp 0.9655
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigTreeBuilder() 0 20 2
A getTokensNode() 0 30 1
A getServicesNode() 0 28 1
1
<?php
2
3
namespace Yokai\SecurityTokenBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
use Symfony\Component\HttpKernel\Kernel;
9
10
/**
11
 * @author Yann Eugoné <[email protected]>
12
 */
13
class Configuration implements ConfigurationInterface
14
{
15
    /**
16
     * @inheritdoc
17
     */
18 4
    public function getConfigTreeBuilder()
19
    {
20 4
        if (version_compare(Kernel::VERSION, '4.2') >= 0) {
21
            $builder = new TreeBuilder('yokai_security_token');
22
            $root = $builder->getRootNode();
23
        } else {
24 4
            $builder = new TreeBuilder();
25 4
            $root = $builder->root('yokai_security_token');
26
        }
27
28 4
        $root->addDefaultsIfNotSet();
29
        $root
30 4
            ->children()
31 4
                ->append($this->getTokensNode())
32 4
                ->append($this->getServicesNode())
33 4
            ->end()
34
        ;
35
36 4
        return $builder;
37
    }
38
39
    /**
40
     * @return NodeDefinition
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Symfony\Component\Confi...der\ArrayNodeDefinition.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
41
     */
42 4
    private function getTokensNode()
43
    {
44 4
        $builder = new TreeBuilder();
45 4
        $node = $builder->root('tokens');
46
47
        $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 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...
48 4
            ->useAttributeAsKey('purpose')
49 4
            ->prototype('array')
50 4
                ->children()
51 4
                    ->scalarNode('generator')
52 4
                        ->defaultValue('yokai_security_token.open_ssl_token_generator')
53 4
                    ->end()
54 4
                    ->scalarNode('duration')
55 4
                        ->defaultValue('+2 days')
56 4
                    ->end()
57 4
                    ->integerNode('usages')
58 4
                        ->defaultValue(1)
59 4
                    ->end()
60 4
                    ->scalarNode('keep')
61 4
                        ->defaultValue('+1 month')
62 4
                    ->end()
63 4
                    ->booleanNode('unique')
64 4
                        ->defaultValue(false)
65 4
                    ->end()
66 4
                ->end()
67 4
            ->end()
68
        ;
69
70 4
        return $node;
71
    }
72
73
    /**
74
     * @return NodeDefinition
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Symfony\Component\Confi...der\ArrayNodeDefinition.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
75
     */
76 4
    private function getServicesNode()
77
    {
78 4
        $builder = new TreeBuilder();
79 4
        $node = $builder->root('services');
80
81 4
        $node->addDefaultsIfNotSet();
82
        $node
83 4
            ->children()
84 4
                ->scalarNode('information_guesser')
85 4
                    ->defaultValue('yokai_security_token.default_information_guesser')
86 4
                ->end()
87 4
                ->scalarNode('token_factory')
88 4
                    ->defaultValue('yokai_security_token.default_token_factory')
89 4
                ->end()
90 4
                ->scalarNode('token_repository')
91 4
                    ->defaultValue('yokai_security_token.default_token_repository')
92 4
                ->end()
93 4
                ->scalarNode('token_manager')
94 4
                    ->defaultValue('yokai_security_token.default_token_manager')
95 4
                ->end()
96 4
                ->scalarNode('archivist')
97 4
                    ->defaultValue('yokai_security_token.delete_archivist')
98 4
                ->end()
99 4
            ->end()
100
        ;
101
102 4
        return $node;
103
    }
104
}
105