Completed
Push — compat-sf4 ( 58eeac...2dc342 )
by Kevin
02:56
created

Configuration   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 98.53%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 147
ccs 67
cts 68
cp 0.9853
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B getConfigTreeBuilder() 0 55 2
A getSourceFactory() 0 4 1
A getNamerFactory() 0 4 1
A getDestinationFactory() 0 4 1
A getProcessorFactory() 0 4 1
A addFactories() 0 23 2
1
<?php
2
3
namespace Zenstruck\BackupBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
use Zenstruck\BackupBundle\DependencyInjection\Factory\Factory;
9
10
/**
11
 * @author Kevin Bond <[email protected]>
12
 */
13
class Configuration implements ConfigurationInterface
14
{
15
    private $namerFactories;
16
    private $processorFactories;
17
    private $sourceFactories;
18
    private $destinationFactories;
19
20
    /**
21
     * @param Factory[] $namerFactories
22
     * @param Factory[] $processorFactories
23
     * @param Factory[] $sourceFactories
24
     * @param Factory[] $destinationFactories
25
     */
26 5
    public function __construct(array $namerFactories, array $processorFactories, array $sourceFactories, array $destinationFactories)
27
    {
28 5
        $this->namerFactories = $namerFactories;
29 5
        $this->processorFactories = $processorFactories;
30 5
        $this->sourceFactories = $sourceFactories;
31 5
        $this->destinationFactories = $destinationFactories;
32 5
    }
33
34 5
    public function getConfigTreeBuilder()
35
    {
36 5
        $treeBuilder = new TreeBuilder('zenstruck_backup');
37
38
        // Keep compatibility with symfony/config < 4.2
39 5
        if (\method_exists($treeBuilder, 'getRootNode')) {
40
            $rootNode = $treeBuilder->getRootNode();
41
        } else {
42 5
            $rootNode = $treeBuilder->root('zenstruck_backup');
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Config...der\TreeBuilder::root() has been deprecated with message: since Symfony 4.3, pass the root name to the constructor instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
43
        }
44
45 5
        $this->addFactories($rootNode, 'namers', 'namer', $this->namerFactories);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
46 5
        $this->addFactories($rootNode, 'processors', 'processor', $this->processorFactories);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
47 5
        $this->addFactories($rootNode, 'sources', 'source', $this->sourceFactories);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
48 5
        $this->addFactories($rootNode, 'destinations', 'destination', $this->destinationFactories);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
49
50
        $rootNode
51 5
            ->children()
52 5
                ->arrayNode('profiles')
53 5
                    ->useAttributeAsKey('name')
54 5
                    ->prototype('array')
55 5
                        ->children()
56 5
                            ->scalarNode('scratch_dir')->defaultValue('%kernel.cache_dir%/backup')->end()
57 5
                            ->arrayNode('sources')
58 5
                                ->isRequired()
59 5
                                ->requiresAtLeastOneElement()
60 5
                                ->prototype('scalar')->end()
61 5
                                ->beforeNormalization()
62 5
                                    ->ifString()
63 5
                                    ->then(function ($v) {
64 1
                                        return array($v);
65 5
                                    })
66 5
                                ->end()
67 5
                            ->end()
68 5
                            ->scalarNode('namer')->isRequired()->end()
69 5
                            ->scalarNode('processor')->isRequired()->end()
70 5
                            ->arrayNode('destinations')
71 5
                                ->isRequired()
72 5
                                ->requiresAtLeastOneElement()
73 5
                                ->prototype('scalar')->end()
74 5
                                ->beforeNormalization()
75 5
                                    ->ifString()
76 5
                                    ->then(function ($v) {
77 3
                                        return array($v);
78 5
                                    })
79 5
                                ->end()
80 5
                            ->end()
81 5
                        ->end()
82 5
                    ->end()
83 5
                ->end()
84 5
            ->end()
85
        ;
86
87 5
        return $treeBuilder;
88
    }
89
90
    /**
91
     * @param string $id
92
     *
93
     * @return Factory
94
     */
95 1
    public function getSourceFactory($id)
96
    {
97 1
        return $this->sourceFactories[$id];
98
    }
99
100
    /**
101
     * @param string $id
102
     *
103
     * @return Factory
104
     */
105 1
    public function getNamerFactory($id)
106
    {
107 1
        return $this->namerFactories[$id];
108
    }
109
110
    /**
111
     * @param string $id
112
     *
113
     * @return Factory
114
     */
115 1
    public function getDestinationFactory($id)
116
    {
117 1
        return $this->destinationFactories[$id];
118
    }
119
120
    /**
121
     * @param string $id
122
     *
123
     * @return Factory
124
     */
125 1
    public function getProcessorFactory($id)
126
    {
127 1
        return $this->processorFactories[$id];
128
    }
129
130
    /**
131
     * @param ArrayNodeDefinition $node
132
     * @param string              $plural
133
     * @param string              $singular
134
     * @param Factory[]           $factories
135
     */
136 5
    private function addFactories(ArrayNodeDefinition $node, $plural, $singular, array $factories)
137
    {
138
        $nodeBuilder = $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 canBeUnset() 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...
139 5
            ->children()
140 5
                ->arrayNode($plural)
141 5
                    ->useAttributeAsKey('name')
142 5
                    ->prototype('array')
143 5
                        ->canBeUnset()
144 5
                        ->validate()
145 5
                            ->ifTrue(function ($v) {
146 1
                                return count($v) > 1;
147 5
                            })
148
                            ->thenInvalid(sprintf('Can only have 1 %s per configuration.', $singular))
149
                        ->end()
150
                        ->children()
151
        ;
152
153
        foreach ($factories as $name => $factory) {
154
            $factoryNode = $nodeBuilder->arrayNode($name);
155
156
            $factory->addConfiguration($factoryNode);
157
        }
158
    }
159
}
160