ForbiddenNamesAsDeclaredSniff::process()   F
last analyzed

Complexity

Conditions 31
Paths 139

Size

Total Lines 126

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 126
rs 3.0733
c 0
b 0
f 0
cc 31
nc 139
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * PHPCompatibility, an external standard for PHP_CodeSniffer.
4
 *
5
 * @package   PHPCompatibility
6
 * @copyright 2012-2019 PHPCompatibility Contributors
7
 * @license   https://opensource.org/licenses/LGPL-3.0 LGPL3
8
 * @link      https://github.com/PHPCompatibility/PHPCompatibility
9
 */
10
11
namespace PHPCompatibility\Sniffs\Keywords;
12
13
use PHPCompatibility\Sniff;
14
use PHP_CodeSniffer_File as File;
15
use PHP_CodeSniffer_Tokens as Tokens;
16
17
/**
18
 * Detects the use of some reserved keywords to name a class, interface, trait or namespace.
19
 *
20
 * Emits errors for reserved words and warnings for soft-reserved words.
21
 *
22
 * PHP version 7.0+
23
 *
24
 * @link https://www.php.net/manual/en/reserved.other-reserved-words.php
25
 * @link https://wiki.php.net/rfc/reserve_more_types_in_php_7
26
 *
27
 * @since 7.0.8
28
 * @since 7.1.4 This sniff now throws a warning (soft reserved) or an error (reserved) depending
29
 *              on the `testVersion` set. Previously it would always throw an error.
30
 */
31
class ForbiddenNamesAsDeclaredSniff extends Sniff
32
{
33
34
    /**
35
     * List of tokens which can not be used as class, interface, trait names or as part of a namespace.
36
     *
37
     * @since 7.0.8
38
     *
39
     * @var array
40
     */
41
    protected $forbiddenTokens = array(
42
        \T_NULL  => '7.0',
43
        \T_TRUE  => '7.0',
44
        \T_FALSE => '7.0',
45
    );
46
47
    /**
48
     * T_STRING keywords to recognize as forbidden names.
49
     *
50
     * @since 7.0.8
51
     *
52
     * @var array
53
     */
54
    protected $forbiddenNames = array(
55
        'null'     => '7.0',
56
        'true'     => '7.0',
57
        'false'    => '7.0',
58
        'bool'     => '7.0',
59
        'int'      => '7.0',
60
        'float'    => '7.0',
61
        'string'   => '7.0',
62
        'iterable' => '7.1',
63
        'void'     => '7.1',
64
        'object'   => '7.2',
65
    );
66
67
    /**
68
     * T_STRING keywords to recognize as soft reserved names.
69
     *
70
     * Using any of these keywords to name a class, interface, trait or namespace
71
     * is highly discouraged since they may be used in future versions of PHP.
72
     *
73
     * @since 7.0.8
74
     *
75
     * @var array
76
     */
77
    protected $softReservedNames = array(
78
        'resource' => '7.0',
79
        'object'   => '7.0',
80
        'mixed'    => '7.0',
81
        'numeric'  => '7.0',
82
    );
83
84
    /**
85
     * Combined list of the two lists above.
86
     *
87
     * Used for quick check whether or not something is a reserved
88
     * word.
89
     * Set from the `register()` method.
90
     *
91
     * @since 7.0.8
92
     *
93
     * @var array
94
     */
95
    private $allForbiddenNames = array();
96
97
98
    /**
99
     * Returns an array of tokens this test wants to listen for.
100
     *
101
     * @since 7.0.8
102
     *
103
     * @return array
104
     */
105
    public function register()
106
    {
107
        // Do the list merge only once.
108
        $this->allForbiddenNames = array_merge($this->forbiddenNames, $this->softReservedNames);
109
110
        $targets = array(
111
            \T_CLASS,
112
            \T_INTERFACE,
113
            \T_TRAIT,
114
            \T_NAMESPACE,
115
            \T_STRING, // Compat for PHPCS < 2.4.0 and PHP < 5.3.
116
        );
117
118
        return $targets;
119
    }
120
121
122
    /**
123
     * Processes this test, when one of its tokens is encountered.
124
     *
125
     * @since 7.0.8
126
     *
127
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
128
     * @param int                   $stackPtr  The position of the current token in the
129
     *                                         stack passed in $tokens.
130
     *
131
     * @return void
132
     */
133
    public function process(File $phpcsFile, $stackPtr)
134
    {
135
        if ($this->supportsAbove('7.0') === false) {
136
            return;
137
        }
138
139
        $tokens         = $phpcsFile->getTokens();
140
        $tokenCode      = $tokens[$stackPtr]['code'];
141
        $tokenType      = $tokens[$stackPtr]['type'];
142
        $tokenContentLc = strtolower($tokens[$stackPtr]['content']);
143
144
        // For string tokens we only care about 'trait' as that is the only one
145
        // which may not be correctly recognized as it's own token.
146
        // This only happens in older versions of PHP where the token doesn't exist yet as a keyword.
147
        if ($tokenCode === \T_STRING && $tokenContentLc !== 'trait') {
148
            return;
149
        }
150
151
        if (\in_array($tokenType, array('T_CLASS', 'T_INTERFACE', 'T_TRAIT'), true)) {
152
            // Check for the declared name being a name which is not tokenized as T_STRING.
153
            $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
154
            if ($nextNonEmpty !== false && isset($this->forbiddenTokens[$tokens[$nextNonEmpty]['code']]) === true) {
155
                $name = $tokens[$nextNonEmpty]['content'];
156
            } else {
157
                // Get the declared name if it's a T_STRING.
158
                $name = $phpcsFile->getDeclarationName($stackPtr);
159
            }
160
            unset($nextNonEmpty);
161
162
            if (isset($name) === false || \is_string($name) === false || $name === '') {
163
                return;
164
            }
165
166
            $nameLc = strtolower($name);
167
            if (isset($this->allForbiddenNames[$nameLc]) === false) {
168
                return;
169
            }
170
171
        } elseif ($tokenCode === \T_NAMESPACE) {
172
            $namespaceName = $this->getDeclaredNamespaceName($phpcsFile, $stackPtr);
173
174
            if ($namespaceName === false || $namespaceName === '') {
175
                return;
176
            }
177
178
            $namespaceParts = explode('\\', $namespaceName);
179
            foreach ($namespaceParts as $namespacePart) {
180
                $partLc = strtolower($namespacePart);
181
                if (isset($this->allForbiddenNames[$partLc]) === true) {
182
                    $name   = $namespacePart;
183
                    $nameLc = $partLc;
184
                    break;
185
                }
186
            }
187
        } elseif ($tokenCode === \T_STRING) {
188
            // Traits which are not yet tokenized as T_TRAIT.
189
            $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
190
            if ($nextNonEmpty === false) {
191
                return;
192
            }
193
194
            $nextNonEmptyCode = $tokens[$nextNonEmpty]['code'];
195
196
            if ($nextNonEmptyCode !== \T_STRING && isset($this->forbiddenTokens[$nextNonEmptyCode]) === true) {
197
                $name   = $tokens[$nextNonEmpty]['content'];
198
                $nameLc = strtolower($tokens[$nextNonEmpty]['content']);
199
            } elseif ($nextNonEmptyCode === \T_STRING) {
200
                $endOfStatement = $phpcsFile->findNext(array(\T_SEMICOLON, \T_OPEN_CURLY_BRACKET), ($stackPtr + 1));
201
                if ($endOfStatement === false) {
202
                    return;
203
                }
204
205
                do {
206
                    $nextNonEmptyLc = strtolower($tokens[$nextNonEmpty]['content']);
207
208
                    if (isset($this->allForbiddenNames[$nextNonEmptyLc]) === true) {
209
                        $name   = $tokens[$nextNonEmpty]['content'];
210
                        $nameLc = $nextNonEmptyLc;
211
                        break;
212
                    }
213
214
                    $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextNonEmpty + 1), $endOfStatement, true);
215
                } while ($nextNonEmpty !== false);
216
            }
217
            unset($nextNonEmptyCode, $nextNonEmptyLc, $endOfStatement);
218
        }
