Completed
Push — master ( 12172d...e218e8 )
by Iman
01:46
created

ClassMethods::requestIssue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope\Analyzers;
4
5
class ClassMethods
6
{
7
    public static function read($tokens)
8
    {
9
        $i = 0;
10
        $class = [
11
            'name' => null,
12
            'methods' => [],
13
            'type' => '',
14
        ];
15
        $methods = [];
16
17
        while (isset($tokens[$i])) {
18
            $token = $tokens[$i];
19
20
            if ($token[0] == T_CLASS && $tokens[$i - 1][0] !== T_DOUBLE_COLON) {
21
                $class['name'] = $tokens[$i + 2];
22
                $class['type'] = T_CLASS;
23
                $class['is_abstract'] = ($tokens[$i - 2][0] === T_ABSTRACT);
24
            } elseif ($token[0] == T_INTERFACE) {
25
                $class['name'] = $tokens[$i + 2];
26
                $class['type'] = T_INTERFACE;
27
            } elseif ($token[0] == T_TRAIT) {
28
                $class['name'] = $tokens[$i + 2];
29
                $class['type'] = T_TRAIT;
30
            }
31
32
            if ($class['name'] === null || $tokens[$i][0] != T_FUNCTION) {
33
                $i++;
34
                continue;
35
            }
36
37
            if (! \is_array($name = $tokens[$i + 2])) {
38
                $i++;
39
                continue;
40
            }
41
42
            [$visibility, $isStatic, $isAbstract] = self::findVisibility($tokens, $i - 2);
0 ignored issues
show
Bug introduced by
The variable $visibility 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 $isStatic 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 $isAbstract 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...
43
            [, $signature, $endSignature] = Ifs::readCondition($tokens, $i + 2);
0 ignored issues
show
Bug introduced by
The variable $signature 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 $endSignature 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...
44
            [$char, $charIndex] = FunctionCall::forwardTo($tokens, $endSignature, [':', ';', '{']);
0 ignored issues
show
Bug introduced by
The variable $char 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 $charIndex 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...
45
46
            [$returnType, $hasNullableReturnType, $char, $charIndex] = self::processReturnType($char, $tokens, $charIndex);
0 ignored issues
show
Bug introduced by
The variable $returnType 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 $hasNullableReturnType 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...
47
48
            if ($char == '{') {
49
                [$body, $i] = FunctionCall::readBody($tokens, $charIndex);
0 ignored issues
show
Bug introduced by
The variable $body does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
50
            } elseif ($char == ';') {
51
                $body = [];
52
            } else {
53
                $code = Refactor::toString($tokens);
54
                self::requestIssue($code);
55
                continue;
56
            }
57
58
            $i++;
59
            $methods[] = [
60
                'name' => $name,
61
                'visibility' => $visibility,
62
                'signature' => $signature,
63
                'body' => Refactor::toString($body),
64
                'startBodyIndex' => [$charIndex, $i],
65
                'returnType' => $returnType,
66
                'nullable_return_type' => $hasNullableReturnType,
67
                'is_static' => $isStatic,
68
                'is_abstract' => $isAbstract,
69
            ];
70
        }
71
72
        $class['methods'] = $methods;
73
74
        return $class;
75
    }
76
77
    private static function requestIssue($content)
78
    {
79
        dump('(O_o)   Well, It seems we had some problem parsing the contents of:   (o_O)');
80
        dump('Submit an issue on github: https://github.com/imanghafoori1/microscope');
81
        dump('Send us the content and mention your php version ('.phpversion().')');
82
        dump($content);
83
    }
84
85
    private static function findVisibility($tokens, $i)
86
    {
87
        $isStatic = $tokens[$i][0] === T_STATIC && $i -= 2;
88
        $isAbstract = $tokens[$i][0] === T_ABSTRACT && $i -= 2;
89
90
        $hasModifier = \in_array($tokens[$i][0], [T_PUBLIC, T_PROTECTED, T_PRIVATE]);
91
        $visibility = $hasModifier ? $tokens[$i] : [T_PUBLIC, 'public'];
92
93
        // We have to cover both syntax:
94
        //     public abstract function x() {
95
        //     abstract public function x() {
96
        ! $isAbstract && $isAbstract = $tokens[$i - 2][0] === T_ABSTRACT;
97
98
        return [$visibility, $isStatic, $isAbstract];
99
    }
100
101
    private static function processReturnType($char, $tokens, $charIndex)
102
    {
103
        if ($char != ':') {
104
            return [null, null, $char, $charIndex];
105
        }
106
107
        [$returnType, $returnTypeIndex] = FunctionCall::getNextToken($tokens, $charIndex);
0 ignored issues
show
Bug introduced by
The variable $returnType 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 $returnTypeIndex 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...
108
109
        // In case the return type is like this: function c() : ?string {...
110
        $hasNullableReturnType = ($returnType == '?');
111
112
        if ($hasNullableReturnType) {
113
            [$returnType, $returnTypeIndex] = FunctionCall::getNextToken($tokens, $returnTypeIndex);
114
        }
115
116
        [$char, $charIndex] = FunctionCall::getNextToken($tokens, $returnTypeIndex);
117
118
        return [$returnType, $hasNullableReturnType, $char, $charIndex];
119
    }
120
}
121