Skip to content

Commit c5213b6

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

14 files changed

+106
-6
lines changed

config/codeigniter.cfg.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<test name="topLevelVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for top level variables -->
5959
<test name="localVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for local variables (inside a class) -->
6060
<test name="memberVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for member variables (inside a function) -->
61+
<test name="functionParameterNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for function parameters -->
6162

6263
<!-- Check Function Naming -->
6364
<test name="functionNaming" regexp="/^[a-z][a-zA-Z0-9]*$/" level="error"/>

config/default.cfg.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<test name="topLevelVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for top level variables -->
5959
<test name="localVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for local variables (inside a class) -->
6060
<test name="memberVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for member variables (inside a function) -->
61+
<test name="functionParameterNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for function parameters -->
6162

6263
<!-- Check Function Naming -->
6364
<test name="functionNaming" regexp="/^[a-z][a-zA-Z0-9]*$/" level="error" />

config/sameline.cfg.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<test name="topLevelVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for top level variables -->
5959
<test name="localVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for local variables (inside a class) -->
6060
<test name="memberVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for member variables (inside a function) -->
61+
<test name="functionParameterNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for function parameters -->
6162

6263
<!-- Check Function Naming -->
6364
<test name="functionNaming" regexp="/^[a-z][a-zA-Z0-9]*$/" level="error"/>

config/spaceindent.cfg.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<test name="topLevelVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for top level variables -->
5959
<test name="localVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for local variables (inside a class) -->
6060
<test name="memberVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for member variables (inside a function) -->
61+
<test name="functionParameterNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for function parameters -->
6162

6263
<!-- Check Function Naming -->
6364
<test name="functionNaming" regexp="/^[a-z][a-zA-Z0-9]*$/" level="error"/>

config/symfony.cfg.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<test name="topLevelVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for top level variables -->
6262
<test name="localVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for local variables (inside a class) -->
6363
<test name="memberVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for member variables (inside a function) -->
64+
<test name="functionParameterNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for function parameters -->
6465

6566
<!-- Check Function Naming -->
6667
<test name="functionNaming" regexp="/^[a-z][a-zA-Z0-9]*$/" level="error" />

config/test.cfg.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<test name="topLevelVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for top level variables -->
5959
<test name="localVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for local variables (inside a class) -->
6060
<test name="memberVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for member variables (inside a function) -->
61+
<test name="functionParameterNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for function parameters -->
6162

6263
<!-- Check Function Naming -->
6364
<test name="functionNaming" regexp="/^[a-z][a-zA-Z0-9]*$/" level="error"/>

config/zend.cfg.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<test name="topLevelVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for top level variables -->
6262
<test name="localVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for local variables (inside a class) -->
6363
<test name="memberVariableNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for member variables (inside a function) -->
64+
<test name="functionParameterNaming" regexp="/^[a-z_][a-zA-Z0-9]*$/" /> <!-- for function parameters -->
6465

6566
<!-- Check Function Naming -->
6667
<test name="functionNaming" regexp="/^[a-z][a-zA-Z0-9]*$/" level="error" />

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/Lang/en-us.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ CONSTANT_NAMING="Constant %s name should follow the pattern %s."
1616
VARIABLE_NAMING="Variable %s name should follow the pattern %s."
1717
LOCAL_VARIABLE_NAMING="Local variable %s name should follow the pattern %s."
1818
MEMBER_VARIABLE_NAMING="Member variable %s name should follow the pattern %s."
19+
FUNCTION_PARAMETER_NAMING="Function parameter %s name should follow the pattern %s."
1920
TOPLEVEL_VARIABLE_NAMING="Top level variable %s name should follow the pattern %s."
2021
FUNCNAME_SPACE_AFTER="Whitespace must not be between the function %s and the opening brace '{'."
2122
PRIVATE_FUNCNAME_NAMING="Private function %s name should follow the pattern %s."

src/PHPCheckstyle/Lang/fr-fr.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ CONSTANT_NAMING="Le nom de la constante %s devrait suivre le pattern %s."
1616
VARIABLE_NAMING="Le nom de la variable %s devrait suivre le pattern %s."
1717
LOCAL_VARIABLE_NAMING="Le nom de la variable locale %s devrait suivre le pattern %s."
1818
MEMBER_VARIABLE_NAMING="Le nom de la variable de classe %s devrait suivre le pattern %s."
19+
FUNCTION_PARAMETER_NAMING="Le nom du paramètre de fonction %s devrait suivre le pattern %s."
1920
TOPLEVEL_VARIABLE_NAMING="Le nom de la variable globale %s devrait suivre le pattern %s."
2021
FUNCNAME_SPACE_AFTER="Supprimer l'espace entre le nom de fonction %s et la parenthèse ouvrante '{'."
2122
PRIVATE_FUNCNAME_NAMING="Le nom de la fonction privée %s devrait suivre le pattern %s."

