|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CodeIgniter_Sniffs_NamingConventions_ConstructorNameSniff. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category PHP |
|
8
|
|
|
* @package PHP_CodeSniffer |
|
9
|
|
|
* @author Thomas Ernest <[email protected]> |
|
10
|
|
|
* @copyright 2011 Thomas Ernest |
|
11
|
|
|
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License |
|
12
|
|
|
* @link http://pear.php.net/package/PHP_CodeSniffer |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace CodeIgniter\Sniffs\NamingConventions; |
|
16
|
|
|
|
|
17
|
|
|
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff; |
|
18
|
|
|
use PHP_CodeSniffer\Files\File; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* CodeIgniter_Sniffs_NamingConventions_ConstructorNameSniff. |
|
22
|
|
|
* |
|
23
|
|
|
* Favor PHP 4 constructor syntax, which uses "function ClassName()". |
|
24
|
|
|
* Avoid PHP 5 constructor syntax, which uses "function __construct()". |
|
25
|
|
|
* |
|
26
|
|
|
* @todo Try to avoid overly long and verbose names. |
|
27
|
|
|
* |
|
28
|
|
|
* @category PHP |
|
29
|
|
|
* @package PHP_CodeSniffer |
|
30
|
|
|
* @author Thomas Ernest <[email protected]> |
|
31
|
|
|
* @copyright 2010 Thomas Ernest |
|
32
|
|
|
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License |
|
33
|
|
|
* @link http://pear.php.net/package/PHP_CodeSniffer |
|
34
|
|
|
*/ |
|
35
|
|
|
class ConstructorNameSniff extends AbstractScopeSniff |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
public $php5Constructors = '1'; |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Constructs the test with the tokens it wishes to listen for. |
|
44
|
|
|
* |
|
45
|
|
|
* @return void |
|
|
|
|
|
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct() |
|
48
|
|
|
{ |
|
49
|
|
|
parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true); |
|
50
|
|
|
|
|
51
|
|
|
}//end __construct() |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Processes this test when one of its tokens is encountered. |
|
56
|
|
|
* |
|
57
|
|
|
* @param File $phpcsFile The current file being scanned. |
|
58
|
|
|
* @param int $stackPtr The position of the current token |
|
59
|
|
|
* in the stack passed in $tokens. |
|
60
|
|
|
* @param int $currScope A pointer to the start of the scope. |
|
61
|
|
|
* |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function processTokenWithinScope( |
|
65
|
|
|
File $phpcsFile, |
|
66
|
|
|
$stackPtr, |
|
67
|
|
|
$currScope |
|
68
|
|
|
) { |
|
69
|
|
|
$methodName = $phpcsFile->getDeclarationName($stackPtr); |
|
70
|
|
|
$className = $phpcsFile->getDeclarationName($currScope); |
|
71
|
|
|
|
|
72
|
|
|
$isPhp4Constructor = strcasecmp($methodName, $className) === 0; |
|
73
|
|
|
$isPhp5Constructor = strcasecmp($methodName, '__construct') === 0; |
|
74
|
|
|
if ($this->php5Constructors != '0') { |
|
75
|
|
|
if ($isPhp4Constructor) { |
|
76
|
|
|
$error = "PHP4 style constructors are not allowed; use \"__construct\" instead"; |
|
77
|
|
|
$phpcsFile->addError($error, $stackPtr); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
} else { |
|
80
|
|
|
if ($isPhp5Constructor) { |
|
81
|
|
|
$error = "PHP5 style constructors are not allowed; use \"$className\" instead"; |
|
82
|
|
|
$phpcsFile->addError($error, $stackPtr); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
if ( ! $isPhp4Constructor && ! $isPhp5Constructor ) { |
|
86
|
|
|
return; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
90
|
|
|
|
|
91
|
|
|
$parentClassName = $phpcsFile->findExtendedClassName($currScope); |
|
92
|
|
|
$wrongConstructor = ''; |
|
93
|
|
|
// prepares the error message and wrong constructor |
|
94
|
|
|
if ($this->php5Constructors != '0') { |
|
95
|
|
|
$error = 'PHP4 style calls to parent constructors are not allowed.'; |
|
96
|
|
|
$error = "$error Please use \"parent::__construct\" instead."; |
|
97
|
|
|
if (false !== $parentClassName) { |
|
98
|
|
|
$wrongConstructor = $parentClassName; |
|
99
|
|
|
} |
|
100
|
|
|
// Else $wrongConstructor will be empty |
|
101
|
|
|
// and the test expression will always be false. |
|
102
|
|
|
// It doesn't check that no parent method should be called |
|
103
|
|
|
// when no parent class is defined. |
|
104
|
|
|
} else { |
|
105
|
|
|
$error = 'PHP5 style calls to parent constructors are not allowed.'; |
|
106
|
|
|
if (false !== $parentClassName) { |
|
107
|
|
|
$error = "$error Please use \"parent::$parentClassName\" instead."; |
|
108
|
|
|
} |
|
109
|
|
|
$wrongConstructor = '__construct'; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
// looks for the use of a wrong constructor. |
|
113
|
|
|
$endFunctionIndex = $tokens[$stackPtr]['scope_closer']; |
|
114
|
|
|
$doubleColonIndex = $phpcsFile->findNext( |
|
115
|
|
|
array(T_DOUBLE_COLON), |
|
116
|
|
|
$stackPtr, |
|
117
|
|
|
$endFunctionIndex |
|
118
|
|
|
); |
|
119
|
|
|
while ($doubleColonIndex) { |
|
|
|
|
|
|
120
|
|
|
if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING |
|
121
|
|
|
&& $tokens[($doubleColonIndex + 1)]['content'] === $wrongConstructor |
|
122
|
|
|
) { |
|
123
|
|
|
$phpcsFile->addError($error, ($doubleColonIndex + 1)); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$doubleColonIndex = $phpcsFile->findNext( |
|
127
|
|
|
array(T_DOUBLE_COLON), |
|
128
|
|
|
$doubleColonIndex + 1, |
|
129
|
|
|
$endFunctionIndex |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
}//end processTokenWithinScope() |
|
134
|
|
|
|
|
135
|
|
|
protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) |
|
136
|
|
|
{ |
|
137
|
|
|
// TODO: Implement processTokenOutsideScope() method. |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
}//end class |
|
141
|
|
|
|
|
142
|
|
|
?> |
|
|
|
|
|
|
143
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.