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 ( 4725ce...7d1107 )
by Baptiste
11s
created

DelegationStrategy::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 5
cp 0.8
crap 2.032
rs 9.4285
c 0
b 0
f 0
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
23
     */
24 7
    public function __construct(StreamInterface $strategies)
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 1
                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)) {
61
            $this
62 1
                ->cache
63 1
                ->get($key)
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);
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