Completed
Push — master ( 502445...002137 )
by Andrii
17:02
created

Interpolator::get()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 2
Metric Value
dl 0
loc 12
ccs 0
cts 8
cp 0
rs 9.2
c 2
b 0
f 2
cc 4
eloc 9
nc 4
nop 2
crap 20
1
<?php
2
/**
3
 * Automation tool mixed with code generator for easier continuous development
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\components;
12
13
use Yii;
14
use yii\helpers\ArrayHelper;
15
16
class Interpolator
17
{
18 2
    public function interpolate(&$data)
19
    {
20 2
        if (is_array($data)) {
21 2
            foreach ($data as &$item) {
22 2
                $this->interpolate($item);
23
            }
24 2
        } elseif (is_string($data)) {
25 2
            $data = preg_replace_callback('/\\$(\\w+)\\[\'(.+?)\'\\]/', function ($matches) {
26
                return $this->get($matches[1], $matches[2]);
27 2
            }, $data);
28
        }
29 2
    }
30
31
    public function get($scope, $name)
0 ignored issues
show
Coding Style introduced by
get uses the super-global variable $_ENV 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...
32
    {
33
        if ($scope === 'params') {
34
            return Yii::$app->params[$name];
35
        } elseif ($scope === '_ENV') {
36
            return $_ENV[$name];
37
        } elseif ($scope === 'config') {
38
            return $this->getConfig($name);
39
        } else {
40
            return "\$${scope}['$name']";
41
        }
42
    }
43
44
    public function getConfig($name)
45
    {
46
        list($goal, $subname) = explode('.', $name, 2);
47
48
        return ArrayHelper::getValue(Yii::$app->get('config')->getGoal($goal), $subname);
49
    }
50
}
51