Failed Conditions
Push — ng ( e90992...13fb6e )
by Florent
17:32
created

AuthorizationEndpointSource::getNodeDefinition()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 37
nc 1
nop 1
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;
15
16
use Fluent\PhpConfigFileLoader;
17
use OAuth2Framework\Bundle\DependencyInjection\Component\Component;
18
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
19
use Symfony\Component\Config\FileLocator;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
22
final class AuthorizationEndpointSource implements Component
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: load, prepend
Loading history...
23
{
24
    /**
25
     * AuthorizationEndpointSource constructor.
26
     */
27
    public function __construct()
28
    {
29
        $this->addSubSource(new AuthorizationEndpointRequestObjectSource());
0 ignored issues
show
Bug introduced by
The method addSubSource() does not seem to exist on object<OAuth2Framework\B...rizationEndpointSource>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
        $this->addSubSource(new AuthorizationEndpointResponseModeSource());
0 ignored issues
show
Bug introduced by
The method addSubSource() does not seem to exist on object<OAuth2Framework\B...rizationEndpointSource>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31
        $this->addSubSource(new AuthorizationEndpointPreConfiguredAuthorizationSource());
0 ignored issues
show
Bug introduced by
The method addSubSource() does not seem to exist on object<OAuth2Framework\B...rizationEndpointSource>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected function continueLoading(string $path, ContainerBuilder $container, array $config)
38
    {
39
        foreach ($config as $k => $v) {
40
            $container->setParameter($path.'.'.$k, $v);
41
        }
42
43
        $loader = new PhpConfigFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/endpoint'));
44
        $loader->load('authorization.php');
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function name(): string
51
    {
52
        return 'authorization';
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getNodeDefinition(NodeDefinition $node)
59
    {
60
61
        $node
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...
62
            ->children()
63
                ->scalarNode('path')
64
                    ->info('The path to the authorization endpoint.')
65
                    ->defaultValue('/authorize')
66
                ->end()
67
                ->scalarNode('login_route_name')
68
                    ->info('The name of the login route. Will be converted into URL and used to redirect the user if not logged in. If you use "FOSUserBundle", the route name should be "fos_user_security_login".')
69
                ->end()
70
                ->arrayNode('login_route_parameters')
71
                    ->info('Parameters associated to the login route (if needed).')
72
                    ->useAttributeAsKey('name')
73
                    ->prototype('scalar')->end()
74
                    ->treatNullLike([])
75
                ->end()
76
                ->scalarNode('template')
77
                    ->info('The consent page template.')
78
                    ->defaultValue('@OAuth2FrameworkBundle/authorization/authorization.html.twig')
79
                ->end()
80
                ->scalarNode('allow_token_type_parameter')
81
                    ->info('If true the "token_type" parameter is allowed, else it will be ignored.')
82
                    ->defaultFalse()
83
                ->end()
84
                ->scalarNode('enforce_state')
85
                    ->info('If true the "state" parameter is mandatory (highly recommended).')
86
                    ->defaultFalse()
87
                ->end()
88
                ->scalarNode('enforce_secured_redirect_uri')
89
                    ->info('If true only secured redirect URIs are allowed.')
90
                    ->defaultTrue()
91
                ->end()
92
                ->scalarNode('enforce_redirect_uri_storage')
93
                    ->info('If true redirect URIs must be registered by the client to be used.')
94
                    ->defaultTrue()
95
                ->end()
96
            ->end();
97
    }
98
}
99