Failed Conditions
Push — master ( d40a11...28d61e )
by Florent
07:16
created

Source/KeyManagement/JWKSetSource.php (1 issue)

strict.coding_against_specific_subtype

Bug Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 Jose\Bundle\JoseFramework\DependencyInjection\Source\KeyManagement;
15
16
use Jose\Bundle\JoseFramework\DependencyInjection\Source\KeyManagement\JWKSetSource\JWKSetSource as JWKSetSourceInterface;
17
use Jose\Bundle\JoseFramework\DependencyInjection\Source\Source;
18
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
19
use Symfony\Component\Config\FileLocator;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
22
23
/**
24
 * Class JWKSetSource.
25
 */
26
final class JWKSetSource implements Source
27
{
28
    /**
29
     * @var null|JWKSetSourceInterface[]
30
     */
31
    private $jwkset_sources = null;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function name(): string
37
    {
38
        return 'key_sets';
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function load(array $configs, ContainerBuilder $container)
45
    {
46
        $sources = $this->getJWKSetSources();
47
        foreach ($configs[$this->name()] as $name => $itemConfig) {
48
            foreach ($itemConfig as $sourceName => $sourceConfig) {
49
                if (array_key_exists($sourceName, $sources)) {
50
                    $source = $sources[$sourceName];
51
                    $source->create($container, 'key_set', $name, $sourceConfig);
52
                } else {
53
                    throw new \LogicException(sprintf('The JWKSet definition "%s" is not configured.', $name));
54
                }
55
            }
56
        }
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getNodeDefinition(ArrayNodeDefinition $node)
63
    {
64
        $sourceNodeBuilder = $node
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method performNoDeepMerging() 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...
65
            ->children()
66
                ->arrayNode('key_sets')
67
                    ->useAttributeAsKey('name')
68
                    ->prototype('array')
69
                        ->performNoDeepMerging()
70
                        ->children();
71
        foreach ($this->getJWKSetSources() as $name => $source) {
72
            $sourceNode = $sourceNodeBuilder->arrayNode($name)->canBeUnset();
73
            $source->addConfiguration($sourceNode);
74
        }
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function prepend(ContainerBuilder $container, array $config): array
81
    {
82
        return [];
83
    }
84
85
    /**
86
     * @return JWKSetSource[]
87
     */
88
    private function getJWKSetSources(): array
89
    {
90
        if (null !== $this->jwkset_sources) {
91
            return $this->jwkset_sources;
92
        }
93
94
        // load bundled adapter factories
95
        $tempContainer = new ContainerBuilder();
96
        $loader = new YamlFileLoader($tempContainer, new FileLocator(__DIR__.'/../../../Resources/config'));
97
        $loader->load('jwkset_sources.yml');
98
99
        $services = $tempContainer->findTaggedServiceIds('jose.jwkset_source');
100
        $jwkset_sources = [];
101
        foreach (array_keys($services) as $id) {
102
            $factory = $tempContainer->get($id);
103
            $jwkset_sources[str_replace('-', '_', $factory->getKeySet())] = $factory;
104
        }
105
106
        return $this->jwkset_sources = $jwkset_sources;
107
    }
108
}
109