Issues (49)

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 5
     * {@inheritDoc}
30
     */
31 5
    public function __invoke(string $varName, array $query, InjectorInterface $injector)
32 5
    {
33 5
        $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 4
        $phpWebContext = $superGlobals[$webContextParam::GLOBAL_KEY];
39
        if (isset($phpWebContext[$this->webContextParam->key])) {
40 4
            return $phpWebContext[$this->webContextParam->key];
41 4
        }
42 4
43
        return ($this->defaultParam)($varName, $query, $injector);
44 4
    }
45 2
46
    /** @param array<string, array<string, mixed>> $globals */
47
    public static function setSuperGlobalsOnlyForTestingPurpose(array $globals): void
48 2
    {
49
        self::$globals = $globals;
50
    }
51
}
52