5 |
aurelien |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Generic_Sniffs_NamingConventions_ConstructorNameSniff.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PHP_CodeSniffer
|
|
|
9 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
10 |
* @author Leif Wickland <lwickland@rightnow.com>
|
|
|
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: ConstructorNameSniff.php,v 1.2 2009/02/24 04:43:44 squiz Exp $
|
|
|
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 |
* Generic_Sniffs_NamingConventions_ConstructorNameSniff.
|
|
|
24 |
*
|
|
|
25 |
* Favor PHP 5 constructor syntax, which uses "function __construct()".
|
|
|
26 |
* Avoid PHP 4 constructor syntax, which uses "function ClassName()".
|
|
|
27 |
*
|
|
|
28 |
* @category PHP
|
|
|
29 |
* @package PHP_CodeSniffer
|
|
|
30 |
* @author Leif Wickland <lwickland@rightnow.com>
|
|
|
31 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
32 |
* @version Release: 1.2.0RC1
|
|
|
33 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
34 |
*/
|
|
|
35 |
class Generic_Sniffs_NamingConventions_ConstructorNameSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff
|
|
|
36 |
{
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Constructs the test with the tokens it wishes to listen for.
|
|
|
41 |
*
|
|
|
42 |
* @return void
|
|
|
43 |
*/
|
|
|
44 |
public function __construct()
|
|
|
45 |
{
|
|
|
46 |
parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true);
|
|
|
47 |
|
|
|
48 |
}//end __construct()
|
|
|
49 |
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Processes this test when one of its tokens is encountered.
|
|
|
53 |
*
|
|
|
54 |
* @param PHP_CodeSniffer_File $phpcsFile The current file being scanned.
|
|
|
55 |
* @param int $stackPtr The position of the current token
|
|
|
56 |
* in the stack passed in $tokens.
|
|
|
57 |
* @param int $currScope A pointer to the start of the scope.
|
|
|
58 |
*
|
|
|
59 |
* @return void
|
|
|
60 |
*/
|
|
|
61 |
protected function processTokenWithinScope(
|
|
|
62 |
PHP_CodeSniffer_File $phpcsFile,
|
|
|
63 |
$stackPtr,
|
|
|
64 |
$currScope
|
|
|
65 |
) {
|
|
|
66 |
$className = $phpcsFile->getDeclarationName($currScope);
|
|
|
67 |
$methodName = $phpcsFile->getDeclarationName($stackPtr);
|
|
|
68 |
|
|
|
69 |
if (strcasecmp($methodName, $className) === 0) {
|
|
|
70 |
$error = 'PHP4 style constructors are not allowed; use "__construct()" instead';
|
|
|
71 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
72 |
} else if (strcasecmp($methodName, '__construct') !== 0) {
|
|
|
73 |
// Not a constructor.
|
|
|
74 |
return;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
$tokens = $phpcsFile->getTokens();
|
|
|
78 |
|
|
|
79 |
$parentClassName = $phpcsFile->findExtendedClassName($currScope);
|
|
|
80 |
if ($parentClassName === false) {
|
|
|
81 |
return;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
$endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
|
|
|
85 |
$startIndex = $stackPtr;
|
|
|
86 |
while ($doubleColonIndex = $phpcsFile->findNext(array(T_DOUBLE_COLON), $startIndex, $endFunctionIndex)) {
|
|
|
87 |
if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING
|
|
|
88 |
&& $tokens[($doubleColonIndex + 1)]['content'] === $parentClassName
|
|
|
89 |
) {
|
|
|
90 |
$error = 'PHP4 style calls to parent constructors are not allowed; use "parent::__construct()" instead';
|
|
|
91 |
$phpcsFile->addError($error, ($doubleColonIndex + 1));
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
$startIndex = ($doubleColonIndex + 1);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
}//end processTokenWithinScope()
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
}//end class
|
|
|
101 |
|
|
|
102 |
?>
|