Completed
Pull Request — master (#310)
by Juliette
01:49
created

PHPCompatibility_Sniffs_PHP_VariableVariablesSniff   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 17
lcom 0
cbo 3
dl 0
loc 88
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
C process() 0 63 16
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_VariableVariables.
4
 *
5
 * PHP version 7.0
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Juliette Reinders Folmer <[email protected]>
10
 */
11
12
/**
13
 * PHPCompatibility_Sniffs_PHP_VariableVariables.
14
 *
15
 * The interpretation of variable variables has changed in PHP 7.0.
16
 *
17
 * PHP version 7.0
18
 *
19
 * @category  PHP
20
 * @package   PHPCompatibility
21
 * @author    Juliette Reinders Folmer <[email protected]>
22
 */
23
class PHPCompatibility_Sniffs_PHP_VariableVariablesSniff extends PHPCompatibility_Sniff
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
    /**
26
     * Returns an array of tokens this test wants to listen for.
27
     *
28
     * @return array
29
     */
30
    public function register()
31
    {
32
        return array(T_VARIABLE);
33
34
    }//end register()
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
41
     *                                        in the 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') === false) {
48
            return;
49
        }
50
51
        $tokens = $phpcsFile->getTokens();
52
53
        // Verify that the next token is a square open bracket. If not, bow out.
54
        $nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true);
55
56
        if ($nextToken === false || $tokens[$nextToken]['code'] !== T_OPEN_SQUARE_BRACKET || isset($tokens[$nextToken]['bracket_closer']) === false) {
57
            return;
58
        }
59
60
        // The previous token has to be a $, -> or ::.
61
        if (isset($tokens[($stackPtr - 1)]) === false || in_array($tokens[($stackPtr - 1)]['code'], array(T_DOLLAR, T_OBJECT_OPERATOR, T_DOUBLE_COLON), true) === false) {
62
            return;
63
        }
64
65
        // For static object calls, it only applies when this is a function call.
66
        if ($tokens[($stackPtr - 1)]['code'] === T_DOUBLE_COLON) {
67
            $hasBrackets = $tokens[$nextToken]['bracket_closer'];
68
            while (($hasBrackets = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($hasBrackets + 1), null, true, null, true)) !== false ) {
69
                if ($tokens[$hasBrackets]['code'] === T_OPEN_SQUARE_BRACKET) {
70
                    if (isset($tokens[$hasBrackets]['bracket_closer'])) {
71
						$hasBrackets = $tokens[$hasBrackets]['bracket_closer'];
72
                        continue;
73
                    }
74
                    else {
75
                        // Live coding.
76
                        return;
77
                    }
78
                }
79
                elseif ($tokens[$hasBrackets]['code'] === T_OPEN_PARENTHESIS) {
80
                    // Caught!
81
                    break;
82
                }
83
                else {
84
                    // Not a function call, so bow out.
85
                    return;
86
                }
87
            }
88
89
            // Now let's also prevent false positives when used with self and static which still work fine.
90
            $classToken = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 2), null, true, null, true);
91
            if ($classToken !== false) {
92
                if ($tokens[$classToken]['code'] === T_STATIC) {
93
                    return;
94
                }
95
                elseif ($tokens[$classToken]['code'] === T_STRING && $tokens[$classToken]['content'] === 'self') {
96
                    return;
97
                }
98
            }
99
        }
100
101
        $phpcsFile->addError(
102
            'Indirect access to variables, properties and methods will be evaluated strictly in left-to-right order since PHP 7.0. Use curly braces to remove ambiguity.',
103
            $stackPtr,
104
            'Found'
105
        );
106
107
    }//end process()
108
109
110
}//end class
111