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

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.3.4/CodeSniffer/Standards/Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2012-05-31 16:37:24 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20120531163724-u6aiaubu8ks5dh5z
Tags: 1.3.4-0.1
* Non-maintainer upload.
* New upstream release (Closes: #599617, #634825).
* Swtiched debian/copyright to format 1.0 (rewrite was needed anyway, as the
upstream license changed).
* Switched package to pkg-php-tools and debhelper 8 sequencer.
* Now running unit tests at build time (so depends on phpunit (>= 3.6)).
* Section is now PHP.
* Added missing Build-Depends-Indep: php-pear.
* Added missing ${misc:Depends}.
* Added Vcs fields.
* Added homepage field.
* Reviewed short and long description.
* Added dependency on php-timer.
* Standards-Version: is now 3.9.3 (lots of changes, see above...).

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-2011 Squiz Pty Ltd (ABN 77 084 670 600)
 
12
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence 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-2011 Squiz Pty Ltd (ABN 77 084 670 600)
 
30
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 
31
 * @version   Release: 1.3.4
 
32
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
33
 */
 
34
class Zend_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
                // Either a var name or a function call, so check for bracket.
 
85
                $bracket = $phpcsFile->findNext(array(T_WHITESPACE), ($var + 1), null, true);
 
86
 
 
87
                if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS) {
 
88
                    $objVarName = $tokens[$var]['content'];
 
89
 
 
90
                    // There is no way for us to know if the var is public or private,
 
91
                    // so we have to ignore a leading underscore if there is one and just
 
92
                    // check the main part of the variable name.
 
93
                    $originalVarName = $objVarName;
 
94
                    if (substr($objVarName, 0, 1) === '_') {
 
95
                        $objVarName = substr($objVarName, 1);
 
96
                    }
 
97
 
 
98
                    if (PHP_CodeSniffer::isCamelCaps($objVarName, false, true, false) === false) {
 
99
                        $error = 'Variable "%s" is not in valid camel caps format';
 
100
                        $data  = array($originalVarName);
 
101
                        $phpcsFile->addError($error, $var, 'NotCamelCaps', $data);
 
102
                    } else if (preg_match('|\d|', $objVarName)) {
 
103
                        $warning = 'Variable "%s" contains numbers but this is discouraged';
 
104
                        $data    = array($originalVarName);
 
105
                        $phpcsFile->addWarning($warning, $stackPtr, 'ContainsNumbers', $data);
 
106
                    }
 
107
                }//end if
 
108
            }//end if
 
109
        }//end if
 
110
 
 
111
        // There is no way for us to know if the var is public or private,
 
112
        // so we have to ignore a leading underscore if there is one and just
 
113
        // check the main part of the variable name.
 
114
        $originalVarName = $varName;
 
115
        if (substr($varName, 0, 1) === '_') {
 
116
            $objOperator = $phpcsFile->findPrevious(array(T_WHITESPACE), ($stackPtr - 1), null, true);
 
117
            if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON) {
 
118
                // The variable lives within a class, and is referenced like
 
119
                // this: MyClass::$_variable, so we don't know its scope.
 
120
                $inClass = true;
 
121
            } else {
 
122
                $inClass = $phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE));
 
123
            }
 
124
 
 
125
            if ($inClass === true) {
 
126
                $varName = substr($varName, 1);
 
127
            }
 
128
        }
 
129
 
 
130
        if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
 
131
            $error = 'Variable "%s" is not in valid camel caps format';
 
132
            $data  = array($originalVarName);
 
133
            $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
 
134
        } else if (preg_match('|\d|', $varName)) {
 
135
            $warning = 'Variable "%s" contains numbers but this is discouraged';
 
136
            $data    = array($originalVarName);
 
137
            $phpcsFile->addWarning($warning, $stackPtr, 'ContainsNumbers', $data);
 
138
        }
 
139
 
 
140
    }//end processVariable()
 
141
 
 
142
 
 
143
    /**
 
144
     * Processes class member variables.
 
145
     *
 
146
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
 
147
     * @param int                  $stackPtr  The position of the current token in the
 
148
     *                                        stack passed in $tokens.
 
149
     *
 
150
     * @return void
 
151
     */
 
152
    protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 
