• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCSStandards / PHP_CodeSniffer / 17663621563

12 Sep 2025 03:25AM UTC coverage: 78.786%. Remained the same
17663621563

push

github

web-flow
Merge pull request #1243 from PHPCSStandards/phpcs-4.x/feature/155-normalize-some-code-style-rules-5

CS: normalize code style rules [5]

294 of 308 new or added lines in 191 files covered. (95.45%)

2354 existing lines in 130 files now uncovered.

19732 of 25045 relevant lines covered (78.79%)

96.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Sniffs/AbstractVariableSniff.php
1
<?php
2
/**
3
 * A class to find T_VARIABLE tokens.
4
 *
5
 * This class can distinguish between normal T_VARIABLE tokens, and those tokens
6
 * that represent class members. If a class member is encountered, then the
7
 * processMemberVar method is called so the extending class can process it. If
8
 * the token is found to be a normal T_VARIABLE token, then processVariable is
9
 * called.
10
 *
11
 * @author    Greg Sherwood <gsherwood@squiz.net>
12
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
13
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
14
 */
15

16
namespace PHP_CodeSniffer\Sniffs;
17

18
use PHP_CodeSniffer\Files\File;
19
use PHP_CodeSniffer\Util\Tokens;
20

21
abstract class AbstractVariableSniff extends AbstractScopeSniff
22
{
23

24
    /**
25
     * List of PHP Reserved variables.
26
     *
27
     * Used by various naming convention sniffs.
28
     *
29
     * @var array<string, true>
30
     */
31
    protected const PHP_RESERVED_VARS = [
32
        '_SERVER'              => true,
33
        '_GET'                 => true,
34
        '_POST'                => true,
35
        '_REQUEST'             => true,
36
        '_SESSION'             => true,
37
        '_ENV'                 => true,
38
        '_COOKIE'              => true,
39
        '_FILES'               => true,
40
        'GLOBALS'              => true,
41
        'http_response_header' => true,
42
        'HTTP_RAW_POST_DATA'   => true,
43
        'php_errormsg'         => true,
44
    ];
45

46
    /**
47
     * List of PHP Reserved variables.
48
     *
49
     * @var array<string, true>
50
     *
51
     * @deprecated 4.0.0 Use the AbstractVariableSniff::PHP_RESERVED_VARS constant instead.
52
     */
53
    protected $phpReservedVars = self::PHP_RESERVED_VARS;
54

55

56
    /**
57
     * Constructs an AbstractVariableTest.
58
     */
59
    public function __construct()
×
60
    {
61
        $scopes = Tokens::OO_SCOPE_TOKENS;
×
62

63
        $listen = [
64
            T_VARIABLE,
×
65
            T_DOUBLE_QUOTED_STRING,
×
66
            T_HEREDOC,
×
67
        ];
68

69
        parent::__construct($scopes, $listen, true);
×
70
    }
71

72

73
    /**
74
     * Processes the token in the specified PHP_CodeSniffer\Files\File.
75
     *
76
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this
77
     *                                               token was found.
78
     * @param int                         $stackPtr  The position where the token was found.
79
     * @param int                         $currScope The current scope opener token.
80
     *
81
     * @return void|int Optionally returns a stack pointer. The sniff will not be
82
     *                  called again on the current file until the returned stack
83
     *                  pointer is reached. Return `$phpcsFile->numTokens` to skip
84
     *                  the rest of the file.
85
     */
UNCOV
86
    final protected function processTokenWithinScope(File $phpcsFile, int $stackPtr, int $currScope)
×
87
    {
UNCOV
88
        $tokens = $phpcsFile->getTokens();
×
89

UNCOV
90
        if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING
×
91
            || $tokens[$stackPtr]['code'] === T_HEREDOC
×
92
        ) {
93
            // Check to see if this string has a variable in it.
UNCOV
94
            $pattern = '|(?<!\\\\)(?:\\\\{2})*\${?[a-zA-Z0-9_]+}?|';
×
95
            if (preg_match($pattern, $tokens[$stackPtr]['content']) !== 0) {
×
96
                return $this->processVariableInString($phpcsFile, $stackPtr);
×
97
            }
98

UNCOV
99
            return;
×
100
        }
101

102
        // If this token is nested inside a function at a deeper
103
        // level than the current OO scope that was found, it's a normal
104
        // variable and not a member var.
UNCOV
105
        $conditions = array_reverse($tokens[$stackPtr]['conditions'], true);
×
106
        $inFunction = false;
×
107
        foreach ($conditions as $scope => $code) {
×
108
            if (isset(Tokens::OO_SCOPE_TOKENS[$code]) === true) {
×
109
                break;
×
110
            }
111

UNCOV
112
            if ($code === T_FUNCTION || $code === T_CLOSURE) {
×
113
                $inFunction = true;
×
114
            }
115
        }
116

UNCOV
117
        if ($scope !== $currScope) {
×
118
            // We found a closer scope to this token, so ignore
119
            // this particular time through the sniff. We will process
120
            // this token when this closer scope is found to avoid
121
            // duplicate checks.
UNCOV
122
            return;
×
123
        }
124

125
        // Just make sure this isn't a variable in a function declaration.
UNCOV
126
        if ($inFunction === false && isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
×
127
            foreach ($tokens[$stackPtr]['nested_parenthesis'] as $opener => $closer) {
×
128
                if (isset($tokens[$opener]['parenthesis_owner']) === false) {
×
129
                    continue;
×
130
                }
131

UNCOV
132
                $owner = $tokens[$opener]['parenthesis_owner'];
×
133
                if ($tokens[$owner]['code'] === T_FUNCTION
×
134
                    || $tokens[$owner]['code'] === T_CLOSURE
×
135
                    || $tokens[$owner]['code'] === T_USE
×
136
                ) {
UNCOV
137
                    $inFunction = true;
×
138
                    break;
×
139
                }
140
            }
141
        }//end if
142

UNCOV
143
        if ($inFunction === true) {
×
144
            return $this->processVariable($phpcsFile, $stackPtr);
×
145
        } else {
UNCOV
146
            return $this->processMemberVar($phpcsFile, $stackPtr);
×
147
        }
148
    }
