| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Squiz_Sniffs_Classes_ClassFileNameSniff.
|
|
|
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: SelfMemberReferenceSniff.php 247629 2007-12-05 03:31:16Z squiz $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
if (class_exists('PHP_CodeSniffer_Standards_AbstractScopeSniff', true) === false) {
|
|
|
18 |
$error = 'Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found';
|
|
|
19 |
throw new PHP_CodeSniffer_Exception($error);
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Tests self member references.
|
|
|
24 |
*
|
|
|
25 |
* Verifies that :
|
|
|
26 |
* <ul>
|
|
|
27 |
* <li>self:: is used instead of Self::</li>
|
|
|
28 |
* <li>self:: is used for local static member reference</li>
|
|
|
29 |
* <li>self:: is used instead of self ::</li>
|
|
|
30 |
* </ul>
|
|
|
31 |
*
|
|
|
32 |
* @category PHP
|
|
|
33 |
* @package PHP_CodeSniffer
|
|
|
34 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
35 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
36 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
37 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
38 |
* @version Release: 1.2.1
|
|
|
39 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
40 |
*/
|
|
|
41 |
class Squiz_Sniffs_Classes_SelfMemberReferenceSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff
|
|
|
42 |
{
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Constructs a Squiz_Sniffs_Classes_SelfMemberReferenceSniff.
|
|
|
47 |
*/
|
|
|
48 |
public function __construct()
|
|
|
49 |
{
|
|
|
50 |
parent::__construct(array(T_CLASS), array(T_DOUBLE_COLON));
|
|
|
51 |
|
|
|
52 |
}//end __construct()
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Processes the function tokens within the class.
|
|
|
57 |
*
|
|
|
58 |
* @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
|
|
|
59 |
* @param int $stackPtr The position where the token was found.
|
|
|
60 |
* @param int $currScope The current scope opener token.
|
|
|
61 |
*
|
|
|
62 |
* @return void
|
|
|
63 |
*/
|
|
|
64 |
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
|
|
|
65 |
{
|
|
|
66 |
$tokens = $phpcsFile->getTokens();
|
|
|
67 |
|
|
|
68 |
$className = ($stackPtr - 1);
|
|
|
69 |
if ($tokens[$className]['code'] === T_SELF) {
|
|
|
70 |
if (strtolower($tokens[$className]['content']) !== $tokens[$className]['content']) {
|
|
|
71 |
$error = 'Must use "self::" for local static member reference; found "'.$tokens[$className]['content'].'::"';
|
|
|
72 |
$phpcsFile->addError($error, $className);
|
|
|
73 |
return;
|
|
|
74 |
}
|
|
|
75 |
} else if ($tokens[$className]['code'] === T_STRING) {
|
|
|
76 |
// Make sure this is another class reference.
|
|
|
77 |
$declarationName = $phpcsFile->getDeclarationName($currScope);
|
|
|
78 |
if ($declarationName === $tokens[$className]['content']) {
|
|
|
79 |
// Class name is the same as the current class.
|
|
|
80 |
$error = 'Must use "self::" for local static member reference';
|
|
|
81 |
$phpcsFile->addError($error, $className);
|
|
|
82 |
return;
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) {
|
|
|
87 |
$found = strlen($tokens[($stackPtr - 1)]['content']);
|
|
|
88 |
$error = "Expected 0 spaces before double colon; $found found";
|
|
|
89 |
$phpcsFile->addError($error, $className);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) {
|
|
|
93 |
$found = strlen($tokens[($stackPtr + 1)]['content']);
|
|
|
94 |
$error = "Expected 0 spaces after double colon; $found found";
|
|
|
95 |
$phpcsFile->addError($error, $className);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
}//end processTokenWithinScope()
|
|
|
99 |
|
|
|
100 |
|
|
|
101 |
}//end class
|
|
|
102 |
|
|
|
103 |
?>
|