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

Source/KeyManagement/JWKSource.php (3 issues)

Labels

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\JWKSource\JWKSource as JWKSourceInterface;
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 JWKSource.
25
 */
26
final class JWKSource implements Source
27
{
28
    /**
29
     * @var null|JWKSourceInterface[]
30
     */
31
    private $jwkSources = null;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function name(): string
37
    {
38
        return 'keys';
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function load(array $configs, ContainerBuilder $container)
45
    {
46
        $sources = $this->getJWKSources();
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', $name, $sourceConfig);
0 ignored issues
show
The method create() does not seem to exist on object<Jose\Bundle\JoseF...eyManagement\JWKSource>.

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...
52
                } else {
53
                    throw new \LogicException(sprintf('The JWK 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('keys')
67
                    ->useAttributeAsKey('name')
68
                    ->prototype('array')
69
                        ->performNoDeepMerging()
70
                        ->children();
71
        foreach ($this->getJWKSources() as $name => $source) {
72
            $sourceNode = $sourceNodeBuilder->arrayNode($name)->canBeUnset();
73
            $source->addConfiguration($sourceNode);
0 ignored issues
show
The method addConfiguration() does not seem to exist on object<Jose\Bundle\JoseF...eyManagement\JWKSource>.

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...
74
        }
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function prepend(ContainerBuilder $container, array $config): array
81
    {
82
        return [];
83
    }
84
85
    /**
86
     * @return JWKSource[]
87
     */
88
    private function getJWKSources(): array
89
    {
90
        if (null !== $this->jwkSources) {
91
            return $this->jwkSources;
92
        }
93
94
        // load bundled adapter factories
95
        $tempContainer = new ContainerBuilder();
96
        $loader = new YamlFileLoader($tempContainer, new FileLocator(__DIR__.'/../../../Resources/config'));
97
        $loader->load('jwk_sources.yml');
98
        $services = $tempContainer->findTaggedServiceIds('jose.jwk_source');
99
        $jwkSources = [];
100
        foreach (array_keys($services) as $id) {
101
            $factory = $tempContainer->get($id);
102
            $jwkSources[str_replace('-', '_', $factory->getKey())] = $factory;
103
        }
104
105
        $this->jwkSources = $jwkSources;
106
107
        return $jwkSources;
108
    }
109
}
110