Completed
Push — master ( 0563c6...cb42e4 )
by Yann
02:20
created

Configuration::getServicesNode()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 25
cts 25
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 26
nc 1
nop 0
crap 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
9
/**
10
 * @author Yann Eugoné <[email protected]>
11
 */
12
class Configuration implements ConfigurationInterface
13
{
14
    /**
15
     * @inheritdoc
16
     */
17 4
    public function getConfigTreeBuilder()
18
    {
19 4
        $builder = new TreeBuilder();
20 4
        $root = $builder->root('yokai_security_token');
21
22 4
        $root->addDefaultsIfNotSet();
23
        $root
24 4
            ->children()
25 4
                ->append($this->getTokensNode())
26 4
                ->append($this->getServicesNode())
27 4
            ->end()
28
        ;
29
30 4
        return $builder;
31
    }
32
33
    /**
34
     * @return NodeDefinition
35
     */
36 4
    private function getTokensNode()
37
    {
38 4
        $builder = new TreeBuilder();
39 4
        $node = $builder->root('tokens');
40
41
        $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...
42 4
            ->useAttributeAsKey('purpose')
43 4
            ->prototype('array')
44 4
                ->children()
45 4
                    ->scalarNode('generator')
46 4
                        ->defaultValue('yokai_security_token.open_ssl_token_generator')
47 4
                    ->end()
48 4
                    ->scalarNode('duration')
49 4
                        ->defaultValue('+2 days')
50 4
                    ->end()
51 4
                    ->integerNode('usages')
52 4
                        ->defaultValue(1)
53 4
                    ->end()
54 4
                ->end()
55 4
            ->end()
56
        ;
57
58 4
        return $node;
59
    }
60
61
    /**
62
     * @return NodeDefinition
63
     */
64 4
    private function getServicesNode()
65
    {
66 4
        $builder = new TreeBuilder();
67 4
        $node = $builder->root('services');
68
69 4
        $node->addDefaultsIfNotSet();
70
        $node
71 4
            ->children()
72 4
                ->scalarNode('information_guesser')
73 4
                    ->defaultValue('yokai_security_token.default_information_guesser')
74 4
                ->end()
75 4
                ->scalarNode('token_factory')
76 4
                    ->defaultValue('yokai_security_token.default_token_factory')
77 4
                ->end()
78 4
                ->scalarNode('token_repository')
79 4
                    ->defaultValue('yokai_security_token.default_token_repository')
80 4
                ->end()
81 4
                ->scalarNode('token_manager')
82 4
                    ->defaultValue('yokai_security_token.default_token_manager')
83 4
                ->end()
84 4
                ->scalarNode('user_manager')
85 4
                    ->defaultValue('yokai_security_token.default_user_manager')
86 4
                ->end()
87 4
                ->scalarNode('archivist')
88 4
                    ->defaultValue('yokai_security_token.delete_archivist')
89 4
                ->end()
90 4
            ->end()
91
        ;
92
93 4
        return $node;
94
    }
95
}
96