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
/**
15
 * @psalm-import-type Query from Types
16
 * @psalm-import-type SuperGlobalsMap from Types
17
 */
18
final class AssistedWebContextParam implements ParamInterface
19
{
20
    /**
21
     * $GLOBALS for testing
22
     *
23
     * @var SuperGlobalsMap
0 ignored issues
show
Bug introduced by
The type BEAR\Resource\SuperGlobalsMap was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
     */
25
    private static array $globals = [];
26
27
    public function __construct(
28
        private readonly AbstractWebContextParam $webContextParam,
29 5
        private readonly ParamInterface $defaultParam,
30
    ) {
31 5
    }
32 5
33 5
    /**
34
     * {@inheritDoc}
35
     */
36
    #[Override]
37
    public function __invoke(string $varName, array $query, InjectorInterface $injector)
38 4
    {
39
        $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...
40 4
        /** @var SuperGlobalsMap $superGlobals */
41 4
        $webContextParam = $this->webContextParam;
42 4
        assert(is_string($webContextParam::GLOBAL_KEY));
43
        /** @psalm-suppress MixedArrayOffset */
44 4
        $phpWebContext = $superGlobals[$webContextParam::GLOBAL_KEY];
45 2
46
        return $phpWebContext[$this->webContextParam->key] ?? ($this->defaultParam)($varName, $query, $injector);
47
    }
48 2
49
    /** @param SuperGlobalsMap $globals */
50
    public static function setSuperGlobalsOnlyForTestingPurpose(array $globals): void
51 5
    {
52
        self::$globals = $globals;
0 ignored issues
show
Documentation Bug introduced by
It seems like $globals of type array is incompatible with the declared type BEAR\Resource\SuperGlobalsMap of property $globals.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53 5
    }
54
}
55