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

fractalzombie / frzb-dependency-injection / 4355207088

pending completion
4355207088

push

github

Mykhailo Shtanko
[DependencyInjection] Added SonarCloud checks, refactor GitHub actions, refactor for using not only id as class

44 of 50 new or added lines in 9 files covered. (88.0%)

7 existing lines in 5 files now uncovered.

210 of 247 relevant lines covered (85.02%)

4.77 hits per line

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

95.65
/Helper/DefinitionHelper.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace FRZB\Component\DependencyInjection\Helper;
6

7
use Fp\Collections\ArrayList;
8
use Fp\Collections\Entry;
9
use Fp\Collections\HashMap;
10
use JetBrains\PhpStorm\Immutable;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
use Symfony\Component\DependencyInjection\Definition;
13
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
14
use Symfony\Component\DependencyInjection\Reference;
15

16
/** @internal */
17
#[Immutable]
18
final class DefinitionHelper
19
{
20
    private const SERVICE_PREFIX = '@';
21
    private const EMPTY_STRING = '';
22

23
    private function __construct()
24
    {
UNCOV
25
    }
×
26

27
    /**
28
     * @template T
29
     *
30
     * @param class-string<T> $attributeName
31
     *
32
     * @return T[]
33
     */
34
    public static function getAttributesFor(\ReflectionClass $reflectionClass, string $attributeName): array
35
    {
36
        return ArrayList::collect($reflectionClass->getAttributes($attributeName, \ReflectionAttribute::IS_INSTANCEOF))
1✔
37
            ->map(static fn (\ReflectionAttribute $reflectionAttribute) => $reflectionAttribute->newInstance())
1✔
38
            ->toArray()
1✔
39
        ;
1✔
40
    }
41

42
    public static function getServiceId(ContainerBuilder $container, \ReflectionClass $reflectionClass, ?string $serviceId): string
43
    {
44
        return match (true) {
5✔
45
            null !== $serviceId && $container->hasDefinition($serviceId) => $serviceId,
5✔
46
            null === $serviceId && $container->hasDefinition($reflectionClass->getName()) => $reflectionClass->getName(),
5✔
47
            default => throw new ServiceNotFoundException($attribute->id ?? $reflectionClass->getName()),
5✔
48
        };
5✔
49
    }
50

51
    public static function getClassForServiceId(ContainerBuilder $container, string $serviceId): string
52
    {
53
        return class_exists($serviceId)
4✔
54
            ? $serviceId
4✔
55
            : $container->getDefinition($serviceId)->getClass()
4✔
56
        ;
4✔
57
    }
58

59
    /** @throws \ReflectionException */
60
    public static function getReflectionClassForServiceId(ContainerBuilder $container, string $serviceId): \ReflectionClass
61
    {
62
        return $container->getReflectionClass(self::getClassForServiceId($container, $serviceId));
4✔
63
    }
64

65
    public static function getDefinitionForServiceId(ContainerBuilder $container, string $serviceId): Definition
66
    {
NEW
67
        return $container->getDefinition(self::getClassForServiceId($container, $serviceId));
×
68
    }
69

70
    public static function mapDefinitionMethodCalls(ContainerBuilder $container, array $methodCalls): array
71
    {
72
        return HashMap::collect($methodCalls)
7✔
73
            ->map(fn (Entry $e) => self::mapDefinitionArguments($container, $e->value))
7✔
74
            ->toArray()
7✔
75
        ;
7✔
76
    }
77

78
    public static function mapDefinitionArguments(ContainerBuilder $container, array $arguments): array
79
    {
80
        $definitionsById = HashMap::collect($arguments)
7✔
81
            ->filter(static fn (Entry $e) => \is_string($e->value))
7✔
82
            ->filter(static fn (Entry $e) => str_contains($e->value, self::SERVICE_PREFIX))
7✔
83
            ->map(static fn (Entry $e) => str_replace(self::SERVICE_PREFIX, self::EMPTY_STRING, $e->value))
7✔
84
            ->filter(static fn (Entry $e) => $container->hasDefinition($e->value))
7✔
85
            ->map(static fn (Entry $e) => new Reference((string) $e->value))
7✔
86
            ->toAssocArray()
7✔
87
            ->getOrElse([])
7✔
88
        ;
7✔
89

90
        $definitionsByClass = HashMap::collect($arguments)
7✔
91
            ->filter(static fn (Entry $e) => \is_string($e->value))
7✔
92
            ->filter(static fn (Entry $e) => class_exists($e->value))
7✔
93
            ->filter(static fn (Entry $e) => $container->hasDefinition($e->value))
7✔
94
            ->map(static fn (Entry $e) => new Reference((string) $e->value))
7✔
95
            ->toAssocArray()
7✔
96
            ->getOrElse([])
7✔
97
        ;
7✔
98

99
        $definitionsByAlias = HashMap::collect($arguments)
7✔
100
            ->filter(static fn (Entry $e) => \is_string($e->value))
7✔
101
            ->filter(static fn (Entry $e) => interface_exists($e->value) || class_exists($e->value))
7✔
102
            ->filter(static fn (Entry $e) => $container->hasAlias($e->value))
7✔
103
            ->map(static fn (Entry $e) => new Reference((string) $e->value))
7✔
104
            ->toAssocArray()
7✔
105
            ->getOrElse([])
7✔
106
        ;
7✔
107

108
        return [...$definitionsById, ...$definitionsByClass, ...$definitionsByAlias];
7✔
109
    }
110
}
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