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

CertificateFile   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createDefinition() 0 14 1
A getKey() 0 4 1
A addConfiguration() 0 13 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 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 CertificateFile 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
            'createFromCertificateFile',
32
        ]);
33
        $definition->setArguments([
34
            $config['path'],
35
            $config['additional_values'],
36
        ]);
37
38
        return $definition;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getKey(): string
45
    {
46
        return 'certificate';
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function addConfiguration(NodeDefinition $node)
53
    {
54
        parent::addConfiguration($node);
55
        $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...
56
            ->children()
57
                ->scalarNode('path')->isRequired()->end()
58
                ->arrayNode('additional_values')
59
                    ->defaultValue([])
60
                    ->useAttributeAsKey('key')
61
                    ->prototype('variable')->end()
62
                ->end()
63
            ->end();
64
    }
65
}
66