Skip to content

Commit

Permalink
ConstantStringSniff: Take into account namespace prefixing
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccahum committed Aug 27, 2019
1 parent b3e134c commit b066e0a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
6 changes: 6 additions & 0 deletions WordPressVIPMinimum/Sniffs/Constants/ConstantStringSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()`.';
Expand Down
12 changes: 9 additions & 3 deletions WordPressVIPMinimum/Tests/Constants/ConstantStringUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ if ( ! defined( 'WPCOM_VIP' ) ) { // Okay.
define( 'WPCOM_VIP', true ); // Okay.
}

if ( ! defined( WPCOM_VIP ) ) { // NOK.
define( WPCOM_VIP ); // NOK.
}
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.
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class ConstantStringUnitTest extends AbstractSniffUnitTest {
*/
public function getErrorList() {
return [
7 => 1,
8 => 1,
7 => 1,
8 => 1,
14 => 1,
];
}

Expand Down

0 comments on commit b066e0a

Please sign in to comment.