Completed
Pull Request — master (#91)
by Michele
04:41 queued 02:04
created

PhpFunctionsScanner   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 20
c 2
b 1
f 0
lcom 1
cbo 1
dl 0
loc 99
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
D decodeString() 0 38 9
D getFunctions() 0 35 10
1
<?php
2
3
namespace Gettext\Utils;
4
5
class PhpFunctionsScanner extends FunctionsScanner
6
{
7
    protected $tokens;
8
9
    /**
10
     * Constructor.
11
     *
12
     * @param string $code The php code to scan
13
     */
14
    public function __construct($code)
15
    {
16
        $this->tokens = token_get_all($code);
17
    }
18
19
    /**
20
     * Decodes a T_CONSTANT_ENCAPSED_STRING string.
21
     *
22
     * @param string $value
23
     *
24
     * @return string
25
     */
26
    public static function decodeString($value)
27
    {
28
        $result = '';
29
        if ($value[0] === "'" || strpos($value, '$') === false) {
30
            if (strpos($value, '\\') === false) {
31
                $result = substr($value, 1, -1);
32
            } else {
33
                $result = eval("return $value;");
34
            }
35
        } else {
36
            $value = substr($value, 1, -1);
37
            while (($p = strpos($value, '\\')) !== false) {
38
                if (!isset($value[$p + 1])) {
39
                    break;
40
                }
41
                if ($p > 0) {
42
                    $result .= substr($value, 0, $p);
43
                }
44
                $value = substr($value, $p + 1);
45
                $p = strpos($value, '$');
46
                if ($p === false) {
47
                    $result .= eval('return "\\'.$value.'";');
48
                    $value = '';
49
                    break;
50
                }
51
                if ($p === 0) {
52
                    $result .= '$';
53
                    $value = substr($value, 1);
54
                } else {
55
                    $result .= eval('return "\\'.substr($value, 0, $p).'";');
56
                    $value = substr($value, $p);
57
                }
58
            }
59
            $result .= $value;
60
        }
61
62
        return $result;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getFunctions()
69
    {
70
        $count = count($this->tokens);
71
        $bufferFunctions = array();
72
        $functions = array();
73
74
        for ($k = 0; $k < $count; ++$k) {
75
            $value = $this->tokens[$k];
76
77
            //close the current function
78
            if (is_string($value)) {
79
                if ($value === ')' && isset($bufferFunctions[0])) {
80
                    $functions[] = array_shift($bufferFunctions);
81
                }
82
83
                continue;
84
            }
85
86
            //add an argument to the current function
87
            if (isset($bufferFunctions[0]) && ($value[0] === T_CONSTANT_ENCAPSED_STRING)) {
88
                $bufferFunctions[0][2][] = static::decodeString($value[1]);
89
                continue;
90
            }
91
92
            //new function found
93
            if (($value[0] === T_STRING) && is_string($this->tokens[$k + 1]) && ($this->tokens[$k + 1] === '(')) {
94
                array_unshift($bufferFunctions, array($value[1], $value[2], array()));
95
                ++$k;
96
97
                continue;
98
            }
99
        }
100
101
        return $functions;
102
    }
103
}
104