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

Source/KeyManagement/JWKUriSource.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\Controller\JWKSetController;
17
use Jose\Bundle\JoseFramework\Controller\JWKSetControllerFactory;
18
use Jose\Bundle\JoseFramework\DependencyInjection\Source\Source;
19
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\Definition;
22
use Symfony\Component\DependencyInjection\Reference;
23
24
/**
25
 * Class JKUriSource.
26
 */
27
final class JWKUriSource implements Source
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function name(): string
33
    {
34
        return 'jwk_uris';
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function load(array $configs, ContainerBuilder $container)
41
    {
42
        foreach ($configs[$this->name()] as $name => $itemConfig) {
43
            $service_id = sprintf('jose.controller.%s', $name);
44
            $definition = new Definition(JWKSetController::class);
45
            $definition->setFactory([new Reference(JWKSetControllerFactory::class), 'create']);
46
            $definition->setArguments([new Reference($itemConfig['id']), $itemConfig['max_age']]);
47
            $definition->addTag('jose.jwk_uri.controller', ['path' => $itemConfig['path']]);
48
            $definition->addTag('controller.service_arguments');
49
            foreach ($itemConfig['tags'] as $id => $attributes) {
50
                $definition->addTag($id, $attributes);
51
            }
52
            $container->setDefinition($service_id, $definition);
53
        }
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getNodeDefinition(ArrayNodeDefinition $node)
60
    {
61
        $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...
62
            ->children()
63
                ->arrayNode('jwk_uris')
64
                    ->useAttributeAsKey('name')
65
                    ->prototype('array')
66
                        ->performNoDeepMerging()
67
                        ->children()
68
                            ->scalarNode('id')
69
                                ->info('The service ID of the Key Set to share.')
70
                                ->defaultNull()
71
                            ->end()
72
                            ->scalarNode('path')
73
                                ->info('To share the JWKSet, then set a valid path (e.g. "/jwkset.json").')
74
                                ->defaultNull()
75
                            ->end()
76
                            ->integerNode('max_age')
77
                                ->info('When share, this value indicates how many seconds the HTTP client should keep the key in cache. Default is 21600 = 6 hours.')
78
                                ->defaultValue(21600)
79
                            ->end()
80
                            ->arrayNode('tags')
81
                                ->info('A list of tags to be associated to the service.')
82
                                ->useAttributeAsKey('name')
83
                                ->treatNullLike([])
84
                                ->treatFalseLike([])
85
                                ->prototype('variable')->end()
86
                            ->end()
87
                        ->end()
88
                    ->end()
89
                ->end()
90
            ->end();
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function prepend(ContainerBuilder $container, array $config): array
97
    {
98
        return [];
99
    }
100
}
101