Test Failed
Pull Request — master (#1190)
by butschster
10:27
created

AbstractResolver   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 32
c 1
b 0
f 0
dl 0
loc 74
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A bind() 0 11 2
A __construct() 0 3 1
A getScope() 0 9 2
C getAliases() 0 40 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Boot\BootloadManager\AttributeResolver;
6
7
use Spiral\Boot\Attribute\AbstractMethod;
8
use Spiral\Boot\Attribute\BindAlias;
9
use Spiral\Boot\Attribute\BindScope;
10
use Spiral\Boot\Bootloader\BootloaderInterface;
11
use Spiral\Boot\BootloadManager\AttributeResolverInterface;
12
use Spiral\Core\BinderInterface;
13
use Spiral\Core\Config\Binding;
14
15
/**
16
 * @template T of AbstractMethod
17
 * @template TBootloader of BootloaderInterface
18
 * @implements AttributeResolverInterface<T, TBootloader>
19
 */
20
abstract class AbstractResolver implements AttributeResolverInterface
21
{
22
    public function __construct(
23
        protected readonly BinderInterface $binder,
24
    ) {}
25
26
    /**
27
     * @psalm-param T $attribute
28
     * @return list<non-empty-string>
0 ignored issues
show
Bug introduced by
The type Spiral\Boot\BootloadManager\AttributeResolver\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
     */
30
    protected function getAliases(object $attribute, \ReflectionMethod $method): array
31
    {
32
        $alias = $attribute->alias ?? null;
33
34
        $aliases = [];
35
        if ($alias !== null) {
36
            $aliases[] = $alias;
37
        }
38
39
        $attrs = $method->getAttributes(name: BindAlias::class);
40
        foreach ($attrs as $attr) {
41
            $aliases = [...$aliases, ...$attr->newInstance()->aliases];
42
        }
43
44
        // If no aliases are provided, we will use the return type as the alias.
45
        if (\count($aliases) > 0 && !$attribute->aliasesFromReturnType) {
46
            return \array_unique(\array_filter($aliases));
47
        }
48
49
        $type = $method->getReturnType();
50
51
        if ($type instanceof \ReflectionUnionType || $type instanceof \ReflectionIntersectionType) {
0 ignored issues
show
Bug introduced by
The type ReflectionIntersectionType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
52
            foreach ($type->getTypes() as $type) {
0 ignored issues
show
Bug introduced by
The method getTypes() does not exist on ReflectionType. It seems like you code against a sub-type of ReflectionType such as ReflectionUnionType. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
            foreach ($type->/** @scrutinizer ignore-call */ getTypes() as $type) {
Loading history...
53
                if ($type->isBuiltin()) {
54
                    continue;
55
                }
56
57
                $aliases[] = $type->getName();
58
            }
59
        } elseif ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) {
60
            $aliases[] = $type->getName();
61
        }
62
63
        if ($aliases === []) {
64
            throw new \LogicException(
65
                "No alias provided for binding {$method->getDeclaringClass()->getName()}::{$method->getName()}",
66
            );
67
        }
68
69
        return \array_unique(\array_filter($aliases));
70
    }
71
72
    protected function getScope(\ReflectionMethod $method): ?string
73
    {
74
        $attrs = $method->getAttributes(name: BindScope::class);
75
76
        if ($attrs === []) {
77
            return null;
78
        }
79
80
        return $attrs[0]->newInstance()->scope;
81
    }
82
83
    protected function bind(array $aliases, Binding $binding, ?string $scope = null): void
84
    {
85
        $binder = $this->binder->getBinder($scope);
0 ignored issues
show
Bug introduced by
The method getBinder() does not exist on Spiral\Core\BinderInterface. It seems like you code against a sub-type of Spiral\Core\BinderInterface such as Spiral\Core\Container. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
        /** @scrutinizer ignore-call */ 
86
        $binder = $this->binder->getBinder($scope);
Loading history...
86
87
        $alias = \array_shift($aliases);
88
        foreach ($aliases as $a) {
89
            $binder->bind($alias, $a);
90
            $alias = \array_shift($aliases);
91
        }
92
93
        $binder->bind($alias, $binding);
94
    }
95
}
96