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

ReflectionObject::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Reflection;
5
6
use Innmind\Reflection\{
7
    InjectionStrategyInterface,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Innmind\Reflection\InjectionStrategyInterface.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
    ExtractionStrategyInterface,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Innmind\Reflection\ExtractionStrategyInterface.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
    ExtractionStrategy\ExtractionStrategies,
10
    InjectionStrategy\InjectionStrategies,
11
    Exception\InvalidArgumentException
12
};
13
use Innmind\Immutable\{
14
    MapInterface,
15
    Map
16
};
17
18
class ReflectionObject
19
{
20
    private $object;
21
    private $properties;
22
    private $injectionStrategy;
23
    private $extractionStrategy;
24
25 13 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...
26
        $object,
27
        MapInterface $properties = null,
28
        InjectionStrategyInterface $injectionStrategy = null,
29
        ExtractionStrategyInterface $extractionStrategy = null
30
    ) {
31 13
        $properties = $properties ?? new Map('string', 'mixed');
32
33
        if (
34 13
            !is_object($object) ||
35 12
            (string) $properties->keyType() !== 'string' ||
36 13
            (string) $properties->valueType() !== 'mixed'
37
        ) {
38 1
            throw new InvalidArgumentException;
39
        }
40
41 12
        $this->object = $object;
42 12
        $this->properties = $properties;
43 12
        $this->injectionStrategy = $injectionStrategy ?? InjectionStrategies::default();
44 12
        $this->extractionStrategy = $extractionStrategy ?? ExtractionStrategies::default();
45 12
    }
46
47
    /**
48
     * Add a property that will be injected
49
     *
50
     * @param string $name
51
     * @param mixed  $value
52
     *
53
     * @return self
54
     */
55 4
    public function withProperty(string $name, $value)
56
    {
57 4
        return new self(
58 4
            $this->object,
59 4
            $this->properties->put($name, $value),
1 ignored issue
show
Documentation introduced by
$name 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...
60 4
            $this->injectionStrategy,
61 4
            $this->extractionStrategy
62
        );
63
    }
64
65
    /**
66
     * Add a set of properties that need to be injected
67
     *
68
     * @param array<string, mixed> $properties
69
     *
70
     * @return self
71
     */
72 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...
73
    {
74 1
        $map = $this->properties;
75
76 1
        foreach ($properties as $key => $value) {
77 1
            $map = $map->put($key, $value);
78
        }
79
80 1
        return new self(
81 1
            $this->object,
82
            $map,
83 1
            $this->injectionStrategy,
84 1
            $this->extractionStrategy
85
        );
86
    }
87
88
    /**
89
     * Return the collection of properties that will be injected in the object
90
     *
91
     * @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...
92
     */
93 2
    public function properties(): MapInterface
94
    {
95 2
        return $this->properties;
96
    }
97
98
    /**
99
     * Return the list of injection strategies used
100
     *
101
     * @return InjectionStrategyInterface
102
     */
103 1
    public function injectionStrategy(): InjectionStrategyInterface
104
    {
105 1
        return $this->injectionStrategy;
106
    }
107
108
    /**
109
     * Return the list of extraction strategies used
110
     *
111
     * @return ExtractionStrategyInterface
112
     */
113 1
    public function extractionStrategy(): ExtractionStrategyInterface
114
    {
115 1
        return $this->extractionStrategy;
116
    }
117
118
    /**
119
     * Return the object with the list of properties set on it
120
     *
121
     * @return object
122
     */
123
    public function build()
124
    {
125 7
        $this->properties->foreach(function(string $key, $value): void {
126 5
            $this->inject($key, $value);
127 7
        });
128
129 5
        return $this->object;
130
    }
131
132
    /**
133
     * Extract the given list of properties
134
     *
135
     * @param string[] $properties
136
     *
137
     * @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...
138
     */
139 2
    public function extract(array $properties): MapInterface
140
    {
141 2
        $map = new Map('string', 'mixed');
142
143 2
        foreach ($properties as $property) {
144 2
            $map = $map->put(
145
                $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...
146 2
                $this->extractProperty($property)
147
            );
148
        }
149
150 1
        return $map;
151
    }
152
153
    /**
154
     * Inject the given key/value pair into the object
155
     *
156
     * @param string $key
157
     * @param mixed  $value
158
     *
159
     * @return void
160
     */
161 5
    private function inject(string $key, $value): void
162
    {
163 5
        $this->injectionStrategy->inject($this->object, $key, $value);
164 3
    }
165
166
    /**
167
     * Extract the given property out of the object
168
     *
169
     * @param string $property
170
     *
171
     * @return mixed
172
     */
173 2
    private function extractProperty(string $property)
174
    {
175 2
        return $this->extractionStrategy->extract($this->object, $property);
176
    }
177
}
178