Completed
Pull Request — master (#91)
by Michele
03:58
created

PhpFunctionsScanner::decodeString()   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 38
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 38
rs 4.909
cc 9
eloc 28
nc 6
nop 1
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
        $simpleDoubleQuote = array(
0 ignored issues
show
Unused Code introduced by
$simpleDoubleQuote is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
74
            'n' => "\n",
75
            'r' => "\r",
76
            't' => "\t",
77
            'v' => "\v",
78
            'e' => "\f",
79
            '\\' => '\\',
80
            '$' => '$',
81
            '"' => '"',
82
        );
83
84
        for ($k = 0; $k < $count; ++$k) {
85
            $value = $this->tokens[$k];
86
87
            //close the current function
88
            if (is_string($value)) {
89
                if ($value === ')' && isset($bufferFunctions[0])) {
90
                    $functions[] = array_shift($bufferFunctions);
91
                }
92
93
                continue;
94
            }
95
96
            //add an argument to the current function
97
            if (isset($bufferFunctions[0]) && ($value[0] === T_CONSTANT_ENCAPSED_STRING)) {
98
                $bufferFunctions[0][2][] = static::decodeString($value[1]);
99
                continue;
100
            }
101
102
            //new function found
103
            if (($value[0] === T_STRING) && is_string($this->tokens[$k + 1]) && ($this->tokens[$k + 1] === '(')) {
104
                array_unshift($bufferFunctions, array($value[1], $value[2], array()));
105
                ++$k;
106
107
                continue;
108
            }
109
        }
110
111
        return $functions;
112
    }
113
}
114