Passed
Pull Request — master (#1190)
by Aleksei
20:19 queued 07:41
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) {
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...
53 8
            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

53
            foreach ($type->/** @scrutinizer ignore-call */ getTypes() as $type) {
Loading history...
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 getScope(\ReflectionMethod $method): ?string
74
    {
75 7
        $attrs = $method->getAttributes(name: BindScope::class);
76
77 7
        if ($attrs === []) {
78 7
            return null;
79
        }
80
81
        return $attrs[0]->newInstance()->scope;
82
    }
83
84 7
    protected function bind(array $aliases, Binding $binding, ?string $scope = null): void
85
    {
86 7
        $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

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