VariableVariablesSniff::process()   C
last analyzed

Complexity

Conditions 17
Paths 14

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 63
rs 5.2166
cc 17
nc 14
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
namespace PHPCompatibility\Sniffs\PHP;
13
14
use PHPCompatibility\Sniff;
15
16
/**
17
 * \PHPCompatibility\Sniffs\PHP\VariableVariables.
18
 *
19
 * The interpretation of variable variables has changed in PHP 7.0.
20
 *
21
 * PHP version 7.0
22
 *
23
 * @category PHP
24
 * @package  PHPCompatibility
25
 * @author   Juliette Reinders Folmer <[email protected]>
26
 */
27
class VariableVariablesSniff extends Sniff
28
{
29
    /**
30
     * Returns an array of tokens this test wants to listen for.
31
     *
32
     * @return array
33
     */
34
    public function register()
35
    {
36
        return array(T_VARIABLE);
37
38
    }//end register()
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
45
     *                                         in the stack passed in $tokens.
46
     *
47
     * @return void
48
     */
49
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
50
    {
51
        if ($this->supportsAbove('7.0') === false) {
52
            return;
53
        }
54
55
        $tokens = $phpcsFile->getTokens();
56
57
        // Verify that the next token is a square open bracket. If not, bow out.
58
        $nextToken = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true);
59
60
        if ($nextToken === false || $tokens[$nextToken]['code'] !== T_OPEN_SQUARE_BRACKET || isset($tokens[$nextToken]['bracket_closer']) === false) {
61
            return;
62
        }
63
64
        // The previous non-empty token has to be a $, -> or ::.
65
        $prevToken = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true);
66
        if ($prevToken === false || in_array($tokens[$prevToken]['code'], array(T_DOLLAR, T_OBJECT_OPERATOR, T_DOUBLE_COLON), true) === false) {
67
            return;
68
        }
69
70
        // For static object calls, it only applies when this is a function call.
71
        if ($tokens[$prevToken]['code'] === T_DOUBLE_COLON) {
72
            $hasBrackets = $tokens[$nextToken]['bracket_closer'];
73
            while (($hasBrackets = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($hasBrackets + 1), null, true, null, true)) !== false) {
74
                if ($tokens[$hasBrackets]['code'] === T_OPEN_SQUARE_BRACKET) {
75
                    if (isset($tokens[$hasBrackets]['bracket_closer'])) {
76
                        $hasBrackets = $tokens[$hasBrackets]['bracket_closer'];
77
                        continue;
78
                    } else {
79
                        // Live coding.
80
                        return;
81
                    }
82
83
                } elseif ($tokens[$hasBrackets]['code'] === T_OPEN_PARENTHESIS) {
84
                    // Caught!
85
                    break;
86
87
                } else {
88
                    // Not a function call, so bow out.
89
                    return;
90
                }
91
            }
92
93
            // Now let's also prevent false positives when used with self and static which still work fine.
94
            $classToken = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($prevToken - 1), null, true, null, true);
95
            if ($classToken !== false) {
96
                if ($tokens[$classToken]['code'] === T_STATIC || $tokens[$classToken]['code'] === T_SELF) {
97
                    return;
98
                }
99
                elseif ($tokens[$classToken]['code'] === T_STRING && $tokens[$classToken]['content'] === 'self') {
100
                    return;
101
                }
102
            }
103
        }
104
105
        $phpcsFile->addError(
106
            '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.',
107
            $stackPtr,
108
            'Found'
109
        );
110
111
    }//end process()
112
113
114
}//end class
115