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

PhpFunctionsScanner::getFunctions()   C

Complexity

Conditions 19
Paths 18

Size

Total Lines 58
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 0
loc 58
rs 6.3822
cc 19
eloc 37
nc 18
nop 0

How to fix   Long Method    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
        $concatenating = false;
28
29
        for ($k = 0; $k < $count; ++$k) {
30
            $value = $this->tokens[$k];
31
32
            if (is_string($value)) {
33
                if ($value === '.') {
34
                    //concatenating strings
35
                    $concatenating = true;
36
                    continue;
37
                }
38
                $concatenating = false;
39
                if ($value === ')' && isset($bufferFunctions[0])) {
40
                    //close the current function
41
                    $functions[] = array_shift($bufferFunctions);
42
                }
43
                continue;
44
            }
45
46
            switch ($value[0]) {
47
                case T_CONSTANT_ENCAPSED_STRING:
48
                    //add an argument to the current function
49
                    if (isset($bufferFunctions[0])) {
50
                        $string = \Gettext\Extractors\PhpCode::convertString($value[1]);
51
                        if ($concatenating && !empty($bufferFunctions[0][2])) {
52
                            $bufferFunctions[0][2][count($bufferFunctions[0][2]) - 1] .= $string;
53
                        } else {
54
                            $bufferFunctions[0][2][] = $string;
55
                        }
56
                    }
57
                    break;
58
                case T_STRING:
59
                    //new function found
60
                    for ($j = $k + 1; $j < $count; $j++) {
61
                        $nextToken = $this->tokens[$j];
62
                        if (is_array($nextToken) && ($nextToken[0] === T_COMMENT || $nextToken[0] === T_WHITESPACE)) {
63
                            continue;
64
                        }
65
                        if ($nextToken === '(') {
66
                            array_unshift($bufferFunctions, array($value[1], $value[2], array()));
67
                            $k = $j;
68
                        }
69
                        break;
70
                    }
71
                    break;
72
            }
73
            if ($concatenating && $value[0] !== T_COMMENT && $value[0] !== T_WHITESPACE) {
74
                $concatenating = false;
75
            }
76
        }
77
78
        return $functions;
79
    }
80
}
81