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
Branch develop (2e636d)
by Baptiste
02:40
created

ReflectionObject::buildObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 0
crap 2
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\StringPrimitive;
11
12
class ReflectionObject
13
{
14
    private $object;
15
    private $properties;
16
    private $setter;
17
18 5
    public function __construct(
19
        $object,
20
        CollectionInterface $properties = null
21
    ) {
22 5
        if (!is_object($object)) {
23 1
            throw new InvalidArgumentException;
24
        }
25
26 4
        $this->object = $object;
27
28 4
        if ($properties === null) {
29 4
            $properties = new Collection([]);
30
        }
31
32 4
        $this->properties = $properties;
33 4
        $this->setter = new StringPrimitive('set%s');
34 4
    }
35
36
    /**
37
     * Add a property that will be injected
38
     *
39
     * @param string $name
40
     * @param mixed $value
41
     *
42
     * @return self
43
     */
44 3
    public function withProperty(string $name, $value)
45
    {
46 3
        return new self(
47 3
            $this->object,
48 3
            $this->properties->set($name, $value)
49
        );
50
    }
51
52
    /**
53
     * Return the collection of properties that will be injected in the object
54
     *
55
     * @return CollectionInterface
56
     */
57 2
    public function getProperties() : CollectionInterface
58
    {
59 2
        return $this->properties;
60
    }
61
62
    /**
63
     * Return the object with the list of properties set on it
64
     *
65
     * @return object
66
     */
67 3
    public function buildObject()
68
    {
69 3
        foreach ($this->properties as $key => $value) {
70 2
            $this->inject($key, $value);
71
        }
72
73 2
        return $this->object;
74
    }
75
76
    /**
77
     * Inject the given key/value pair into the object
78
     *
79
     * @param string $key
80
     * @param mixed $value
81
     *
82
     * @return void
83
     */
84 2
    private function inject(string $key, $value)
85
    {
86 2
        $refl = new \ReflectionObject($this->object);
87
88 2
        $setter = $this->setter->sprintf(ucfirst($key));
89
90 2
        if ($refl->hasMethod((string) $setter)) {
91 1
            $this->injectBySetter((string) $setter, $value);
92 2
        } else if ($refl->hasMethod($key)) {
93 1
            $this->injectBySetter($key, $value);
94 2
        } else if ($refl->hasProperty($key)) {
95 1
            $this->injectByReflection(
96 1
                $refl->getProperty($key),
97
                $value
98
            );
99
        } else {
100 1
            throw new LogicException(sprintf(
101 1
                'Property "%s" not found',
102
                $key
103
            ));
104
        }
105 1
    }
106
107
    /**
108
     * Inject the value through the given setter
109
     *
110
     * @param string $setter
111
     * @param mixed $value
112
     *
113
     * @return void
114
     */
115 1
    private function injectBySetter(string $setter, $value)
116
    {
117 1
        $this->object->$setter($value);
118 1
    }
119
120
    /**
121
     * Inject the given value through reflection
122
     *
123
     * @param ReflectionProperty $refl
124
     * @param mixed $value
125
     *
126
     * @return void
127
     */
128 1
    public function injectByReflection(\ReflectionProperty $refl, $value)
129
    {
130 1
        if (!$refl->isPublic()) {
131 1
            $refl->setAccessible(true);
132
        }
133
134 1
        $refl->setValue($this->object, $value);
135
136 1
        if (!$refl->isPublic()) {
137 1
            $refl->setAccessible(false);
138
        }
139 1
    }
140
}
141