Completed
Push — feature/constantarrays-handle-... ( cae592 )
by Juliette
01:55
created

ConstantArraysUsingConstSniff::process()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 17
nc 5
nop 2
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\PHP\ConstantArraysUsingConstSniff.
4
 *
5
 * PHP version 5.6
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\ConstantArraysUsingConstSniff.
18
 *
19
 * Constant arrays using the constant keyword in PHP 5.6
20
 *
21
 * PHP version 5.6
22
 *
23
 * @category PHP
24
 * @package  PHPCompatibility
25
 * @author   Juliette Reinders Folmer <[email protected]>
26
 */
27
class ConstantArraysUsingConstSniff extends Sniff
28
{
29
30
    /**
31
     * Returns an array of tokens this test wants to listen for.
32
     *
33
     * @return array
34
     */
35
    public function register()
36
    {
37
        return array(T_CONST);
38
    }
39
40
    /**
41
     * Processes this test, when one of its tokens is encountered.
42
     *
43
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
44
     * @param int                   $stackPtr  The position of the current token in the
45
     *                                         stack passed in $tokens.
46
     *
47
     * @return void
48
     */
49
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
50
    {
51
        if ($this->supportsBelow('5.5') !== true) {
52
            return;
53
        }
54
55
        $tokens = $phpcsFile->getTokens();
56
        $find   = array(
57
            T_ARRAY             => T_ARRAY,
58
            T_OPEN_SHORT_ARRAY  => T_OPEN_SHORT_ARRAY,
59
        );
60
61
        while (($hasArray = $phpcsFile->findNext($find, ($stackPtr + 1), null, false, null, true)) !== false) {
62
            $phpcsFile->addError(
63
                'Constant arrays using the "const" keyword are not allowed in PHP 5.5 or earlier',
64
                $hasArray,
65
                'Found'
66
            );
67
68
            // Skip past the content of the array.
69
            $stackPtr = $hasArray;
70
            if ($tokens[$hasArray]['code'] === T_OPEN_SHORT_ARRAY && isset($tokens[$hasArray]['bracket_closer'])) {
71
                $stackPtr = $tokens[$hasArray]['bracket_closer'];
72
            } elseif ($tokens[$hasArray]['code'] === T_ARRAY && isset($tokens[$hasArray]['parenthesis_closer'])) {
73
                $stackPtr = $tokens[$hasArray]['parenthesis_closer'];
74
            }
75
        }
76
    }
77
}
78