src/PHPCheckstyle/Lang/kr.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ CONSTANT_NAMING = "상수 %s 이름은 %s 패턴을 따라야합니다."
1616
VARIABLE_NAMING = "변수 %s 이름은 %s 패턴을 따라야합니다."
1717
LOCAL_VARIABLE_NAMING = "로컬 변수 %s 의 이름이 %s 패턴을 따라야합니다."
1818
MEMBER_VARIABLE_NAMING = "멤버 변수 %s 의 이름이 %s 패턴을 따라야합니다."
19+
FUNCTION_PARAMETER_NAMING = "멤버 변수 %s 의 이름이 %s 패턴을 따라야합니다."
1920
TOPLEVEL_VARIABLE_NAMING = "최상위 변수 %s 이름이 %s 패턴을 따라야합니다."
2021
FUNCNAME_SPACE_AFTER = "공백은 함수 %s 와 여는 중괄호 '{' 사이에 있어서는 안됩니다."
2122
PRIVATE_FUNCNAME_NAMING = "Private 함수 %s 이름이 %s 패턴을 따라야합니다."

src/PHPCheckstyle/PHPCheckstyle.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,8 +1543,14 @@ private function _checkConstantNaming($text) {
15431543
*/
15441544
private function _checkVariableNaming($text) {
15451545
if ($this->_inClass || $this->_inInterface) {
1546-
if ($this->_inFunction || $this->_inFunctionStatement || $this->_inInterfaceStatement) {
1547-
$this->_checkScopedVariableNaming($text, 'localVariableNaming', 'LOCAL_VARIABLE_NAMING');
1546+
if ($this->_inFunctionStatement || $this->_inInterfaceStatement) {
1547+
$this->_checkScopedVariableNaming($text, 'functionParameterNaming', 'FUNCTION_PARAMETER_NAMING');
1548+
} else if ($this->_inFunction) {
1549+
if (in_array($text, $this->_functionParameters)) {
1550+
$this->_checkScopedVariableNaming($text, 'functionParameterNaming', 'FUNCTION_PARAMETER_NAMING');
1551+
} else {
1552+
$this->_checkScopedVariableNaming($text, 'localVariableNaming', 'LOCAL_VARIABLE_NAMING');
1553+
}
15481554
} else {
15491555
$this->_checkScopedVariableNaming($text, 'memberVariableNaming', 'MEMBER_VARIABLE_NAMING');
15501556
}

test/NamingTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,29 @@ public function testNaming() {
2525
$this->assertEquals(0, $errorCounts['info'], 'We expect 0 info');
2626
$this->assertEquals(5, $errorCounts['warning'], 'We expect 5 warnings');
2727
}
28+
29+
30+
31+
32+
/**
33+
* Test function naming rules.
34+
*/
35+
public function testFunctionNaming() {
36+
$phpcheckstyle = $GLOBALS['PHPCheckstyle'];
37+
38+
// Change the default configuration, function parameters should start with "_"
39+
$phpcheckstyle->getConfig()->setTestProperty('functionParameterNaming', 'regexp', "/^[_][a-zA-Z0-9]*$/");
40+
41+
$phpcheckstyle->processFiles(array(
42+
'./test/sample/good_function_naming.php'
43+
));
44+
45+
$errorCounts = $phpcheckstyle->getErrorCounts();
46+
47+
$this->assertEquals(0, $errorCounts['error'], 'We expect 0 errors of naming');
48+
$this->assertEquals(0, $errorCounts['ignore'], 'We expect 0 ignored checks');
49+
$this->assertEquals(0, $errorCounts['info'], 'We expect 0 info');
50+
$this->assertEquals(0, $errorCounts['warning'], 'We expect 0 warnings');
51+
}
2852
}
2953
?>

test/sample/good_function_naming.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/**
4+
* This file is an exemple of PHP file containing good naming with specific rule for function parameters.
5+
*/
6+
7+
// constant naming :: PHPCHECKSTYLE_CONSTANT_NAMING
8+
define(NAMED_CONSTANT, "A_CONSTANT_VALUE");
9+
10+
// constant naming :: PHPCHECKSTYLE_CONSTANT_NAMING
11+
const CONSTANT = "CONSTANT";
12+
13+
// top level var naming :: PHPCHECKSTYLE_TOPLEVEL_VARIABLE_NAMING
14+
$foo = 1;
15+
16+
//
17+
/**
18+
* class naming :: PHPCHECKSTYLE_CLASSNAME_NAMING
19+
*
20+
* @SuppressWarnings checkUnusedVariables
21+
*/
22+
class NamedClass {
23+
24+
// member level var naming :: PHPCHECKSTYLE_MEMBER_VARIABLE_NAMING
25+
var $bar = 1;
26+
27+
/**
28+
* constructor Naming
29+
*/
30+
function __construct() {}
31+
32+
/**
33+
* function naming :: PHPCHECKSTYLE_FUNCNAME_NAMING
34+
*
35+
* @param $_myparam a param
36+
*/
37+
function namedFunction($_myparam) {
38+
39+
// local level var naming :: PHPCHECKSTYLE_LOCAL_VARIABLE_NAMING
40+
$fuu = 1;
41+
42+
$_myparam = 0;
43+
}
44+
}
45+
46+
/**
47+
* interface naming
48+
*/
49+
interface MyInterface {
50+
51+
/**
52+
* function naming :: PHPCHECKSTYLE_FUNCNAME_NAMING
53+
*
54+
* @param $_name a name
55+
* @param $_var a var
56+
*/
57+
public function setVariable($_name, $_var);
58+
}
59+
60+

0 commit comments

Comments
 (0)