SetterMethod::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 18
c 0
b 0
f 0
rs 9.9332
cc 4
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Exception;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Ray\Di\Exception. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use LogicException;
9
use Ray\Di\Exception\Unbound;
10
use ReflectionMethod;
11
12
use function call_user_func_array;
13
use function is_callable;
14
15
final class SetterMethod implements AcceptInterface
16
{
17
    private readonly string $method;
18
    private readonly Arguments $arguments;
19
20
    /**
21
     * Is optional binding ?
22
     */
23
    private bool $isOptional = false;
24
25
    public function __construct(ReflectionMethod $method, Name $name)
26
    {
27
        $this->method = $method->name;
0 ignored issues
show
Bug introduced by
The property method is declared read-only in Ray\Di\SetterMethod.
Loading history...
28
        $this->arguments = new Arguments($method, $name);
0 ignored issues
show
Bug introduced by
The property arguments is declared read-only in Ray\Di\SetterMethod.
Loading history...
29
    }
30
31
    /**
32
     * @param object $instance
33
     *
34
     * @throws Unbound
35
     * @throws Exception
36
     */
37
    public function __invoke($instance, Container $container): void
38
    {
39
        try {
40
            $parameters = $this->arguments->inject($container);
41
        } catch (Unbound $unbound) {
42
            if ($this->isOptional) {
43
                return;
44
            }
45
46
            throw $unbound;
47
        }
48
49
        $callable = [$instance, $this->method];
50
        if (! is_callable($callable)) {
51
            throw new LogicException(); // @codeCoverageIgnore
52
        }
53
54
        call_user_func_array($callable, $parameters);
55
    }
56
57
    public function setOptional(): void
58
    {
59
        $this->isOptional = true;
60
    }
61
62
    /** @inheritDoc */
63
    public function accept(VisitorInterface $visitor): void
64
    {
65
        try {
66
            $visitor->visitSetterMethod($this->method, $this->arguments);
67
        } catch (Unbound $unbound) {
68
            if ($this->isOptional) {
69
                // Return when no dependency given and @Inject(optional=true) annotated to setter method.
70
                return;
71
            }
72
73
            // Throw exception when no dependency given and @Inject(optional=false) annotated to setter method.
74
            throw $unbound;
75
        }
76
    }
77
}
78