CheckAmbiguousReferencesPass   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 68.97%

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 3
dl 0
loc 54
ccs 20
cts 29
cp 0.6897
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 10 2
B processArguments() 0 20 6
B processFactory() 0 16 6
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