Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — master (#291)
by Jérémiah
17:30
created

autowireSolutionImplementingContainerAwareInterface()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 16
cts 16
cp 1
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 15
nc 2
nop 2
crap 4
1
<?php
2
3
namespace Overblog\GraphQLBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Definition;
9
use Symfony\Component\DependencyInjection\Reference;
10
11
abstract class TaggedServiceMappingPass implements CompilerPassInterface
12
{
13 30
    private function getTaggedServiceMapping(ContainerBuilder $container, $tagName)
14
    {
15 30
        $serviceMapping = [];
16
17 30
        $taggedServices = $container->findTaggedServiceIds($tagName, true);
18 30
        $isType = TypeTaggedServiceMappingPass::TAG_NAME === $tagName;
19
20 30
        foreach ($taggedServices as $id => $tags) {
21 30
            $className = $container->findDefinition($id)->getClass();
22 30
            foreach ($tags as $attributes) {
23 30
                $this->checkRequirements($id, $attributes);
24 28
                $attributes = self::resolveAttributes($attributes, $id, !$isType);
25 28
                $solutionID = $className;
26
27 28
                if (!$isType && '__invoke' !== $attributes['method']) {
28 17
                    $solutionID = sprintf('%s::%s', $className, $attributes['method']);
29
                }
30
31 28
                if (!isset($serviceMapping[$solutionID])) {
32 28
                    $serviceMapping[$solutionID] = $attributes;
33
                }
34
35 28
                if (isset($attributes['alias']) && $solutionID !== $attributes['alias']) {
36 28
                    $serviceMapping[$solutionID]['aliases'][] = $attributes['alias'];
37
                }
38
            }
39
        }
40
41 28
        return $serviceMapping;
42
    }
43
44 30
    public function process(ContainerBuilder $container)
45
    {
46 30
        $mapping = $this->getTaggedServiceMapping($container, $this->getTagName());
47 28
        $resolverDefinition = $container->findDefinition($this->getResolverServiceID());
48
49 28
        foreach ($mapping as $solutionID => $attributes) {
50 28
            $attributes['aliases'] = array_unique($attributes['aliases']);
51 28
            $aliases = $attributes['aliases'];
52 28
            $serviceID = $attributes['id'];
53
54 28
            $solutionDefinition = $container->findDefinition($serviceID);
55
            // make solution service public to improve lazy loading
56 28
            $solutionDefinition->setPublic(true);
57 28
            $this->autowireSolutionImplementingContainerAwareInterface($solutionDefinition, empty($attributes['generated']));
58
59 28
            $resolverDefinition->addMethodCall(
60 28
                'addSolution',
61 28
                [$solutionID, [[new Reference('service_container'), 'get'], [$serviceID]], $aliases, $attributes]
62
            );
63
        }
64 28
    }
65
66 30
    protected function checkRequirements($id, array $tag)
67
    {
68 30 View Code Duplication
        if (isset($tag['alias']) && !is_string($tag['alias'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69 1
            throw new \InvalidArgumentException(
70 1
                sprintf('Service tagged "%s" must have valid "alias" argument.', $id)
71
            );
72
        }
73 29
    }
74
75
    /**
76
     * @param array  $attributes
77
     * @param string $id
78
     * @param bool   $withMethod
79
     *
80
     * @return array
81
     */
82 28
    private static function resolveAttributes(array $attributes, $id, $withMethod)
83
    {
84 28
        $default = ['id' => $id, 'aliases' => []];
85 28
        if ($withMethod) {
86 28
            $default['method'] = '__invoke';
87
        }
88 28
        $attributes = array_replace($default, $attributes);
89
90 28
        return $attributes;
91
    }
92
93
    /**
94
     * @param Definition $solutionDefinition
95
     * @param bool       $isGenerated
96
     */
97 28
    private function autowireSolutionImplementingContainerAwareInterface(Definition $solutionDefinition, $isGenerated)
98
    {
99 28
        $methods = array_map(
100 28
            function ($methodCall) {
101 2
                return $methodCall[0];
102 28
            },
103 28
            $solutionDefinition->getMethodCalls()
104
        );
105
        if (
106 28
            $isGenerated
107 28
            && is_subclass_of($solutionDefinition->getClass(), ContainerAwareInterface::class)
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Symfony\Component\Depen...erAwareInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
108 28
            && !in_array('setContainer', $methods)
109
        ) {
110 2
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
111 2
                sprintf(
112 2
                    'Autowire method "%s::setContainer" for custom tagged (type, resolver or mutation) services is deprecated as of 0.9 and will be removed in 1.0.',
113 2
                    ContainerAwareInterface::class
114
                ),
115 2
                E_USER_DEPRECATED
116
            );
117 2
            $solutionDefinition->addMethodCall('setContainer', [new Reference('service_container')]);
118
        }
119 28
    }
120
121
    abstract protected function getTagName();
122
123
    /**
124
     * @return string
125
     */
126
    abstract protected function getResolverServiceID();
127
}
128