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

ReflectionClass   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 132
Duplicated Lines 12.88 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 97.73%
Metric Value
wmc 9
lcom 1
cbo 10
dl 17
loc 132
ccs 43
cts 44
cp 0.9773
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A withProperty() 0 9 1
A withProperties() 0 9 1
A getProperties() 0 4 1
A getInjectionStrategies() 0 4 1
A getInstanciator() 0 4 1
A buildObject() 0 19 1
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\InjectionStrategy\SetterStrategy;
7
use Innmind\Reflection\InjectionStrategy\NamedMethodStrategy;
8
use Innmind\Reflection\InjectionStrategy\ReflectionStrategy;
9
use Innmind\Reflection\Instanciator\ReflectionInstanciator;
10
use Innmind\Immutable\Collection;
11
use Innmind\Immutable\CollectionInterface;
12
use Innmind\Immutable\TypedCollection;
13
use Innmind\Immutable\TypedCollectionInterface;
14
15
class ReflectionClass
16
{
17
    private $class;
18
    private $properties;
19
    private $injectionStrategies;
20
    private $instanciator;
21
22 5
    public function __construct(
23
        string $class,
24
        CollectionInterface $properties = null,
25
        TypedCollectionInterface $injectionStrategies = null,
26
        InstanciatorInterface $instanciator = null
27
    ) {
28 5
        $this->class = $class;
29 5
        $this->properties = $properties ?? new Collection([]);
30 5
        $this->initInjectionStrategies($injectionStrategies);
31 5
        $this->instanciator = $instanciator ?? new ReflectionInstanciator;
32 5
    }
33
34
    /**
35
     * Add a property to be injected in the new object
36
     *
37
     * @param string $property
38
     * @param mixed $value
39
     *
40
     * @return self
41
     */
42 2
    public function withProperty(string $property, $value): self
43
    {
44 2
        return new self(
45 2
            $this->class,
46 2
            $this->properties->set($property, $value),
47 2
            $this->injectionStrategies,
48 2
            $this->instanciator
49
        );
50
    }
51
52
    /**
53
     * Add a set of properties that need to be injected
54
     *
55
     * @param array $properties
56
     *
57
     * @return self
58
     */
59 1
    public function withProperties(array $properties): self
60
    {
61 1
        return new self(
62 1
            $this->class,
63 1
            $this->properties->merge(new Collection($properties)),
64 1
            $this->injectionStrategies,
65 1
            $this->instanciator
66
        );
67
    }
68
69
    /**
70
     * Return the collection of properties that will be injected in the object
71
     *
72
     * @return CollectionInterface
73
     */
74 1
    public function getProperties(): CollectionInterface
75
    {
76 1
        return $this->properties;
77
    }
78
79
    /**
80
     * Return the list of injection strategies used
81
     *
82
     * @return TypedCollectionInterface
83
     */
84 1
    public function getInjectionStrategies(): TypedCollectionInterface
85
    {
86 1
        return $this->injectionStrategies;
87
    }
88
89
    /**
90
     * Return the object instanciator
91
     *
92
     * @return InstanciatorInterface
93
     */
94 1
    public function getInstanciator(): InstanciatorInterface
95
    {
96 1
        return $this->instanciator;
97
    }
98
99
    /**
100
     * Return a new instance of the class
101
     *
102
     * @return object
103
     */
104 2
    public function buildObject()
105
    {
106 2
        $object = $this->instanciator->build($this->class, $this->properties);
107 2
        $parameters = $this->instanciator->getParameters($this->class);
108
109
        //avoid injecting the properties already used in the constructor
110
        $properties = $this
111 2
            ->properties
112 2
            ->filter(function ($value, $property) use ($parameters) {
113 1
                return !$parameters->contains($property);
0 ignored issues
show
Bug introduced by
The method contains() does not seem to exist on object<Innmind\Immutable\CollectionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
114 2
            });
115 2
        $refl = new ReflectionObject(
116
            $object,
117
            $properties,
118 2
            $this->injectionStrategies
119
        );
120
121 2
        return $refl->buildObject();
122
    }
123
124
    /**
125
     * @param TypedCollectionInterface $strategies
126
     *
127
     * @return void
128
     */
129 5 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...
130
    {
131 5
        $strategies = $strategies ?? new TypedCollection(
132 5
            InjectionStrategyInterface::class,
133
            [
134 5
                new SetterStrategy,
135 5
                new NamedMethodStrategy,
136 5
                new ReflectionStrategy,
137
            ]
138
        );
139
140 5
        if ($strategies->getType() !== InjectionStrategyInterface::class) {
141
            throw new InvalidArgumentException;
142
        }
143
144 5
        $this->injectionStrategies = $strategies;
145 5
    }
146
}
147