Completed
Push — master ( e39a6b...d536ea )
by Jakub
01:57
created

Req2CmdConfiguration::addCommandBusNode()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 28
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
            ->end();
26
27
        return $builder;
28
    }
29
30
    private function addExtractorNode(): NodeDefinition
31
    {
32
        $builder = new TreeBuilder();
33
        $root = $builder->root('extractor');
34
        $root
35
            ->addDefaultsIfNotSet()
36
            ->beforeNormalization()
37
                ->ifString()
38
                ->then(function ($extractorName) {
39
                    return ['service_id' => 'eps.req2cmd.extractor.' . $extractorName];
40
                })
41
            ->end()
42
            ->children()
43
                ->scalarNode('service_id')
44
                    ->cannotBeEmpty()
45
                    ->defaultValue('eps.req2cmd.extractor.serializer')
46
                ->end()
47
            ->end();
48
49
        return $root;
50
    }
51
52
    private function addCommandBusNode(): NodeDefinition
53
    {
54
        $builder = new TreeBuilder();
55
        $node = $builder->root('command_bus');
56
        $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...
57
            ->addDefaultsIfNotSet()
58
            ->beforeNormalization()
59
                ->ifString()
60
                ->then(function (string $svcId) {
61
                    return ['service_id' => 'eps.req2cmd.command_bus.' . $svcId];
62
                })
63
            ->end()
64
            ->children()
65
                ->scalarNode('service_id')
66
                    ->cannotBeEmpty()
67
                    ->defaultValue('eps.req2cmd.command_bus.tactician')
68
                ->end()
69
                ->scalarNode('name')
70
                    ->cannotBeEmpty()
71
                    ->defaultValue('default')
72
                ->end()
73
            ->end()
74
            ->validate()
75
                ->ifTrue(function ($config) {
76
                    return $config['service_id'] !== 'eps.req2cmd.command_bus.tactician';
77
                })
78
                ->then(function ($config) {
79
                    unset($config['name']);
80
                    return $config;
81
                })
82
            ->end();
83
84
        return $node;
85
    }
86
}
87