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
Pull Request — develop (#2)
by Mathieu
03:14
created

DelegationStrategy   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 97.3%

Importance

Changes 0
Metric Value
dl 0
loc 78
ccs 36
cts 37
cp 0.973
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A generateKey() 0 3 1
A supports() 0 12 2
B inject() 0 33 5
A __construct() 0 8 2
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