ForbiddenNamesAsDeclaredSniff   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 33
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 224
rs 9.76

2 Methods

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