Issues (55)

src/AssistedWebContextParam.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use Ray\Di\InjectorInterface;
8
use Ray\WebContextParam\Annotation\AbstractWebContextParam;
9
10
use function assert;
11
use function is_string;
12
13
final class AssistedWebContextParam implements ParamInterface
14
{
15
    /**
16
     * $GLOBALS for testing
17
     *
18
     * @var array<string, array<string, mixed>>
19
     */
20
    private static array $globals = [];
21
22
    public function __construct(
23
        private readonly AbstractWebContextParam $webContextParam,
24
        private readonly ParamInterface $defaultParam,
25
    ) {
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31
    public function __invoke(string $varName, array $query, InjectorInterface $injector)
32
    {
33
        $superGlobals = static::$globals ?: $GLOBALS;
0 ignored issues
show
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...
34
        /** @var array<string, array<string, string>> $superGlobals */
35
        $webContextParam = $this->webContextParam;
36
        assert(is_string($webContextParam::GLOBAL_KEY));
37
        /** @psalm-suppress MixedArrayOffset */
38
        $phpWebContext = $superGlobals[$webContextParam::GLOBAL_KEY];
39
40
        return $phpWebContext[$this->webContextParam->key] ?? ($this->defaultParam)($varName, $query, $injector);
41
    }
42
43
    /** @param array<string, array<string, mixed>> $globals */
44
    public static function setSuperGlobalsOnlyForTestingPurpose(array $globals): void
45
    {
46
        self::$globals = $globals;
47
    }
48
}
49