Completed
Pull Request — master (#98)
by Michele
01:57
created

PhpFunctionsScanner::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Gettext\Utils;
4
5
use Gettext\Extractors\PhpCode;
6
7
class PhpFunctionsScanner extends FunctionsScanner
8
{
9
    protected $tokens;
10
11
    /**
12
     * Constructor.
13
     *
14
     * @param string $code The php code to scan
15
     */
16
    public function __construct($code)
17
    {
18
        $this->tokens = token_get_all($code);
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function getFunctions()
25
    {
26
        $count = count($this->tokens);
27
        $bufferFunctions = array();
28
        /* @var ParsedFunction[] $bufferFunctions */
29
        $functions = array();
30
        /* @var ParsedFunction[] $functions */
31
32
        for ($k = 0; $k < $count; ++$k) {
33
            $value = $this->tokens[$k];
34
35
            if (is_string($value)) {
36
                $s = $value;
0 ignored issues
show
Unused Code introduced by
$s 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...
37
            } else {
38
                $s = token_name($value[0]).' >'.$value[1].'<';
0 ignored issues
show
Unused Code introduced by
$s 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...
39
            }
40
41
            if (is_string($value)) {
42
                if (isset($bufferFunctions[0])) {
43
                    switch ($value) {
44
                        case ',':
45
                            $bufferFunctions[0]->nextArgument();
46
                            break;
47
                        case ')':
48
                            $functions[] = array_shift($bufferFunctions)->close();
49
                            break;
50
                        case '.':
51
                            break;
52
                        default:
53
                            $bufferFunctions[0]->stopArgument();
54
                            break;
55
                    }
56
                }
57
                continue;
58
            }
59
60
            switch ($value[0]) {
61
                case T_CONSTANT_ENCAPSED_STRING:
62
                    //add an argument to the current function
63
                    if (isset($bufferFunctions[0])) {
64
                        $bufferFunctions[0]->addArgumentChunk(PhpCode::convertString($value[1]));
65
                    }
66
                    break;
67
                case T_STRING:
68
                    if (isset($bufferFunctions[0])) {
69
                        $bufferFunctions[0]->stopArgument();
70
                    }
71
                    //new function found
72
                    for ($j = $k + 1; $j < $count; ++$j) {
73
                        $nextToken = $this->tokens[$j];
74
                        if (is_array($nextToken) && ($nextToken[0] === T_COMMENT || $nextToken[0] === T_WHITESPACE)) {
75
                            continue;
76
                        }
77
                        if ($nextToken === '(') {
78
                            array_unshift($bufferFunctions, new ParsedFunction($value[1], $value[2]));
79
                            $k = $j;
80
                        }
81
                        break;
82
                    }
83
                    break;
84
                case T_WHITESPACE:
85
                case T_COMMENT:
86
                    break;
87
                default:
88
                    if (isset($bufferFunctions[0])) {
89
                        $bufferFunctions[0]->stopArgument();
90
                    }
91
                    break;
92
            }
93
        }
94
95
        return $functions;
96
    }
97
}
98