Completed
Push — master ( 2a3256...770069 )
by Andrii
04:50
created

Interpolator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 9
lcom 0
cbo 2
dl 0
loc 35
ccs 0
cts 16
cp 0
rs 10
c 2
b 0
f 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A interpolate() 0 12 4
A get() 0 12 4
A getConfig() 0 6 1
1
<?php
2
3
namespace hidev\components;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
8
class Interpolator
9
{
10
    public function interpolate(&$data)
11
    {
12
        if (is_array($data)) {
13
            foreach ($data as &$item) {
14
                $this->interpolate($item);
15
            }
16
        } elseif (is_string($data)) {
17
            $data = preg_replace_callback('/\\$(\\w+)\\[\'(.+?)\'\\]/', function ($matches) {
18
                return $this->get($matches[1], $matches[2]);
19
            }, $data);
20
        }
21
    }
22
23
    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...
24
    {
25
        if ($scope === 'params') {
26
            return Yii::$app->params[$name];
27
        } elseif ($scope === '_ENV') {
28
            return $_ENV[$name];
29
        } elseif ($scope === 'config') {
30
            return $this->getConfig($name);
31
        } else {
32
            return "\$${scope}['$name']";
33
        }
34
    }
35
36
    public function getConfig($name)
37
    {
38
        list($goal, $subname) = explode('.', $name, 2);
39
40
        return ArrayHelper::getValue(Yii::$app->get('config')->getGoal($goal), $subname);
41
    }
42
}
43