Passed
Pull Request — master (#1104)
by Aleksei
26:20
created

Target::fromReflectionMethod()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
ccs 14
cts 14
cp 1
rs 9.9
c 0
b 0
f 0
cc 4
nc 2
nop 2
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Interceptors\Context;
6
7
/**
8
 * @template-covariant TController of object|null
9
 * @implements TargetInterface<TController>
10
 */
11
final class Target implements TargetInterface
12
{
13
    /**
14
     * @param list<string> $path
0 ignored issues
show
Bug introduced by
The type Spiral\Interceptors\Context\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...
15
     * @param \ReflectionFunctionAbstract|null $reflection
16
     * @param TController|null $object
0 ignored issues
show
Bug introduced by
The type Spiral\Interceptors\Context\TController 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...
17
     */
18 239
    private function __construct(
19
        private array $path,
20
        private ?\ReflectionFunctionAbstract $reflection = null,
21
        private readonly ?object $object = null,
22
        private string $delimiter = '.',
23
    ) {
24 239
    }
25
26 17
    public function __toString(): string
27
    {
28 17
        return \implode($this->delimiter, $this->path);
29
    }
30
31
    /**
32
     * Create a target from a method reflection.
33
     *
34
     * @template T of object
35
     *
36
     * @param \ReflectionMethod $reflection
37
     * @param class-string<T>|T $classOrObject The original class name or object.
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T>|T at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>|T.
Loading history...
38
     *        It's required because the reflection may be referring to a parent class method.
39
     *        THe path will contain the original class name and the method name.
40
     *
41
     * @psalmif
42
     *
43
     * @return self<T>
44
     */
45 158
    public static function fromReflectionMethod(
46
        \ReflectionFunctionAbstract $reflection,
47
        string|object $classOrObject,
48
    ): self {
49
        /** @var self<T> $result */
50 158
        $result = \is_object($classOrObject)
51 102
            ? new self(
52 102
                path: [$classOrObject::class, $reflection->getName()],
53 102
                reflection: $reflection,
54 102
                object: $classOrObject,
55 102
                delimiter: $reflection->isStatic() ? '::' : '->',
0 ignored issues
show
Bug introduced by
The method isStatic() does not exist on ReflectionFunctionAbstract. It seems like you code against a sub-type of ReflectionFunctionAbstract such as ReflectionMethod. ( Ignorable by Annotation )

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

55
                delimiter: $reflection->/** @scrutinizer ignore-call */ isStatic() ? '::' : '->',
Loading history...
56 102
            )
57 56
            : new self(
58 56
                path: [$classOrObject, $reflection->getName()],
59 56
                reflection: $reflection,
60 56
                delimiter: $reflection->isStatic() ? '::' : '->',
61 56
            );
62 158
        return $result;
63
    }
64
65
    /**
66
     * Create a target from a function reflection.
67
     *
68
     * @param list<string> $path
69
     *
70
     * @return self<null>
71
     */
72 5
    public static function fromReflectionFunction(\ReflectionFunction $reflection, array $path = []): self
73
    {
74
        /** @var self<null> $result */
75 5
        $result = new self(path: $path, reflection: $reflection);
76 5
        return $result;
77
    }
78
79
    /**
80
     * Create a target from a closure.
81
     *
82
     * @param list<string> $path
83
     *
84
     * @return self<null>
85
     */
86 2
    public static function fromClosure(\Closure $closure, array $path = []): self
87
    {
88 2
        return self::fromReflectionFunction(new \ReflectionFunction($closure), $path);
89
    }
90
91
    /**
92
     * Create a target from a path string without reflection.
93
     *
94
     * @param non-empty-string $delimiter
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
95
     *
96
     * @return self<null>
97
     */
98 7
    public static function fromPathString(string $path, string $delimiter = '.'): self
99
    {
100 7
        return self::fromPathArray(\explode($delimiter, $path), $delimiter);
101
    }
102
103
    /**
104
     * Create a target from a path array without reflection.
105
     *
106
     * @param list<string> $path
107
     * @return self<null>
108
     */
109 78
    public static function fromPathArray(array $path, string $delimiter = '.'): self
110
    {
111
        /** @var self<null> $result */
112 78
        $result = new self(path: $path, delimiter: $delimiter);
113 78
        return $result;
114
    }
115
116
    /**
117
     * Create a target from a controller and action pair.
118
     * If the action is a method of the controller, the reflection will be set.
119
     *
120
     * @template T of object
121
     *
122
     * @param non-empty-string|class-string<T>|T $controller
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|class-string<T>|T at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|class-string<T>|T.
Loading history...
123
     * @param non-empty-string $action
124
     *
125
     * @return ($controller is class-string|T ? self<T> : self<null>)
0 ignored issues
show
Documentation Bug introduced by
The doc comment ($controller at position 1 could not be parsed: Unknown type name '$controller' at position 1 in ($controller.
Loading history...
126
     */
127 186
    public static function fromPair(string|object $controller, string $action): self
128
    {
129
        /** @psalm-suppress ArgumentTypeCoercion */
130 186
        if (\is_object($controller) || \method_exists($controller, $action)) {
131
            /** @var T|class-string<T> $controller */
132 156
            return self::fromReflectionMethod(new \ReflectionMethod($controller, $action), $controller);
133
        }
134
135 32
        return self::fromPathArray([$controller, $action]);
136
    }
137
138 223
    public function getPath(): array
139
    {
140 223
        return $this->path;
141
    }
142
143 2
    public function withPath(array $path, ?string $delimiter = null): static
144
    {
145 2
        $clone = clone $this;
146 2
        $clone->path = $path;
0 ignored issues
show
Documentation Bug introduced by
It seems like $path of type array is incompatible with the declared type Spiral\Interceptors\Context\list of property $path.

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...
147 2
        $clone->delimiter = $delimiter ?? $clone->delimiter;
148 2
        return $clone;
149
    }
150
151 50
    public function getReflection(): ?\ReflectionFunctionAbstract
152
    {
153 50
        return $this->reflection;
154
    }
155
156 42
    public function getObject(): ?object
157
    {
158 42
        return $this->object;
159
    }
160
}
161