Completed
Push — master ( 48aefa...87bdd5 )
by Andrii
03:08
created

Interpolator::do()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 7
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 1
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\base;
12
13
use Yii;
14
use yii\helpers\ArrayHelper;
15
16
class Interpolator
17
{
18
    public $data;
19
20
    public function interpolate(&$data)
21
    {
22
        $this->data = &$data;
23
        $this->do($data);
24
    }
25
26
    private function do(&$data)
0 ignored issues
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
27
    {
28
        if (is_array($data)) {
29
            foreach ($data as &$item) {
30
                $this->do($item);
31
            }
32
        } elseif (is_string($data)) {
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $data.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
33
            $data = preg_replace_callback('/\\$(\\w+)\\[\'(.+?)\'\\]/', function ($matches) {
34
                return $this->get($matches[1], $matches[2]);
35
            }, $data);
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $data.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
36
        }
37
    }
38
39
    public function get($scope, $name)
40
    {
41
        if ($scope === 'params') {
42
            return $this->data['params'][$name];
43
        } elseif ($scope === '_ENV') {
44
            return $_ENV[$name];
45
        } else {
46
            return "\$${scope}['$name']";
47
        }
48
    }
49
50
    public function getConfig($name)
51
    {
52
        list($goal, $subname) = explode('.', $name, 2);
53
54
        return ArrayHelper::getValue(Yii::$app->get('config')->getGoal($goal), $subname);
55
    }
56
}
57