3
* Generic_Sniffs_NamingConventions_ConstructorNameSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @author Leif Wickland <lwickland@rightnow.com>
11
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
12
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
13
* @link http://pear.php.net/package/PHP_CodeSniffer
16
if (class_exists('PHP_CodeSniffer_Standards_AbstractScopeSniff', true) === false) {
17
$error = 'Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found';
18
throw new PHP_CodeSniffer_Exception($error);
22
* Generic_Sniffs_NamingConventions_ConstructorNameSniff.
24
* Favor PHP 5 constructor syntax, which uses "function __construct()".
25
* Avoid PHP 4 constructor syntax, which uses "function ClassName()".
28
* @package PHP_CodeSniffer
29
* @author Leif Wickland <lwickland@rightnow.com>
30
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
31
* @version Release: 1.5.0RC2
32
* @link http://pear.php.net/package/PHP_CodeSniffer
34
class Generic_Sniffs_NamingConventions_ConstructorNameSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff
39
* Constructs the test with the tokens it wishes to listen for.
43
public function __construct()
45
parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true);
51
* Processes this test when one of its tokens is encountered.
53
* @param PHP_CodeSniffer_File $phpcsFile The current file being scanned.
54
* @param int $stackPtr The position of the current token
55
* in the stack passed in $tokens.
56
* @param int $currScope A pointer to the start of the scope.
60
protected function processTokenWithinScope(
61
PHP_CodeSniffer_File $phpcsFile,
65
$className = $phpcsFile->getDeclarationName($currScope);
66
$methodName = $phpcsFile->getDeclarationName($stackPtr);
68
if (strcasecmp($methodName, $className) === 0) {
69
$error = 'PHP4 style constructors are not allowed; use "__construct()" instead';
70
$phpcsFile->addError($error, $stackPtr, 'OldStyle');
71
} else if (strcasecmp($methodName, '__construct') !== 0) {
76
$tokens = $phpcsFile->getTokens();
78
$parentClassName = $phpcsFile->findExtendedClassName($currScope);
79
if ($parentClassName === false) {
83
$endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
84
$startIndex = $stackPtr;
85
while ($doubleColonIndex = $phpcsFile->findNext(array(T_DOUBLE_COLON), $startIndex, $endFunctionIndex)) {
86
if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING
87
&& $tokens[($doubleColonIndex + 1)]['content'] === $parentClassName
89
$error = 'PHP4 style calls to parent constructors are not allowed; use "parent::__construct()" instead';
90
$phpcsFile->addError($error, ($doubleColonIndex + 1), 'OldStyleCall');
93
$startIndex = ($doubleColonIndex + 1);
96
}//end processTokenWithinScope()