Completed
Push — master ( 6dc507...18cad2 )
by Steevan
02:03
created

Parser::parseValues()   C

Complexity

Conditions 16
Paths 24

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
c 0
b 0
f 0
rs 5.0151
cc 16
eloc 23
nc 24
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace steevanb\PhpYaml;
4
5
use steevanb\PhpYaml\Exception\FunctionIdNotFoundException;
6
use steevanb\PhpYaml\Exception\FunctionNotFoundException;
7
use Symfony\Component\Yaml\Parser as SymfonyYamlParser;
8
9
class Parser extends SymfonyYamlParser
10
{
11
    /** @var array */
12
    protected static $functions = [];
13
14
    /**
15
     * @param string $id
16
     * @param callable $callable
17
     */
18
    public static function addFunction($id, $callable)
19
    {
20
        static::$functions[$id] = $callable;
21
    }
22
23
    public static function addFileFunction()
24
    {
25
        static::addFunction('file', function($fileName) {
26
            return file_get_contents($fileName);
27
        });
28
    }
29
30
    public static function addDateFunction()
31
    {
32
        static::addFunction('date', function($date = null) {
33
            return new \DateTime($date);
34
        });
35
    }
36
37
    /**
38
     * @param string $value
39
     * @param int $flags
40
     * @return array|null
41
     */
42
    public function parse($value, $flags = 0)
43
    {
44
        $return = parent::parse($value, $flags);
45
46
        if (is_array($return)) {
47
            $this->parseValues($return);
48
        }
49
50
        return $return;
51
    }
52
53
    /**
54
     * @param array $values
55
     * @return $this
56
     * @throws \Exception
57
     */
58
    protected function parseValues(array &$values)
59
    {
60
        foreach ($values as &$value) {
61
            if (is_array($value)) {
62
                $this->parseValues($value);
63
            } elseif (is_string($value) && substr($value, 0, 1) === '<' && substr($value, -1) === '>') {
64
                $functionId = null;
65
                $parameters = [];
66
                foreach (token_get_all('<?php ' . $value) as $token) {
67
                    if (is_array($token)) {
68
                        if ($token[0] === T_STRING && $functionId === null) {
69
                            $functionId = $token[1];
70
                        } elseif ($token[0] === T_STRING) {
71
                            $parameters[] = $token[1];
72
                        } elseif ($token[0] === T_CONSTANT_ENCAPSED_STRING) {
73
                            $parameters[] = substr($token[1], 1, -1);
74
                        } elseif ($token[0] === T_LNUMBER || $token[0] === T_DNUMBER) {
75
                            $parameters[] = $token[1];
76
                        }
77
                    }
78
                }
79
80
                if ($functionId === null) {
81
                    throw new FunctionIdNotFoundException('Function name cannont be found in ' . $value . '.');
82
                } elseif (isset(static::$functions[$functionId]) === false) {
83
                    throw new FunctionNotFoundException('Function "' . $functionId . '" not found.');
84
                }
85
86
                $value = call_user_func_array(static::$functions[$functionId], $parameters);
87
            }
88
        }
89
90
        return $this;
91
    }
92
}
93