Parser::parse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
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 registerFunction($id, $callable)
19
    {
20
        static::$functions[$id] = $callable;
21
    }
22
23
    /** @param string|null $path */
24
    public static function registerFileFunction($path = null)
25
    {
26
        static::registerFunction('file', function($fileName) use ($path) {
27
            $path = $path === null ? __DIR__ : rtrim($path, DIRECTORY_SEPARATOR);
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $path, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
28
            $filePath = $fileName[0] === DIRECTORY_SEPARATOR ? $fileName : $path . DIRECTORY_SEPARATOR . $fileName;
29
            if (is_readable($filePath) === false) {
30
                throw new \Exception('File "' . $filePath . '" not found.');
31
            }
32
33
            return file_get_contents($filePath);
34
        });
35
    }
36
37
    public static function registerDateFunction()
38
    {
39
        static::registerFunction('date', function($date = null) {
40
            return new \DateTime($date);
41
        });
42
    }
43
44
    /**
45
     * @param string $value
46
     * @param int $flags
47
     * @return array|null
48
     */
49
    public function parse($value, $flags = 0)
50
    {
51
        $return = parent::parse($value, $flags);
52
53
        if (is_array($return)) {
54
            $this->parseValues($return);
55
        }
56
57
        return $return;
58
    }
59
60
    /**
61
     * @param array $values
62
     * @return $this
63
     * @throws \Exception
64
     */
65
    protected function parseValues(array &$values)
66
    {
67
        foreach ($values as &$value) {
68
            if (is_array($value)) {
69
                $this->parseValues($value);
70
            } elseif (is_string($value) && substr($value, 0, 1) === '<' && substr($value, -1) === '>') {
71
                $functionId = null;
72
                $parameters = [];
73
                foreach (token_get_all('<?php ' . $value) as $token) {
74
                    if (is_array($token)) {
75
                        if ($token[0] === T_STRING && $functionId === null) {
76
                            $functionId = $token[1];
77
                        } elseif ($token[0] === T_STRING) {
78
                            $parameters[] = $token[1];
79
                        } elseif ($token[0] === T_CONSTANT_ENCAPSED_STRING) {
80
                            $parameters[] = substr($token[1], 1, -1);
81
                        } elseif ($token[0] === T_LNUMBER || $token[0] === T_DNUMBER) {
82
                            $parameters[] = $token[1];
83
                        }
84
                    }
85
                }
86
87
                if ($functionId === null) {
88
                    throw new FunctionIdNotFoundException('Function name cannont be found in ' . $value . '.');
89
                } elseif (isset(static::$functions[$functionId]) === false) {
90
                    throw new FunctionNotFoundException('Function "' . $functionId . '" not found.');
91
                }
92
93
                $value = call_user_func_array(static::$functions[$functionId], $parameters);
94
            }
95
        }
96
97
        return $this;
98
    }
99
}
100