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::__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\ExtractionStrategy;
5
6
use Innmind\Reflection\{
7
    ExtractionStrategyInterface,
8
    Exception\InvalidArgumentException,
9
    Exception\PropertyCannotBeExtractedException
10
};
11
use Innmind\Immutable\{
12
    StreamInterface,
13
    Map
14
};
15
16
final class DelegationStrategy implements ExtractionStrategyInterface
17
{
18
    private $strategies;
19
    private $cache;
20
21
    /**
22
     * @param StreamInterface<ExtractionStrategyInterface> $strategies
23
     */
24 7
    public function __construct(StreamInterface $strategies)
25
    {
26 7
        if ((string) $strategies->type() !== ExtractionStrategyInterface::class) {
27
            throw new InvalidArgumentException;
28
        }
29
30 7
        $this->strategies = $strategies;
31 7
        $this->cache = new Map('string', ExtractionStrategyInterface::class);
32 7
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 1
    public function supports($object, string $property): bool
38
    {
39
        return $this
40 1
            ->strategies
41 1
            ->reduce(
42 1
                false,
43 1
                function(bool $supports, ExtractionStrategyInterface $strategy) use ($object, $property): bool {
44 1
                    if ($supports === true) {
45 1
                        return true;
46
                    }
47
48 1
                    return $strategy->supports($object, $property);
49 1
                }
50
            );
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 6
    public function extract($object, string $property)
57
    {
58 6
        $key = $this->generateKey($object, $property);
59
60 6
        if ($this->cache->contains($key)) {
61
            return $this
62 1
                ->cache
63 1
                ->get($key)
64 1
                ->extract($object, $property);
65
        }
66
67 6
        $strategy = $this->strategies->reduce(
68 6
            null,
69 6
            function($target, ExtractionStrategyInterface $strategy) use ($object, $property) {
70 6
                if ($target instanceof ExtractionStrategyInterface) {
71 3
                    return $target;
72
                }
73
74 6
                if ($strategy->supports($object, $property)) {
75 4
                    return $strategy;
76
                }
77 6
            }
78
        );
79
80 6
        if (!$strategy instanceof ExtractionStrategyInterface) {
81 2
            throw new PropertyCannotBeExtractedException($property);
82
        }
83
84 4
        $this->cache = $this->cache->put($key, $strategy);
85
86 4
        return $strategy->extract($object, $property);
87
    }
88
89 6
    private function generateKey($object, string $property): string
90
    {
91 6
        return get_class($object).'::'.$property;
92
    }
93
}
94