Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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