Completed
Pull Request — master (#93)
by Michele
03:04 queued 01:21
created

PhpFunctionsScanner::decodeString()   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 42
rs 4.909
cc 9
eloc 25
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
     * {@inheritdoc}
21
     */
22
    public function getFunctions()
23
    {
24
        $count = count($this->tokens);
25
        $bufferFunctions = array();
26
        $functions = array();
27
28
        for ($k = 0; $k < $count; ++$k) {
29
            $value = $this->tokens[$k];
30
31
            //close the current function
32
            if (is_string($value)) {
33
                if ($value === ')' && isset($bufferFunctions[0])) {
34
                    $functions[] = array_shift($bufferFunctions);
35
                }
36
37
                continue;
38
            }
39
40
            //add an argument to the current function
41
            if (isset($bufferFunctions[0]) && ($value[0] === T_CONSTANT_ENCAPSED_STRING)) {
42
                $bufferFunctions[0][2][] = Strings::fromPhp($value[1]);
43
                continue;
44
            }
45
46
            //new function found
47
            if (($value[0] === T_STRING) && is_string($this->tokens[$k + 1]) && ($this->tokens[$k + 1] === '(')) {
48
                array_unshift($bufferFunctions, array($value[1], $value[2], array()));
49
                ++$k;
50
51
                continue;
52
            }
53
        }
54
55
        return $functions;
56
    }
57
}
58