5 |
aurelien |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* PEAR_Sniffs_NamingConventions_ValidVariableNameSniff.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PHP_CodeSniffer
|
|
|
9 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
10 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
11 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
12 |
* @version CVS: $Id: ValidVariableNameSniff.php,v 1.2 2008/06/13 04:10:43 squiz Exp $
|
|
|
13 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) {
|
|
|
17 |
$error = 'Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found';
|
|
|
18 |
throw new PHP_CodeSniffer_Exception($error);
|
|
|
19 |
}
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* PEAR_Sniffs_NamingConventions_ValidVariableNameSniff.
|
|
|
23 |
*
|
|
|
24 |
* Checks the naming of member variables.
|
|
|
25 |
*
|
|
|
26 |
* @category PHP
|
|
|
27 |
* @package PHP_CodeSniffer
|
|
|
28 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
29 |
* @copyright 2006 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.2.0RC1
|
|
|
32 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
33 |
*/
|
|
|
34 |
class PEAR_Sniffs_NamingConventions_ValidVariableNameSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
|
|
|
35 |
{
|
|
|
36 |
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Processes class member variables.
|
|
|
40 |
*
|
|
|
41 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
42 |
* @param int $stackPtr The position of the current token
|
|
|
43 |
* in the stack passed in $tokens.
|
|
|
44 |
*
|
|
|
45 |
* @return void
|
|
|
46 |
*/
|
|
|
47 |
protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
48 |
{
|
|
|
49 |
$tokens = $phpcsFile->getTokens();
|
|
|
50 |
|
|
|
51 |
$memberProps = $phpcsFile->getMemberProperties($stackPtr);
|
|
|
52 |
if (empty($memberProps) === true) {
|
|
|
53 |
return;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
$memberName = ltrim($tokens[$stackPtr]['content'], '$');
|
|
|
57 |
$isPublic = ($memberProps['scope'] === 'private') ? false : true;
|
|
|
58 |
$scope = $memberProps['scope'];
|
|
|
59 |
$scopeSpecified = $memberProps['scope_specified'];
|
|
|
60 |
|
|
|
61 |
// If it's a private member, it must have an underscore on the front.
|
|
|
62 |
if ($isPublic === false && $memberName{0} !== '_') {
|
|
|
63 |
$error = "Private member variable \"$memberName\" must be prefixed with an underscore";
|
|
|
64 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
65 |
return;
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
// If it's not a private member, it must not have an underscore on the front.
|
|
|
69 |
if ($isPublic === true && $scopeSpecified === true && $memberName{0} === '_') {
|
|
|
70 |
$error = ucfirst($scope)." member variable \"$memberName\" must not be prefixed with an underscore";
|
|
|
71 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
72 |
return;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
}//end processMemberVar()
|
|
|
76 |
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Processes normal variables.
|
|
|
80 |
*
|
|
|
81 |
* @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
|
|
|
82 |
* @param int $stackPtr The position where the token was found.
|
|
|
83 |
*
|
|
|
84 |
* @return void
|
|
|
85 |
*/
|
|
|
86 |
protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
87 |
{
|
|
|
88 |
// We don't care about normal variables.
|
|
|
89 |
return;
|
|
|
90 |
|
|
|
91 |
}//end processVariable()
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Processes variables in double quoted strings.
|
|
|
96 |
*
|
|
|
97 |
* @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
|
|
|
98 |
* @param int $stackPtr The position where the token was found.
|
|
|
99 |
*
|
|
|
100 |
* @return void
|
|
|
101 |
*/
|
|
|
102 |
protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
103 |
{
|
|
|
104 |
// We don't care about normal variables.
|
|
|
105 |
return;
|
|
|
106 |
|
|
|
107 |
}//end processVariableInString()
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
}//end class
|
|
|
111 |
|
|
|
112 |
?>
|