RegisterAsIgnoredAttributesPass   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 15
c 1
b 0
f 0
dl 0
loc 31
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 18 2
A accept() 0 3 2
A __construct() 0 3 1
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) 2024 Mykhailo Shtanko [email protected]
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 Fp\Collections\ArrayList;
19
use FRZB\Component\DependencyInjection\Attribute\AsAlias;
20
use FRZB\Component\DependencyInjection\Attribute\AsIgnored;
21
use FRZB\Component\DependencyInjection\Attribute\AsService;
22
use FRZB\Component\DependencyInjection\Helper\DefinitionHelper;
23
use FRZB\Component\DependencyInjection\Helper\EnvironmentHelper;
24
use Symfony\Component\DependencyInjection\ContainerBuilder;
25
use Symfony\Component\DependencyInjection\Definition;
26
27
/**
28
 * @internal
29
 *
30
 * Register #[AsAlias] attribute on alias that is autoconfigured
31
 *
32
 * @author Mykhailo Shtanko <[email protected]>
33
 */
34
final class RegisterAsIgnoredAttributesPass extends AbstractRegisterAttributePass
35
{
36
    public function __construct()
37
    {
38
        parent::__construct(AsIgnored::class);
39
    }
40
41
    public function register(ContainerBuilder $container, \ReflectionClass $reflectionClass, AsIgnored $attribute): void
42
    {
43
        if (!EnvironmentHelper::isPermittedEnvironment($container, $reflectionClass)) {
44
            return;
45
        }
46
47
        ArrayList::collect(DefinitionHelper::getAttributesFor($reflectionClass, AsService::class))
48
            ->map(static fn (\ReflectionAttribute $reflectionAttribute) => $reflectionAttribute->newInstance())
49
            ->filter(static fn (AsService $asService) => $container->hasDefinition(DefinitionHelper::getServiceId($container, $reflectionClass, $asService->id)))
50
            ->map(static fn (AsService $asService) => DefinitionHelper::getServiceId($container, $reflectionClass, $asService->id))
51
            ->tap(static fn (string $serviceId) => $container->removeDefinition($serviceId))
52
        ;
53
54
        ArrayList::collect(DefinitionHelper::getAttributesFor($reflectionClass, AsAlias::class))
55
            ->map(static fn (\ReflectionAttribute $reflectionAttribute) => $reflectionAttribute->newInstance())
56
            ->filter(static fn (AsAlias $asAlias) => $container->hasAlias(DefinitionHelper::getServiceId($container, $reflectionClass, $asAlias->getServiceAlias())))
57
            ->map(static fn (AsAlias $asAlias) => DefinitionHelper::getServiceId($container, $reflectionClass, $asAlias->service))
58
            ->tap(static fn (string $serviceId) => $container->removeAlias($serviceId))
59
        ;
60
    }
61
62
    protected function accept(Definition $definition): bool
63
    {
64
        return $definition->isAutoconfigured() && $this->isAttributesIgnored($definition);
65
    }
66
}
67