Completed
Push — php70 ( 83abde...b6aa8a )
by Wim
05:28 queued 02:49
created

ForbiddenGlobalVariableVariableSniff::process()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 15
rs 9.2
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_ForbiddenGlobalVariableVariableSniff.
4
 *
5
 * PHP version 7.0
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Wim Godden <[email protected]>
10
 */
11
12
/**
13
 * PHPCompatibility_Sniffs_PHP_ForbiddenGlobalVariableVariableSniff.
14
 *
15
 * Variable variables are forbidden with global
16
 *
17
 * PHP version 7.0
18
 *
19
 * @category  PHP
20
 * @package   PHPCompatibility
21
 * @author    Wim Godden <[email protected]>
22
 */
23
class PHPCompatibility_Sniffs_PHP_ForbiddenGlobalVariableVariableSniff extends PHPCompatibility_Sniff
24
{
25
26
    /**
27
     * Returns an array of tokens this test wants to listen for.
28
     *
29
     * @return array
30
     */
31
    public function register()
32
    {
33
        return array(T_GLOBAL);
34
    }
35
36
    /**
37
     * Processes this test, when one of its tokens is encountered.
38
     *
39
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
40
     * @param int                  $stackPtr  The position of the current token in the
41
     *                                        stack passed in $tokens.
42
     *
43
     * @return void
44
     */
45
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
46
    {
47
        if ($this->supportsAbove('7.0')) {
48
            $tokens = $phpcsFile->getTokens();
49
50
            $variable = $phpcsFile->findNext(T_VARIABLE, $stackPtr, $stackPtr + 4, false);
51
52
            if (isset($tokens[$variable - 1]) && $tokens[$variable - 1]['type'] == 'T_DOLLAR') {
53
                $error = sprintf(
54
                    "Global with variable variables are not allowed since PHP 7.0"
55
                );
56
                $phpcsFile->addError($error, $stackPtr);
57
            }
58
        }
59
    }
60
}
61