• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

overblog / GraphQLBundle / 7925843142

01 Feb 2024 03:10PM UTC coverage: 98.431%. Remained the same
7925843142

push

github

web-flow
Merge pull request #1157 from cfv1000/master

fix issue with parsing invalid files

4267 of 4335 relevant lines covered (98.43%)

39.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.12
/src/DependencyInjection/Compiler/TaggedServiceMappingPass.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Overblog\GraphQLBundle\DependencyInjection\Compiler;
6

7
use InvalidArgumentException;
8
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
9
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Reference;
12

13
use function array_replace;
14
use function array_unique;
15
use function is_string;
16
use function sprintf;
17

18
abstract class TaggedServiceMappingPass implements CompilerPassInterface
19
{
20
    private function getTaggedServiceMapping(ContainerBuilder $container, string $tagName): array
21
    {
22
        $serviceMapping = [];
41✔
23

24
        $taggedServices = $container->findTaggedServiceIds($tagName, true);
41✔
25
        $isType = TypeTaggedServiceMappingPass::TAG_NAME === $tagName;
41✔
26

27
        foreach ($taggedServices as $id => $tags) {
41✔
28
            foreach ($tags as $attributes) {
41✔
29
                $this->checkRequirements($id, $attributes);
41✔
30
                $attributes = self::resolveAttributes($attributes, $id, !$isType);
41✔
31
                $solutionID = $id;
41✔
32

33
                if (!$isType && '__invoke' !== $attributes['method']) {
41✔
34
                    $solutionID = sprintf('%s::%s', $id, $attributes['method']);
19✔
35
                }
36

37
                if (!isset($serviceMapping[$solutionID])) {
41✔
38
                    $serviceMapping[$solutionID] = $attributes;
41✔
39
                }
40

41
                if (isset($attributes['alias']) && $solutionID !== $attributes['alias']) {
41✔
42
                    $serviceMapping[$solutionID]['aliases'][] = $attributes['alias'];
41✔
43
                }
44
            }
45
        }
46

47
        return $serviceMapping;
41✔
48
    }
49

50
    public function process(ContainerBuilder $container): void
51
    {
52
        $mapping = $this->getTaggedServiceMapping($container, $this->getTagName());
41✔
53
        $resolverDefinition = $container->findDefinition($this->getResolverServiceID());
41✔
54

55
        foreach ($mapping as $solutionID => $attributes) {
41✔
56
            $attributes['aliases'] = array_unique($attributes['aliases']);
41✔
57
            $aliases = $attributes['aliases'];
41✔
58
            $serviceID = $attributes['id'];
41✔
59

60
            $resolverDefinition->addMethodCall(
41✔
61
                'addSolution',
41✔
62
                [$solutionID, new ServiceClosureArgument(new Reference($serviceID)), $aliases, $attributes]
41✔
63
            );
64
        }
65
    }
41✔
66

67
    protected function checkRequirements(string $id, array $tag): void
68
    {
69
        if (isset($tag['alias']) && !is_string($tag['alias'])) {
41✔
70
            throw new InvalidArgumentException(
×
71
                sprintf('Service tagged "%s" must have valid "alias" argument.', $id)
×
72
            );
73
        }
74
    }
41✔
75

76
    private static function resolveAttributes(array $attributes, string $id, bool $withMethod): array
77
    {
78
        $default = ['id' => $id, 'aliases' => []];
41✔
79
        if ($withMethod) {
41✔
80
            $default['method'] = '__invoke';
41✔
81
        }
82
        $attributes = array_replace($default, $attributes);
41✔
83

84
        return $attributes;
41✔
85
    }
86

87
    abstract protected function getTagName(): string;
88

89
    abstract protected function getResolverServiceID(): string;
90
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc