Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

// The next non-empty token must be the constant name.
// Bow out if next non-empty token after the opening parenthesis is not a string (the
// constant name). This could happen when live coding, if the constant is a variable or an
// expression, or if handling a first-class callable or a function definition outside the
// global scope.
$constPtr = $phpcsFile->findNext(Tokens::$emptyTokens, ($openBracket + 1), null, true);
if ($constPtr === false || $tokens[$constPtr]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace foo\bar;
namespace bar\foo\baz;

define('VALID_NAME', true);
define('invalidName', true);
DEFINE('invalidName', true);
define("VALID_NAME", true);
define("invalidName", true);
define('bar\foo\baz\VALID_NAME_WITH_NAMESPACE', true);
Expand Down Expand Up @@ -37,9 +37,9 @@ class TypedConstants {
const MISSING_VALUE; // Parse error.
const MyClass MYCONST = new MyClass;
const int VALID_NAME = 0;
const INT invalid_name = 0;
final public const INT invalid_name = 0;
const FALSE false = false; // Yes, false can be used as a constant name, don't ask.
const array ARRAY = array(); // Same goes for array.
final protected const array ARRAY = array(); // Same goes for array.
}

define /* comment */ ( /* comment */ 'CommentsInUnconventionalPlaces', 'value' );
Expand Down Expand Up @@ -67,3 +67,16 @@ class MyClass {}
define('some param')
]
class MyClass {}

const MixedCase = 1;

define('lower_case_name', 'value');
define($var, 'sniff should bow out');
define(constantName(), 'sniff should bow out');
define($obj->constantName(), 'sniff should bow out');
define(MyClass::constantName(), 'sniff should bow out');
define(condition() ? 'name1' : 'name2', 'sniff should bow out');

// Valid if outside the global namespace. Sniff should bow out.
function define($param) {}
$callable = define(...);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Intentional parse error (missing opening parenthesis).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.

define
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Intentional parse error (missing constant name).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.

const =
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public function getErrorList($testFile='')
41 => 1,
45 => 1,
51 => 1,
71 => 1,
73 => 1,
];
default:
return [];
Expand Down