Completed
Push — 1.x ( a68f5e...4e2cc2 )
by Akihito
03:41
created

AssistedWebContextParam   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 49
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 12 3
A setSuperGlobalsOnlyForTestingPurpose() 0 4 1
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
final class AssistedWebContextParam implements ParamInterface
11
{
12
    /**
13
     * $GLOBALS for testing
14
     *
15
     * @var array<string, array<string, string>>
16
     */
17
    private static $globals = [];
18
19
    /**
20
     * @var AbstractWebContextParam
21
     */
22
    private $webContextParam;
23
24
    /**
25
     * @var ParamInterface
26
     */
27
    private $defaultParam;
28
29 5
    public function __construct(AbstractWebContextParam $webContextParam, ParamInterface $defaultParam)
30
    {
31 5
        $this->webContextParam = $webContextParam;
32 5
        $this->defaultParam = $defaultParam;
33 5
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 4
    public function __invoke(string $varName, array $query, InjectorInterface $injector)
39
    {
40 4
        $superGlobals = static::$globals ? static::$globals : $GLOBALS;
0 ignored issues
show
Comprehensibility introduced by
Since BEAR\Resource\AssistedWebContextParam is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
41 4
        $webContextParam = $this->webContextParam;
42 4
        $phpWebContext = $superGlobals[$webContextParam::GLOBAL_KEY];
43
44 4
        if (isset($phpWebContext[$this->webContextParam->key])) {
45 2
            return  $phpWebContext[$this->webContextParam->key];
46
        }
47
48 2
        return ($this->defaultParam)($varName, $query, $injector);
49
    }
50
51 5
    /**
52
     * @param array<string, array<string, string>> $globals
53 5
     */
54 5
    public static function setSuperGlobalsOnlyForTestingPurpose(array $globals) : void
55
    {
56
        self::$globals = $globals;
57
    }
58
}
59