Configuration::getConfigTreeBuilder()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 23
cts 23
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 23
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Happyr\TranslationBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6
use Symfony\Component\Config\Definition\ConfigurationInterface;
7
8
/**
9
 * This is the class that validates and merges configuration from your app/config files.
10
 */
11
class Configuration implements ConfigurationInterface
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16 1
    public function getConfigTreeBuilder()
17
    {
18 1
        $treeBuilder = new TreeBuilder();
19 1
        $root = $treeBuilder->root('happyr_translation');
20
21 1
        $root->children()
22 1
            ->enumNode('translation_service')->values(array('blackhole', 'filesystem', 'loco'))->defaultValue('loco')->end()
23 1
            ->scalarNode('target_dir')->defaultValue('%kernel.root_dir%/Resources/translations')->end()
24 1
            ->scalarNode('httplug_client')->defaultValue('httplug.client')->cannotBeEmpty()->end()
25 1
            ->scalarNode('httplug_message_factory')->defaultValue('httplug.message_factory')->cannotBeEmpty()->end()
26 1
            ->booleanNode('sync_empty_translations')->defaultTrue()->end()
27 1
            ->booleanNode('auto_add_assets')->defaultFalse()->end()
28 1
            ->booleanNode('allow_edit')->defaultTrue()->end()
29 1
            ->enumNode('file_extension')->values(array('csv', 'ini', 'json', 'mo', 'php', 'po', 'qt', 'yml', 'xlf'))->defaultValue('xlf')->end()
30 1
            ->arrayNode('locales')
31 1
                ->requiresAtLeastOneElement()
32 1
                ->prototype('scalar')->end()
33 1
            ->end()
34 1
            ->arrayNode('domains')
35 1
                ->requiresAtLeastOneElement()
36 1
                ->prototype('scalar')->end()
37 1
            ->end()
38 1
            ->append($this->getProjectNode())
39 1
        ->end();
40
41 1
        return $treeBuilder;
42
    }
43
44
    /**
45
     * @return \Symfony\Component\Config\Definition\Builder\NodeDefinition
46
     */
47 1
    private function getProjectNode()
48
    {
49 1
        $treeBuilder = new TreeBuilder();
50 1
        $node = $treeBuilder->root('projects');
51
        $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...
52 1
            ->useAttributeAsKey('name')
53 1
            ->prototype('array')
54 1
            ->children()
55 1
                ->scalarNode('api_key')->isRequired()->end()
56 1
                ->arrayNode('locales')
57 1
                    ->requiresAtLeastOneElement()
58 1
                    ->prototype('scalar')->end()
59 1
                ->end()
60 1
                ->arrayNode('domains')
61 1
                    ->requiresAtLeastOneElement()
62 1
                    ->prototype('scalar')->end()
63 1
                ->end()
64 1
            ->end()
65 1
        ->end();
66
67 1
        return $node;
68
    }
69
}
70