NewConstantArraysUsingConstSniff::register()   A
last analyzed

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 0
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\InitialValue;
12
13
use PHPCompatibility\Sniff;
14
use PHP_CodeSniffer_File as File;
15
16
/**
17
 * Detect declaration of constants using the `const` keyword with a (constant) array value
18
 * as supported since PHP 5.6.
19
 *
20
 * PHP version 5.6
21
 *
22
 * @link https://wiki.php.net/rfc/const_scalar_exprs
23
 * @link https://www.php.net/manual/en/language.constants.syntax.php
24
 *
25
 * @since 7.1.4
26
 * @since 9.0.0 Renamed from `ConstantArraysUsingConstSniff` to `NewConstantArraysUsingConstSniff`.
27
 */
28
class NewConstantArraysUsingConstSniff extends Sniff
29
{
30
31
    /**
32
     * Returns an array of tokens this test wants to listen for.
33
     *
34
     * @since 7.1.4
35
     *
36
     * @return array
37
     */
38
    public function register()
39
    {
40
        return array(\T_CONST);
41
    }
42
43
    /**
44
     * Processes this test, when one of its tokens is encountered.
45
     *
46
     * @since 7.1.4
47
     *
48
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
49
     * @param int                   $stackPtr  The position of the current token in the
50
     *                                         stack passed in $tokens.
51
     *
52
     * @return void
53
     */
54
    public function process(File $phpcsFile, $stackPtr)
55
    {
56
        if ($this->supportsBelow('5.5') !== true) {
57
            return;
58
        }
59
60
        $tokens = $phpcsFile->getTokens();
61
        $find   = array(
62
            \T_ARRAY            => \T_ARRAY,
63
            \T_OPEN_SHORT_ARRAY => \T_OPEN_SHORT_ARRAY,
64
        );
65
66
        while (($hasArray = $phpcsFile->findNext($find, ($stackPtr + 1), null, false, null, true)) !== false) {
67
            $phpcsFile->addError(
68
                'Constant arrays using the "const" keyword are not allowed in PHP 5.5 or earlier',
69
                $hasArray,
70
                'Found'
71
            );
72
73
            // Skip past the content of the array.
74
            $stackPtr = $hasArray;
75 View Code Duplication
            if ($tokens[$hasArray]['code'] === \T_OPEN_SHORT_ARRAY && isset($tokens[$hasArray]['bracket_closer'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
                $stackPtr = $tokens[$hasArray]['bracket_closer'];
77
            } elseif ($tokens[$hasArray]['code'] === \T_ARRAY && isset($tokens[$hasArray]['parenthesis_closer'])) {
78
                $stackPtr = $tokens[$hasArray]['parenthesis_closer'];
79
            }
80
        }
81
    }
82
}
83