Configuration   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 48
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getConfigTreeBuilder() 0 42 2
1
<?php
2
3
/*
4
 * This file is part of the GesdinetJWTRefreshTokenBundle package.
5
 *
6
 * (c) Gesdinet <http://www.gesdinet.com/>
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 Gesdinet\JWTRefreshTokenBundle\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
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
21
 */
22
class Configuration implements ConfigurationInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27 2
    public function getConfigTreeBuilder()
28
    {
29 2
        $treeBuilder = new TreeBuilder('gesdinet_jwt_refresh_token');
30 2
        $rootNode = method_exists(TreeBuilder::class, 'getRootNode') ? $treeBuilder->getRootNode() : $treeBuilder->root('gesdinet_jwt_refresh_token');
31
32
        $rootNode
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...
33 2
            ->children()
34 2
                ->integerNode('ttl')->defaultValue(2592000)->end()
35 2
                ->booleanNode('ttl_update')->defaultFalse()->end()
36 2
                ->scalarNode('firewall')->defaultValue('api')->end()
37 2
                ->scalarNode('user_provider')->defaultNull()->end()
38 2
                ->scalarNode('user_identity_field')->defaultValue('username')->end()
39 2
                ->scalarNode('manager_type')
40 2
                    ->defaultValue('orm')
41 2
                    ->info('Set manager mode instead of default one (orm)')
42 2
                    ->end()
43 2
                ->scalarNode('refresh_token_class')
44 2
                    ->defaultNull()
45 2
                    ->info('Set another refresh token class to use instead of default one (Gesdinet\JWTRefreshTokenBundle\Entity\RefreshToken)')
46 2
                    ->end()
47
                ->scalarNode('object_manager')
48 2
                    ->defaultNull()
49
                    ->info('Set object manager to use (default: doctrine.orm.entity_manager)')
50
                    ->end()
51
                ->scalarNode('user_checker')->defaultValue('security.user_checker')->end()
52
                ->scalarNode('refresh_token_entity')
53
                    ->defaultNull()
54
                    ->info('Deprecated, use refresh_token_class instead')
55
                    ->end()
56
                ->scalarNode('entity_manager')
57
                    ->defaultNull()
58
                    ->info('Deprecated, use object_manager instead')
59
                    ->end()
60
                ->scalarNode('single_use')
61
                    ->defaultFalse()
62
                    ->info('When true, generate a new refresh token on consumption (deleting the old one)')
63
                    ->end()
64
                ->scalarNode('token_parameter_name')->defaultValue('refresh_token')->end()
65
            ->end();
66
67
        return $treeBuilder;
68
    }
69
}
70