~ubuntu-branches/ubuntu/vivid/php-codesniffer/vivid

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.4/CodeSniffer/Standards/AbstractVariableSniff.php

  • Committer: Package Import Robot
  • Author(s): David Prévot, Greg Sherwood, Alexey, Emily, David Prévot
  • Date: 2014-09-26 13:44:35 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20140926134435-wvjq16miqq4d60y0
Tags: 1.5.5-1
[ Greg Sherwood ]
* Improved closure support in Generic ScopeIndentSniff
* Improved indented PHP tag support in Generic ScopeIndentSniff
* Standards can now be located within hidden directories
 (further fix for bug #20323)
* Fixed bug #20373 : Inline comment sniff tab handling way
* Fixed bug #20378 : Report appended to existing file if no errors
  found in run
* Fixed bug #20381 : Invalid "Comment closer must be on a new line"
* PHP tokenizer no longer converts class/function names to special
  tokens types
* Fixed bug #20386 : Squiz.Commenting.ClassComment.SpacingBefore
  thrown if first block comment
* Squiz and PEAR FunctionCommentSnif now support _()
* PEAR ValidFunctionNameSniff no longer throws an error for _()
* Fixed bug #248 : FunctionCommentSniff expects ampersand on param name
* Fixed bug #248 in Squiz sniff as well
* Fixed bug #265 : False positives with type hints in ForbiddenFunctionsSniff
* Prepare for 1.5.5 release

[ Alexey ]
* Allowed single undersored methods and functions

[ Emily ]
* Added var_dump to discouraged functions sniff

[ David Prévot ]
* Revert "Add XS-Testsuite still needed for ci.d.n"
* Add self to uploaders
* Bump standards version to 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
 * A class to find T_VARIABLE tokens.
4
 
 *
5
 
 * PHP version 5
6
 
 *
7
 
 * @category  PHP
8
 
 * @package   PHP_CodeSniffer
9
 
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 
 * @author    Marc McIntyre <mmcintyre@squiz.net>
11
 
 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
12
 
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
13
 
 * @link      http://pear.php.net/package/PHP_CodeSniffer
14
 
 */
15
 
 
16
 
if (class_exists('PHP_CodeSniffer_Standards_AbstractScopeSniff', true) === false) {
17
 
    $error = 'Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found';
18
 
    throw new PHP_CodeSniffer_Exception($error);
19
 
}
20
 
 
21
 
/**
22
 
 * A class to find T_VARIABLE tokens.
23
 
 *
24
 
 * This class can distinguish between normal T_VARIABLE tokens, and those tokens
25
 
 * that represent class members. If a class member is encountered, then then
26
 
 * processMemberVar method is called so the extending class can process it. If
27
 
 * the token is found to be a normal T_VARIABLE token, then processVariable is
28
 
 * called.
29
 
 *
30
 
 * @category  PHP
31
 
 * @package   PHP_CodeSniffer
32
 
 * @author    Greg Sherwood <gsherwood@squiz.net>
33
 
 * @author    Marc McIntyre <mmcintyre@squiz.net>
34
 
 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
35
 
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
36
 
 * @version   Release: 1.5.4
37
 
 * @link      http://pear.php.net/package/PHP_CodeSniffer
38
 
 */
39
 
abstract class PHP_CodeSniffer_Standards_AbstractVariableSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff
40
 
{
41
 
 
42
 
    /**
43
 
     * The end token of the current function that we are in.
44
 
     *
45
 
     * @var int
46
 
     */
47
 
    private $_endFunction = -1;
48
 
 
49
 
    /**
50
 
     * true if a function is currently open.
51
 
     *
52
 
     * @var boolean
53
 
     */
54
 
    private $_functionOpen = false;
55
 
 
56
 
    /**
57
 
     * The current PHP_CodeSniffer file that we are processing.
58
 
     *
59
 
     * @var PHP_CodeSniffer_File
60
 
     */
61
 
    protected $currentFile = null;
62
 
 
63
 
 
64
 
    /**
65
 
     * Constructs an AbstractVariableTest.
66
 
     */
67
 
    public function __construct()
68
 
    {
69
 
        $scopes = array(
70
 
                   T_CLASS,
71
 
                   T_TRAIT,
72
 
                   T_INTERFACE,
73
 
                  );
74
 
 
75
 
        $listen = array(
76
 
                   T_FUNCTION,
77
 
                   T_VARIABLE,
78
 
                   T_DOUBLE_QUOTED_STRING,
79
 
                   T_HEREDOC,
80
 
                  );
81
 
 
82
 
        parent::__construct($scopes, $listen, true);
83
 
 
84
 
    }//end __construct()
85
 
 
86
 
 
87
 
    /**
88
 
     * Processes the token in the specified PHP_CodeSniffer_File.
89
 
     *
90
 
     * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
91
 
     *                                        token was found.
92
 
     * @param int                  $stackPtr  The position where the token was found.
93
 
     * @param array                $currScope The current scope opener token.
94
 
     *
95
 
     * @return void
96
 
     */
97
 
    protected final function processTokenWithinScope(
98
 
        PHP_CodeSniffer_File $phpcsFile,
99
 
        $stackPtr,
100
 
        $currScope
101
 
    ) {
102
 
        if ($this->currentFile !== $phpcsFile) {
103
 
            $this->currentFile   = $phpcsFile;
104
 
            $this->_functionOpen = false;
105
 
            $this->_endFunction  = -1;
106
 
        }
107
 
 
108
 
        $tokens = $phpcsFile->getTokens();
109
 
 
110
 
        if ($stackPtr > $this->_endFunction) {
111
 
            $this->_functionOpen = false;
112
 
        }
113
 
 
114
 
        if ($tokens[$stackPtr]['code'] === T_FUNCTION
115
 
            && $this->_functionOpen === false
116
 
        ) {
117
 
            $this->_functionOpen = true;
118
 
 
119
 
            $methodProps = $phpcsFile->getMethodProperties($stackPtr);
120
 
 
121
 
            // If the function is abstract, or is in an interface,
122
 
            // then set the end of the function to it's closing semicolon.
123
 
            if ($methodProps['is_abstract'] === true
124
 
                || $tokens[$currScope]['code'] === T_INTERFACE
125
 
            ) {
126
 
                $this->_endFunction
127
 
                    = $phpcsFile->findNext(array(T_SEMICOLON), $stackPtr);
128
 
            } else {
129
 
                if (isset($tokens[$stackPtr]['scope_closer']) === false) {
130
 
                    $error = 'Possible parse error: non-abstract method defined as abstract';
131
 
                    $phpcsFile->addWarning($error, $stackPtr);
132
 
                    return;
133
 
                }
134
 
 
135
 
                $this->_endFunction = $tokens[$stackPtr]['scope_closer'];
136
 
            }
137
 
        }
138
 
 
139
 
        if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING
140
 
            || $tokens[$stackPtr]['code'] === T_HEREDOC
141
 
        ) {
142
 
            // Check to see if this string has a variable in it.
143
 
            $pattern = '|(?<!\\\\)(?:\\\\{2})*\${?[a-zA-Z0-9_]+}?|';
144
 
            if (preg_match($pattern, $tokens[$stackPtr]['content']) !== 0) {
145
 
                $this->processVariableInString($phpcsFile, $stackPtr);
146
 
            }
147
 
 
148
 
            return;
149
 
        }
150
 
 
151
 
        if ($this->_functionOpen === true) {
152
 
            if ($tokens[$stackPtr]['code'] === T_VARIABLE) {
153
 
                $this->processVariable($phpcsFile, $stackPtr);
154
 
            }
155
 
        } else {
156
 
            // What if we assign a member variable to another?
157
 
            // ie. private $_count = $this->_otherCount + 1;.
158
 
            $this->processMemberVar($phpcsFile, $stackPtr);
159
 
        }
160
 
 
161
 
    }//end processTokenWithinScope()
162
 
 
163
 
 
164
 
    /**
165
 
     * Processes the token outside the scope in the file.
166
 
     *
167
 
     * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
168
 
     *                                        token was found.
169
 
     * @param int                  $stackPtr  The position where the token was found.
170
 
     *
171
 
     * @return void
172
 
     */
173
 
    protected final function processTokenOutsideScope(
174
 
        PHP_CodeSniffer_File $phpcsFile,
175
 
        $stackPtr
176
 
    ) {
177
 
        $tokens = $phpcsFile->getTokens();
178
 
        // These variables are not member vars.
179
 
        if ($tokens[$stackPtr]['code'] === T_VARIABLE) {
180
 
            $this->processVariable($phpcsFile, $stackPtr);
181
 
        } else if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING
182
 
            || $tokens[$stackPtr]['code'] === T_HEREDOC
183
 
        ) {
184
 
            // Check to see if this string has a variable in it.
185
 
            $pattern = '|(?<!\\\\)(?:\\\\{2})*\${?[a-zA-Z0-9_]+}?|';
186
 
            if (preg_match($pattern, $tokens[$stackPtr]['content']) !== 0) {
187
 
                $this->processVariableInString($phpcsFile, $stackPtr);
188
 
            }
189
 
        }
190
 
 
191
 
    }//end processTokenOutsideScope()
