GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#67)
by
unknown
01:21
created

ReflectionUtils::isSkipped()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spatie\DataTransferObject;
6
7
use ReflectionClass;
8
use RuntimeException;
9
10
class ReflectionUtils
11
{
12
13
    private const SKIPPED = [
14
        'string',
15
        'int',
16
        'integer',
17
        'float',
18
        'bool',
19
        'double',
20
        'array',
21
        'object',
22
        'callable',
23
        'callback',
24
        'iterable',
25
        'void',
26
        'null',
27
        'mixed',
28
    ];
29
30
    /**
31
     * @param string $type
32
     * @return bool
33
     */
34
    public static function isSkipped(string $type): bool
35
    {
36
        return in_array(strtolower($type), self::SKIPPED, true);
37
    }
38
39
    /**
40
     * @param ReflectionClass $class
41
     * @return array
42
     */
43
    public static function getUsesAndAliases(ReflectionClass $class): array
44
    {
45
        try {
46
            if ($class->isAnonymous()) {
47
                throw new RuntimeException('Anonymous classes are not supported.');
48
            }
49
            static $cache = [];
50
            if (! isset($cache[$name = $class->getName()])) {
51
                if ($class->isInternal()) {
52
                    $cache[$name] = [];
53
                } else {
54
                    $code = file_get_contents($class->getFileName());
55
                    $cache = self::parseUsesAndAliases($code, $name) + $cache;
56
                }
57
            }
58
            return $cache[$name];
59
        } catch (\Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
60
61
        }
62
63
        return [];
64
    }
65
66
    /**
67
     * @param string $code
68
     * @param string|null $forClass
69
     * @return array
70
     */
71
    private static function parseUsesAndAliases(string $code, string $forClass = null): array
72
    {
73
        try {
74
            $tokens = token_get_all($code, TOKEN_PARSE);
75
        } catch (\Exception $e) {
76
            $tokens = [];
77
        }
78
        $namespace = $class = $classLevel = $level = null;
79
        $res = $uses = [];
80
81
        while ($token = current($tokens)) {
82
            next($tokens);
83
            switch (is_array($token) ? $token[0] : $token) {
84
                case T_NAMESPACE:
85
                    $namespace = ltrim(self::fetch($tokens, [T_STRING, T_NS_SEPARATOR]).'\\', '\\');
86
                    $uses = [];
87
                    break;
88
89
                case T_CLASS:
90
                case T_INTERFACE:
91
                case T_TRAIT:
92
                    if ($name = self::fetch($tokens, T_STRING)) {
93
                        $class = $namespace.$name;
94
                        $classLevel = $level + 1;
95
                        $res[$class] = $uses;
96
                        if ($class === $forClass) {
97
                            return $res;
98
                        }
99
                    }
100
                    break;
101
102
                case T_USE:
103
                    while (! $class && ($name = self::fetch($tokens, [T_STRING, T_NS_SEPARATOR]))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $class of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
104
                        $name = ltrim($name, '\\');
105
                        if (self::fetch($tokens, '{')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::fetch($tokens, '{') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
106
                            while ($suffix = self::fetch($tokens, [T_STRING, T_NS_SEPARATOR])) {
107
                                /* @noinspection NotOptimalIfConditionsInspection */
108
                                if (self::fetch($tokens, T_AS)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::fetch($tokens, T_AS) of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
109
                                    $uses[self::fetch($tokens, T_STRING)] = $name.$suffix;
110
                                } else {
111
                                    $tmp = explode('\\', $suffix);
112
                                    $uses[end($tmp)] = $name.$suffix;
113
                                }
114
                                if (! self::fetch($tokens, ',')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::fetch($tokens, ',') of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
115
                                    break;
116
                                }
117
                            }
118
119
                        } elseif (self::fetch($tokens, T_AS)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::fetch($tokens, T_AS) of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
120
                            $uses[self::fetch($tokens, T_STRING)] = $name;
121
122
                        } else {
123
                            $tmp = explode('\\', $name);
124
                            $uses[end($tmp)] = $name;
125
                        }
126
                        if (! self::fetch($tokens, ',')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::fetch($tokens, ',') of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
127
                            break;
128
                        }
129
                    }
130
                    break;
131
132
                case T_CURLY_OPEN:
133
                case T_DOLLAR_OPEN_CURLY_BRACES:
134
                case '{':
135
                    $level++;
136
                    break;
137
138
                case '}':
139
                    if ($level === $classLevel) {
140
                        $class = $classLevel = null;
141
                    }
142
                    $level--;
143
            }
144
        }
145
146
        return $res;
147
    }
148
149
    private static function fetch(&$tokens, $take): ?string
150
    {
151
        $res = null;
152
        while ($token = current($tokens)) {
153
            [$token, $s] = is_array($token) ? $token : [$token, $token];
0 ignored issues
show
Bug introduced by
The variable $s 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...
154
            if (in_array($token, (array) $take, true)) {
155
                $res .= $s;
156
            } elseif (! in_array($token, [T_DOC_COMMENT, T_WHITESPACE, T_COMMENT], true)) {
157
                break;
158
            }
159
            next($tokens);
160
        }
161
        return $res;
162
    }
163
}
164