Skip to content

Commit eef593b

Browse files
rodrigoprimojrfnl
authored andcommitted
Generic/UpperCaseConstantName: improve code coverage
Includes updating a sniff code comment to better describe what it does. Some of the cases described were not covered by tests before the changes in this commit.
1 parent 2e68850 commit eef593b

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ public function process(File $phpcsFile, $stackPtr)
108108
return;
109109
}
110110

111-
// The next non-empty token must be the constant name.
111+
// Bow out if next non-empty token after the opening parenthesis is not a string (the
112+
// constant name). This could happen when live coding, if the constant is a variable or an
113+
// expression, or if handling a first-class callable or a function definition outside the
114+
// global scope.
112115
$constPtr = $phpcsFile->findNext(Tokens::$emptyTokens, ($openBracket + 1), null, true);
113116
if ($constPtr === false || $tokens[$constPtr]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
114117
return;

src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace foo\bar;
55
namespace bar\foo\baz;
66

77
define('VALID_NAME', true);
8-
define('invalidName', true);
8+
DEFINE('invalidName', true);
99
define("VALID_NAME", true);
1010
define("invalidName", true);
1111
define('bar\foo\baz\VALID_NAME_WITH_NAMESPACE', true);
@@ -37,9 +37,9 @@ class TypedConstants {
3737
const MISSING_VALUE; // Parse error.
3838
const MyClass MYCONST = new MyClass;
3939
const int VALID_NAME = 0;
40-
const INT invalid_name = 0;
40+
final public const INT invalid_name = 0;
4141
const FALSE false = false; // Yes, false can be used as a constant name, don't ask.
42-
const array ARRAY = array(); // Same goes for array.
42+
final protected const array ARRAY = array(); // Same goes for array.
4343
}
4444

4545
define /* comment */ ( /* comment */ 'CommentsInUnconventionalPlaces', 'value' );
@@ -67,3 +67,16 @@ class MyClass {}
6767
define('some param')
6868
]
6969
class MyClass {}
70+
71+
const MixedCase = 1;
72+
73+
define('lower_case_name', 'value');
74+
define($var, 'sniff should bow out');
75+
define(constantName(), 'sniff should bow out');
76+
define($obj->constantName(), 'sniff should bow out');
77+
define(MyClass::constantName(), 'sniff should bow out');
78+
define(condition() ? 'name1' : 'name2', 'sniff should bow out');
79+
80+
// Valid if outside the global namespace. Sniff should bow out.
81+
function define($param) {}
82+
$callable = define(...);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error (missing opening parenthesis).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered.
6+
7+
define
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error (missing constant name).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered.
6+
7+
const =

src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public function getErrorList($testFile='')
4646
41 => 1,
4747
45 => 1,
4848
51 => 1,
49+
71 => 1,
50+
73 => 1,
4951
];
5052
default:
5153
return [];

0 commit comments

Comments
 (0)