Failed Conditions
Pull Request — master (#31)
by Florent
03:43
created

OpenIdConnectSource::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 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\Server\DependencyInjection\Source\OpenIdConnect;
15
16
use Fluent\PhpConfigFileLoader;
17
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\ActionableSource;
18
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\SourceInterface;
19
use Symfony\Component\Config\FileLocator;
20
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
23
final class OpenIdConnectSource extends ActionableSource
24
{
25
    /**
26
     * @var SourceInterface[]
27
     */
28
    private $subSources;
29
30
    /**
31
     * OpenIdConnectSource constructor.
32
     */
33
    public function __construct()
34
    {
35
        $this->subSources = [
36
            new UserinfoEndpointSource(),
37
            new IdTokenSource(),
38
            new AuthorizationEndpointIdTokenHintSource(),
39
            new PairwiseSubjectSource(),
40
        ];
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function name(): string
47
    {
48
        return 'openid_connect';
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    protected function continueLoading(string $path, ContainerBuilder $container, array $config)
55
    {
56
        foreach ($this->subSources as $source) {
57
            $source->load($path, $container, $config);
58
        }
59
        foreach (['claims_supported', 'claims_locales_supported'] as $k) {
60
            $container->setParameter($path.'.'.$k, $config[$k]);
61
        }
62
        $loader = new PhpConfigFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/openid_connect'));
63
        $loader->load('openid_connect.php');
64
    }
65
66
    public function prepend(array $bundleConfig, string $path, ContainerBuilder $container)
67
    {
68
        parent::prepend($bundleConfig, $path, $container);
69
        foreach ($this->subSources as $source) {
70
            $source->prepend($bundleConfig, $path.'['.$this->name().']', $container);
71
        }
72
    }
73
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    protected function continueConfiguration(NodeDefinition $node)
79
    {
80
        parent::continueConfiguration($node);
81
        $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...
82
            ->children()
83
                ->arrayNode('claims_supported')
84
                    ->info('Supported claims.')
85
                    ->useAttributeAsKey('name')
86
                    ->prototype('scalar')->end()
87
                    ->treatNullLike([])
88
                ->end()
89
                ->arrayNode('claims_locales_supported')
90
                    ->info('Supported claims locales.')
91
                    ->useAttributeAsKey('name')
92
                    ->prototype('scalar')->end()
93
                    ->treatNullLike([])
94
                ->end()
95
            ->end();
96
        foreach ($this->subSources as $source) {
97
            $source->addConfiguration($node);
98
        }
99
    }
100
}
101