Completed
Pull Request — master (#55)
by Márk
06:32
created

DiscoveryPass   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
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 Http\Client\HttpClient;
6
use Http\HttplugBundle\Util\HttplugFactory;
7
use Http\Message\MessageFactory;
8
use Http\Message\StreamFactory;
9
use Http\Message\UriFactory;
10
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
13
use Symfony\Component\DependencyInjection\Reference;
14
15
/**
16
 * Adds fallback and discovery services.
17
 *
18
 * @author Márk Sági-Kazár <[email protected]>
19
 */
20
final class DiscoveryPass implements CompilerPassInterface
21
{
22
    /**
23
     * Fallback services and classes.
24
     *
25
     * @var array
26
     */
27
    private $services = [
28
        'client' => HttpClient::class,
29
        'message_factory' => MessageFactory::class,
30
        'uri_factory' => UriFactory::class,
31
        'stream_factory' => StreamFactory::class,
32
    ];
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function process(ContainerBuilder $container)
38
    {
39
        $useDiscovery = false;
40
41
        foreach ($this->services as $service => $class) {
42
            $serviceId = sprintf('httplug.%s.default', $service);
43
44
            if (false === $container->has($serviceId)) {
45
                // Register and create factory for the first time
46
                if (false === $useDiscovery) {
47
                    $this->registerFactory($container);
48
49
                    $factory = [
50
                        new Reference('httplug.factory'),
51
                        'find',
52
                    ];
53
54
                    $useDiscovery = true;
55
                }
56
57
                $definition = $container->register($serviceId, $class);
58
59
                $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...
60
                $definition->addArgument($class);
61
            }
62
        }
63
    }
64
65
    /**
66
     * @param ContainerBuilder $container
67
     *
68
     * @throws RuntimeException
69
     */
70
    private function registerFactory(ContainerBuilder $container)
71
    {
72
        if (false === $container->has('puli.discovery')) {
73
            throw new RuntimeException(
74
                'You need to install puli/symfony-bundle or add configuration at httplug.classes in order to use this bundle. Refer to http://docs.php-http/en/latest/integrations/index.html'
75
            );
76
        }
77
78
        $definition = $container->register('httplug.factory', HttplugFactory::class);
79
80
        $definition
81
            ->addArgument(new Reference('puli.discovery'))
82
            ->setPublic(false)
83
        ;
84
    }
85
}
86