~ubuntu-branches/ubuntu/utopic/php-codesniffer/utopic-proposed

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.0RC2/CodeSniffer/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2014-07-21 14:42:41 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20140721144241-g4orlcuk4jzn9mhs
Tags: 1.5.3-1
* Team upload
* Focus on stable release
* Update copyright
* Bump standards version to 3.9.5
* Update Homepage
* Use ${phppear:…} instead of hardcoding them
* Run tests in dh_auto_test
* Update patch with gbp pq
* Simplify configuration installation
* Edit package.xml to move script
* Add DEP-8 tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
 * Squiz_Sniffs_NamingConventions_ValidVariableNameSniff.
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-2012 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_AbstractVariableSniff', true) === false) {
17
 
    throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found');
18
 
}
19
 
 
20
 
/**
21
 
 * Squiz_Sniffs_NamingConventions_ValidVariableNameSniff.
22
 
 *
23
 
 * Checks the naming of variables and member variables.
24
 
 *
25
 
 * @category  PHP
26
 
 * @package   PHP_CodeSniffer
27
 
 * @author    Greg Sherwood <gsherwood@squiz.net>
28
 
 * @author    Marc McIntyre <mmcintyre@squiz.net>
29
 
 * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
30
 
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
31
 
 * @version   Release: 1.5.0RC2
32
 
 * @link      http://pear.php.net/package/PHP_CodeSniffer
33
 
 */
34
 
class Squiz_Sniffs_NamingConventions_ValidVariableNameSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
35
 
{
36
 
 
37
 
    /**
38
 
     * Tokens to ignore so that we can find a DOUBLE_COLON.
39
 
     *
40
 
     * @var array
41
 
     */
42
 
    private $_ignore = array(
43
 
                        T_WHITESPACE,
44
 
                        T_COMMENT,
45
 
                       );
46
 
 
47
 
 
48
 
    /**
49
 
     * Processes this test, when one of its tokens is encountered.
50
 
     *
51
 
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
52
 
     * @param int                  $stackPtr  The position of the current token in the
53
 
     *                                        stack passed in $tokens.
54
 
     *
55
 
     * @return void
56
 
     */
57
 
    protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
58
 
    {
59
 
        $tokens  = $phpcsFile->getTokens();
60
 
        $varName = ltrim($tokens[$stackPtr]['content'], '$');
61
 
 
62
 
        $phpReservedVars = array(
63
 
                            '_SERVER',
64
 
                            '_GET',
65
 
                            '_POST',
66
 
                            '_REQUEST',
67
 
                            '_SESSION',
68
 
                            '_ENV',
69
 
                            '_COOKIE',
70
 
                            '_FILES',
71
 
                            'GLOBALS',
72
 
                           );
73
 
 
74
 
        // If it's a php reserved var, then its ok.
75
 
        if (in_array($varName, $phpReservedVars) === true) {
76
 
            return;
77
 
        }
78
 
 
79
 
        $objOperator = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true);
80
 
        if ($tokens[$objOperator]['code'] === T_OBJECT_OPERATOR) {
81
 
            // Check to see if we are using a variable from an object.
82
 
            $var = $phpcsFile->findNext(array(T_WHITESPACE), ($objOperator + 1), null, true);
83
 
            if ($tokens[$var]['code'] === T_STRING) {
84
 
                $bracket = $objOperator = $phpcsFile->findNext(array(T_WHITESPACE), ($var + 1), null, true);
85
 
                if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS) {
86
 
                    $objVarName = $tokens[$var]['content'];
87
 
 
88
 
                    // There is no way for us to know if the var is public or private,
89
 
                    // so we have to ignore a leading underscore if there is one and just
90
 
                    // check the main part of the variable name.
91
 
                    $originalVarName = $objVarName;
92
 
                    if (substr($objVarName, 0, 1) === '_') {
93
 
                        $objVarName = substr($objVarName, 1);
94
 
                    }
95
 
 
96
 
                    if (PHP_CodeSniffer::isCamelCaps($objVarName, false, true, false) === false) {
97
 
                        $error = 'Variable "%s" is not in valid camel caps format';
98
 
                        $data  = array($originalVarName);
99
 
                        $phpcsFile->addError($error, $var, 'NotCamelCaps', $data);
100
 
                    }
101
 
                }//end if
102
 
            }//end if
103
 
        }//end if
104
 
 
105
 
        // There is no way for us to know if the var is public or private,
106
 
        // so we have to ignore a leading underscore if there is one and just
107
 
        // check the main part of the variable name.
108
 
        $originalVarName = $varName;
109
 
        if (substr($varName, 0, 1) === '_') {
110
 
            $objOperator = $phpcsFile->findPrevious(array(T_WHITESPACE), ($stackPtr - 1), null, true);
111
 
            if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON) {
112
 
                // The variable lives within a class, and is referenced like
113
 
                // this: MyClass::$_variable, so we don't know its scope.
114
 
                $inClass = true;
115
 
            } else {
116
 
                $inClass = $phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE));
