InjectionPoints::getSetterMethod()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use ReflectionException;
8
use ReflectionMethod;
9
10
final class InjectionPoints
11
{
12
    /**
13
     * Injection points
14
     *
15
     * @var array<array{0:string, 1:string, 2:bool}>
16
     */
17
    private $points = [];
18
19
    /**
20
     * @param class-string $class
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...
21
     *
22
     * @throws ReflectionException
23
     */
24
    public function __invoke(string $class): SetterMethods
25
    {
26
        $points = [];
27
        foreach ($this->points as $point) {
28
            $points[] = $this->getSetterMethod($class, $point);
29
        }
30
31
        return new SetterMethods($points);
32
    }
33
34
    public function addMethod(string $method, string $name = Name::ANY): self
35
    {
36
        $this->points[] = [$method, $name, false];
37
38
        return $this;
39
    }
40
41
    public function addOptionalMethod(string $method, string $name = Name::ANY): self
42
    {
43
        $this->points[] = [$method, $name, true];
44
45
        return $this;
46
    }
47
48
    /**
49
     * @param class-string                         $class
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...
50
     * @param array{0: string, 1: string, 2: bool} $point
51
     *
52
     * @throws ReflectionException
53
     */
54
    private function getSetterMethod(string $class, array $point): SetterMethod
55
    {
56
        $setterMethod = new SetterMethod(new ReflectionMethod($class, $point[0]), new Name($point[1]));
57
        if ($point[2]) {
58
            $setterMethod->setOptional();
59
        }
60
61
        return $setterMethod;
62
    }
63
}
64