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

PhpFunctionsScanner::getFunctions()   C

Complexity

Conditions 13
Paths 9

Size

Total Lines 44
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 44
rs 5.1234
cc 13
eloc 26
nc 9
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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