From b066e0a4998ec0cc5cea8ce045aea532403c7dc3 Mon Sep 17 00:00:00 2001 From: Rebecca Hum Date: Tue, 27 Aug 2019 15:49:19 -0600 Subject: [PATCH] ConstantStringSniff: Take into account namespace prefixing --- .../Sniffs/Constants/ConstantStringSniff.php | 6 ++++++ .../Tests/Constants/ConstantStringUnitTest.inc | 12 +++++++++--- .../Tests/Constants/ConstantStringUnitTest.php | 5 +++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/WordPressVIPMinimum/Sniffs/Constants/ConstantStringSniff.php b/WordPressVIPMinimum/Sniffs/Constants/ConstantStringSniff.php index dfb2cef2..dbf8fb40 100644 --- a/WordPressVIPMinimum/Sniffs/Constants/ConstantStringSniff.php +++ b/WordPressVIPMinimum/Sniffs/Constants/ConstantStringSniff.php @@ -56,6 +56,12 @@ public function process_token( $stackPtr ) { } $nextToken = $this->phpcsFile->findNext( Tokens::$emptyTokens, $nextToken + 1, null, true, null, true ); + $nextNextToken = $this->phpcsFile->findNext( Tokens::$emptyTokens, $nextToken + 1, null, true, null, true ); + + if ( T_NS_C === $this->tokens[ $nextToken ]['code'] && T_STRING_CONCAT === $this->tokens[ $nextNextToken ]['code'] ) { + // Namespacing being used, skip to next. + $nextToken = $this->phpcsFile->findNext( Tokens::$emptyTokens, $nextNextToken + 1, null, true, null, true ); + } if ( T_CONSTANT_ENCAPSED_STRING !== $this->tokens[ $nextToken ]['code'] ) { $message = 'Constant name, as a string, should be used along with `%s()`.'; diff --git a/WordPressVIPMinimum/Tests/Constants/ConstantStringUnitTest.inc b/WordPressVIPMinimum/Tests/Constants/ConstantStringUnitTest.inc index a330da69..5bace853 100644 --- a/WordPressVIPMinimum/Tests/Constants/ConstantStringUnitTest.inc +++ b/WordPressVIPMinimum/Tests/Constants/ConstantStringUnitTest.inc @@ -4,6 +4,12 @@ if ( ! defined( 'WPCOM_VIP' ) ) { // Okay. define( 'WPCOM_VIP', true ); // Okay. } -if ( ! defined( WPCOM_VIP ) ) { // NOK. - define( WPCOM_VIP ); // NOK. -} \ No newline at end of file +if ( ! defined( WPCOM_VIP ) ) { // Error. + define( WPCOM_VIP ); // Error. +} + +namespace Foo/Bar; +const REST_ALLOWED_META_PREFIXES = [ 'foo-', 'bar-', 'baz-' ]; +if ( defined( __NAMESPACE__ . '\REST_ALLOWED_META_PREFIXES' ) && in_array( 'foo-', REST_ALLOWED_META_PREFIXES, true ) ) { // Ok. + define( __NAMESPACE__ . REST_ALLOWED_META_PREFIXES ); // Error. +} diff --git a/WordPressVIPMinimum/Tests/Constants/ConstantStringUnitTest.php b/WordPressVIPMinimum/Tests/Constants/ConstantStringUnitTest.php index 7d3abc44..032f8f8a 100644 --- a/WordPressVIPMinimum/Tests/Constants/ConstantStringUnitTest.php +++ b/WordPressVIPMinimum/Tests/Constants/ConstantStringUnitTest.php @@ -23,8 +23,9 @@ class ConstantStringUnitTest extends AbstractSniffUnitTest { */ public function getErrorList() { return [ - 7 => 1, - 8 => 1, + 7 => 1, + 8 => 1, + 14 => 1, ]; }