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

ForbiddenGlobalVariableVariableSniff   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 38
rs 10
wmc 5
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A process() 0 15 4
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