Completed
Push — feature/issue-367-phpcs-3.x-co... ( e185aa )
by Juliette
01:53
created

ForbiddenNamesAsDeclaredSniff::process()   D

Complexity

Conditions 32
Paths 139

Size

Total Lines 126
Code Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 126
rs 4.1588
c 0
b 0
f 0
cc 32
eloc 75
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\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
    );
60
61
    /**
62
     * T_STRING keywords to recognize as soft reserved names.
63
     *
64
     * Using any of these keywords to name a class, interface, trait or namespace
65
     * is highly discouraged since they may be used in future versions of PHP.
66
     *
67
     * @var array
68
     */
69
    protected $softReservedNames = array(
70
        'resource' => '7.0',
71
        'object'   => '7.0',
72
        'mixed'    => '7.0',
73
        'numeric'  => '7.0',
74
    );
75
76
    /**
77
     * Combined list of the two lists above.
78
     *
79
     * Used for quick check whether or not something is a reserved
80
     * word.
81
     * Set from the `register()` method.
82
     *
83
     * @var array
84
     */
85
    private $allForbiddenNames = array();
86
87
88
    /**
89
     * Returns an array of tokens this test wants to listen for.
90
     *
91
     * @return array
92
     */
93
    public function register()
94
    {
95
        // Do the list merge only once.
96
        $this->allForbiddenNames = array_merge($this->forbiddenNames, $this->softReservedNames);
97
98
        return array(
99
            T_CLASS,
100
            T_INTERFACE,
101
            T_NAMESPACE,
102
            T_TRAIT,
103
            T_STRING, // Compat for PHPCS 1.x and PHP < 5.3.
104
        );
105
106
    }//end register()
107
108
109
    /**
110
     * Processes this test, when one of its tokens is encountered.
111
     *
112
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
113
     * @param int                   $stackPtr  The position of the current token in the
114
     *                                         stack passed in $tokens.
115
     *
116
     * @return void
117
     */
118
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
119
    {
120
        if ($this->supportsAbove('7.0') === false) {
121
            return;
122
        }
123
124
        $tokens         = $phpcsFile->getTokens();
125
        $tokenCode      = $tokens[$stackPtr]['code'];
126
        $tokenContentLc = strtolower($tokens[$stackPtr]['content']);
127
128
        // For string tokens we only care about 'trait' and 'namespace' as those are
129
        // the only ones which may not be correctly recognized as it's own token.
130
        // This only happens in older versions of PHP where the token doesn't exist yet as a keyword.
131
        if ($tokenCode === T_STRING && ($tokenContentLc !== 'trait' && $tokenContentLc !== 'namespace')) {
132
            return;
133
        }
134
135
        if (in_array($tokenCode, array(T_CLASS, T_INTERFACE, T_TRAIT), true)) {
136
            // Check for the declared name being a name which is not tokenized as T_STRING.
137
            $nextNonEmpty = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
138
            if ($nextNonEmpty !== false && isset($this->forbiddenTokens[$tokens[$nextNonEmpty]['code']]) === true) {
139
                $name = $tokens[$nextNonEmpty]['content'];
140
            } else {
141
                // Get the declared name if it's a T_STRING.
142
                $name = $phpcsFile->getDeclarationName($stackPtr);
143
            }
144
            unset($nextNonEmpty);
145
146
            if (isset($name) === false || is_string($name) === false || $name === '') {
147
                return;
148
            }
149
150
            $nameLc = strtolower($name);
151
            if (isset($this->allForbiddenNames[$nameLc]) === false) {
152
                return;
153
            }
154
155
        } elseif ($tokenCode === T_NAMESPACE) {
156
            $namespaceName = $this->getDeclaredNamespaceName($phpcsFile, $stackPtr);
157
158
            if ($namespaceName === false || $namespaceName === '') {
159
                return;
160
            }
161
162
            $namespaceParts = explode('\\', $namespaceName);
163
            foreach ($namespaceParts as $namespacePart) {
164
                $partLc = strtolower($namespacePart);
165
                if (isset($this->allForbiddenNames[$partLc]) === true) {
166
                    $name   = $namespacePart;
167
                    $nameLc = $partLc;
168
                    break;
169
                }
170
            }
171
        } elseif ($tokenCode === T_STRING) {
172
            // Traits and namespaces which are not yet tokenized as T_TRAIT/T_NAMESPACE.
173
            $nextNonEmpty = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
174
            if ($nextNonEmpty === false) {
175
                return;
176
            }
177
178
            $nextNonEmptyCode = $tokens[$nextNonEmpty]['code'];
179
180
            if ($nextNonEmptyCode !== T_STRING && isset($this->forbiddenTokens[$nextNonEmptyCode]) === true) {
181
                $name   = $tokens[$nextNonEmpty]['content'];
182
                $nameLc = strtolower($tokens[$nextNonEmpty]['content']);
183
            } elseif ($nextNonEmptyCode === T_STRING) {
184
                $endOfStatement = $phpcsFile->findNext(array(T_SEMICOLON, T_OPEN_CURLY_BRACKET), ($stackPtr + 1));
185
                if ($endOfStatement === false) {
186
                    return;
187
                }
188
189
                do {
190
                    $nextNonEmptyLc = strtolower($tokens[$nextNonEmpty]['content']);
191
192
                    if (isset($this->allForbiddenNames[$nextNonEmptyLc]) === true) {
193
                        $name   = $tokens[$nextNonEmpty]['content'];
194
                        $nameLc = $nextNonEmptyLc;
195
                        break;
196
                    }
197
198
                    $nextNonEmpty = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($nextNonEmpty + 1), $endOfStatement, true);
199
                } while ($nextNonEmpty !== false);
200
            }
201
            unset($nextNonEmptyCode, $nextNonEmptyLc, $endOfStatement);
202
        }
203
204
        if (isset($name, $nameLc) === false) {
205
            return;
206
        }
207
208
        // Still here, so this is one of the reserved words.
209
        // Build up the error message.
210
        $error     = "'%s' is a";
211
        $isError   = null;
212
        $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...
213
        $data      = array(
214
            $nameLc,
215
        );
216
217
        if (isset($this->softReservedNames[$nameLc]) === true
218
            && $this->supportsAbove($this->softReservedNames[$nameLc]) === true
219
        ) {
220
            $error  .= ' soft reserved keyword as of PHP version %s';
221
            $isError = false;
222
            $data[]  = $this->softReservedNames[$nameLc];
223
        }
224
225
        if (isset($this->forbiddenNames[$nameLc]) === true
226
            && $this->supportsAbove($this->forbiddenNames[$nameLc]) === true
227
        ) {
228
            if (isset($isError) === true) {
229
                $error .= ' and a';
230
            }
231
            $error  .= ' reserved keyword as of PHP version %s';
232
            $isError = true;
233
            $data[]  = $this->forbiddenNames[$nameLc];
234
        }
235
236
        if (isset($isError) === true) {
237
            $error .= ' and should not be used to name a class, interface or trait or as part of a namespace (%s)';
238
            $data[] = $tokens[$stackPtr]['type'];
239
240
            $this->addMessage($phpcsFile, $error, $stackPtr, $isError, $errorCode, $data);
241
        }
242
243
    }//end process()
244
245
}//end class
246