Completed
Push — master ( c7a8ed...7c53a3 )
by Florent
08:28
created

Keys::addConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 11
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
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 SpomkyLabs\JoseBundle\DependencyInjection\JWKSetSource;
13
14
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\Reference;
18
19
class Keys implements JWKSetSourceInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function create(ContainerBuilder $container, $id, array $config)
25
    {
26
        $definition = new Definition('Jose\Object\JWKSet');
27
        $definition->setFactory([
28
            new Reference('jose.factory.jwkset'),
29
            'createFromKeys',
30
        ]);
31
        $definition->setArguments([
32
            $this->fromKeysToReferences($config['id']),
33
        ]);
34
35
        $container->setDefinition($id, $definition);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getKeySet()
42
    {
43
        return 'keys';
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function addConfiguration(NodeDefinition $node)
50
    {
51
        $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...
52
            ->children()
53
                ->arrayNode('id')
54
                    ->prototype('scalar')
55
                    ->end()
56
                    ->isRequired()
57
                ->end()
58
            ->end();
59
    }
60
61
    /**
62
     * @param string[] $keys
63
     *
64
     * @return \Symfony\Component\DependencyInjection\Reference[]
65
     */
66
    private function fromKeysToReferences(array $keys)
67
    {
68
        $result = [];
69
        foreach ($keys as $key) {
70
            $result[] = new Reference(sprintf('jose.key.%s', $key));
71
        }
72
73
        return $result;
74
    }
75
}
76