Completed
Push — master ( abc0a7...2228b3 )
by Iman
01:32
created

TokenManager::readBody()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 3
nop 3
dl 0
loc 29
rs 8.5226
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope\Analyzers;
4
5
class TokenManager
6
{
7
    public static function removeTokens($tokens, $from, $to, $at)
8
    {
9
        $refactoredTokens = [];
10
        foreach ($tokens as $i => $oldToken) {
11
            if ($i > $from && $i <= $to) {
12
                continue;
13
            }
14
15
            if ($i == $at) {
16
                continue;
17
            }
18
            $refactoredTokens[] = $oldToken;
19
        }
20
21
        return $refactoredTokens;
22
    }
23
24
    public static function getNextToken($tokens, $i)
25
    {
26
        $i++;
27
        $token = $tokens[$i] ?? '_';
28
        while ($token[0] == T_WHITESPACE || $token[0] == T_COMMENT) {
29
            $i++;
30
            $token = $tokens[$i] ?? [null, null];
31
        }
32
33
        return [$token, $i];
34
    }
35
36
    public static function getPrevToken($tokens, $i)
37
    {
38
        $i--;
39
        $token = $tokens[$i];
40
        while ($token[0] == T_WHITESPACE || $token[0] == T_COMMENT) {
41
            $i--;
42
            $token = $tokens[$i];
43
        }
44
45
        return [$token, $i];
46
    }
47
48
    public static function forwardTo($tokens, $i, $tokenType)
49
    {
50
        $i++;
51
        $nextToken = $tokens[$i] ?? '_';
52
        while (! \in_array($nextToken[0], $tokenType)) {
53
            $i++;
54
            $nextToken = $tokens[$i] ?? [null, null];
55
        }
56
57
        return [$nextToken, $i];
58
    }
59
60
    public static function readBodyBack(&$tokens, $i)
61
    {
62
        $body = [];
63
        $level = 0;
64
        while (true) {
65
            [$token, $i] = self::getPrevToken($tokens, $i);
0 ignored issues
show
Bug introduced by
The variable $token does not exist. Did you mean $tokens?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
66
67
            if (\in_array($token[0], [']', ')', '}'])) {
0 ignored issues
show
Bug introduced by
The variable $token does not exist. Did you mean $tokens?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
68
                $level--;
69
            }
70
71
            $isOpening = \in_array($token[0], ['[', '(', '{', T_CURLY_OPEN]);
0 ignored issues
show
Bug introduced by
The variable $token does not exist. Did you mean $tokens?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
72
73
            if ($level == 0 && $isOpening) {
74
                break;
75
            }
76
77
            if ($isOpening) {
78
                $level++;
79
            }
80
81
            $body[] = $token;
0 ignored issues
show
Bug introduced by
The variable $token does not exist. Did you mean $tokens?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
82
        }
83
84
        return [$body, $i];
85
    }
86
87
    public static function readBody(&$tokens, $i, $until = '}')
88
    {
89
        $body = [];
90
        $level = 0;
91
        while (true) {
92
            $i++;
93
            $nextToken = $tokens[$i] ?? '_';
94
95
            if ($nextToken == '_') {
96
                break;
97
            }
98
99
            if ($level == 0 && $nextToken[0] == $until) {
100
                break;
101
            }
102
103
            if (\in_array($nextToken[0], ['[', '(', '{', T_CURLY_OPEN])) {
104
                $level++;
105
            }
106
107
            if (\in_array($nextToken[0], [']', ')', '}'])) {
108
                $level--;
109
            }
110
111
            $body[] = $nextToken;
112
        }
113
114
        return [$body, $i];
115
    }
116
117
    public static function readBackUntil(&$tokens, $i, $chars = ['}'])
118
    {
119
        $orphanBlock = [];
120
        while (true) {
121
            [$token, $i] = self::getPrevToken($tokens, $i);
0 ignored issues
show
Bug introduced by
The variable $token does not exist. Did you mean $tokens?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
122
123
            $depth = 0;
124
            if (\in_array($token[0], $chars)) {
0 ignored issues
show
Bug introduced by
The variable $token does not exist. Did you mean $tokens?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
125
                [$ifBody, $openIfIndex] = self::readBodyBack($tokens, $i);
0 ignored issues
show
Bug introduced by
The variable $openIfIndex does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $ifBody does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
126
                [, $closeParenIndex] = self::getPrevToken($tokens, $openIfIndex);
0 ignored issues
show
Bug introduced by
The variable $closeParenIndex does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
127
                [$condition, $openParenIndex] = self::readBodyBack($tokens, $closeParenIndex);
0 ignored issues
show
Bug introduced by
The variable $condition does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $openParenIndex does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
128
                [$ownerOfClosing] = self::getPrevToken($tokens, $openParenIndex);
0 ignored issues
show
Bug introduced by
The variable $ownerOfClosing does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
129
130
                if ($ownerOfClosing[0] == T_IF) {
131
                    break;
132
                } else {
133
                    return [null, null];
134
                }
135
            }
136
137
            if ($token[0] == '{') {
0 ignored issues
show
Bug introduced by
The variable $token does not exist. Did you mean $tokens?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
138
                $depth--;
139
140
                if ($depth === -1) {
141
                    return [null, null];
142
                }
143
            }
144
145
            $orphanBlock[] = $token;
0 ignored issues
show
Bug introduced by
The variable $token does not exist. Did you mean $tokens?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
146
        }
147
148
        return [[$ifBody, [$openIfIndex, $i]], [$condition, [$openParenIndex, $closeParenIndex]], $orphanBlock, $i];
149
    }
150
}
151