Failed Conditions
Push — v7 ( e446d4...9b9adb )
by Florent
02:29
created

JWSLoader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 5
dl 0
loc 67
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 4 1
A createService() 0 14 1
B getNodeDefinition() 0 27 1
A prepend() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 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\Signature\DependencyInjection\Source;
15
16
use Jose\Bundle\JoseFramework\DependencyInjection\Source\SourceInterface;
17
use Jose\Component\Signature\JWSLoaderFactory;
18
use Jose\Component\Signature\JWSLoader as JWSLoaderService;
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
final class JWSLoader implements SourceInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function name(): string
30
    {
31
        return 'jws_loaders';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function createService(string $name, array $config, ContainerBuilder $container)
38
    {
39
        $service_id = sprintf('jose.jws_loader.%s', $name);
40
        $definition = new Definition(JWSLoaderService::class);
41
        $definition
42
            ->setFactory([new Reference(JWSLoaderFactory::class), 'create'])
43
            ->setArguments([
44
                $config['signature_algorithms'],
45
                $config['header_checkers'],
46
            ])
47
            ->setPublic($config['is_public']);
48
49
        $container->setDefinition($service_id, $definition);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getNodeDefinition(ArrayNodeDefinition $node)
56
    {
57
        $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...
58
            ->children()
59
                ->arrayNode($this->name())
60
                    ->useAttributeAsKey('name')
61
                    ->prototype('array')
62
                        ->children()
63
                            ->booleanNode('is_public')
64
                                ->info('If true, the service will be public, else private.')
65
                                ->defaultTrue()
66
                            ->end()
67
                            ->arrayNode('signature_algorithms')
68
                                ->useAttributeAsKey('name')
69
                                ->isRequired()
70
                                ->prototype('scalar')->end()
71
                            ->end()
72
                            ->arrayNode('header_checkers')
73
                                ->useAttributeAsKey('name')
74
                                ->isRequired()
75
                                ->prototype('scalar')->end()
76
                            ->end()
77
                        ->end()
78
                    ->end()
79
                ->end()
80
            ->end();
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function prepend(ContainerBuilder $container, array $config): ?array
87
    {
88
        return null;
89
    }
90
}
91