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 — master ( 379179...de8df8 )
by Baptiste
02:40
created

ReflectionClass::buildObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
crap 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A ReflectionClass::instanciator() 0 4 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Reflection;
5
6
use Innmind\Reflection\{
7
    InjectionStrategy\InjectionStrategies,
8
    Instanciator\ReflectionInstanciator,
9
    Exception\InvalidArgumentException
10
};
11
use Innmind\Immutable\{
12
    MapInterface,
13
    Map
14
};
15
16
class ReflectionClass
17
{
18
    private $class;
19
    private $properties;
20
    private $injectionStrategy;
21
    private $instanciator;
22
23 5 View Code Duplication
    public function __construct(
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...
24
        string $class,
25
        MapInterface $properties = null,
26
        InjectionStrategyInterface $injectionStrategy = null,
27
        InstanciatorInterface $instanciator = null
28
    ) {
29 5
        $properties = $properties ?? new Map('string', 'mixed');
30
31
        if (
32 5
            (string) $properties->keyType() !== 'string' ||
33 5
            (string) $properties->valueType() !== 'mixed'
34
        ) {
35
            throw new InvalidArgumentException;
36
        }
37
38 5
        $this->class = $class;
39 5
        $this->properties = $properties;
40 5
        $this->injectionStrategy = $injectionStrategy ?? InjectionStrategies::default();
41 5
        $this->instanciator = $instanciator ?? new ReflectionInstanciator;
42 5
    }
43
44
    /**
45
     * Add a property to be injected in the new object
46
     *
47
     * @param string $property
48
     * @param mixed  $value
49
     *
50
     * @return self
51
     */
52 2
    public function withProperty(string $property, $value): self
53
    {
54 2
        return new self(
55 2
            $this->class,
56 2
            $this->properties->put($property, $value),
1 ignored issue
show
Documentation introduced by
$property is of type string, but the function expects a object<Innmind\Immutable\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57 2
            $this->injectionStrategy,
58 2
            $this->instanciator
59
        );
60
    }
61
62
    /**
63
     * Add a set of properties that need to be injected
64
     *
65
     * @param array<string, mixed> $properties
66
     *
67
     * @return self
68
     */
69 1 View Code Duplication
    public function withProperties(array $properties): self
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...
70
    {
71 1
        $map = $this->properties;
72
73 1
        foreach ($properties as $key => $value) {
74 1
            $map = $map->put($key, $value);
75
        }
76
77 1
        return new self(
78 1
            $this->class,
79
            $map,
80 1
            $this->injectionStrategy,
81 1
            $this->instanciator
82
        );
83
    }
84
85
    /**
86
     * Return the collection of properties that will be injected in the object
87
     *
88
     * @return MapInterface<string, mixed>
0 ignored issues
show
Documentation introduced by
The doc-type MapInterface<string, could not be parsed: Expected "|" or "end of type", but got "<" at position 12. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
89
     */
90 1
    public function properties(): MapInterface
91
    {
92 1
        return $this->properties;
93
    }
94
95
    /**
96
     * Return the list of injection strategies used
97
     *
98
     * @return InjectionStrategiesInterface
99
     */
100 1
    public function injectionStrategy(): InjectionStrategyInterface
101
    {
102 1
        return $this->injectionStrategy;
103
    }
104
105
    /**
106
     * Return the object instanciator
107
     *
108
     * @return InstanciatorInterface
109
     */
110 1
    public function instanciator(): InstanciatorInterface
111
    {
112 1
        return $this->instanciator;
113
    }
114
115
    /**
116
     * Return a new instance of the class
117
     *
118
     * @return object
119
     */
120 2
    public function build()
121
    {
122 2
        $object = $this->instanciator->build($this->class, $this->properties);
123 2
        $parameters = $this->instanciator->parameters($this->class);
124
125
        //avoid injecting the properties already used in the constructor
126
        $properties = $this
127 2
            ->properties
128 2
            ->filter(function(string $property) use ($parameters) {
129 1
                return !$parameters->contains($property);
1 ignored issue
show
Documentation introduced by
$property is of type string, but the function expects a object<Innmind\Immutable\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
130 2
            });
131 2
        $refl = new ReflectionObject(
132
            $object,
133
            $properties,
134 2
            $this->injectionStrategy
135
        );
136
137 2
        return $refl->build();
138
    }
139
}
140