Passed
Push — master ( 5e9345...9dff6c )
by Alexander
02:08 queued 38s
created

DefinitionExtractor::fromClassName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Factory\Extractors;
6
7
use Yiisoft\Factory\Definitions\DefinitionInterface;
8
use Yiisoft\Factory\Definitions\ClassDefinition;
9
use Yiisoft\Factory\Definitions\ParameterDefinition;
10
use Yiisoft\Factory\Exceptions\NotInstantiableException;
11
12
/**
13
 * Class DefinitionExtractor
14
 * This implementation resolves dependencies by using class type hints.
15
 * Note that service names need not match the parameter names, parameter names are ignored
16
 */
17
class DefinitionExtractor implements ExtractorInterface
18
{
19 12
    public function fromClassName(string $class): array
20
    {
21 12
        $reflectionClass = new \ReflectionClass($class);
22 12
        if (!$reflectionClass->isInstantiable()) {
23 5
            throw new NotInstantiableException($class);
24
        }
25 12
        $constructor = $reflectionClass->getConstructor();
26 12
        return $constructor === null ? [] : $this->fromFunction($constructor);
27
    }
28
29
    /**
30
     * @suppress PhanUndeclaredMethod
31
     */
32 10
    private function fromFunction(\ReflectionFunctionAbstract $reflectionFunction): array
33
    {
34 10
        $result = [];
35 10
        foreach ($reflectionFunction->getParameters() as $parameter) {
36 10
            $result[$parameter->getName()] = $this->fromParameter($parameter);
37
        }
38 10
        return $result;
39
    }
40
41
    /**
42
     * @suppress PhanUndeclaredMethod
43
     */
44 10
    private function fromParameter(\ReflectionParameter $parameter): DefinitionInterface
45
    {
46 10
        $type = $parameter->getType();
47 10
        $hasDefault = $parameter->isOptional() || $parameter->isDefaultValueAvailable() || (isset($type) && $type->allowsNull());
0 ignored issues
show
Unused Code introduced by
The assignment to $hasDefault is dead and can be removed.
Loading history...
48
49
        // Our parameter has a class type hint
50 10
        if ($type !== null && !$type->isBuiltin()) {
51 7
            return new ClassDefinition($type->getName(), $type->allowsNull());
52
        }
53
54
        // Our parameter does not have a class type hint and either has a default value or is nullable.
55 4
        return new ParameterDefinition(
56
            $parameter,
57 4
            $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null,
58 4
            $type !== null ? $type->getName() : null
59
        );
60
    }
61
62
    public function fromCallable(callable $callable): array
63
    {
64
        return $this->fromFunction(new \ReflectionFunction(\Closure::fromCallable($callable)));
65
    }
66
}
67