Passed
Branch master (7315b7)
by Scott
02:53
created

DotGlobal::id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Desmond\functions\core;
3
use Desmond\functions\DesmondFunction;
4
use Desmond\TypeHelper;
5
use Desmond\ArgumentHelper;
6
use Desmond\exceptions\ArgumentException;
7
8
class DotGlobal extends DesmondFunction
9
{
10
    use TypeHelper;
11
    use ArgumentHelper;
12
13 285
    public function id()
14
    {
15 285
        return '.global';
16
    }
17
18 3
    public function run(array $args)
0 ignored issues
show
Coding Style introduced by
run 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...
19
    {
20 3
        if (empty($args)) {
21 1
            $globals = $this->globalsCopy(); // Recusion kills fromPhpType.
22 1
            return self::fromPhpType($globals);
23
        }
24 2
        $this->expectArguments('.post', [['Symbol', 'String']], $args);
25 2
        $index = $args[0]->value();
26 2
        if (isset($args[1])) {
27 1
            $GLOBALS[$index] = $args[1]->value();
28
        }
29 2
        return self::fromPhpType($GLOBALS[$index]);
30
    }
31
32 1
    private function globalsCopy()
0 ignored issues
show
Coding Style introduced by
globalsCopy 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...
33
    {
34 1
        $globals = [];
35 1
        foreach ($GLOBALS as $key => $data) {
36 1
            if ($key == 'GLOBALS') {
37 1
                continue; // Remove recursive reference.
38
            }
39 1
            $globals[$key] = $data;
40
        }
41 1
        return $globals;
42
    }
43
}
44