Passed
Push — master ( 477224...3259b8 )
by Aleksei
06:12 queued 18s
created

AbstractResolver::getAliases()   C

Complexity

Conditions 12
Paths 28

Size

Total Lines 40
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 40
ccs 23
cts 23
cp 1
rs 6.9666
cc 12
nc 28
nop 2
crap 12

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 * @internal
17
 * @template T of AbstractMethod
18
 * @template TBootloader of BootloaderInterface
19
 * @implements AttributeResolverInterface<T, TBootloader>
20
 */
21
abstract class AbstractResolver implements AttributeResolverInterface
22
{
23 19
    public function __construct(
24
        protected readonly BinderInterface $binder,
25 19
    ) {}
26
27
    /**
28
     * @psalm-param T $attribute
29
     * @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...
30
     */
31 11
    protected function getAliases(object $attribute, \ReflectionMethod $method): array
32
    {
33 11
        $alias = $attribute->alias ?? null;
34
35 11
        $aliases = [];
36 11
        if ($alias !== null) {
37 5
            $aliases[] = $alias;
38
        }
39
40 11
        $attrs = $method->getAttributes(name: BindAlias::class);
41 11
        foreach ($attrs as $attr) {
42 1
            $aliases = [...$aliases, ...$attr->newInstance()->aliases];
43
        }
44
45
        // If no aliases are provided, we will use the return type as the alias.
46 11
        if (\count($aliases) > 0 && !$attribute->aliasesFromReturnType) {
47 5
            return \array_unique(\array_filter($aliases));
48
        }
49
50 11
        $type = $method->getReturnType();
51
52 11
        if ($type instanceof \ReflectionUnionType || $type instanceof \ReflectionIntersectionType) {
53 8
            foreach ($type->getTypes() as $type) {
54 8
                if ($type->isBuiltin()) {
55 6
                    continue;
56
                }
57
58 6
                $aliases[] = $type->getName();
59
            }
60 7
        } elseif ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) {
61 5
            $aliases[] = $type->getName();
62
        }
63
64 11
        if ($aliases === []) {
65 4
            throw new \LogicException(
66 4
                "No alias provided for binding {$method->getDeclaringClass()->getName()}::{$method->getName()}",
67 4
            );
68
        }
69
70 7
        return \array_unique(\array_filter($aliases));
71
    }
72
73 7
    protected function getScopes(\ReflectionMethod $method): array
74
    {
75 7
        $attrs = $method->getAttributes(name: BindScope::class);
76
77 7
        if ($attrs === []) {
78 7
            return [];
79
        }
80
81
        return \array_map(
82
            static fn(\ReflectionAttribute $attr): string => $attrs[0]->newInstance()->scope,
0 ignored issues
show
Unused Code introduced by
The parameter $attr is not used and could be removed. ( Ignorable by Annotation )

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

82
            static fn(/** @scrutinizer ignore-unused */ \ReflectionAttribute $attr): string => $attrs[0]->newInstance()->scope,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
            $attrs,
84
        );
85
    }
86
87 7
    protected function bind(array $aliases, Binding $binding, array $scopes): void
88
    {
89 7
        $binders = [];
90 7
        foreach ($scopes as $scope) {
91
            $binders[] = $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

91
            /** @scrutinizer ignore-call */ 
92
            $binders[] = $this->binder->getBinder($scope);
Loading history...
92
        }
93
94 7
        if ($binders === []) {
95 7
            $binders[] = $this->binder;
96
        }
97
98 7
        foreach ($binders as $binder) {
99 7
            $alias = \array_shift($aliases);
100 7
            foreach ($aliases as $a) {
101 7
                $binder->bind($alias, $a);
102 7
                $alias = \array_shift($aliases);
103
            }
104
105 7
            $binder->bind($alias, $binding);
106
        }
107
    }
108
}
109