Configuration::getConfigNode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 19
cts 19
cp 1
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 1
crap 1
1
<?php
2
3
namespace SyncFS\Configuration;
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
 * Class Configuration
11
 *
12
 * @package SyncFS\Configuration
13
 * @author  Matej Velikonja <[email protected]>
14
 */
15
class Configuration implements ConfigurationInterface
16
{
17
    /**
18
     * Generates the configuration tree builder.
19
     *
20
     * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
21
     */
22 5
    public function getConfigTreeBuilder()
23
    {
24 5
        $treeBuilder = new TreeBuilder();
25 5
        $rootNode    = $treeBuilder->root('filesystem');
26
27 5
        $this->getConfigNode($rootNode);
28
29 5
        return $treeBuilder;
30
    }
31
32
    /**
33
     * @param NodeDefinition $node
34
     *
35
     * @return \Symfony\Component\Config\Definition\Builder\NodeDefinition
36
     */
37 5
    public function getConfigNode(NodeDefinition $node)
38
    {
39
        $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...
40 5
            ->children()
41 5
                ->scalarNode('default_client')->defaultValue('rsync')->end()
42 5
                ->arrayNode('maps')
43 5
                    ->isRequired()
44 5
                    ->requiresAtLeastOneElement()
45 5
                    ->useAttributeAsKey('name')
46 5
                    ->prototype('array')
47 5
                        ->children()
48 5
                            ->scalarNode('src')->isRequired()->end()
49 5
                            ->scalarNode('dst')->isRequired()->end()
50 5
                            ->scalarNode('client')->defaultNull()->end()
51 5
                        ->end()
52 5
                    ->end()
53 5
                ->end()
54 5
                ->scalarNode('timeout')->defaultValue(15 * 60)->end()
55 5
            ->end()
56 5
        ->end();
57
58 5
        return $node;
59
    }
60
}
61