RegisterRestWebServicesPass   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 40.26 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 4
dl 31
loc 77
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 17 3
A addWsseDefinition() 15 15 2
A addUrlDefinition() 16 16 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the AMFWebServicesClientBundle package.
5
 *
6
 * (c) Amine Fattouch <http://github.com/fattouchsquall>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace AMF\WebServicesClientBundle\DependencyInjection\Compiler;
13
14
use Symfony\Component\DependencyInjection\ContainerBuilder;
15
use Symfony\Component\DependencyInjection\DefinitionDecorator;
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\Reference;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
20
/**
21
 * Pass of compilation to register Rest webservices.
22
 *
23
 * @author Mohamed Amine Fattouch <[email protected]>
24
 */
25
class RegisterRestWebServicesPass implements CompilerPassInterface
26
{
27
28
    /**
29
     * Register rest webservices definition.
30
     *
31
     * @param ContainerBuilder $container Instance of container.
32
     */
33
    public function process(ContainerBuilder $container)
34
    {
35
        if ($container->hasParameter('amf_web_services_client.rest.endpoints') === false) {
36
            return;
37
        }
38
39
        $endpoints = $container->getParameter('amf_web_services_client.rest.endpoints');
40
        foreach ($endpoints as $key => $endpoint) {
41
            $restEndpoint = 'amf_web_services_client.rest.'.$key;
42
            $container->setDefinition($restEndpoint, new DefinitionDecorator('amf_web_services_client.rest.endpoint'))
43
                    ->setClass($endpoint['class'])
44
                    ->replaceArgument(3, $this->addUrlDefinition($container, $key, $url))
0 ignored issues
show
Bug introduced by
The variable $url does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
45
                    ->replaceArgument(4, $this->addWsseDefinition($container, $key, $wsse))
0 ignored issues
show
Bug introduced by
The variable $wsse does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
46
                    ->replaceArgument(5, $endpoint['request_format'])
47
                    ->replaceArgument(6, $endpoint['response_format']);
48
        }
49
    }
50
51
    /**
52
     * Adds wsse defintion to container only if it's enabled..
53
     *
54
     * @param ContainerInterface $container
55
     * @param string             $key
56
     * @param array              $wsse
57
     *
58
     * @return Reference|null
59
     */
60 View Code Duplication
    private function addWsseDefinition(ContainerInterface $container, $key, array $wsse)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
    {
62
        if (($wsse['enabled'] === true)) {
63
            $restWsse = 'amf_web_services_client.rest.wsse.'.$key;
64
            $container
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Depend...tion\ContainerInterface as the method setDefinition() does only exist in the following implementations of said interface: Container14\ProjectServiceContainer, Symfony\Component\Depend...ection\ContainerBuilder, Symfony\Component\Depend...\Tests\ProjectContainer.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
65
                    ->setDefinition($restWsse, new DefinitionDecorator('amf_web_services_client.rest.wsse'))
66
                    ->replaceArgument(0, $wsse['username'])
67
                    ->replaceArgument(1, $wsse['password'])
68
                    ->replaceArgument(2, $wsse['options']);
69
70
            return new Reference($restWsse);
71
        }
72
73
        return null;
74
    }
75
76
    /**
77
     * Adds url defintion to container only if it's enabled.
78
     *
79
     * @param ContainerInterface $container
80
     * @param string             $key
81
     * @param array              $wsse
0 ignored issues
show
Bug introduced by
There is no parameter named $wsse. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
82
     *
83
     * @return Reference|null
84
     */
85 View Code Duplication
    private function addUrlDefinition(ContainerInterface $container, $key, $url)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87
        if (($url['enabled'] === true)) {
88
            $restUrl = 'amf_web_services_client.rest.url.'.$key;
89
            $container
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Depend...tion\ContainerInterface as the method setDefinition() does only exist in the following implementations of said interface: Container14\ProjectServiceContainer, Symfony\Component\Depend...ection\ContainerBuilder, Symfony\Component\Depend...\Tests\ProjectContainer.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
90
                    ->setDefinition($restUrl, new DefinitionDecorator('amf_web_services_client.rest.url'))
91
                    ->replaceArgument(0, $url['hostname'])
92
                    ->replaceArgument(1, $url['scheme'])
93
                    ->replaceArgument(2, $url['port'])
94
                    ->replaceArgument(3, $url['query_delimiter']);
95
96
            return new Reference($restUrl);
97
        }
98
99
        return null;
100
    }
101
}
102