AssistedWebContextParam::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 3
dl 0
loc 11
rs 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 2
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