Passed
Push — master ( 40971c...cb0a1d )
by Alexander
04:06
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\ClassDefinition;
8
use Yiisoft\Factory\Definitions\DefinitionInterface;
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
    /**
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 12
    public function fromClassName(string $class): array
23
    {
24 12
        $reflectionClass = new \ReflectionClass($class);
25 12
        if (!$reflectionClass->isInstantiable()) {
26 5
            throw new NotInstantiableException($class);
27
        }
28 12
        $constructor = $reflectionClass->getConstructor();
29 12
        return $constructor === null ? [] : $this->fromFunction($constructor);
30
    }
31
32
    /**
33
     * @suppress PhanUndeclaredMethod
34
     */
35 11
    private function fromFunction(\ReflectionFunctionAbstract $reflectionFunction): array
36
    {
37 11
        $result = [];
38 11
        foreach ($reflectionFunction->getParameters() as $parameter) {
39 11
            $result[$parameter->getName()] = $this->fromParameter($parameter);
40
        }
41 11
        return $result;
42
    }
43
44
    /**
45
     * @suppress PhanUndeclaredMethod
46
     */
47 11
    private function fromParameter(\ReflectionParameter $parameter): DefinitionInterface
48
    {
49 11
        $type = $parameter->getType();
50
51
        // PHP 8 union type is used as type hint
52
        /** @psalm-suppress UndefinedClass, TypeDoesNotContainType */
53 11
        if ($type instanceof \ReflectionUnionType) {
0 ignored issues
show
Bug introduced by
The type ReflectionUnionType 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...
54
            $types = [];
55
            foreach ($type->getTypes() as $unionType) {
0 ignored issues
show
Bug introduced by
The method getTypes() does not exist on ReflectionType. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
            foreach ($type->/** @scrutinizer ignore-call */ getTypes() as $unionType) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
                if (!$unionType->isBuiltin()) {
57
                    $types[] = $unionType->getName();
58
                }
59
            }
60
            return new ClassDefinition(implode('|', $types), $type->allowsNull());
61
        }
62
63
        // Our parameter has a class type hint
64 11
        if ($type !== null && !$type->isBuiltin()) {
65 7
            return new ClassDefinition($type->getName(), $type->allowsNull());
66
        }
67
68
        // Our parameter does not have a class type hint and either has a default value or is nullable.
69 5
        return new ParameterDefinition(
70 5
            $parameter,
71 5
            $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null,
72 5
            $type !== null ? $type->getName() : null
73
        );
74
    }
75
76
    public function fromCallable(callable $callable): array
77
    {
78
        return $this->fromFunction(new \ReflectionFunction(\Closure::fromCallable($callable)));
79
    }
80
}
81