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
Pull Request — develop (#1)
by Hugues
10:01
created

ReflectionObject::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2
Metric Value
dl 0
loc 17
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 11
nc 2
nop 4
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Reflection;
5
6
use Innmind\Immutable\Collection;
7
use Innmind\Immutable\CollectionInterface;
8
use Innmind\Immutable\TypedCollectionInterface;
9
use Innmind\Reflection\Exception\InvalidArgumentException;
10
use Innmind\Reflection\ExtractionStrategy\DefaultExtractionStrategies;
11
use Innmind\Reflection\ExtractionStrategy\ExtractionStrategies;
12
use Innmind\Reflection\InjectionStrategy\DefaultInjectionStrategies;
13
use Innmind\Reflection\InjectionStrategy\InjectionStrategies;
14
15
class ReflectionObject
16
{
17
    private $object;
18
    private $properties;
19
    private $injectionStrategies;
20
    private $extractionStrategies;
21
22 13
    public function __construct(
23
        $object,
24
        CollectionInterface $properties = null,
25
        InjectionStrategies $injectionStrategies = null,
26
        ExtractionStrategies $extractionStrategies = null
27
    ) {
28 13
        if (!is_object($object)) {
29 1
            throw new InvalidArgumentException;
30
        }
31
32 12
        $this->injectionStrategies = $injectionStrategies ?? new DefaultInjectionStrategies();
33 12
        $this->extractionStrategies = $extractionStrategies ?? new DefaultExtractionStrategies();
34
35 12
        $this->object = $object;
36
37 12
        $this->properties = $properties ?? new Collection([]);
38 12
    }
39
40
    /**
41
     * Add a property that will be injected
42
     *
43
     * @param string $name
44
     * @param mixed $value
45
     *
46
     * @return self
47
     */
48 4
    public function withProperty(string $name, $value)
49
    {
50 4
        return new self(
51 4
            $this->object,
52 4
            $this->properties->set($name, $value),
53 4
            $this->injectionStrategies,
54 4
            $this->extractionStrategies
55
        );
56
    }
57
58
    /**
59
     * Add a set of properties that need to be injected
60
     *
61
     * @param array $properties
62
     *
63
     * @return self
64
     */
65 1
    public function withProperties(array $properties): self
66
    {
67 1
        return new self(
68 1
            $this->object,
69 1
            $this->properties->merge(new Collection($properties)),
70 1
            $this->injectionStrategies,
71 1
            $this->extractionStrategies
72
        );
73
    }
74
75
    /**
76
     * Return the collection of properties that will be injected in the object
77
     *
78
     * @return CollectionInterface
79
     */
80 2
    public function getProperties(): CollectionInterface
81
    {
82 2
        return $this->properties;
83
    }
84
85
    /**
86
     * Return the list of injection strategies used
87
     *
88
     * @return InjectionStrategies
89
     */
90 1
    public function getInjectionStrategies(): InjectionStrategies
91
    {
92 1
        return $this->injectionStrategies;
93
    }
94
95
    /**
96
     * Return the list of extraction strategies used
97
     *
98
     * @return ExtractionStrategies
99
     */
100 1
    public function getExtractionStrategies(): ExtractionStrategies
101
    {
102 1
        return $this->extractionStrategies;
103
    }
104
105
    /**
106
     * Return the object with the list of properties set on it
107
     *
108
     * @return object
109
     */
110 7
    public function buildObject()
111
    {
112 7
        foreach ($this->properties as $key => $value) {
113 5
            $this->inject($key, $value);
114
        }
115
116 5
        return $this->object;
117
    }
118
119
    /**
120
     * Extract the given list of properties
121
     *
122
     * @param array $properties
123
     *
124
     * @return CollectionInterface
125
     */
126 2
    public function extract(array $properties): CollectionInterface
127
    {
128 2
        $values = [];
129
130 2
        foreach ($properties as $property) {
131 2
            $values[$property] = $this->extractProperty($property);
132
        }
133
134 1
        return new Collection($values);
135
    }
136
137
    /**
138
     * Inject the given key/value pair into the object
139
     *
140
     * @param string $key
141
     * @param mixed  $value
142
     *
143
     * @return void
144
     */
145 5
    private function inject(string $key, $value)
146
    {
147
        $this
148 5
            ->injectionStrategies
149 5
            ->get($this->object, $key, $value)
150 3
            ->inject($this->object, $key, $value);
151 3
    }
152
153
    /**
154
     * Extract the given property out of the object
155
     *
156
     * @param string $property
157
     *
158
     * @return mixed
159
     */
160 2
    private function extractProperty(string $property)
161
    {
162
        return $this
163 2
            ->extractionStrategies
164 2
            ->get($this->object,$property)
165 1
            ->extract($this->object,$property);
166
    }
167
}
168