Completed
Push — 2.0 ( 245720...b43a39 )
by Peter
04:51 queued 02:20
created

Configuration::getConfigTreeBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 13
ccs 11
cts 11
cp 1
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
namespace AnimeDb\Bundle\AniDbBrowserBundle\DependencyInjection;
10
11
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
12
use Symfony\Component\Config\Definition\ConfigurationInterface;
13
14
class Configuration implements ConfigurationInterface
15
{
16
    /**
17
     * Config tree builder.
18
     *
19
     * Example config:
20
     *
21
     * anime_db_ani_db_browser:
22
     *     client: 'cache'
23
     *     app:
24
     *         version: 2
25
     *         client: 'animedbplugin'
26
     *         code: 'api-team-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
27
     *
28
     * @return TreeBuilder
29
     */
30 11
    public function getConfigTreeBuilder()
31
    {
32 11
        return (new TreeBuilder())
33 11
            ->root('anime_db_ani_db_browser')
34 11
                ->children()
35 11
                    ->scalarNode('client')
36 11
                        ->cannotBeEmpty()
37 11
                        ->defaultValue('cache')
38 11
                    ->end()
39 11
                ->end()
40 11
                ->append($this->getApp())
41 11
            ->end();
42
    }
43
44
    /**
45
     * @return TreeBuilder
46
     */
47 11
    protected function getApp()
48
    {
49 11
        return (new TreeBuilder())
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...
50 11
            ->root('app')
51 11
                ->isRequired()
52 11
                ->children()
53 11
                    ->integerNode('version')
54 11
                        ->isRequired()
55 11
                    ->end()
56 11
                    ->scalarNode('client')
57 11
                        ->cannotBeEmpty()
58 11
                        ->isRequired()
59 11
                    ->end()
60 11
                    ->scalarNode('code')
61 11
                        ->cannotBeEmpty()
62 11
                        ->isRequired()
63 11
                    ->end()
64 11
                ->end();
65
    }
66
}
67