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

PHPCSStandards / PHP_CodeSniffer / 15253296250

26 May 2025 11:55AM UTC coverage: 78.632% (+0.3%) from 78.375%
15253296250

Pull #1105

github

web-flow
Merge d9441d98f into caf806050
Pull Request #1105: Skip tests when 'git' command is not available

19665 of 25009 relevant lines covered (78.63%)

88.67 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
    }//end __construct()
72

73

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

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

100
            return;
×
101
        }
102

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

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

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

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

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

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

150
    }//end processTokenWithinScope()
151

152

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

181
    }//end processTokenOutsideScope()
182

183

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

198

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

213

214
    /**
215
     * Called to process variables found in double quoted strings or heredocs.
216
     *
217
     * Note that there may be more than one variable in the string, which will
218
     * result only in one call for the string or one call per line for heredocs.
219
     *
220
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this
221
     *                                               token was found.
222
     * @param int                         $stackPtr  The position where the double quoted
223
     *                                               string was found.
224
     *
225
     * @return void|int Optionally returns a stack pointer. The sniff will not be
226
     *                  called again on the current file until the returned stack
227
     *                  pointer is reached. Return `$phpcsFile->numTokens` to skip
228
     *                  the rest of the file.
229
     */
230
    abstract protected function processVariableInString(File $phpcsFile, $stackPtr);
231

232

233
}//end class
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc