Skip to content

Commit b066e0a

Browse files
committed
ConstantStringSniff: Take into account namespace prefixing
1 parent b3e134c commit b066e0a

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

WordPressVIPMinimum/Sniffs/Constants/ConstantStringSniff.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ public function process_token( $stackPtr ) {
5656
}
5757

5858
$nextToken = $this->phpcsFile->findNext( Tokens::$emptyTokens, $nextToken + 1, null, true, null, true );
59+
$nextNextToken = $this->phpcsFile->findNext( Tokens::$emptyTokens, $nextToken + 1, null, true, null, true );
60+
61+
if ( T_NS_C === $this->tokens[ $nextToken ]['code'] && T_STRING_CONCAT === $this->tokens[ $nextNextToken ]['code'] ) {
62+
// Namespacing being used, skip to next.
63+
$nextToken = $this->phpcsFile->findNext( Tokens::$emptyTokens, $nextNextToken + 1, null, true, null, true );
64+
}
5965

6066
if ( T_CONSTANT_ENCAPSED_STRING !== $this->tokens[ $nextToken ]['code'] ) {
6167
$message = 'Constant name, as a string, should be used along with `%s()`.';

WordPressVIPMinimum/Tests/Constants/ConstantStringUnitTest.inc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ if ( ! defined( 'WPCOM_VIP' ) ) { // Okay.
44
define( 'WPCOM_VIP', true ); // Okay.
55
}
66

7-
if ( ! defined( WPCOM_VIP ) ) { // NOK.
8-
define( WPCOM_VIP ); // NOK.
9-
}
7+
if ( ! defined( WPCOM_VIP ) ) { // Error.
8+
define( WPCOM_VIP ); // Error.
9+
}
10+
11+
namespace Foo/Bar;
12+
const REST_ALLOWED_META_PREFIXES = [ 'foo-', 'bar-', 'baz-' ];
13+
if ( defined( __NAMESPACE__ . '\REST_ALLOWED_META_PREFIXES' ) && in_array( 'foo-', REST_ALLOWED_META_PREFIXES, true ) ) { // Ok.
14+
define( __NAMESPACE__ . REST_ALLOWED_META_PREFIXES ); // Error.
15+
}

WordPressVIPMinimum/Tests/Constants/ConstantStringUnitTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ class ConstantStringUnitTest extends AbstractSniffUnitTest {
2323
*/
2424
public function getErrorList() {
2525
return [
26-
7 => 1,
27-
8 => 1,
26+
7 => 1,
27+
8 => 1,
28+
14 => 1,
2829
];
2930
}
3031

0 commit comments

Comments
 (0)