AssistedWebContextParam   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 35
rs 10
c 0
b 0
f 0
ccs 11
cts 11
cp 1
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setSuperGlobalsOnlyForTestingPurpose() 0 3 1
A __invoke() 0 11 2
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use Override;
8
use Ray\Di\InjectorInterface;
9
use Ray\WebContextParam\Annotation\AbstractWebContextParam;
10
11
use function assert;
12
use function is_string;
13
14
final class AssistedWebContextParam implements ParamInterface
15
{
16
    /**
17
     * $GLOBALS for testing
18
     *
19
     * @var array<string, array<string, mixed>>
20
     */
21
    private static array $globals = [];
22
23
    public function __construct(
24
        private readonly AbstractWebContextParam $webContextParam,
25
        private readonly ParamInterface $defaultParam,
26
    ) {
27
    }
28
29 5
    /**
30
     * {@inheritDoc}
31 5
     */
32 5
    #[Override]
33 5
    public function __invoke(string $varName, array $query, InjectorInterface $injector)
34
    {
35
        $superGlobals = static::$globals ?: $GLOBALS;
0 ignored issues
show
Bug introduced by
Since $globals is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $globals to at least protected.
Loading history...
36
        /** @var array<string, array<string, string>> $superGlobals */
37
        $webContextParam = $this->webContextParam;
38 4
        assert(is_string($webContextParam::GLOBAL_KEY));
39
        /** @psalm-suppress MixedArrayOffset */
40 4
        $phpWebContext = $superGlobals[$webContextParam::GLOBAL_KEY];
41 4
42 4
        return $phpWebContext[$this->webContextParam->key] ?? ($this->defaultParam)($varName, $query, $injector);
43
    }
44 4
45 2
    /** @param array<string, array<string, mixed>> $globals */
46
    public static function setSuperGlobalsOnlyForTestingPurpose(array $globals): void
47
    {
48 2
        self::$globals = $globals;
49
    }
50
}
51