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 ( fb2cf2...a0a09f )
by Baptiste
02:11
created

ReflectionObject::extractProperty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3
Metric Value
dl 13
loc 13
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Reflection;
5
6
use Innmind\Reflection\Exception\InvalidArgumentException;
7
use Innmind\Reflection\Exception\LogicException;
8
use Innmind\Immutable\Collection;
9
use Innmind\Immutable\CollectionInterface;
10
use Innmind\Immutable\TypedCollectionInterface;
11
12
class ReflectionObject
13
{
14
    private $object;
15
    private $properties;
16
    private $injectionStrategies;
17
18 13
    public function __construct(
19
        $object,
20
        CollectionInterface $properties = null,
21
        TypedCollectionInterface $injectionStrategies = null,
22
        TypedCollectionInterface $extractionStrategies = null
23
    ) {
24 13
        if (!is_object($object)) {
25 1
            throw new InvalidArgumentException;
26
        }
27
28 12
        $injectionStrategies = $injectionStrategies ?? InjectionStrategies::defaults();
29 12
        $extractionStrategies = $extractionStrategies ?? ExtractionStrategies::defaults();
30
31 12
        if ($injectionStrategies->getType() !== InjectionStrategyInterface::class) {
32
            throw new InvalidArgumentException;
33
        }
34
35 12
        if ($extractionStrategies->getType() !== ExtractionStrategyInterface::class) {
36
            throw new InvalidArgumentException;
37
        }
38
39 12
        $this->object = $object;
40
41 12
        $this->properties = $properties ?? new Collection([]);
42 12
        $this->injectionStrategies = $injectionStrategies;
43 12
        $this->extractionStrategies = $extractionStrategies;
0 ignored issues
show
Bug introduced by
The property extractionStrategies does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44 12
    }
45
46
    /**
47
     * Add a property that will be injected
48
     *
49
     * @param string $name
50
     * @param mixed $value
51
     *
52
     * @return self
53
     */
54 4
    public function withProperty(string $name, $value)
55
    {
56 4
        return new self(
57 4
            $this->object,
58 4
            $this->properties->set($name, $value),
59 4
            $this->injectionStrategies,
60 4
            $this->extractionStrategies
61
        );
62
    }
63
64
    /**
65
     * Add a set of properties that need to be injected
66
     *
67
     * @param array $properties
68
     *
69
     * @return self
70
     */
71 1
    public function withProperties(array $properties): self
72
    {
73 1
        return new self(
74 1
            $this->object,
75 1
            $this->properties->merge(new Collection($properties)),
76 1
            $this->injectionStrategies,
77 1
            $this->extractionStrategies
78
        );
79
    }
80
81
    /**
82
     * Return the collection of properties that will be injected in the object
83
     *
84
     * @return CollectionInterface
85
     */
86 2
    public function getProperties(): CollectionInterface
87
    {
88 2
        return $this->properties;
89
    }
90
91
    /**
92
     * Return the list of injection strategies used
93
     *
94
     * @return TypedCollectionInterface
95
     */
96 1
    public function getInjectionStrategies(): TypedCollectionInterface
97
    {
98 1
        return $this->injectionStrategies;
99
    }
100
101
    /**
102
     * Return the list of extraction strategies used
103
     *
104
     * @return TypedCollectionInterface
105
     */
106 1
    public function getExtractionStrategies(): TypedCollectionInterface
107
    {
108 1
        return $this->extractionStrategies;
109
    }
110
111
    /**
112
     * Return the object with the list of properties set on it
113
     *
114
     * @return object
115
     */
116 7
    public function buildObject()
117
    {
118 7
        foreach ($this->properties as $key => $value) {
119 5
            $this->inject($key, $value);
120
        }
121
122 5
        return $this->object;
123
    }
124
125
    /**
126
     * Extract the given list of properties
127
     *
128
     * @param array $properties
129
     *
130
     * @return CollectionInterface
131
     */
132 2
    public function extract(array $properties): CollectionInterface
133
    {
134 2
        $values = [];
135
136 2
        foreach ($properties as $property) {
137 2
            $values[$property] = $this->extractProperty($property);
138
        }
139
140 1
        return new Collection($values);
141
    }
142
143
    /**
144
     * Inject the given key/value pair into the object
145
     *
146
     * @param string $key
147
     * @param mixed $value
148
     *
149
     * @return void
150
     */
151 5 View Code Duplication
    private function inject(string $key, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
    {
153 5
        foreach ($this->injectionStrategies as $strategy) {
154 5
            if ($strategy->supports($this->object, $key, $value)) {
155 3
                $strategy->inject($this->object, $key, $value);
156
157 5
                return;
158
            }
159
        }
160
161 2
        throw new LogicException(sprintf(
162 2
            'Property "%s" cannot be injected',
163
            $key
164
        ));
165
    }
166
167
    /**
168
     * Extract the given property out of the object
169
     *
170
     * @param string $property
171
     *
172
     * @return mixed
173
     */
174 2 View Code Duplication
    private function extractProperty(string $property)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176 2
        foreach ($this->extractionStrategies as $strategy) {
177 2
            if ($strategy->supports($this->object, $property)) {
178 2
                return $strategy->extract($this->object, $property);
179
            }
180
        }
181
182 1
        throw new LogicException(sprintf(
183 1
            'Property "%s" cannot be extracted',
184
            $property
185
        ));
186
    }
187
}
188