RegisterAsDeprecatedAttributesPass::register()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 3
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\AsDeprecated;
20
use FRZB\Component\DependencyInjection\Attribute\AsService;
21
use FRZB\Component\DependencyInjection\Helper\DefinitionHelper;
22
use FRZB\Component\DependencyInjection\Helper\EnvironmentHelper;
23
use Symfony\Component\DependencyInjection\ContainerBuilder;
24
use Symfony\Component\DependencyInjection\Definition;
25
26
/**
27
 * @internal
28
 *
29
 * Register #[AsDeprecated] attribute on definition that is autoconfigured
30
 *
31
 * @author Mykhailo Shtanko <[email protected]>
32
 */
33
final class RegisterAsDeprecatedAttributesPass extends AbstractRegisterAttributePass
34
{
35
    public function __construct()
36
    {
37
        parent::__construct(AsDeprecated::class);
38
    }
39
40
    public function register(ContainerBuilder $container, \ReflectionClass $reflectionClass, AsDeprecated $attribute): void
41
    {
42
        if (!EnvironmentHelper::isPermittedEnvironment($container, $reflectionClass)) {
43
            return;
44
        }
45
46
        ArrayList::collect($reflectionClass->getAttributes(AsService::class, \ReflectionAttribute::IS_INSTANCEOF))
47
            ->map(static fn (\ReflectionAttribute $reflectionAttribute) => $reflectionAttribute->newInstance())
48
            ->map(static fn (AsService $asService) => $container->getDefinition(DefinitionHelper::getServiceId($container, $reflectionClass, $asService->id)))
49
            ->appended($container->getDefinition($reflectionClass->getName()))
50
            ->tap(static fn (Definition $definition) => $definition->setDeprecated($attribute->package, $attribute->version, $attribute->message))
51
        ;
52
    }
53
}
54