Passed
Pull Request — 2.x (#310)
by Akihito
02:43 queued 01:26
created

Arguments::accept()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Ray\Di\Exception\NoHint;
8
use Ray\Di\Exception\Unbound;
9
use ReflectionMethod;
10
11
use function sprintf;
12
13
/**
14
 * @psalm-import-type ArgumentsList from Types
15
 * @psalm-import-type MethodArguments from Types
16
 */
17
final class Arguments implements AcceptInterface
18
{
19
    /** @var ArgumentsList */
0 ignored issues
show
Bug introduced by
The type Ray\Di\ArgumentsList 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...
20
    private array $arguments = [];
21
22
    public function __construct(ReflectionMethod $method, Name $name)
23
    {
24
        $parameters = $method->getParameters();
25
        foreach ($parameters as $parameter) {
26
            $this->arguments[] = new Argument($parameter, $name($parameter));
27
        }
28
    }
29
30
    /**
31
     * Return arguments
32
     *
33
     * @return list<mixed>
0 ignored issues
show
Bug introduced by
The type Ray\Di\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...
34
     *
35
     * @throws Exception\Unbound
36
     */
37
    public function inject(Container $container): array
38
    {
39
        $parameters = [];
40
        foreach ($this->arguments as $parameter) {
41
            /** @psalm-suppress MixedAssignment */
42
            $parameters[] = $this->getParameter($container, $parameter);
43
        }
44
45
        return $parameters;
46
    }
47
48
    public function accept(VisitorInterface $visitor): void
49
    {
50
        $visitor->visitArguments($this->arguments);
51
    }
52
53
    /**
54
     * @return mixed
55
     *
56
     * @throws Unbound
57
     */
58
    private function getParameter(Container $container, Argument $argument)
59
    {
60
        $this->bindInjectionPoint($container, $argument);
61
        try {
62
            return $container->getDependency((string) $argument);
63
        } catch (Unbound $unbound) {
64
            if ($argument->isDefaultAvailable()) {
65
                return $argument->getDefaultValue();
66
            }
67
68
            if ($unbound instanceof NoHint) {
69
                throw new NoHint($this->getNoHintMsg($argument), 0, $unbound);
70
            }
71
72
            throw new Unbound($argument->getMeta(), 0, $unbound);
73
        }
74
    }
75
76
    private function bindInjectionPoint(Container $container, Argument $argument): void
77
    {
78
        $isSelf = (string) $argument === InjectionPointInterface::class . '-' . Name::ANY;
79
        if ($isSelf) {
80
            return;
81
        }
82
83
        (new Bind($container, InjectionPointInterface::class))->toInstance(new InjectionPoint($argument->get()));
84
    }
85
86
    private function getNoHintMsg(Argument $argument): string
87
    {
88
        $ref = $argument->get();
89
        $func = $ref->getDeclaringFunction();
90
        $fileName = $func->getFileName();
91
92
        return sprintf(
93
            '$%s (%s:%d)',
94
            $ref->getName(),
95
            $fileName !== false ? $fileName : 'unknown',
96
            $func->getStartLine()
97
        );
98
    }
99
}
100