Completed
Push — master ( d536ea...5d53e7 )
by Jakub
02:06
created

Req2CmdConfiguration::addEventListenersNode()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 24
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Eps\Req2CmdBundle\DependencyInjection;
5
6
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
7
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8
use Symfony\Component\Config\Definition\ConfigurationInterface;
9
10
final class Req2CmdConfiguration implements ConfigurationInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     * @throws \RuntimeException
15
     */
16
    public function getConfigTreeBuilder(): TreeBuilder
17
    {
18
        $builder = new TreeBuilder();
19
20
        $root = $builder->root('req2cmd');
21
        $root
22
            ->children()
23
                ->append($this->addExtractorNode())
24
                ->append($this->addCommandBusNode())
25
                ->append($this->addEventListenersNode())
26
            ->end();
27
28
        return $builder;
29
    }
30
31
    private function addExtractorNode(): NodeDefinition
32
    {
33
        $builder = new TreeBuilder();
34
        $root = $builder->root('extractor');
35
        $root
36
            ->addDefaultsIfNotSet()
37
            ->beforeNormalization()
38
                ->ifString()
39
                ->then(function ($extractorName) {
40
                    return ['service_id' => 'eps.req2cmd.extractor.' . $extractorName];
41
                })
42
            ->end()
43
            ->children()
44
                ->scalarNode('service_id')
45
                    ->cannotBeEmpty()
46
                    ->defaultValue('eps.req2cmd.extractor.serializer')
47
                ->end()
48
            ->end();
49
50
        return $root;
51
    }
52
53
    private function addCommandBusNode(): NodeDefinition
54
    {
55
        $builder = new TreeBuilder();
56
        $node = $builder->root('command_bus');
57
        $node
1 ignored issue
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...
58
            ->addDefaultsIfNotSet()
59
            ->beforeNormalization()
60
                ->ifString()
61
                ->then(function (string $svcId) {
62
                    return ['service_id' => 'eps.req2cmd.command_bus.' . $svcId];
63
                })
64
            ->end()
65
            ->children()
66
                ->scalarNode('service_id')
67
                    ->cannotBeEmpty()
68
                    ->defaultValue('eps.req2cmd.command_bus.tactician')
69
                ->end()
70
                ->scalarNode('name')
71
                    ->cannotBeEmpty()
72
                    ->defaultValue('default')
73
                ->end()
74
            ->end()
75
            ->validate()
76
                ->ifTrue(function ($config) {
77
                    return $config['service_id'] !== 'eps.req2cmd.command_bus.tactician';
78
                })
79
                ->then(function ($config) {
80
                    unset($config['name']);
81
                    return $config;
82
                })
83
            ->end();
84
85
        return $node;
86
    }
87
88
    private function addEventListenersNode(): NodeDefinition
89
    {
90
        $builder = new TreeBuilder();
91
        $node = $builder->root('listeners');
92
93
        $node
94
            ->addDefaultsIfNotSet()
95
            ->children()
96
                ->arrayNode('extractor')
97
                    ->addDefaultsIfNotSet()
98
                    ->beforeNormalization()
99
                        ->ifTrue(function ($enabled) { return is_bool($enabled); })
100
                        ->then(function ($isEnabled) {
101
                            return ['enabled' => $isEnabled];
102
                        })
103
                    ->end()
104
                    ->children()
105
                        ->booleanNode('enabled')
106
                            ->defaultValue(true)
107
                        ->end()
108
                        ->integerNode('priority')
109
                            ->defaultValue(0)
110
                        ->end()
111
                    ->end()
112
                ->end()
113
            ->end();
114
115
        return $node;
116
    }
117
}
118