Test Failed
Pull Request — master (#1095)
by Aleksei
10:19
created

Target::withPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Interceptors\Context;
6
7
final class Target implements TargetInterface
8
{
9
    /**
10
     * @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...
11
     * @param \ReflectionFunctionAbstract|null $reflection
12
     */
13
    private function __construct(
14
        private ?array $path = null,
15
        private ?\ReflectionFunctionAbstract $reflection = null,
16
        private readonly string $delimiter = '.',
17
    ) {
18
    }
19
20
    public function __toString(): string
21
    {
22
        return match (true) {
23
            $this->path !== null => \implode($this->delimiter, $this->path),
0 ignored issues
show
Bug introduced by
It seems like $this->path can also be of type null; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

23
            $this->path !== null => \implode($this->delimiter, /** @scrutinizer ignore-type */ $this->path),
Loading history...
24
            $this->reflection !== null => $this->reflection->getName(),
0 ignored issues
show
Bug introduced by
The method getName() does not exist on null. ( Ignorable by Annotation )

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

24
            $this->reflection !== null => $this->reflection->/** @scrutinizer ignore-call */ getName(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
        };
26
    }
27
28
    public static function fromReflection(\ReflectionFunctionAbstract $reflection): self
29
    {
30
        return new self(reflection: $reflection);
31
    }
32
33
    public static function fromPathString(string $path, string $delimiter = '.'): self
34
    {
35
        /** @psalm-suppress ArgumentTypeCoercion */
36
        return new self(path: \explode($delimiter, $path), delimiter: $delimiter);
37
    }
38
39
    /**
40
     * @param list<string> $path
41
     */
42
    public static function fromPathArray(array $path, string $delimiter = '.'): self
43
    {
44
        return new self(path: $path, delimiter: $delimiter);
45
    }
46
47
    public static function fromPair(string $controller, string $action): self
48
    {
49
        return \class_exists($controller)
50
            ? self::fromReflection(new \ReflectionMethod($controller, $action))
51
            : self::fromPathArray([$controller, $action]);
52
    }
53
54
    public function getPath(): array
55
    {
56
        return match (true) {
57
            $this->path !== null => $this->path,
58
            $this->reflection instanceof \ReflectionMethod => [
59
                $this->reflection->getDeclaringClass()->getName(),
0 ignored issues
show
Bug introduced by
The method getDeclaringClass() 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

59
                $this->reflection->/** @scrutinizer ignore-call */ 
60
                                   getDeclaringClass()->getName(),
Loading history...
60
                $this->reflection->getName(),
61
            ],
62
            $this->reflection instanceof \ReflectionFunction => [$this->reflection->getName()],
63
            default => [],
64
        };
65
    }
66
67
    public function withPath(array $path): static
68
    {
69
        $clone = clone $this;
70
        $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...
71
        return $clone;
72
    }
73
74
    public function getReflection(): ?\ReflectionFunctionAbstract
75
    {
76
        return $this->reflection;
77
    }
78
79
    public function withReflection(?\ReflectionFunctionAbstract $reflection): static
80
    {
81
        $clone = clone $this;
82
        $clone->reflection = $reflection;
83
        return $clone;
84
    }
85
}
86