Completed
Push — master ( 28dda4...aabe55 )
by Craig
06:13
created

HookCollectorPass::process()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 16
nc 14
nop 1
dl 0
loc 30
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
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 Zikula\Bundle\HookBundle\DependencyInjection\Compiler;
13
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Reference;
17
18
class HookCollectorPass implements CompilerPassInterface
19
{
20
    public function process(ContainerBuilder $container)
21
    {
22
        if (!$container->hasDefinition('zikula_hook_bundle.collector.hook_collector')) {
23
            return;
24
        }
25
26
        $definition = $container->getDefinition('zikula_hook_bundle.collector.hook_collector');
27
28
        foreach ($container->findTaggedServiceIds('zikula.hook_provider') as $id => $tagParameters) {
29
            foreach ($tagParameters as $tagParameter) {
30
                if (!isset($tagParameter['areaName'])) {
31
                    throw new \InvalidArgumentException(sprintf('Service "%s" must define the "areaName" attribute on "zikula.hook_provider" tags.', $id));
32
                }
33
                $areaName = $tagParameter['areaName'];
34
            }
35
36
            $definition->addMethodCall('addProvider', [$areaName, $id, new Reference($id)]);
0 ignored issues
show
Bug introduced by
The variable $areaName 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...
37
        }
38
39
        foreach ($container->findTaggedServiceIds('zikula.hook_subscriber') as $id => $tagParameters) {
40
            foreach ($tagParameters as $tagParameter) {
41
                if (!isset($tagParameter['areaName'])) {
42
                    throw new \InvalidArgumentException(sprintf('Service "%s" must define the "areaName" attribute on "zikula.hook_subscriber" tags.', $id));
43
                }
44
                $areaName = $tagParameter['areaName'];
45
            }
46
47
            $definition->addMethodCall('addSubscriber', [$areaName, new Reference($id)]);
48
        }
49
    }
50
}
51