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.
Passed
Push — develop ( 7d1107...57445d )
by Baptiste
03:12
created

ReflectionClass::of()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 4
dl 0
loc 7
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Reflection;
5
6
use Innmind\Reflection\{
7
    InjectionStrategy\InjectionStrategies,
8
    Instanciator\ReflectionInstanciator,
9
    Exception\InvalidArgumentException,
10
};
11
use Innmind\Immutable\{
12
    MapInterface,
13
    Map,
14
    SetInterface,
15
    Set,
16
};
17
18
final class ReflectionClass
19
{
20
    private $class;
21
    private $properties;
22
    private $injectionStrategy;
23
    private $instanciator;
24
25 3
    public function __construct(
26
        string $class,
27
        MapInterface $properties = null,
28
        InjectionStrategy $injectionStrategy = null,
29
        Instanciator $instanciator = null
30
    ) {
31 3
        $properties = $properties ?? new Map('string', 'mixed');
32
33
        if (
34 3
            (string) $properties->keyType() !== 'string' ||
35 3
            (string) $properties->valueType() !== 'mixed'
36
        ) {
37
            throw new \TypeError('Argument 2 must be of type MapInterface<string, mixed>');
38
        }
39
40 3
        $this->class = $class;
41 3
        $this->properties = $properties;
42 3
        $this->injectionStrategy = $injectionStrategy ?? InjectionStrategies::default();
43 3
        $this->instanciator = $instanciator ?? new ReflectionInstanciator;
44 3
    }
45
46 3
    public static function of(
47
        string $class,
48
        MapInterface $properties = null,
49
        InjectionStrategy $injectionStrategy = null,
50
        Instanciator $instanciator = null
51
    ): self {
52 3
        return new self($class, $properties, $injectionStrategy, $instanciator);
53
    }
54
55
    /**
56
     * Add a property to be injected in the new object
57
     *
58
     * @param mixed  $value
59
     */
60 1
    public function withProperty(string $property, $value): self
61
    {
62 1
        return new self(
63 1
            $this->class,
64 1
            $this->properties->put($property, $value),
65 1
            $this->injectionStrategy,
66 1
            $this->instanciator
67
        );
68
    }
69
70
    /**
71
     * Add a set of properties that need to be injected
72
     *
73
     * @param array<string, mixed> $properties
74
     */
75 1
    public function withProperties(array $properties): self
76
    {
77 1
        $map = $this->properties;
78
79 1
        foreach ($properties as $key => $value) {
80 1
            $map = $map->put($key, $value);
81
        }
82
83 1
        return new self(
84 1
            $this->class,
85 1
            $map,
86 1
            $this->injectionStrategy,
87 1
            $this->instanciator
88
        );
89
    }
90
91
    /**
92
     * Return a new instance of the class
93
     */
94 2
    public function build(): object
95
    {
96 2
        $object = $this->instanciator->build($this->class, $this->properties);
97 2
        $parameters = $this->instanciator->parameters($this->class);
98
99
        //avoid injecting the properties already used in the constructor
100
        $properties = $this
101 2
            ->properties
102
            ->filter(function(string $property) use ($parameters) {
103 1
                return !$parameters->contains($property);
104 2
            });
105 2
        $refl = new ReflectionObject(
106 2
            $object,
107 2
            $properties,
108 2
            $this->injectionStrategy
109
        );
110
111 2
        return $refl->build();
112
    }
113
114
    /**
115
     * Return all the properties defined on the class
116
     *
117
     * It will not extract properties defined in a parent class
118
     *
119
     * @return SetInterface<string>
120
     */
121 1
    public function properties(): SetInterface
122
    {
123 1
        $refl = new \ReflectionClass($this->class);
124 1
        $properties = Set::of('string');
125
126 1
        foreach ($refl->getProperties() as $property) {
127 1
            $properties = $properties->add($property->getName());
128
        }
129
130 1
        return $properties;
131
    }
132
}
133