Failed Conditions
Push — ng ( ca946d...fcb055 )
by Florent
10:43
created

InitialAccessTokenSource::getNodeDefinition()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 53
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 8.9849
c 0
b 0
f 0
cc 4
eloc 48
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Bundle\DependencyInjection\Component\Endpoint\ClientRegistration;
15
16
use OAuth2Framework\Bundle\DependencyInjection\Component\Component;
17
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
18
use Symfony\Component\Config\FileLocator;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
21
22
class InitialAccessTokenSource implements Component
23
{
24
    /**
25
     * @return string
26
     */
27
    public function name(): string
28
    {
29
        return 'initial_access_token';
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function load(array $configs, ContainerBuilder $container)
36
    {
37
        if (!$configs['endpoint']['client_registration']['initial_access_token']['enabled']) {
38
            return;
39
        }
40
        $container->setParameter('oauth2_server.endpoint.client_registration.initial_access_token.required', $configs['endpoint']['client_registration']['initial_access_token']['required']);
41
        $container->setParameter('oauth2_server.endpoint.client_registration.initial_access_token.realm', $configs['endpoint']['client_registration']['initial_access_token']['realm']);
42
        $container->setParameter('oauth2_server.endpoint.client_registration.initial_access_token.authorization_header', $configs['endpoint']['client_registration']['initial_access_token']['authorization_header']);
43
        $container->setParameter('oauth2_server.endpoint.client_registration.initial_access_token.query_string', $configs['endpoint']['client_registration']['initial_access_token']['query_string']);
44
        $container->setParameter('oauth2_server.endpoint.client_registration.initial_access_token.request_body', $configs['endpoint']['client_registration']['initial_access_token']['request_body']);
45
        $container->setParameter('oauth2_server.endpoint.client_registration.initial_access_token.min_length', $configs['endpoint']['client_registration']['initial_access_token']['min_length']);
46
        $container->setParameter('oauth2_server.endpoint.client_registration.initial_access_token.max_length', $configs['endpoint']['client_registration']['initial_access_token']['max_length']);
47
        $container->setAlias('oauth2_server.endpoint.client_registration.initial_access_token.repository', $configs['endpoint']['client_registration']['initial_access_token']['repository']);
48
49
        $loader = new PhpFileLoader($container, new FileLocator(__DIR__ . '/../../../../Resources/config/endpoint/client_registration'));
50
        $loader->load('initial_access_token.php');
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getNodeDefinition(NodeDefinition $node)
57
    {
58
        $node->children()
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...
59
            ->arrayNode($this->name())
60
            ->addDefaultsIfNotSet()
61
            ->canBeEnabled()
62
            ->validate()
63
                ->ifTrue(function ($config) {
64
                    return true === $config['enabled'] && empty($config['realm']);
65
                })
66
                ->thenInvalid('The option "realm" must be set.')
67
            ->end()
68
            ->validate()
69
                ->ifTrue(function ($config) {
70
                    return true === $config['enabled'] && empty($config['repository']);
71
                })
72
                ->thenInvalid('The option "repository" must be set.')
73
            ->end()
74
            ->validate()
75
                ->ifTrue(function ($config) {
76
                    return true === $config['enabled'] && $config['max_length'] < $config['min_length'];
77
                })
78
                ->thenInvalid('The option "max_length" must be greater than "min_length".')
79
            ->end()
80
            ->children()
81
                ->booleanNode('required')
82
                    ->defaultFalse()
83
                ->end()
84
                ->scalarNode('realm')
85
                    ->defaultNull()
86
                ->end()
87
                ->booleanNode('authorization_header')
88
                    ->defaultTrue()
89
                ->end()
90
                ->booleanNode('query_string')
91
                    ->defaultFalse()
92
                ->end()
93
                ->booleanNode('request_body')
94
                    ->defaultFalse()
95
                ->end()
96
                ->integerNode('min_length')
97
                    ->defaultValue(50)
98
                    ->min(0)
99
                ->end()
100
                ->integerNode('max_length')
101
                    ->defaultValue(100)
102
                    ->min(1)
103
                ->end()
104
                ->scalarNode('repository')
105
                    ->defaultNull()
106
                ->end()
107
            ->end();
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function prepend(ContainerBuilder $container, array $config): array
114
    {
115
        return [];
116
    }
117
}
118