Skip to content

Commit 8133241

Browse files
committed
#82 : Cannot define idependent rules for function parameter namings and
local variable namings
1 parent c5213b6 commit 8133241

File tree

4 files changed

+52
-37
lines changed

4 files changed

+52
-37
lines changed

RELEASE_NOTES.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
===========================
33

44

5-
Version 0.14.7 (in dev)
5+
Version 0.14.7
66
--------------
77
* Issue #81 : Fail to detect private functions with spaces after function (thanks to Hans-Christian Halfbrodt)
88
* Fix square bracket for arrays on method assignation with test case. (thanks to GuillermoMI)
99
* Add flag to force error level only on console report (thanks to Jean-Philippe Quéméner)
10+
* Issue #82 : Cannot define independent rules for function parameter namings and local variable namings (thanks to dpalic)
1011

1112

1213
Version 0.14.6

phpunit.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<phpunit bootstrap="./test/TestRunner.php" colors="true">
22

33
<testsuite name="PHPUnitTestSuite">
4-
<!-- <file>./test/AliasesTest.php</file>
4+
<file>./test/AliasesTest.php</file>
55
<file>./test/CommentsTest.php</file>
66
<file>./test/DeprecationTest.php</file>
77
<file>./test/GoodTest.php</file>
88
<file>./test/IndentationTest.php</file>
9-
<file>./test/MetricsTest.php</file> -->
9+
<file>./test/MetricsTest.php</file>
1010
<file>./test/NamingTest.php</file>
11-
<!-- <file>./test/OptimizationTest.php</file>
11+
<file>./test/OptimizationTest.php</file>
1212
<file>./test/OtherTest.php</file>
1313
<file>./test/PHPTagsTest.php</file>
1414
<file>./test/ProhibitedTest.php</file>
1515
<file>./test/StrictCompareTest.php</file>
16-
<file>./test/UnusedTest.php</file> -->
16+
<file>./test/UnusedTest.php</file>
1717
</testsuite>
1818

1919
<filter>

