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

FunctionCall   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 148
rs 10
c 0
b 0
f 0
wmc 25
lcom 1
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A isSolidString() 0 7 2
A isGlobalCall() 0 21 3
A isStaticCall() 0 11 2
A isMethodCallOnThis() 0 11 1
A checkTokens() 0 23 5
B readParameters() 0 30 7
A isEqual() 0 4 2
A level() 0 12 3
1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope\Analyzers;
4
5
class FunctionCall
6
{
7
    public static function isSolidString($tokens)
8
    {
9
        [$nextToken,] = TokenManager::getNextToken($tokens, 0);
0 ignored issues
show
Bug introduced by
The variable $nextToken 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...
10
11
        // we make sure that the string is not concatinated.
12
        return ($tokens[0][0] == T_CONSTANT_ENCAPSED_STRING) && ($nextToken !== '.');
13
    }
14
15
    public static function isGlobalCall($funcName, &$tokens, $i)
16
    {
17
        $expectedTokens = [
18
            ['('],
19
            [T_STRING, $funcName],
20
        ];
21
22
        if (empty($indexes = self::checkTokens($expectedTokens, $tokens, $i))) {
23
            return null;
24
        }
25
26
        $index = array_pop($indexes);
27
        [$prev, $p2] = TokenManager::getPrevToken($tokens, $index);
0 ignored issues
show
Bug introduced by
The variable $p2 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 $prev 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...
28
        $ops = [T_DOUBLE_COLON, T_OBJECT_OPERATOR, T_NEW, T_FUNCTION];
29
30
        if (\in_array($prev[0], $ops)) {
31
            return null;
32
        }
33
34
        return $index;
35
    }
36
37
    public static function isStaticCall($methodName, &$tokens, $i, $className = null)
38
    {
39
        $expectedTokens = [
40
            ['('],
41
            [T_STRING, $methodName],
42
            [T_DOUBLE_COLON, '::'],
43
        ];
44
        $className && ($expectedTokens[] = [T_STRING, $className]);
45
46
        return self::checkTokens($expectedTokens, $tokens, $i);
47
    }
48
49
    public static function isMethodCallOnThis($methodName, &$tokens, $i)
50
    {
51
        $expectedTokens = [
52
            ['('],
53
            [T_STRING, $methodName],
54
            [T_OBJECT_OPERATOR, '->'],
55
            [T_VARIABLE, '$this'],
56
        ];
57
58
        return self::checkTokens($expectedTokens, $tokens, $i);
59
    }
60
61
    public static function checkTokens($expectedTokens, &$tokens, $j)
62
    {
63
        if ($tokens[$j][0] != '(') {
64
            return [];
65
        }
66
67
        array_shift($expectedTokens); // remove ( from the array.
68
69
        $results = [];
70
        foreach ($expectedTokens as $i => $expectedToken) {
71
            try {
72
                [$actualToken, $j] = TokenManager::getPrevToken($tokens, $j);
0 ignored issues
show
Bug introduced by
The variable $actualToken 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...
73
            } catch (\Throwable $e) {
74
                return [];
75
            }
76
            if (! self::isEqual($expectedToken, $actualToken)) {
77
                return [];
78
            }
79
            $results[] = $j;
80
        }
81
82
        return $results;
83
    }
84
85
    public static function readParameters(&$tokens, $i)
86
    {
87
        $params = [];
88
        $p = 0;
89
        $level = 1;
90
        while (true) {
91
            [$nextToken, $i] = TokenManager::getNextToken($tokens, $i);
0 ignored issues
show
Bug introduced by
The variable $nextToken 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...
92
93
            $level = self::level($nextToken, $level);
94
95
            if ($level == 0 && $nextToken == ')') {
96
                break;
97
            }
98
99
            // Fixes: https://github.com/imanghafoori1/laravel-microscope/issues/135
100
            // To avoid infinite loop in case of wrong syntax
101
            if ($nextToken == '_') {
102
                break;
103
            }
104
105
            if ($level == 1 && $nextToken == ',') {
106
                $p++;
107
                continue;
108
            }
109
110
            $params[$p][] = $nextToken;
111
        }
112
113
        return $params;
114
    }
115
116
    /* public static function readConditions(&$tokens, $i)
117
     {
118
         $params = [];
119
         $level = 1;
120
         while (true) {
121
             [$nextToken, $i] = self::getNextToken($tokens, $i);
122
123
             $level = self::level($nextToken, $level);
124
125
             if ($level == 0 && $nextToken == ')') {
126
                 break;
127
             }
128
129
             $params[] = $nextToken;
130
         }
131
132
         return [$params, $i];
133
     }*/
134
135
    private static function isEqual($expectedToken, $actualToken)
136
    {
137
        return $expectedToken[0] == $actualToken[0] && ($expectedToken[1] ?? '') == ($actualToken[1] ?? '');
138
    }
139
140
    private static function level($nextToken, $level)
141
    {
142
        if (\in_array($nextToken[0], ['[', '(', '{', T_CURLY_OPEN])) {
143
            $level++;
144
        }
145
146
        if (\in_array($nextToken[0], [']', ')', '}'])) {
147
            $level--;
148
        }
149
150
        return $level;
151
    }
152
}
153