Completed
Pull Request — master (#55)
by Márk
28:20 queued 18:21
created

DiscoveryPass   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 66
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B process() 0 27 4
A registerFactory() 0 15 2
1
<?php
2
3
namespace Http\HttplugBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
/**
11
 * Adds fallback and discovery services.
12
 *
13
 * @author Márk Sági-Kazár <[email protected]>
14
 */
15
final class DiscoveryPass implements CompilerPassInterface
16
{
17
    /**
18
     * Fallback services and classes.
19
     *
20
     * @var array
21
     */
22
    private $services = [
23
        'client' => 'Http\Client\HttpClient',
24
        'message_factory' => 'Http\Message\MessageFactory',
25
        'uri_factory' => 'Http\Message\UriFactory',
26
        'stream_factory' => 'Http\Message\StreamFactory',
27
    ];
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function process(ContainerBuilder $container)
33
    {
34
        $useDiscovery = false;
35
36
        foreach ($this->services as $service => $class) {
37
            $serviceId = sprintf('httplug.%s.default', $service);
38
39
            if (false === $container->has($serviceId)) {
40
                // Register and create factory for the first time
41
                if (false === $useDiscovery) {
42
                    $this->registerFactory($container);
43
44
                    $factory = [
45
                        new Reference('httplug.factory'),
46
                        'find',
47
                    ];
48
49
                    $useDiscovery = true;
50
                }
51
52
                $definition = $container->register($serviceId, $class);
53
54
                $definition->setFactory($factory);
0 ignored issues
show
Bug introduced by
The variable $factory does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
55
                $definition->addArgument($class);
56
            }
57
        }
58
    }
59
60
    /**
61
     * @param ContainerBuilder $container
62
     *
63
     * @throws RuntimeException
64
     */
65
    private function registerFactory(ContainerBuilder $container)
66
    {
67
        if (false === $container->has('puli.discovery')) {
68
            throw new RuntimeException(
69
                'You need to install puli/symfony-bundle or add configuration at httplug.classes in order to use this bundle. Refer to http://some.doc'
70
            );
71
        }
72
73
        $definition = $container->register('httplug.factory', 'Http\HttplugBundle\Util\HttplugFactory');
74
75
        $definition
76
            ->addArgument(new Reference('puli.discovery'))
77
            ->setPublic(false)
78
        ;
79
    }
80
}
81