| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Ensures that self is not used to call public method in action classes.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PHP_CodeSniffer_MySource
|
|
|
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: DisallowSelfActionsSniff.php 265572 2008-08-28 03:43:37Z squiz $
|
|
|
13 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Ensures that self is not used to call public method in action classes.
|
|
|
18 |
*
|
|
|
19 |
* @category PHP
|
|
|
20 |
* @package PHP_CodeSniffer_MySource
|
|
|
21 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
22 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
23 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
24 |
* @version Release: 1.2.1
|
|
|
25 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
26 |
*/
|
|
|
27 |
class MySource_Sniffs_Channels_DisallowSelfActionsSniff implements PHP_CodeSniffer_Sniff
|
|
|
28 |
{
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Returns an array of tokens this test wants to listen for.
|
|
|
33 |
*
|
|
|
34 |
* @return array
|
|
|
35 |
*/
|
|
|
36 |
public function register()
|
|
|
37 |
{
|
|
|
38 |
return array(T_CLASS);
|
|
|
39 |
|
|
|
40 |
}//end register()
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Processes this sniff, when one of its tokens is encountered.
|
|
|
45 |
*
|
|
|
46 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
47 |
* @param int $stackPtr The position of the current token in
|
|
|
48 |
* the stack passed in $tokens.
|
|
|
49 |
*
|
|
|
50 |
* @return void
|
|
|
51 |
*/
|
|
|
52 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
53 |
{
|
|
|
54 |
$tokens = $phpcsFile->getTokens();
|
|
|
55 |
|
|
|
56 |
// We are only interested in Action classes.
|
|
|
57 |
$classNameToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
|
|
|
58 |
$className = $tokens[$classNameToken]['content'];
|
|
|
59 |
if (substr($className, -7) !== 'Actions') {
|
|
|
60 |
return;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
$foundFunctions = array();
|
|
|
64 |
$foundCalls = array();
|
|
|
65 |
|
|
|
66 |
// Find all static method calls in the form self::method() in the class.
|
|
|
67 |
$classEnd = $tokens[$stackPtr]['scope_closer'];
|
|
|
68 |
for ($i = ($classNameToken + 1); $i < $classEnd; $i++) {
|
|
|
69 |
if ($tokens[$i]['code'] !== T_DOUBLE_COLON) {
|
|
|
70 |
if ($tokens[$i]['code'] === T_FUNCTION) {
|
|
|
71 |
// Cache the function information.
|
|
|
72 |
$funcName = $phpcsFile->findNext(T_STRING, ($i + 1));
|
|
|
73 |
$funcScope = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, ($i - 1));
|
|
|
74 |
$foundFunctions[$tokens[$funcName]['content']] = strtolower($tokens[$funcScope]['content']);
|
|
|
75 |
}
|
|
|
76 |
continue;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($i - 1), null, true);
|
|
|
80 |
if ($tokens[$prevToken]['content'] !== 'self') {
|
|
|
81 |
continue;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
$funcNameToken = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), null, true);
|
|
|
85 |
if ($tokens[$funcNameToken]['code'] === T_VARIABLE) {
|
|
|
86 |
// We are only interested in function calls.
|
|
|
87 |
continue;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
$funcName = $tokens[$funcNameToken]['content'];
|
|
|
91 |
|
|
|
92 |
// We've found the function, now we need to find it and see if it is
|
|
|
93 |
// public, private or protected. If it starts with an underscore we
|
|
|
94 |
// can assume it is private.
|
|
|
95 |
if ($funcName{0} === '_') {
|
|
|
96 |
continue;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
$foundCalls[$i] = $funcName;
|
|
|
100 |
}//end for
|
|
|
101 |
|
|
|
102 |
$errorClassName = substr($className, 0, -7);
|
|
|
103 |
|
|
|
104 |
foreach ($foundCalls as $token => $funcName) {
|
|
|
105 |
if (isset($foundFunctions[$funcName]) === false) {
|
|
|
106 |
// Function was not in this class, might have come from the parent.
|
|
|
107 |
// Either way, we can't really check this.
|
|
|
108 |
continue;
|
|
|
109 |
} else if ($foundFunctions[$funcName] === 'public') {
|
|
|
110 |
$error = "Static calls to public methods in Action classes must not use the self keyword; use $errorClassName::$funcName() instead";
|
|
|
111 |
$phpcsFile->addError($error, $token);
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
}//end processTokenWithinScope()
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
}//end class
|
|
|
119 |
|
|
|
120 |
?>
|