Passed
Push — master ( f2a84f...718fb6 )
by Melech
03:59
created

MethodDispatch::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Dispatcher\Data;
15
16
use Override;
0 ignored issues
show
Bug introduced by
The type Override 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
use Valkyrja\Dispatcher\Data\Contract\MethodDispatch as Contract;
18
use Valkyrja\Dispatcher\Exception\InvalidArgumentException;
19
20
use function is_array;
21
use function is_string;
22
23
/**
24
 * Class MethodDispatch.
25
 *
26
 * @author Melech Mizrachi
27
 */
28
class MethodDispatch extends ClassDispatch implements Contract
29
{
30
    /**
31
     * @param class-string                 $class        The class name
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
32
     * @param non-empty-string             $method       The method name
33
     * @param array<array-key, mixed>|null $arguments    The arguments
34
     * @param class-string[]|null          $dependencies The dependencies
35
     */
36
    public function __construct(
37
        string $class,
38
        protected string $method,
39
        protected bool $isStatic = false,
40
        array|null $arguments = null,
41
        array|null $dependencies = null
42
    ) {
43
        parent::__construct(
44
            class: $class,
45
            arguments: $arguments,
46
            dependencies: $dependencies
47
        );
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53
    #[Override]
54
    public static function fromCallableOrArray(callable|array $callable): static
55
    {
56
        if (! is_array($callable)) {
0 ignored issues
show
introduced by
The condition is_array($callable) is always true.
Loading history...
57
            throw new InvalidArgumentException('Callable must be an array.');
58
        }
59
60
        /** @var class-string|object $className */
61
        $className = $callable[0]
62
            ?? throw new InvalidArgumentException('Callable must be an array with a valid class name');
63
64
        if (! is_string($className)) {
65
            throw new InvalidArgumentException('First part of the callable array must be a class-string');
66
        }
67
68
        $method = $callable[1]
69
            ?? throw new InvalidArgumentException('Callable must be an array with a valid method name');
70
71
        return new static(
72
            class: $className,
73
            method: $method,
74
            isStatic: true
75
        );
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    #[Override]
82
    public function getMethod(): string
83
    {
84
        return $this->method;
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90
    #[Override]
91
    public function withMethod(string $method): static
92
    {
93
        $new = clone $this;
94
95
        $new->method = $method;
96
97
        return $new;
98
    }
99
100
    /**
101
     * @inheritDoc
102
     */
103
    #[Override]
104
    public function isStatic(): bool
105
    {
106
        return $this->isStatic;
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112
    #[Override]
113
    public function withIsStatic(bool $isStatic): static
114
    {
115
        $new = clone $this;
116
117
        $new->isStatic = $isStatic;
118
119
        return $new;
120
    }
121
122
    /**
123
     * @inheritDoc
124
     */
125
    #[Override]
126
    public function __toString(): string
127
    {
128
        return $this->class
129
            . ($this->isStatic ? '::' : '->')
130
            . "$this->method()";
131
    }
132
}
133