• 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

83.33
/Compiler/RegisterAsAliasAttributesPass.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\Compiler;
17

18
use FRZB\Component\DependencyInjection\Attribute\AsAlias;
19
use FRZB\Component\DependencyInjection\Enum\AliasType;
20
use FRZB\Component\DependencyInjection\Exception\AttributeException;
21
use FRZB\Component\DependencyInjection\Helper\DefinitionHelper;
22
use FRZB\Component\DependencyInjection\Helper\EnvironmentHelper;
23
use Symfony\Component\DependencyInjection\ContainerBuilder;
24

25
/**
26
 * @internal
27
 *
28
 * Register #[AsAlias] attribute on alias that is autoconfigured
29
 *
30
 * @author Mykhailo Shtanko <fractalzombie@gmail.com>
31
 */
32
final class RegisterAsAliasAttributesPass extends AbstractRegisterAttributePass
33
{
34
    public function __construct()
35
    {
36
        parent::__construct(AsAlias::class);
12✔
37
    }
38

39
    public function process(ContainerBuilder $container): void
40
    {
41
        foreach ($container->getAliases() as $id => $alias) {
12✔
42
            if ($class = $container->getReflectionClass($id, false)) {
12✔
43
                $this->processClass($container, $class);
12✔
44
            }
45
        }
46
    }
47

48
    public function register(ContainerBuilder $container, \ReflectionClass $reflectionClass, AsAlias $attribute): void
49
    {
50
        if (!EnvironmentHelper::isPermittedEnvironment($container, $reflectionClass)) {
12✔
UNCOV
51
            return;
×
52
        }
53

54
        try {
55
            $definitionClass = DefinitionHelper::getReflectionClassForServiceId($container, $attribute->service);
12✔
UNCOV
56
        } catch (\ReflectionException $e) {
×
57
            throw AttributeException::noDefinitionInContainer($attribute, $e);
×
58
        }
59

60
        if ($reflectionClass->isInterface() && !$definitionClass?->isSubclassOf($reflectionClass->getName())) {
12✔
UNCOV
61
            throw AttributeException::invalidImplementation($attribute, $reflectionClass);
×
62
        }
63

64
        match ($attribute->aliasType) {
12✔
65
            AliasType::WithArgumentName => $this->registerAliasWithArgument($container, $reflectionClass, $attribute),
12✔
66
            AliasType::WithoutArgumentName => $this->registerAliasWithoutArgument($container, $reflectionClass, $attribute),
12✔
67
            AliasType::LogicException => throw AttributeException::unexpected($attribute),
12✔
68
        };
12✔
69
    }
70

71
    private function registerAliasWithArgument(ContainerBuilder $container, \ReflectionClass $rClass, AsAlias $attribute): void
72
    {
73
        $container
12✔
74
            ->registerAliasForArgument($attribute->service, $rClass->getName(), $attribute->aliasForArgument)
12✔
75
            ->setPublic($attribute->isPublic)
12✔
76
        ;
12✔
77
    }
78

79
    private function registerAliasWithoutArgument(ContainerBuilder $container, \ReflectionClass $rClass, AsAlias $attribute): void
80
    {
81
        $container
12✔
82
            ->setAlias($rClass->getName(), $attribute->service)
12✔
83
            ->setPublic($attribute->isPublic)
12✔
84
        ;
12✔
85
    }
86
}
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