Passed
Pull Request — master (#1190)
by butschster
11:23
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
 * @template T of AbstractMethod
17
 * @template TBootloader of BootloaderInterface
18
 * @implements AttributeResolverInterface<T, TBootloader>
19
 */
20
abstract class AbstractResolver implements AttributeResolverInterface
21
{
22 19
    public function __construct(
23
        protected readonly BinderInterface $binder,
24 19
    ) {}
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 11
    protected function getAliases(object $attribute, \ReflectionMethod $method): array
31
    {
32 11
        $alias = $attribute->alias ?? null;
33
34 11
        $aliases = [];
35 11
        if ($alias !== null) {
36 5
            $aliases[] = $alias;
37
        }
38
39 11
        $attrs = $method->getAttributes(name: BindAlias::class);
40 11
        foreach ($attrs as $attr) {
41 1
            $aliases = [...$aliases, ...$attr->newInstance()->aliases];
42
        }
43
44
        // If no aliases are provided, we will use the return type as the alias.
45 11
        if (\count($aliases) > 0 && !$attribute->aliasesFromReturnType) {
46 5
            return \array_unique(\array_filter($aliases));
47
        }
48
49 11
        $type = $method->getReturnType();
50
51 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...
52 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

52
            foreach ($type->/** @scrutinizer ignore-call */ getTypes() as $type) {
Loading history...
53 8
                if ($type->isBuiltin()) {
54 6
                    continue;
55
                }
56
57 6
                $aliases[] = $type->getName();
58
            }
59 7
        } elseif ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) {
60 5
            $aliases[] = $type->getName();
61
        }
62
63 11
        if ($aliases === []) {
64 4
            throw new \LogicException(
65 4
                "No alias provided for binding {$method->getDeclaringClass()->getName()}::{$method->getName()}",
66 4
            );
67
        }
68
69 7
        return \array_unique(\array_filter($aliases));
70
    }
71
72 7
    protected function getScope(\ReflectionMethod $method): ?string
73
    {
74 7
        $attrs = $method->getAttributes(name: BindScope::class);
75
76 7
        if ($attrs === []) {
77 7
            return null;
78
        }
79
80
        return $attrs[0]->newInstance()->scope;
81
    }
82
83 7
    protected function bind(array $aliases, Binding $binding, ?string $scope = null): void
84
    {
85 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

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