ClassMethods::findVisibility()   A
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 16
nop 2
dl 0
loc 15
rs 9.4555
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] = TokenManager::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] = TokenManager::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
                break;
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($endingChar, $tokens, $charIndex)
102
    {
103
        // No return type is defined.
104
        if ($endingChar != ':') {
105
            return [null, null, $endingChar, $charIndex];
106
        }
107
108
        [$returnType, $returnTypeIndex] = TokenManager::getNextToken($tokens, $charIndex);
0 ignored issues
show
Bug introduced by
The variable $returnType seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

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...
109
110
        // In case the return type is like this: function c() : ?string {...
111
        $hasNullableReturnType = ($returnType == '?');
0 ignored issues
show
Bug introduced by
The variable $returnType seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
112
113
        if ($hasNullableReturnType) {
114
            [$returnType, $returnTypeIndex] = TokenManager::getNextToken($tokens, $returnTypeIndex);
0 ignored issues
show
Bug introduced by
The variable $returnType seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
115
        }
116
117
        [$endingChar, $charIndex] = TokenManager::getNextToken($tokens, $returnTypeIndex);
118
119
        $returnType = [$returnType];
0 ignored issues
show
Bug introduced by
The variable $returnType seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
120
121
        while ($endingChar == '|') {
122
            [$returnType2, $charIndex] = TokenManager::getNextToken($tokens, $charIndex);
0 ignored issues
show
Bug introduced by
The variable $returnType2 does not exist. Did you mean $returnType?

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...
123
            $returnType[] = $returnType2;
0 ignored issues
show
Bug introduced by
The variable $returnType2 does not exist. Did you mean $returnType?

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...
124
            [$endingChar, $charIndex] = TokenManager::getNextToken($tokens, $charIndex);
125
        }
126
127
        return [$returnType, $hasNullableReturnType, $endingChar, $charIndex];
128
    }
129
}
130