Completed
Push — master ( e3dddb...97971f )
by Daniel
07:06 queued 04:32
created

ObjectAgentPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 37 5
1
<?php
2
3
namespace Psi\Bundle\ObjectAgent\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
class ObjectAgentPass implements CompilerPassInterface
11
{
12
    public function process(ContainerBuilder $container)
13
    {
14
        if (!$container->has('psi_object_agent.agent_finder')) {
15
            return;
16
        }
17
18
        $taggedIds = $container->findTaggedServiceIds('psi_object_agent.agent');
19
        $registryDef = $container->getDefinition('psi_object_agent.agent_finder');
20
        $enabledAgents = $container->getParameter('psi_object_agent.enabled_agents');
21
22
        foreach ($taggedIds as $serviceId => $attributes) {
23
            $attributes = $attributes[0];
24
            if (!isset($attributes['alias'])) {
25
                throw new InvalidArgumentException(sprintf(
26
                    $this->context . ' "%s" has no "alias" attribute in its tag',
0 ignored issues
show
Bug introduced by
The property context does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
                    $serviceId
28
                ));
29
            }
30
31
            $alias = $attributes['alias'];
32
33
            $agents[$alias] = new Reference($serviceId);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$agents was never initialized. Although not strictly required by PHP, it is generally a good practice to add $agents = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
34
        }
35
36
        if ($diff = array_diff($enabledAgents, array_keys($agents))) {
0 ignored issues
show
Bug introduced by
The variable $agents 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
            throw new \RuntimeException(sprintf(
38
                'Unknown agent(s) "%s". Known agents: "%s"',
39
                implode('", "', $diff), implode('", "', array_keys($agents))
40
            ));
41
        }
42
43
        $agents = array_filter($agents, function ($key) use ($enabledAgents) {
44
            return in_array($key, $enabledAgents);
45
        }, ARRAY_FILTER_USE_KEY);
46
47
        $registryDef->replaceArgument(0, $agents);
48
    }
49
}
50