219
220
        if (isset($name, $nameLc) === false) {
221
            return;
222
        }
223
224
        // Still here, so this is one of the reserved words.
225
        // Build up the error message.
226
        $error     = "'%s' is a";
227
        $isError   = null;
228
        $errorCode = $this->stringToErrorCode($nameLc) . 'Found';
229
        $data      = array(
230
            $nameLc,
231
        );
232
233
        if (isset($this->softReservedNames[$nameLc]) === true
234
            && $this->supportsAbove($this->softReservedNames[$nameLc]) === true
235
        ) {
236
            $error  .= ' soft reserved keyword as of PHP version %s';
237
            $isError = false;
238
            $data[]  = $this->softReservedNames[$nameLc];
239
        }
240
241
        if (isset($this->forbiddenNames[$nameLc]) === true
242
            && $this->supportsAbove($this->forbiddenNames[$nameLc]) === true
243
        ) {
244
            if (isset($isError) === true) {
245
                $error .= ' and a';
246
            }
247
            $error  .= ' reserved keyword as of PHP version %s';
248
            $isError = true;
249
            $data[]  = $this->forbiddenNames[$nameLc];
250
        }
251
252
        if (isset($isError) === true) {
253
            $error .= ' and should not be used to name a class, interface or trait or as part of a namespace (%s)';
254
            $data[] = $tokens[$stackPtr]['type'];
255
256
            $this->addMessage($phpcsFile, $error, $stackPtr, $isError, $errorCode, $data);
257
        }
258
    }
259
}
260