Failed Conditions
Push — v7 ( 477009...5356df )
by Florent
03:33
created

JWKSet::createDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Bundle\JoseFramework\DependencyInjection\Source\JWKSource;
13
14
use Jose\Bundle\JoseFramework\DependencyInjection\Source\AbstractSource;
15
use Jose\Component\Core\JWKFactory;
16
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
final class JWKSet extends AbstractSource implements JWKSourceInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function createDefinition(ContainerBuilder $container, array $config): Definition
27
    {
28
        $definition = new Definition(JWK::class);
29
        $definition->setFactory([
30
            new Reference(JWKFactory::class),
31
            'createFromKeySet',
32
        ]);
33
        $definition->setArguments([new Reference($config['key_set']), $config['index']]);
34
35
        return $definition;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getKey(): string
42
    {
43
        return 'jwkset';
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function addConfiguration(NodeDefinition $node)
50
    {
51
        parent::addConfiguration($node);
52
        $node
1 ignored issue
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...
53
            ->children()
54
                ->scalarNode('key_set')
55
                    ->info('The key set service.')
56
                    ->isRequired()->end()
57
                ->integerNode('index')
58
                    ->info('The index of the key in the key set.')
59
                    ->isRequired()
60
                ->end()
61
            ->end();
62
    }
63
}
64