Completed
Push — 1.x ( ed8928...51f4d3 )
by Akihito
06:08 queued 04:26
created

AssistedWebContextParam   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 46
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
 * This file is part of the BEAR.Resource package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Resource;
8
9
use Ray\Di\InjectorInterface;
10
use Ray\WebContextParam\Annotation\AbstractWebContextParam;
11
12
final class AssistedWebContextParam implements ParamInterface
13
{
14
    /**
15
     * $GLOBALS for testing
16
     *
17
     * @var array
18
     */
19
    private static $globals = [];
20
21
    /**
22
     * @var AbstractWebContextParam
23
     */
24
    private $webContextParam;
25
26
    /**
27
     * @var ParamInterface
28
     */
29
    private $defaultParam;
30
31 5
    public function __construct(AbstractWebContextParam $webContextParam, ParamInterface $defaultParam)
32
    {
33 5
        $this->webContextParam = $webContextParam;
34 5
        $this->defaultParam = $defaultParam;
35 5
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 4
    public function __invoke($varName, array $query, InjectorInterface $injector)
0 ignored issues
show
Coding Style introduced by
__invoke uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
41
    {
42 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...
43 4
        $webContextParam = $this->webContextParam;
44 4
        $phpWebContext = $superGlobals[$webContextParam::GLOBAL_KEY];
45
46 4
        if (isset($phpWebContext[$this->webContextParam->key])) {
47 2
            return  $phpWebContext[$this->webContextParam->key];
48
        }
49
50 2
        return $this->defaultParam->__invoke($varName, $query, $injector);
51
    }
52
53 5
    public static function setSuperGlobalsOnlyForTestingPurpose(array $globals)
54
    {
55 5
        self::$globals = $globals;
56 5
    }
57
}
58