149

150

151
    /**
152
     * Processes the token outside the scope in the file.
153
     *
154
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this
155
     *                                               token was found.
156
     * @param int                         $stackPtr  The position where the token was found.
157
     *
158
     * @return void|int Optionally returns a stack pointer. The sniff will not be
159
     *                  called again on the current file until the returned stack
160
     *                  pointer is reached. Return `$phpcsFile->numTokens` to skip
161
     *                  the rest of the file.
162
     */
UNCOV
163
    final protected function processTokenOutsideScope(File $phpcsFile, int $stackPtr)
×
164
    {
165
        $tokens = $phpcsFile->getTokens();
×
166
        // These variables are not member vars.
167
        if ($tokens[$stackPtr]['code'] === T_VARIABLE) {
×
UNCOV
168
            return $this->processVariable($phpcsFile, $stackPtr);
×
169
        } elseif ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING
×
170
            || $tokens[$stackPtr]['code'] === T_HEREDOC
×
171
        ) {
172
            // Check to see if this string has a variable in it.
UNCOV
173
            $pattern = '|(?<!\\\\)(?:\\\\{2})*\${?[a-zA-Z0-9_]+}?|';
×
UNCOV
174
            if (preg_match($pattern, $tokens[$stackPtr]['content']) !== 0) {
×
175
                return $this->processVariableInString($phpcsFile, $stackPtr);
×
176
            }
177
        }
178
    }
179

180

181
    /**
182
     * Called to process class member vars.
183
     *
184
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this
185
     *                                               token was found.
186
     * @param int                         $stackPtr  The position where the token was found.
187
     *
188
     * @return void|int Optionally returns a stack pointer. The sniff will not be
189
     *                  called again on the current file until the returned stack
190
     *                  pointer is reached. Return `$phpcsFile->numTokens` to skip
191
     *                  the rest of the file.
192
     */
193
    abstract protected function processMemberVar(File $phpcsFile, int $stackPtr);
194

195

196
    /**
197
     * Called to process normal member vars.
198
     *
199
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this
200
     *                                               token was found.
201
     * @param int                         $stackPtr  The position where the token was found.
202
     *
203
     * @return void|int Optionally returns a stack pointer. The sniff will not be
204
     *                  called again on the current file until the returned stack
205
     *                  pointer is reached. Return `$phpcsFile->numTokens` to skip
206
     *                  the rest of the file.
207
     */
208
    abstract protected function processVariable(File $phpcsFile, int $stackPtr);
209

210

211
    /**
212
     * Called to process variables found in double quoted strings or heredocs.
213
     *
214
     * Note that there may be more than one variable in the string, which will
215
     * result only in one call for the string or one call per line for heredocs.
216
     *
217
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this
218
     *                                               token was found.
219
     * @param int                         $stackPtr  The position where the double quoted
220
     *                                               string was found.
221
     *
222
     * @return void|int Optionally returns a stack pointer. The sniff will not be
223
     *                  called again on the current file until the returned stack
224
     *                  pointer is reached. Return `$phpcsFile->numTokens` to skip
225
     *                  the rest of the file.
226
     */
227
    abstract protected function processVariableInString(File $phpcsFile, int $stackPtr);
228
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc