Issues (11)

Compiler/RegisterAsAliasAttributesPass.php (2 issues)

Labels
Severity
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 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 <[email protected]>
31
 */
32
final class RegisterAsAliasAttributesPass extends AbstractRegisterAttributePass
33
{
34
    public function __construct()
35
    {
36
        parent::__construct(AsAlias::class);
37
    }
38
39
    public function process(ContainerBuilder $container): void
40
    {
41
        foreach ($container->getAliases() as $id => $alias) {
42
            if ($class = $container->getReflectionClass($id, false)) {
43
                $this->processClass($container, $class);
44
            }
45
        }
46
    }
47
48
    public function register(ContainerBuilder $container, \ReflectionClass $reflectionClass, AsAlias $attribute): void
49
    {
50
        if (!EnvironmentHelper::isPermittedEnvironment($container, $reflectionClass)) {
51
            return;
52
        }
53
54
        try {
55
            $definitionClass = DefinitionHelper::getReflectionClassForServiceId($container, $attribute->service);
56
        } catch (\ReflectionException $e) {
57
            throw AttributeException::noDefinitionInContainer($attribute, $e);
58
        }
59
60
        if ($reflectionClass->isInterface() && !$definitionClass?->isSubclassOf($reflectionClass->getName())) {
61
            throw AttributeException::invalidImplementation($attribute, $reflectionClass);
62
        }
63
64
        match ($attribute->aliasType) {
65
            AliasType::WithArgumentName => $this->registerAliasWithArgument($container, $reflectionClass, $attribute),
0 ignored issues
show
Are you sure the usage of $this->registerAliasWith...ctionClass, $attribute) targeting FRZB\Component\Dependenc...sterAliasWithArgument() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
66
            AliasType::WithoutArgumentName => $this->registerAliasWithoutArgument($container, $reflectionClass, $attribute),
0 ignored issues
show
Are you sure the usage of $this->registerAliasWith...ctionClass, $attribute) targeting FRZB\Component\Dependenc...rAliasWithoutArgument() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
67
            AliasType::LogicException => throw AttributeException::unexpected($attribute),
68
        };
69
    }
70
71
    private function registerAliasWithArgument(ContainerBuilder $container, \ReflectionClass $rClass, AsAlias $attribute): void
72
    {
73
        $container
74
            ->registerAliasForArgument($attribute->service, $rClass->getName(), $attribute->aliasForArgument)
75
            ->setPublic($attribute->isPublic)
76
        ;
77
    }
78
79
    private function registerAliasWithoutArgument(ContainerBuilder $container, \ReflectionClass $rClass, AsAlias $attribute): void
80
    {
81
        $container
82
            ->setAlias($rClass->getName(), $attribute->service)
83
            ->setPublic($attribute->isPublic)
84
        ;
85
    }
86
}
87