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 ( ae3fd9...4725ce )
by Baptiste
03:13
created

DelegationStrategy::inject()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 20
cts 20
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 19
nc 3
nop 3
crap 5
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Reflection\InjectionStrategy;
5
6
use Innmind\Reflection\{
7
    InjectionStrategyInterface,
8
    Exception\InvalidArgumentException,
9
    Exception\PropertyCannotBeInjectedException
10
};
11
use Innmind\Immutable\{
12
    StreamInterface,
13
    Map
14
};
15
16
final class DelegationStrategy implements InjectionStrategyInterface
17
{
18
    private $strategies;
19
    private $cache;
20
21
    /**
22
     * @param StreamInterface<InjectionStrategyInterface> $strategies
0 ignored issues
show
Documentation introduced by
The doc-type StreamInterface<InjectionStrategyInterface> could not be parsed: Expected "|" or "end of type", but got "<" at position 15. (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...
23
     */
24 7 View Code Duplication
    public function __construct(StreamInterface $strategies)
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...
25
    {
26 7
        if ((string) $strategies->type() !== InjectionStrategyInterface::class) {
27
            throw new InvalidArgumentException;
28
        }
29
30 7
        $this->strategies = $strategies;
31 7
        $this->cache = new Map('string', InjectionStrategyInterface::class);
32 7
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 1
    public function supports($object, string $property, $value): bool
38
    {
39
        return $this
40 1
            ->strategies
41 1
            ->reduce(
42 1
                false,
43
                function(bool $supports, InjectionStrategyInterface $strategy) use ($object, $property, $value): bool {
44 1
                    if ($supports === true) {
45 1
                        return true;
46
                    }
47
48 1
                    return $strategy->supports($object, $property, $value);
49 1
                }
50
            );
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 9
    public function inject($object, string $property, $value): void
57
    {
58 9
        $key = $this->generateKey($object, $property);
59
60 9
        if ($this->cache->contains($key)) {
1 ignored issue
show
Documentation introduced by
$key 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...
61
            $this
62 1
                ->cache
63 1
                ->get($key)
1 ignored issue
show
Documentation introduced by
$key 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...
64 1
                ->inject($object, $property, $value);
65
66 1
            return;
67
        }
68
69 9
        $strategy = $this->strategies->reduce(
70 9
            null,
71 9
            function($target, InjectionStrategyInterface $strategy) use ($object, $property, $value) {
72 9
                if ($target instanceof InjectionStrategyInterface) {
73 4
                    return $target;
74
                }
75
76 9
                if ($strategy->supports($object, $property, $value)) {
77 6
                    return $strategy;
78
                }
79 9
            }
80
        );
81
82 9
        if (!$strategy instanceof InjectionStrategyInterface) {
83 3
            throw new PropertyCannotBeInjectedException($property);
84
        }
85
86 6
        $this->cache = $this->cache->put($key, $strategy);
2 ignored issues
show
Documentation introduced by
$key 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...
Documentation introduced by
$strategy is of type object<Innmind\Reflectio...ctionStrategyInterface>, but the function expects a object<Innmind\Immutable\S>.

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...
87
88 6
        $strategy->inject($object, $property, $value);
89 6
    }
90
91 9
    private function generateKey($object, string $property): string
92
    {
93 9
        return get_class($object).'::'.$property;
94
    }
95
}
96