153
    {
 
154
        $tokens      = $phpcsFile->getTokens();
 
155
        $varName     = ltrim($tokens[$stackPtr]['content'], '$');
 
156
        $memberProps = $phpcsFile->getMemberProperties($stackPtr);
 
157
        $public      = ($memberProps['scope'] === 'public');
 
158
 
 
159
        if ($public === true) {
 
160
            if (substr($varName, 0, 1) === '_') {
 
161
                $error = 'Public member variable "%s" must not contain a leading underscore';
 
162
                $data  = array($varName);
 
163
                $phpcsFile->addError($error, $stackPtr, 'PublicHasUnderscore', $data);
 
164
                return;
 
165
            }
 
166
        } else {
 
167
            if (substr($varName, 0, 1) !== '_') {
 
168
                $scope = ucfirst($memberProps['scope']);
 
169
                $error = '%s member variable "%s" must contain a leading underscore';
 
170
                $data  = array(
 
171
                          $scope,
 
172
                          $varName,
 
173
                         );
 
174
                $phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $data);
 
175
                return;
 
176
            }
 
177
        }
 
178
 
 
179
        if (PHP_CodeSniffer::isCamelCaps($varName, false, $public, false) === false) {
 
180
            $error = 'Variable "%s" is not in valid camel caps format';
 
181
            $data  = array($varName);
 
182
            $phpcsFile->addError($error, $stackPtr, 'MemberVarNotCamelCaps', $data);
 
183
        } else if (preg_match('|\d|', $varName)) {
 
184
            $warning = 'Variable "%s" contains numbers but this is discouraged';
 
185
            $data    = array($varName);
 
186
            $phpcsFile->addWarning($warning, $stackPtr, 'MemberVarContainsNumbers', $data);
 
187
        }
 
188
 
 
189
    }//end processMemberVar()
 
190
 
 
191
 
 
192
    /**
 
193
     * Processes the variable found within a double quoted string.
 
194
     *
 
195
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
 
196
     * @param int                  $stackPtr  The position of the double quoted
 
197
     *                                        string.
 
198
     *
 
199
     * @return void
 
200
     */
 
201
    protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 
202
    {
 
203
        $tokens = $phpcsFile->getTokens();
 
204
 
 
205
        $phpReservedVars = array(
 
206
                            '_SERVER',
 
207
                            '_GET',
 
208
                            '_POST',
 
209
                            '_REQUEST',
 
210
                            '_SESSION',
 
211
                            '_ENV',
 
212
                            '_COOKIE',
 
213
                            '_FILES',
 
214
                            'GLOBALS',
 
215
                           );
 
216
 
 
217
        if (preg_match_all('|[^\\\]\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|', $tokens[$stackPtr]['content'], $matches) !== 0) {
 
218
            foreach ($matches[1] as $varName) {
 
219
                // If it's a php reserved var, then its ok.
 
220
                if (in_array($varName, $phpReservedVars) === true) {
 
221
                    continue;
 
222
                }
 
223
 
 
224
                // There is no way for us to know if the var is public or private,
 
225
                // so we have to ignore a leading underscore if there is one and just
 
226
                // check the main part of the variable name.
 
227
                $originalVarName = $varName;
 
228
                if (substr($varName, 0, 1) === '_') {
 
229
                    if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE)) === true) {
 
230
                        $varName = substr($varName, 1);
 
231
                    }
 
232
                }
 
233
 
 
234
                if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
 
235
                    $varName = $matches[0];
 
236
                    $error   = 'Variable "%s" is not in valid camel caps format';
 
237
                    $data    = array($originalVarName);
 
238
                    $phpcsFile->addError($error, $stackPtr, 'StringVarNotCamelCaps', $data);
 
239
                } else if (preg_match('|\d|', $varName)) {
 
240
                    $warning = 'Variable "%s" contains numbers but this is discouraged';
 
241
                    $data    = array($originalVarName);
 
242
                    $phpcsFile->addWarning($warning, $stackPtr, 'StringVarContainsNumbers', $data);
 
243
                }
 
244
            }
 
245
        }//end if
 
246
 
 
247
    }//end processVariableInString()
 
248
 
 
249
 
 
250
}//end class
 
251
 
 
252
?>