117
 
            }
118
 
 
119
 
            if ($inClass === true) {
120
 
                $varName = substr($varName, 1);
121
 
            }
122
 
        }
123
 
 
124
 
        if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
125
 
            $error = 'Variable "%s" is not in valid camel caps format';
126
 
            $data  = array($originalVarName);
127
 
            $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
128
 
        }
129
 
 
130
 
    }//end processVariable()
131
 
 
132
 
 
133
 
    /**
134
 
     * Processes class member variables.
135
 
     *
136
 
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
137
 
     * @param int                  $stackPtr  The position of the current token in the
138
 
     *                                        stack passed in $tokens.
139
 
     *
140
 
     * @return void
141
 
     */
142
 
    protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
143
 
    {
144
 
        $tokens = $phpcsFile->getTokens();
145
 
 
146
 
        $varName     = ltrim($tokens[$stackPtr]['content'], '$');
147
 
        $memberProps = $phpcsFile->getMemberProperties($stackPtr);
148
 
        if (empty($memberProps) === true) {
149
 
            // Couldn't get any info about this variable, which
150
 
            // generally means it is invalid or possibly has a parse
151
 
            // error. Any errors will be reported by the core, so
152
 
            // we can ignore it.
153
 
            return;
154
 
        }
155
 
 
156
 
        $public    = ($memberProps['scope'] !== 'private');
157
 
        $errorData = array($varName);
158
 
 
159
 
        if ($public === true) {
160
 
            if (substr($varName, 0, 1) === '_') {
161
 
                $error = '%s member variable "%s" must not contain a leading underscore';
162
 
                $data  = array(
163
 
                          ucfirst($memberProps['scope']),
164
 
                          $errorData[0],
165
 
                         );
166
 
                $phpcsFile->addError($error, $stackPtr, 'PublicHasUnderscore', $data);
167
 
                return;
168
 
            }
169
 
        } else {
170
 
            if (substr($varName, 0, 1) !== '_') {
171
 
                $error = 'Private member variable "%s" must contain a leading underscore';
172
 
                $phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $errorData);
173
 
                return;
174
 
            }
175
 
        }
176
 
 
177
 
        if (PHP_CodeSniffer::isCamelCaps($varName, false, $public, false) === false) {
178
 
            $error = 'Variable "%s" is not in valid camel caps format';
179
 
            $phpcsFile->addError($error, $stackPtr, 'MemberNotCamelCaps', $errorData);
180
 
        }
181
 
 
182
 
    }//end processMemberVar()
183
 
 
184
 
 
185
 
    /**
186
 
     * Processes the variable found within a double quoted string.
187
 
     *
188
 
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
189
 
     * @param int                  $stackPtr  The position of the double quoted
190
 
     *                                        string.
191
 
     *
192
 
     * @return void
193
 
     */
194
 
    protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
195
 
    {
196
 
        $tokens = $phpcsFile->getTokens();
197
 
 
198
 
        $phpReservedVars = array(
199
 
                            '_SERVER',
200
 
                            '_GET',
201
 
                            '_POST',
202
 
                            '_REQUEST',
203
 
                            '_SESSION',
204
 
                            '_ENV',
205
 
                            '_COOKIE',
206
 
                            '_FILES',
207
 
                            'GLOBALS',
208
 
                           );
209
 
        if (preg_match_all('|[^\\\]\${?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|', $tokens[$stackPtr]['content'], $matches) !== 0) {
210
 
            foreach ($matches[1] as $varName) {
211
 
                // If it's a php reserved var, then its ok.
212
 
                if (in_array($varName, $phpReservedVars) === true) {
213
 
                    continue;
214
 
                }
215
 
 
216
 
                // There is no way for us to know if the var is public or private,
217
 
                // so we have to ignore a leading underscore if there is one and just
218
 
                // check the main part of the variable name.
219
 
                $originalVarName = $varName;
220
 
                if (substr($varName, 0, 1) === '_') {
221
 
                    if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE)) === true) {
222
 
                        $varName = substr($varName, 1);
223
 
                    }
224
 
                }
225
 
 
226
 
                if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
227
 
                    $varName = $matches[0];
228
 
                    $error = 'Variable "%s" is not in valid camel caps format';
229
 
                    $data  = array($originalVarName);
230
 
                    $phpcsFile->addError($error, $stackPtr, 'StringNotCamelCaps', $data);
231
 
                    
232
 
                }
233
 
            }
234
 
        }//end if
235
 
 
236
 
    }//end processVariableInString()
237
 
 
238
 
 
239
 
}//end class
240
 
 
241
 
?>