Configuration   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 1
dl 0
loc 77
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getConfigTreeBuilder() 0 71 1
1
<?php
2
3
/*
4
 * This file is part of the takeit/AmpHtmlBundle package.
5
 *
6
 * (c) Rafał Muszyński <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Takeit\Bundle\AmpHtmlBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
15
use Symfony\Component\Config\Definition\ConfigurationInterface;
16
17
/**
18
 * This is the class that validates and merges configuration from your app/config files.
19
 *
20
 * @author Rafał Muszyński <[email protected]>
21
 */
22
class Configuration implements ConfigurationInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getConfigTreeBuilder()
28
    {
29
        $treeBuilder = new TreeBuilder('takeit_amp_html');
30
        $treeBuilder->getRootNode()
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 canBeDisabled() 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...
31
            ->canBeDisabled()
32
                ->info('Enable or disable Google AMP HTML support.')
33
            ->addDefaultsIfNotSet()
34
            ->children()
35
                ->arrayNode('theme')
36
                    ->addDefaultsIfNotSet()
37
                    ->children()
38
                        ->scalarNode('loader')
39
                            ->defaultValue('takeit_amp_html.loader.theme.default')
40
                            ->cannotBeEmpty()
41
                        ->end()
42
                        ->scalarNode('theme_path')
43
                            ->defaultValue('%kernel.root_dir%/Resources/amp/amp-theme')
44
                        ->end()
45
                    ->end()
46
                ->end()
47
                ->scalarNode('checker')
48
                    ->defaultValue('takeit_amp_html.checker.default')
49
                    ->cannotBeEmpty()
50
                ->end()
51
                ->arrayNode('routing')
52
                    ->isRequired()
53
                    ->addDefaultsIfNotSet()
54
                    ->children()
55
                        ->arrayNode('route_strategy')
56
                            ->canBeEnabled()
57
                            ->addDefaultsIfNotSet()
58
                            ->children()
59
                                ->scalarNode('prefix')
60
                                    ->defaultValue('/platform/amp')
61
                                ->end()
62
                                ->scalarNode('pattern')
63
                                    ->isRequired()
64
                                    ->cannotBeEmpty()
65
                                ->end()
66
                                    ->scalarNode('parameter')
67
                                    ->cannotBeEmpty()
68
                                    ->defaultValue('slug')
69
                                ->end()
70
                                ->scalarNode('parameterRegex')
71
                                    ->defaultValue('.+')
72
                                    ->info('Parameter\'s regular expression.')
73
                                ->end()
74
                            ->end()
75
                        ->end()
76
                        ->arrayNode('parameter_strategy')
77
                            ->canBeEnabled()
78
                        ->end()
79
                        ->scalarNode('controller')
80
                            ->defaultValue('takeit_amp_html.amp_controller:viewAction')
81
                        ->end()
82
                    ->end()
83
                ->end()
84
                ->scalarNode('model')
85
                    ->cannotBeEmpty()
86
                    ->isRequired()
87
                    ->info('Fully qualified class name of your model.')
88
                ->end()
89
                ->scalarNode('converter')
90
                    ->defaultValue('takeit_amp_html.amp_converter')
91
                    ->info('Custom converter service id.')
92
                ->end()
93
            ->end()
94
        ;
95
96
        return $treeBuilder;
97
    }
98
}
99