Completed
Push — master ( 8c5b1a...9a26dd )
by Tomáš
17:38
created

getAutowireTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of Symplify
5
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
6
 */
7
8
namespace Symplify\DefaultAutowire\DependencyInjection\Compiler;
9
10
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
use Symplify\DefaultAutowire\Config\Definition\ConfigurationResolver;
13
14
final class DefaultAutowireTypesCompilerPass implements CompilerPassInterface
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 2
    public function process(ContainerBuilder $containerBuilder)
20
    {
21 2
        $autowireTypes = $this->getAutowireTypes($containerBuilder);
22 2
        foreach ($autowireTypes as $type => $serviceName) {
23 2
            if (!$containerBuilder->has($serviceName)) {
24 2
                continue;
25
            }
26
27 2
            if ($containerBuilder->hasAlias($serviceName)) {
28 1
                $serviceName = $containerBuilder->getAlias($serviceName);
29
            }
30
31 2
            $containerBuilder->getDefinition($serviceName)
0 ignored issues
show
Bug introduced by
It seems like $serviceName defined by $containerBuilder->getAlias($serviceName) on line 28 can also be of type object<Symfony\Component...endencyInjection\Alias>; however, Symfony\Component\Depend...uilder::getDefinition() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
32 2
                ->setAutowiringTypes([$type]);
33
        }
34 2
    }
35
36
    /**
37
     * @return string[]
38
     */
39 2
    private function getAutowireTypes(ContainerBuilder $containerBuilder)
40
    {
41 2
        $config = (new ConfigurationResolver())->resolveFromContainerBuilder($containerBuilder);
42
43 2
        return $config['autowire_types'];
44
    }
45
}
46