Completed
Pull Request — master (#98)
by Michele
02:24
created

PhpFunctionsScanner   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 14
c 6
b 1
f 0
lcom 1
cbo 2
dl 0
loc 62
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C getFunctions() 0 44 13
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
            switch ($value[0]) {
41
                case T_CONSTANT_ENCAPSED_STRING:
42
                    //add an argument to the current function
43
                    if (isset($bufferFunctions[0])) {
44
                        $bufferFunctions[0][2][] = \Gettext\Extractors\PhpCode::convertString($value[1]);
45
                    }
46
                    break;
47
                case T_STRING:
48
                    //new function found
49
                    for ($j = $k + 1; $j < $count; $j++) {
50
                        $nextToken = $this->tokens[$j];
51
                        if (is_array($nextToken) && ($nextToken[0] === T_COMMENT || $nextToken[0] === T_WHITESPACE)) {
52
                            continue;
53
                        }
54
                        if ($nextToken === '(') {
55
                            array_unshift($bufferFunctions, array($value[1], $value[2], array()));
56
                            $k = $j;
57
                        }
58
                        break;
59
                    }
60
                    break;
61
            }
62
        }
63
64
        return $functions;
65
    }
66
}
67