src/PHPCheckstyle/PHPCheckstyle.php

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* @SuppressWarnings checkUnusedPrivateFunctions functionMaxParameters
2727
*/
2828
class PHPCheckstyle {
29+
2930
// The class used to export the result
3031
public $_reporter;
3132

@@ -78,13 +79,15 @@ class PHPCheckstyle {
7879
".svn", // SVN directory
7980
".git*"
8081
);
82+
8183
// Accounts for .git, .gitignore .gitmodules etc
8284

8385
//
8486
// variables used while processing control structure
8587
//
8688
// Left parenthesis opened in control statement or function statement
8789
private $_csLeftParenthesis = 0;
90+
8891
// Left parenthesis opened in function call
8992
private $_fcLeftParenthesis = 0;
9093

@@ -93,8 +96,10 @@ class PHPCheckstyle {
9396
private $token = false;
9497

9598
private $lineNumber = 0;
99+
96100
// Store the current line number
97101
private $_isLineStart = true;
102+
98103
// Start of a line (just after a return)
99104

100105
// Indicate if we are in a control statement declaration (for, if, while, ...)
@@ -108,54 +113,78 @@ class PHPCheckstyle {
108113
private $_beforeArrayDeclaration = false;
109114

110115
private $_inClassStatement = false;
116+
111117
// We are in a class statement (declaration)
112118
private $_inInterfaceStatement = false;
119+
113120
// We are in an interface statement (declaration)
114121
private $_inFunctionStatement = false;
122+
115123
// We are in a function statement (declaration)
116124
private $_inFuncCall = false;
125+
117126
// We are in a function call
118127
private $_inFunction = false;
128+
119129
// We are inside a function
120130
private $_inClass = false;
131+
121132
// We are inside a class
122133
private $_inInterface = false;
134+
123135
// We are inside an interface
124136
private $_privateFunctions = array();
137+
125138
// The list of private functions in the class
126139
private $_privateFunctionsStartLines = array();
127140

128141
private $_functionParameters = array();
142+
129143
// The list of function parameters
130144
private $_currentFuncCall = [];
145+
131146
// The stack of function calls
132147
private $_usedFunctions = array();
148+
133149
// The list of functions that are used in the class
134150
private $_variables = array();
151+
135152
// The variables used. Array of VariableInfo.
136153
private $_nbFunctionParameters = 0;
154+
137155
// Count the number of parameters of the current function
138156
private $_justAfterFuncStmt = false;
157+
139158
// We are just after a control statement (last } )
140159
private $_justAfterControlStmt = false;
160+
141161
// We are just after a function statement (last } )
142162
private $_functionStartLine = 0;
163+
143164
// Starting line of the current function
144165
private $_switchStartLine = 0;
166+
145167
// Starting line of the current switch statement
146168
private $_functionReturns = false;
169+
147170
// Does the function return a value ?
148171
private $_functionThrows = false;
172+
149173
// Does the function throw an exception ?
150174
private $_functionLevel = 0;
175+
151176
// Level of Nesting of the function
152177
private $_functionVisibility = 'PUBLIC';
178+
153179
// PUBLIC, PRIVATE or PROTECTED or ANONYMOUS
154180
private $_functionStatic = false;
181+
155182
// Is the function static
156183
private $_classLevel = 0;
184+
157185
// Level of Nesting of the class
158186
private $_interfaceLevel = 0;
187+
159188
// Level of Nesting of the interface
160189
private $_constantDef = false;
161190

@@ -171,28 +200,33 @@ class PHPCheckstyle {
171200

172201
private $_currentFunctionName = null;
173202

174-
175203
// Number of @params in the docblock of a function
176204
private $_docblocNbParams = 0;
205+
177206
// Number of @return in the docblock of a function
178207
private $_docblocNbReturns = 0;
208+
179209
// Number of @throw in the docblock of a function
180210
private $_docblocNbThrows = 0;
211+
181212
// Does the function inherits its doc
182213
private $_docblocInheritDoc = false;
183214

184-
185215
private $_cyclomaticComplexity = 0;
186216

187217
private $_npathComplexity = 0;
188218

189219
private $_fileSuppressWarnings = array();
220+
190221
// List of warnings to ignore for this file
191222
private $_classSuppressWarnings = array();
223+
192224
// List of warnings to ignore for this class
193225
private $_interfaceSuppressWarnings = array();
226+
194227
// List of warnings to ignore for this interface
195228
private $_functionSuppressWarnings = array();
229+
196230
// List of warnings to ignore for this function
197231

198232
// For MVC frameworks
@@ -1246,7 +1280,6 @@ private function _processSquareBracketOpen($token) {
12461280
$stackitem->name = 'square_bracket_open';
12471281

12481282
$this->statementStack->push($stackitem);
1249-
12501283
}
12511284
}
12521285

@@ -1259,8 +1292,7 @@ private function _processSquareBracketOpen($token) {
12591292
private function _processSquareBracketClose($token) {
12601293

12611294
// We are in a array declaration, we unstack
1262-
if ($this->statementStack->getCurrentStackItem()->type === StatementItem::TYPE_ARRAY
1263-
&& $this->statementStack->getCurrentStackItem()->name === 'square_bracket_open') {
1295+
if ($this->statementStack->getCurrentStackItem()->type === StatementItem::TYPE_ARRAY && $this->statementStack->getCurrentStackItem()->name === 'square_bracket_open') {
12641296
$this->statementStack->pop();
12651297
}
12661298
}
@@ -1346,8 +1378,7 @@ private function _processParenthesisClose($token) {
13461378
if ($this->statementStack->getCurrentStackItem()->openParentheses === 0) {
13471379

13481380
// We are in a array declaration, we unstack
1349-
if ($this->statementStack->getCurrentStackItem()->type === StatementItem::TYPE_ARRAY AND
1350-
$this->statementStack->getCurrentStackItem()->name !== 'square_bracket_open') {
1381+
if ($this->statementStack->getCurrentStackItem()->type === StatementItem::TYPE_ARRAY and $this->statementStack->getCurrentStackItem()->name !== 'square_bracket_open') {
13511382
$this->statementStack->pop();
13521383
}
13531384
}
@@ -1464,9 +1495,7 @@ private function _processBracesClose($token) {
14641495
if (!is_String($currentStackItem)) {
14651496

14661497
// Test for the end of a switch bloc
1467-
if ($currentStackItem->type === StatementItem::TYPE_SWITCH
1468-
|| $currentStackItem->type === StatementItem::TYPE_DEFAULT
1469-
|| $currentStackItem->type === StatementItem::TYPE_CASE) {
1498+
if ($currentStackItem->type === StatementItem::TYPE_SWITCH || $currentStackItem->type === StatementItem::TYPE_DEFAULT || $currentStackItem->type === StatementItem::TYPE_CASE) {
14701499
$this->_processSwitchStop();
14711500
}
14721501

@@ -1546,7 +1575,7 @@ private function _checkVariableNaming($text) {
15461575
if ($this->_inFunctionStatement || $this->_inInterfaceStatement) {
15471576
$this->_checkScopedVariableNaming($text, 'functionParameterNaming', 'FUNCTION_PARAMETER_NAMING');
15481577
} else if ($this->_inFunction) {
1549-
if (in_array($text, $this->_functionParameters)) {
1578+
if (array_key_exists($text, $this->_functionParameters)) {
15501579
$this->_checkScopedVariableNaming($text, 'functionParameterNaming', 'FUNCTION_PARAMETER_NAMING');
15511580
} else {
15521581
$this->_checkScopedVariableNaming($text, 'localVariableNaming', 'LOCAL_VARIABLE_NAMING');
@@ -2088,10 +2117,7 @@ private function _checkDocBlockParameters() {
20882117
// For anonymous functions, we don't check the docblock
20892118
$isAnonymous = $this->statementStack->getCurrentStackItem()->visibility === 'ANONYMOUS';
20902119

2091-
if ($this->_isActive('docBlocks')
2092-
&& !$isAnonymous
2093-
&& !$this->_config->isException('docBlocks', $this->_currentFunctionName)
2094-
&& !$this->_docblocInheritDoc) {
2120+
if ($this->_isActive('docBlocks') && !$isAnonymous && !$this->_config->isException('docBlocks', $this->_currentFunctionName) && !$this->_docblocInheritDoc) {
20952121

20962122
// If the function is not private and we check the doc
20972123
$isPrivateExcluded = $this->_config->getTestProperty('docBlocks', 'excludePrivateMembers');
@@ -2352,8 +2378,7 @@ private function _processSwitchStart() {
23522378
*/
23532379
private function _processSwitchStop() {
23542380
// If we already are in a "case", we remove it from the stack
2355-
if ($this->statementStack->getCurrentStackItem()->type === StatementItem::TYPE_CASE
2356-
|| $this->statementStack->getCurrentStackItem()->type === StatementItem::TYPE_DEFAULT) {
2381+
if ($this->statementStack->getCurrentStackItem()->type === StatementItem::TYPE_CASE || $this->statementStack->getCurrentStackItem()->type === StatementItem::TYPE_DEFAULT) {
23572382

23582383
// Test if the previous case had a break
23592384
$this->_checkSwitchCaseNeedBreak();
@@ -2588,8 +2613,7 @@ private function _checkEmptyBlock() {
25882613
* This function is launched when the current token is T_ENCAPSED_AND_WHITESPACE.
25892614
*/
25902615
private function _checkEncapsedVariablesInsideString() {
2591-
if ($this->_isActive('encapsedVariablesInsideString') AND (!$this->statementStack->getCurrentStackItem()->inHeredoc
2592-
OR ($this->_inFuncCall AND !$this->_config->isException('encapsedVariablesInsideString', end($this->_currentFuncCall))))) {
2616+
if ($this->_isActive('encapsedVariablesInsideString') and (!$this->statementStack->getCurrentStackItem()->inHeredoc or ($this->_inFuncCall and !$this->_config->isException('encapsedVariablesInsideString', end($this->_currentFuncCall))))) {
25932617
$this->_writeError('encapsedVariablesInsideString', $this->_getMessage('VARIABLE_INSIDE_STRING'));
25942618
}
25952619
}
@@ -2730,7 +2754,6 @@ private function _checkUnusedFunctionParameters() {
27302754
$this->_writeError('checkUnusedFunctionParameters', $msg);
27312755
}
27322756
}
2733-
27342757
}
27352758
}
27362759
}
@@ -2979,7 +3002,8 @@ private function _checkWhiteSpaceAfter($text) {
29793002
/**
29803003
* Check for the absence of a white space after the text.
29813004
*
2982-
* @param TokenInfo $token the token
3005+
* @param TokenInfo $token
3006+
* the token
29833007
*/
29843008
private function _checkNoWhiteSpaceAfter($token) {
29853009
if ($this->_isActive('noSpaceAfterToken')) {
@@ -3113,14 +3137,7 @@ private function _checkIndentationLevel($token) {
31133137
}
31143138

31153139
// don't check empty lines, when we are inside a control statement, when the next token is a object opérator, ...
3116-
if ($this->_inControlStatement
3117-
|| $this->_inFuncCall
3118-
|| !isset($this->lineNumber)
3119-
|| $this->tokenizer->checkNextToken(T_NEW_LINE)
3120-
|| $this->tokenizer->checkNextValidToken(T_PARENTHESIS_CLOSE)
3121-
|| $this->tokenizer->checkNextValidToken(T_BOOLEAN_AND)
3122-
|| $this->tokenizer->checkNextValidToken(T_BOOLEAN_OR)
3123-
|| $this->tokenizer->checkNextValidToken(T_OBJECT_OPERATOR)) {
3140+
if ($this->_inControlStatement || $this->_inFuncCall || !isset($this->lineNumber) || $this->tokenizer->checkNextToken(T_NEW_LINE) || $this->tokenizer->checkNextValidToken(T_PARENTHESIS_CLOSE) || $this->tokenizer->checkNextValidToken(T_BOOLEAN_AND) || $this->tokenizer->checkNextValidToken(T_BOOLEAN_OR) || $this->tokenizer->checkNextValidToken(T_OBJECT_OPERATOR)) {
31243141
return;
31253142
}
31263143

test/NamingTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ public function testNaming() {
2626
$this->assertEquals(5, $errorCounts['warning'], 'We expect 5 warnings');
2727
}
2828

29-
30-
31-
3229
/**
3330
* Test function naming rules.
3431
*/

0 commit comments

Comments
 (0)