Completed
Push — 6.0 ( eb641c...92cb73 )
by Ruud
148:12 queued 131:31
created

KunstmaanTranslatorCompilerPass::process()   D

Complexity

Conditions 9
Paths 48

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 4.909
c 0
b 0
f 0
cc 9
eloc 24
nc 48
nop 1
1
<?php
2
3
namespace Kunstmaan\TranslatorBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
class KunstmaanTranslatorCompilerPass implements CompilerPassInterface
11
{
12
    public function process(ContainerBuilder $container)
13
    {
14
        $loaderRefs = array();
15
        $loaderAliases = array();
0 ignored issues
show
Unused Code introduced by
$loaderAliases is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
16
        $exporterRefs = array();
17
18
        foreach ($container->findTaggedServiceIds('translation.loader', true) as $id => $attributes) {
19
            $loaderRefs[$id] = new Reference($id);
20
            $loaders[$id][] = $attributes[0]['alias'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$loaders was never initialized. Although not strictly required by PHP, it is generally a good practice to add $loaders = 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...
21
            if (isset($attributes[0]['legacy-alias'])) {
22
                $loaders[$id][] = $attributes[0]['legacy-alias'];
23
            }
24
        }
25
26
        if ($container->hasDefinition('kunstmaan_translator.service.importer.importer')) {
27
            $definition = $container->getDefinition('kunstmaan_translator.service.importer.importer');
28
            foreach ($loaders as $id => $formats) {
0 ignored issues
show
Bug introduced by
The variable $loaders 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...
29
                foreach ($formats as $format) {
30
                    $definition->addMethodCall('addLoader', array($format, $loaderRefs[$id]));
31
                }
32
            }
33
        }
34
35
        if ($container->hasDefinition('kunstmaan_translator.service.translator.translator')) {
36
            //Create custom ServiceLocator to inject in the translator
37
            $serviceIds = array_merge($loaderRefs, ['request_stack' => new Reference('request_stack')]);
38
            $serviceLocator = ServiceLocatorTagPass::register($container, $serviceIds);
39
40
41
            $container->getDefinition('kunstmaan_translator.service.translator.translator')
42
                ->replaceArgument(0, $serviceLocator)
43
                ->replaceArgument(3, $loaders);
44
        }
45
46
        // add all exporter into the translation exporter
47
        foreach ($container->findTaggedServiceIds('translation.exporter') as $id => $attributes) {
48
            $exporterRefs[$attributes[0]['alias']] = new Reference($id);
49
        }
50
51
        if ($container->hasDefinition('kunstmaan_translator.service.exporter.exporter')) {
52
            $container->getDefinition('kunstmaan_translator.service.exporter.exporter')->addMethodCall('setExporters', array($exporterRefs));
53
        }
54
    }
55
}
56