Configuration::getConfigTreeBuilder()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 37
ccs 33
cts 33
cp 1
rs 8.8571
cc 1
eloc 34
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Ivory Lucene Search package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\LuceneSearchBundle\DependencyInjection;
13
14
use Ivory\LuceneSearchBundle\Model\LuceneManager;
15
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
16
use Symfony\Component\Config\Definition\ConfigurationInterface;
17
18
/**
19
 * @author GeLo <[email protected]>
20
 */
21
class Configuration implements ConfigurationInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 27
    public function getConfigTreeBuilder()
27
    {
28 27
        $treeBuilder = new TreeBuilder();
29
        $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...
30 27
            ->root('ivory_lucene_search')
31 27
            ->useAttributeAsKey('name')
32 27
            ->prototype('array')
33 27
                ->children()
34 27
                    ->scalarNode('path')
35 27
                        ->isRequired()
36 27
                    ->end()
37 27
                    ->scalarNode('analyzer')
38 27
                        ->defaultValue(LuceneManager::DEFAULT_ANALYZER)
39 27
                    ->end()
40 27
                    ->scalarNode('max_buffered_docs')
41 27
                        ->defaultValue(LuceneManager::DEFAULT_MAX_BUFFERED_DOCS)
42 27
                    ->end()
43 27
                    ->scalarNode('max_merge_docs')
44 27
                        ->defaultValue(LuceneManager::DEFAULT_MAX_MERGE_DOCS)
45 27
                    ->end()
46 27
                    ->scalarNode('merge_factor')
47 27
                        ->defaultValue(LuceneManager::DEFAULT_MERGE_FACTOR)
48 27
                    ->end()
49 27
                    ->scalarNode('permissions')
50 27
                        ->defaultValue(LuceneManager::DEFAULT_PERMISSIONS)
51 27
                    ->end()
52 27
                    ->scalarNode('auto_optimized')
53 27
                        ->defaultValue(LuceneManager::DEFAULT_AUTO_OPTIMIZED)
54 27
                    ->end()
55 27
                    ->scalarNode('query_parser_encoding')
56 27
                        ->defaultValue(LuceneManager::DEFAULT_QUERY_PARSER_ENCODING)
57 27
                    ->end()
58 27
                ->end()
59 27
            ->end();
60
61 27
        return $treeBuilder;
62
    }
63
}
64