CheckAmbiguousReferencesPass::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This is part of the symfonette/class-named-services package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Symfonette\ClassNamedServices;
14
15
use Symfonette\ClassNamedServices\Exception\AmbiguousReferenceException;
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
class CheckAmbiguousReferencesPass implements CompilerPassInterface
22
{
23
    /** @var ContainerBuilder */
24
    private $container;
25
26 6
    public function process(ContainerBuilder $container)
27
    {
28 6
        $this->container = $container;
29 6
        foreach ($container->getDefinitions() as $id => $definition) {
30 6
            $this->processArguments($id, $definition->getArguments());
31 6
            $this->processArguments($id, $definition->getMethodCalls());
32 6
            $this->processArguments($id, $definition->getProperties());
33 6
            $this->processFactory($id, $definition->getFactory());
0 ignored issues
show
Unused Code introduced by
The call to CheckAmbiguousReferencesPass::processFactory() has too many arguments starting with $definition->getFactory().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
34
        }
35 5
    }
36
37 6
    private function processArguments($id, array $arguments)
38
    {
39 6
        foreach ($arguments as $argument) {
40 3
            $definition = $argument;
41
42 3
            if (is_array($argument)) {
43 1
                $this->processArguments($id, $argument);
44
            } elseif ($argument instanceof Reference) {
45
                try {
46 1
                    $definition = $this->container->findDefinition((string) $argument);
47
                } catch (ServiceNotFoundException $e) {
48
                    continue;
49
                }
50
            }
51
52 3
            if ($definition instanceof AmbiguousDefinition) {
53 3
                throw new AmbiguousReferenceException($definition->getClass(), $id, $definition->getServices());
54
            }
55
        }
56 6
    }
57
58 6
    private function processFactory($factory)
59
    {
60 6
        if (null === $factory || !is_array($factory) || !$factory[0] instanceof Reference) {
61 6
            return;
62
        }
63
64
        try {
65
            $definition = $this->container->findDefinition($id = (string) $factory[0]);
66
        } catch (ServiceNotFoundException $e) {
67
            return;
68
        }
69
70
        if ($definition instanceof AmbiguousDefinition) {
71
            throw new AmbiguousReferenceException($definition->getClass(), $id, $definition->getServices());
72
        }
73
    }
74
}
75