RegisterSoapWebServicesPass   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 25.45 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 14
loc 55
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 23 3
A addWsseDefinition() 14 14 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 Soap webservices.
22
 *
23
 * @author Mohamed Amine Fattouch <[email protected]>
24
 */
25
class RegisterSoapWebServicesPass implements CompilerPassInterface
26
{
27
    /**
28
     * Register soap webservices definition.
29
     *
30
     * @param ContainerBuilder $container Instance of container.
31
     */
32
    public function process(ContainerBuilder $container)
33
    {
34
        if ($container->hasParameter('amf_web_services_client.soap.endpoints') === false) {
35
            return;
36
        }
37
38
        $endpoints = $container->getParameter('amf_web_services_client.soap.endpoints');
39
        foreach ($endpoints as $key => $endpoint) {
40
            $soapClient = $container->getDefinition('amf_web_services_client.soap')
41
                                    ->replaceArgument(0, $endpoint['wsdl'])
42
                                    ->replaceArgument(0, $endpoint['options']);
43
44
            $soapEndpoint = 'amf_web_services_client.soap.'.$key;
45
            $container->setDefinition(
46
                $soapEndpoint,
47
                new DefinitionDecorator('amf_web_services_client.soap.endpoint')
48
            )
49
                    ->setClass($endpoint['class'])
50
                    ->replaceArgument(0, $soapClient)
51
                    ->replaceArgument(1, $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...
52
                    ->replaceArgument(3, $endpoint['wsse']['enabled']);
53
        }
54
    }
55
56
    /**
57
     * Adds wsse defintion to container only if it's enabled..
58
     *
59
     * @param ContainerInterface $container
60
     * @param string             $key
61
     * @param array              $wsse
62
     *
63
     * @return Reference|null
64
     */
65 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...
66
    {
67
        if (($wsse['enabled'] === true)) {
68
            $soapWsse = 'amf_web_services_client.soap.wsse.'.$key;
69
            $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...
70
                    ->setDefinition($soapWsse, new DefinitionDecorator('amf_web_services_client.soap.wsse'))
71
                    ->replaceArgument(0, $wsse['wsse']['username'])
72
                    ->replaceArgument(1, $wsse['wsse']['password']);
73
74
            return new Reference($soapWsse);
75
        }
76
77
        return null;
78
    }
79
}
80