Completed
Push — hotfix ( c7cd9f )
by Akihito
01:12
created

UntargetedBind::getType()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use ReflectionMethod;
8
use ReflectionNamedType;
9
use ReflectionParameter;
10
11
use function class_exists;
12
use function in_array;
13
14
final class UntargetedBind
15
{
16
    public function __invoke(Container $container, ReflectionMethod $method): void
17
    {
18
        $parameters = $method->getParameters();
19
        foreach ($parameters as $parameter) {
20
            $this->addConcreteClass($container, $parameter);
21
        }
22
    }
23
24
    private function addConcreteClass(Container $container, ReflectionParameter $parameter): void
25
    {
26
        $class = $this->getType($parameter);
27
        if (class_exists($class)) {
28
            new Bind($container, $class);
29
        }
30
    }
31
32
    private function getType(ReflectionParameter $parameter): string
33
    {
34
        $type = $parameter->getType();
35
36
        return $type instanceof ReflectionNamedType && ! in_array($type->getName(), Argument::UNBOUND_TYPE, true) ? $type->getName() : '';
0 ignored issues
show
Bug introduced by
The class ReflectionNamedType does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
37
    }
38
}
39