192
 
 
193
 
 
194
 
    /**
195
 
     * Called to process class member vars.
196
 
     *
197
 
     * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
198
 
     *                                        token was found.
199
 
     * @param int                  $stackPtr  The position where the token was found.
200
 
     *
201
 
     * @return void
202
 
     */
203
 
    abstract protected function processMemberVar(
204
 
        PHP_CodeSniffer_File $phpcsFile,
205
 
        $stackPtr
206
 
    );
207
 
 
208
 
 
209
 
    /**
210
 
     * Called to process normal member vars.
211
 
     *
212
 
     * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
213
 
     *                                        token was found.
214
 
     * @param int                  $stackPtr  The position where the token was found.
215
 
     *
216
 
     * @return void
217
 
     */
218
 
    abstract protected function processVariable(
219
 
        PHP_CodeSniffer_File $phpcsFile,
220
 
        $stackPtr
221
 
    );
222
 
 
223
 
 
224
 
    /**
225
 
     * Called to process variables found in double quoted strings or heredocs.
226
 
     *
227
 
     * Note that there may be more than one variable in the string, which will
228
 
     * result only in one call for the string or one call per line for heredocs.
229
 
     *
230
 
     * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
231
 
     *                                        token was found.
232
 
     * @param int                  $stackPtr  The position where the double quoted
233
 
     *                                        string was found.
234
 
     *
235
 
     * @return void
236
 
     */
237
 
    abstract protected function processVariableInString(
238
 
        PHP_CodeSniffer_File
239
 
        $phpcsFile,
240
 
        $stackPtr
241
 
    );
242
 
 
243
 
 
244
 
}//end class
245
 
 
246
 
?>