Completed
Pull Request — master (#55)
by Márk
09:03
created

DiscoveryPass::process()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.5806
cc 4
eloc 14
nc 4
nop 1
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
final class DiscoveryPass implements CompilerPassInterface
11
{
12
    /**
13
     * Fallback services and classes.
14
     *
15
     * @var array
16
     */
17
    private $services = [
18
        'client' => 'Http\Client\HttpClient',
19
        'message_factory' => 'Http\Message\MessageFactory',
20
        'uri_factory' => 'Http\Message\UriFactory',
21
        'stream_factory' => 'Http\Message\StreamFactory',
22
    ];
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function process(ContainerBuilder $container)
28
    {
29
        $useDiscovery = false;
30
31
        foreach ($this->services as $service => $class) {
32
            $serviceId = sprintf('httplug.%s.default', $service);
33
34
            if (false === $container->has($serviceId)) {
35
                // Register and create factory for the first time
36
                if (false === $useDiscovery) {
37
                    $this->registerFactory($container);
38
39
                    $factory = [
40
                        new Reference('httplug.factory'),
41
                        'find',
42
                    ];
43
44
                    $useDiscovery = true;
45
                }
46
47
                $definition = $container->register($serviceId, $class);
48
49
                $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...
50
                $definition->addArgument($class);
51
            }
52
        }
53
    }
54
55
    /**
56
     * @param ContainerBuilder $container
57
     *
58
     * @throws RuntimeException
59
     */
60
    private function registerFactory(ContainerBuilder $container)
61
    {
62
        if (false === $container->has('puli.discovery')) {
63
            throw new RuntimeException(
64
                'You need to install puli/symfony-bundle or add configuration at httplug.classes in order to use this bundle. Refer to http://some.doc'
65
            );
66
        }
67
68
        $definition = $container->register('httplug.factory', 'Http\HttplugBundle\Util\HttplugFactory');
69
70
        $definition
71
            ->addArgument(new Reference('puli.discovery'))
72
            ->setPublic(false)
73
        ;
74
    }
75
}
76