GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 366021...222b88 )
by Baptiste
02:10
created

ReflectionInstanciator::build()   B

Complexity

Conditions 3
Paths 7

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3
Metric Value
dl 0
loc 27
ccs 12
cts 12
cp 1
rs 8.8571
cc 3
eloc 17
nc 7
nop 2
crap 3
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Reflection\Instanciator;
5
6
use Innmind\Reflection\InstanciatorInterface;
7
use Innmind\Reflection\Exception\InstanciationFailedException;
8
use Innmind\Immutable\CollectionInterface;
9
use Innmind\Immutable\Collection;
10
11
class ReflectionInstanciator implements InstanciatorInterface
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16 4
    public function build(string $class, CollectionInterface $properties)
17
    {
18
        try {
19 4
            $refl = new \ReflectionClass($class);
20
21 4
            if (!$refl->hasMethod('__construct')) {
22 3
                return $refl->newInstance();
23
            }
24
25 3
            $constructor = $refl->getMethod('__construct');
26
27 3
            return $refl->newInstanceArgs(
28
                $this
29 3
                    ->computeArguments($constructor, $properties)
30 3
                    ->toPrimitive()
31
            );
32 1
        } catch (\TypeError $e) {
33 1
            throw new InstanciationFailedException(
34
                sprintf(
35 1
                    'Class "%s" cannot be instanciated',
36
                    $class
37
                ),
38 1
                $e->getCode(),
39
                $e
40
            );
41
        }
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 3
    public function getParameters(string $class): CollectionInterface
48
    {
49 3
        $refl = new \ReflectionClass($class);
50
51 3
        if (!$refl->hasMethod('__construct')) {
52 3
            return new Collection([]);
53
        }
54
55 2
        $refl = $refl->getMethod('__construct');
56 2
        $parameters = [];
57
58 2
        foreach ($refl->getParameters() as $parameter) {
59 2
            $parameters[] = $parameter->name;
60
        }
61
62 2
        return new Collection($parameters);
63
    }
64
65
    /**
66
     * @param ReflectionMethod $constructor
67
     * @param CollectionInterface $properties
68
     *
69
     * @return CollectionInterface
70
     */
71 3
    private function computeArguments(
72
        \ReflectionMethod $constructor,
73
        CollectionInterface $properties
74
    ): CollectionInterface {
75 3
        $arguments = new Collection([]);
76
77 3
        foreach ($constructor->getParameters() as $parameter) {
78 3
            if ($this->canInject($parameter, $properties)) {
79 2
                $arguments = $arguments->set(
80 2
                    $parameter->name,
81 3
                    $properties[$parameter->name]
82
                );
83
            }
84
        }
85
86 3
        return $arguments;
87
    }
88
89
    /**
90
     * @param ReflectionParameter $parameter
91
     * @param CollectionInterface $properties
92
     *
93
     * @return bool
94
     */
95 3
    private function canInject(
96
        \ReflectionParameter $parameter,
97
        CollectionInterface $properties
98
    ): bool {
99
        if (
100 3
            !$parameter->allowsNull() &&
101 3
            !$properties->hasKey($parameter->name)
102
        ) {
103 1
            return false;
104
        } else if (
105 3
            $parameter->allowsNull() &&
106 3
            !$properties->hasKey($parameter->name)
107
        ) {
108 2
            return false;
109
        }
110
111 2
        $property = $properties[$parameter->name];
112
113 2
        if ($parameter->hasType()) {
114 1
            $type = $parameter->getType();
115
116 1
            if ($type->isBuiltin()) {
117 1
                return (string) $type === gettype($property);
118 1
            } else if (!is_object($property)) {
119
                return false;
120
            }
121
122 1
            $refl = new \ReflectionObject($property);
123 1
            $wishedClass = (string) $type;
124
125 1
            return get_class($property) === $wishedClass ||
126 1
                $refl->isSubClassOf($wishedClass);
127
        }
128
129 2
        return true;
130
    }
131
}
132