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

fractalzombie / frzb-dependency-injection / 7325061485

25 Dec 2023 11:51PM UTC coverage: 84.36%. Remained the same
7325061485

push

github

fractalzombie
[DependencyInjection] Update functional library with breaking changes

28 of 29 new or added lines in 9 files covered. (96.55%)

21 existing lines in 8 files now uncovered.

178 of 211 relevant lines covered (84.36%)

13.68 hits per line

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

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

3
declare(strict_types=1);
4

5
/**
6
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
7
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
9
 *
10
 * Copyright (c) 2023 Mykhailo Shtanko fractalzombie@gmail.com
11
 *
12
 * For the full copyright and license information, please view the LICENSE.MD
13
 * file that was distributed with this source code.
14
 */
15

16
namespace FRZB\Component\DependencyInjection\Helper;
17

18
use Fp\Collections\ArrayList;
19
use Fp\Collections\HashMap;
20
use JetBrains\PhpStorm\Immutable;
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
use Symfony\Component\DependencyInjection\Definition;
23
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
24
use Symfony\Component\DependencyInjection\Reference;
25

26
/** @internal */
27
#[Immutable]
28
final class DefinitionHelper
29
{
30
    private const SERVICE_PREFIX = '@';
31
    private const EMPTY_STRING = '';
32

33
    private function __construct() {}
34

35
    /**
36
     * @template T
37
     *
38
     * @param class-string<T> $attributeName
39
     *
40
     * @return T[]
41
     */
42
    public static function getAttributesFor(\ReflectionClass $reflectionClass, string $attributeName): array
43
    {
44
        return ArrayList::collect($reflectionClass->getAttributes($attributeName, \ReflectionAttribute::IS_INSTANCEOF))
3✔
45
            ->map(static fn (\ReflectionAttribute $reflectionAttribute) => $reflectionAttribute->newInstance())
3✔
46
            ->toArray()
3✔
47
        ;
3✔
48
    }
49

50
    public static function getServiceId(ContainerBuilder $container, \ReflectionClass $reflectionClass, ?string $serviceId): string
51
    {
52
        return match (true) {
15✔
53
            null !== $serviceId && $container->hasDefinition($serviceId) => $serviceId,
15✔
54
            null === $serviceId && $container->hasDefinition($reflectionClass->getName()) => $reflectionClass->getName(),
15✔
55
            default => throw new ServiceNotFoundException($attribute->id ?? $reflectionClass->getName()),
15✔
56
        };
15✔
57
    }
58

59
    public static function getClassForServiceId(ContainerBuilder $container, string $serviceId): string
60
    {
61
        return class_exists($serviceId)
12✔
62
            ? $serviceId
12✔
63
            : $container->getDefinition($serviceId)->getClass();
12✔
64
    }
65

66
    /** @throws \ReflectionException */
67
    public static function getReflectionClassForServiceId(ContainerBuilder $container, string $serviceId): \ReflectionClass
68
    {
69
        return $container->getReflectionClass(self::getClassForServiceId($container, $serviceId));
12✔
70
    }
71

72
    public static function getDefinitionForServiceId(ContainerBuilder $container, string $serviceId): Definition
73
    {
UNCOV
74
        return $container->getDefinition(self::getClassForServiceId($container, $serviceId));
×
75
    }
76

77
    public static function mapDefinitionMethodCalls(ContainerBuilder $container, array $methodCalls): array
78
    {
79
        return HashMap::collect($methodCalls)
21✔
80
            ->map(fn (array $value) => self::mapDefinitionArguments($container, $value))
21✔
81
            ->toArray()
21✔
82
        ;
21✔
83
    }
84

85
    public static function mapDefinitionArguments(ContainerBuilder $container, array $arguments): array
86
    {
87
        $definitionsById = HashMap::collect($arguments)
21✔
88
            ->filter(static fn (string $value) => \is_string($value))
21✔
89
            ->filter(static fn (string $value) => str_contains($value, self::SERVICE_PREFIX))
21✔
90
            ->map(static fn (string $value) => str_replace(self::SERVICE_PREFIX, self::EMPTY_STRING, $value))
21✔
91
            ->filter(static fn (string $value) => $container->hasDefinition($value))
21✔
92
            ->map(static fn (string $value) => new Reference($value))
21✔
93
            ->toArray()
21✔
94
        ;
21✔
95

96
        $definitionsByClass = HashMap::collect($arguments)
21✔
97
            ->filter(static fn (string $value) => \is_string($value))
21✔
98
            ->filter(static fn (string $value) => class_exists($value))
21✔
99
            ->filter(static fn (string $value) => $container->hasDefinition($value))
21✔
100
            ->map(static fn (string $value) => new Reference($value))
21✔
101
            ->toArray()
21✔
102
        ;
21✔
103

104
        $definitionsByAlias = HashMap::collect($arguments)
21✔
105
            ->filter(static fn (string $value) => \is_string($value))
21✔
106
            ->filter(static fn (string $value) => interface_exists($value) || class_exists($value))
21✔
107
            ->filter(static fn (string $value) => $container->hasAlias($value))
21✔
108
            ->map(static fn (string $value) => new Reference($value))
21✔
109
            ->toArray()
21✔
110
        ;
21✔
111

112
        return [...$definitionsById, ...$definitionsByClass, ...$definitionsByAlias];
21✔
113
    }
114
}
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