Passed
Push — php82 ( f26871 )
by Akihito
02:19
created

Name::parseName()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 19
rs 9.6111
cc 5
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Ray\Di\Di\Named;
8
use Ray\Di\Di\Qualifier;
9
use ReflectionAttribute;
10
use ReflectionClass;
11
use ReflectionException;
12
use ReflectionMethod;
13
use ReflectionParameter;
14
15
use function class_exists;
16
use function get_class;
17
use function is_string;
18
19
/**
20
 * @psalm-import-type ParameterNameMapping from Types
21
 */
22
final class Name
23
{
24
    /**
25
     * 'Unnamed' name
26
     */
27
    public const ANY = '';
28
29
    /** @var string */
30
    private $name = '';
31
32
    /**
33
     * Named database
34
     *
35
     * format: array<varName, NamedName>
36
     *
37
     * @var ParameterNameMapping
0 ignored issues
show
Bug introduced by
The type Ray\Di\ParameterNameMapping 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...
38
     */
39
    private $names;
40
41
    /**
42
     * @param string|ParameterNameMapping $name
43
     */
44
    public function __construct(string|array $name)
45
    {
46
        if (is_string($name)) {
0 ignored issues
show
introduced by
The condition is_string($name) is always false.
Loading history...
47
            $this->setName($name);
48
49
            return;
50
        }
51
52
        $this->names = $name;
0 ignored issues
show
Documentation Bug introduced by
It seems like $name of type array is incompatible with the declared type Ray\Di\ParameterNameMapping of property $names.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53
    }
54
55
    /**
56
     * Create instance from PHP8 attributes
57
     *
58
     * psalm does not know ReflectionAttribute?? PHPStan produces no type error here.
59
     */
60
    public static function withAttributes(ReflectionMethod $method): ?self
61
    {
62
        $params = $method->getParameters();
63
        $names = [];
64
        foreach ($params as $param) {
65
            /** @var array<ReflectionAttribute> $attributes */
66
            $attributes = $param->getAttributes();
67
            if ($attributes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $attributes of type ReflectionAttribute[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
68
                $name = self::getName($attributes);
69
                $names[$param->name] = $name;
70
            }
71
        }
72
73
        if ($names) {
74
            return new self($names);
75
        }
76
77
        return null;
78
    }
79
80
    /**
81
     * @param non-empty-array<ReflectionAttribute> $attributes
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-array<ReflectionAttribute> at position 0 could not be parsed: Unknown type name 'non-empty-array' at position 0 in non-empty-array<ReflectionAttribute>.
Loading history...
82
     *
83
     * @throws ReflectionException
84
     *
85
     * @psalm-suppress MixedAssignment
86
     * @psalm-suppress MixedArgument
87
     */
88
    private static function getName(array $attributes): string
89
    {
90
        $refAttribute = $attributes[0];
91
        $attribute = $refAttribute->newInstance();
92
        if ($attribute instanceof Named) {
93
            return $attribute->value;
94
        }
95
96
        $isQualifier = (bool) (new ReflectionClass($attribute))->getAttributes(Qualifier::class);
97
        if ($isQualifier) {
98
            return get_class($attribute);
99
        }
100
101
        return '';
102
    }
103
104
    public function __invoke(ReflectionParameter $parameter): string
105
    {
106
        // single variable named binding
107
        if ($this->name) {
108
            return $this->name;
109
        }
110
111
        $parameterName = $parameter->name;
112
113
        // multiple variable named binding
114
        return $this->names[$parameterName] ?? $this->names[self::ANY] ?? self::ANY;
115
    }
116
117
    private function setName(string $name): void
118
    {
119
        // annotation
120
        if (class_exists($name, false)) {
121
            $this->name = $name;
122
123
            return;
124
        }
125
126
        // single name
127
        // @Named(name)
128
        $this->name = $name;
129
    }
130
}
131