Completed
Push — 2.0 ( 359ae1...221cc7 )
by Rafał
51:27 queued 18:07
created

Configuration::getConfigTreeBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35

Duplication

Lines 35
Ratio 100 %

Importance

Changes 0
Metric Value
dl 35
loc 35
rs 9.36
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Publisher Redirect Route Bundle.
7
 *
8
 * Copyright 2019 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2019 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\RedirectRouteBundle\DependencyInjection;
18
19
use SWP\Bundle\RedirectRouteBundle\Model\RedirectRoute;
20
use SWP\Bundle\RedirectRouteBundle\Model\RedirectRouteInterface;
21
use SWP\Bundle\StorageBundle\Doctrine\ORM\EntityRepository;
22
use SWP\Component\Storage\Factory\Factory;
23
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
24
use Symfony\Component\Config\Definition\ConfigurationInterface;
25
26 View Code Duplication
class Configuration implements ConfigurationInterface
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getConfigTreeBuilder()
32
    {
33
        $treeBuilder = new TreeBuilder('swp_redirect_route');
34
        $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 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...
35
            ->children()
36
                ->arrayNode('persistence')
37
                    ->addDefaultsIfNotSet()
38
                    ->children()
39
                        ->arrayNode('orm')
40
                            ->addDefaultsIfNotSet()
41
                            ->canBeEnabled()
42
                            ->children()
43
                                ->arrayNode('classes')
44
                                ->addDefaultsIfNotSet()
45
                                ->children()
46
                                    ->arrayNode('redirect_route')
47
                                        ->addDefaultsIfNotSet()
48
                                        ->children()
49
                                            ->scalarNode('model')->cannotBeEmpty()->defaultValue(RedirectRoute::class)->end()
50
                                            ->scalarNode('repository')->defaultValue(EntityRepository::class)->end()
51
                                            ->scalarNode('factory')->defaultValue(Factory::class)->end()
52
                                            ->scalarNode('interface')->defaultValue(RedirectRouteInterface::class)->end()
53
                                            ->scalarNode('object_manager_name')->defaultValue(null)->end()
54
                                        ->end()
55
                                    ->end()
56
                                ->end()
57
                            ->end()
58
                        ->end()
59
                    ->end()
60
                ->end()
61
            ->end()
62
        ;
63
64
        return $treeBuilder;
65
    }
66
}
67