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 — master ( de8df8...cc25ac )
by Baptiste
03:22
created

ReflectionInstanciator   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 98%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 119
ccs 49
cts 50
cp 0.98
rs 10
c 0
b 0
f 0
wmc 18

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parameters() 0 16 3
A build() 0 25 3
B canInject() 0 35 9
A computeArguments() 0 16 3
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Reflection\Instanciator;
5
6
use Innmind\Reflection\{
7
    Instanciator,
8
    Exception\InstanciationFailed,
9
};
10
use Innmind\Immutable\{
11
    MapInterface,
12
    SetInterface,
13
    Map,
14
    Set,
15
};
16
17
final class ReflectionInstanciator implements Instanciator
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 4
    public function build(string $class, MapInterface $properties): object
23
    {
24
        try {
25 4
            $refl = new \ReflectionClass($class);
26
27 4
            if (!$refl->hasMethod('__construct')) {
28 3
                return $refl->newInstance();
29
            }
30
31 3
            $constructor = $refl->getMethod('__construct');
32
33 3
            return $refl->newInstanceArgs(
34
                $this
35 3
                    ->computeArguments($constructor, $properties)
36 3
                    ->reduce(
37 3
                        [],
38
                        function(array $carry, string $property, $value): array {
39 2
                            $carry[$property] = $value;
40
41 2
                            return $carry;
42 3
                        }
43
                    )
44
            );
45 1
        } catch (\TypeError $e) {
46 1
            throw new InstanciationFailed($class, $e);
47
        }
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 3
    public function parameters(string $class): SetInterface
54
    {
55 3
        $parameters = new Set('string');
56 3
        $refl = new \ReflectionClass($class);
57
58 3
        if (!$refl->hasMethod('__construct')) {
59 3
            return $parameters;
60
        }
61
62 2
        $refl = $refl->getMethod('__construct');
63
64 2
        foreach ($refl->getParameters() as $parameter) {
65 2
            $parameters = $parameters->add($parameter->name);
66
        }
67
68 2
        return $parameters;
69
    }
70
71
    /**
72
     * @param ReflectionMethod $constructor
0 ignored issues
show
Bug introduced by
The type Innmind\Reflection\Instanciator\ReflectionMethod was not found. Did you mean ReflectionMethod? If so, make sure to prefix the type with \.
Loading history...
73
     * @param MapInterface<string, variable> $properties
74
     *
75
     * @return MapInterface<string, variable>
76
     */
77 3
    private function computeArguments(
78
        \ReflectionMethod $constructor,
79
        MapInterface $properties
80
    ): MapInterface {
81 3
        $arguments = $properties->clear();
82
83 3
        foreach ($constructor->getParameters() as $parameter) {
84 3
            if ($this->canInject($parameter, $properties)) {
85 2
                $arguments = $arguments->put(
86 2
                    $parameter->name,
87 3
                    $properties->get($parameter->name)
88
                );
89
            }
90
        }
91
92 3
        return $arguments;
93
    }
94
95
    /**
96
     * @param ReflectionParameter $parameter
0 ignored issues
show
Bug introduced by
The type Innmind\Reflection\Insta...tor\ReflectionParameter was not found. Did you mean ReflectionParameter? If so, make sure to prefix the type with \.
Loading history...
97
     * @param MapInterface<string, variable> $properties
98
     *
99
     * @return bool
100
     */
101 3
    private function canInject(
102
        \ReflectionParameter $parameter,
103
        MapInterface $properties
104
    ): bool {
105
        if (
106 3
            !$parameter->allowsNull() &&
107 3
            !$properties->contains($parameter->name)
108
        ) {
109 1
            return false;
110
        } else if (
111 3
            $parameter->allowsNull() &&
112 3
            !$properties->contains($parameter->name)
113
        ) {
114 2
            return false;
115
        }
116
117 2
        $property = $properties->get($parameter->name);
118
119 2
        if ($parameter->hasType()) {
120 1
            $type = $parameter->getType();
121
122 1
            if ($type->isBuiltin()) {
123 1
                return (string) $type === gettype($property);
124 1
            } else if (!is_object($property)) {
125
                return false;
126
            }
127
128 1
            $refl = new \ReflectionObject($property);
129 1
            $wishedClass = (string) $type;
130
131 1
            return get_class($property) === $wishedClass ||
132 1
                $refl->isSubClassOf($wishedClass);
133
        }
134
135 2
        return true;
136
    }
137
}
138