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 ( 366021...222b88 )
by Baptiste
02:10
created

ReflectionObject   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 135
Duplicated Lines 12.59 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 97.62%
Metric Value
wmc 13
lcom 1
cbo 9
dl 17
loc 135
ccs 41
cts 42
cp 0.9762
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A withProperty() 0 8 1
A withProperties() 0 8 1
A getProperties() 0 4 1
A getInjectionStrategies() 0 4 1
A buildObject() 0 8 2
A inject() 0 15 3
A initInjectionStrategies() 17 17 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Reflection\InjectionStrategy\SetterStrategy;
9
use Innmind\Reflection\InjectionStrategy\NamedMethodStrategy;
10
use Innmind\Reflection\InjectionStrategy\ReflectionStrategy;
11
use Innmind\Immutable\Collection;
12
use Innmind\Immutable\CollectionInterface;
13
use Innmind\Immutable\TypedCollection;
14
use Innmind\Immutable\TypedCollectionInterface;
15
16
class ReflectionObject
17
{
18
    private $object;
19
    private $properties;
20
    private $injectionStrategies;
21
22 10
    public function __construct(
23
        $object,
24
        CollectionInterface $properties = null,
25
        TypedCollectionInterface $injectionStrategies = null
26
    ) {
27 10
        if (!is_object($object)) {
28 1
            throw new InvalidArgumentException;
29
        }
30
31 9
        $this->object = $object;
32
33 9
        $this->properties = $properties ?? new Collection([]);
34 9
        $this->initInjectionStrategies($injectionStrategies);
35 9
    }
36
37
    /**
38
     * Add a property that will be injected
39
     *
40
     * @param string $name
41
     * @param mixed $value
42
     *
43
     * @return self
44
     */
45 4
    public function withProperty(string $name, $value)
46
    {
47 4
        return new self(
48 4
            $this->object,
49 4
            $this->properties->set($name, $value),
50 4
            $this->injectionStrategies
51
        );
52
    }
53
54
    /**
55
     * Add a set of properties that need to be injected
56
     *
57
     * @param array $properties
58
     *
59
     * @return self
60
     */
61 1
    public function withProperties(array $properties): self
62
    {
63 1
        return new self(
64 1
            $this->object,
65 1
            $this->properties->merge(new Collection($properties)),
66 1
            $this->injectionStrategies
67
        );
68
    }
69
70
    /**
71
     * Return the collection of properties that will be injected in the object
72
     *
73
     * @return CollectionInterface
74
     */
75 2
    public function getProperties(): CollectionInterface
76
    {
77 2
        return $this->properties;
78
    }
79
80
    /**
81
     * Return the list of injection strategies used
82
     *
83
     * @return TypedCollectionInterface
84
     */
85 1
    public function getInjectionStrategies(): TypedCollectionInterface
86
    {
87 1
        return $this->injectionStrategies;
88
    }
89
90
    /**
91
     * Return the object with the list of properties set on it
92
     *
93
     * @return object
94
     */
95 7
    public function buildObject()
96
    {
97 7
        foreach ($this->properties as $key => $value) {
98 5
            $this->inject($key, $value);
99
        }
100
101 5
        return $this->object;
102
    }
103
104
    /**
105
     * Inject the given key/value pair into the object
106
     *
107
     * @param string $key
108
     * @param mixed $value
109
     *
110
     * @return void
111
     */
112 5
    private function inject(string $key, $value)
113
    {
114 5
        foreach ($this->injectionStrategies as $strategy) {
115 5
            if ($strategy->supports($this->object, $key, $value)) {
116 3
                $strategy->inject($this->object, $key, $value);
117
118 5
                return;
119
            }
120
        }
121
122 2
        throw new LogicException(sprintf(
123 2
            'Property "%s" cannot be injected',
124
            $key
125
        ));
126
    }
127
128
    /**
129
     * @param TypedCollectionInterface $strategies
130
     *
131
     * @return void
132
     */
133 9 View Code Duplication
    private function initInjectionStrategies(TypedCollectionInterface $strategies = null)
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...
134
    {
135 9
        $strategies = $strategies ?? new TypedCollection(
136 7
            InjectionStrategyInterface::class,
137
            [
138 7
                new SetterStrategy,
139 7
                new NamedMethodStrategy,
140 9
                new ReflectionStrategy,
141
            ]
142
        );
143
144 9
        if ($strategies->getType() !== InjectionStrategyInterface::class) {
145
            throw new InvalidArgumentException;
146
        }
147
148 9
        $this->injectionStrategies = $strategies;
149 9
    }
150
}
151