Skip to content

Commit b0c6578

Browse files
authored
Merge pull request #444 from Automattic/rebecca/namespacing_to_-ConstantStringSniff
2 parents 2b057a0 + b01bca1 commit b0c6578

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

WordPressVIPMinimum/Sniffs/Constants/ConstantStringSniff.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,26 @@ public function process_token( $stackPtr ) {
5555
return;
5656
}
5757

58-
$nextToken = $this->phpcsFile->findNext( Tokens::$emptyTokens, $nextToken + 1, null, true, null, true );
58+
$param = $this->get_function_call_parameter( $stackPtr, 1 );
59+
if ( $param === false ) {
60+
// Target parameter not found.
61+
return;
62+
}
63+
64+
$search = Tokens::$emptyTokens;
65+
$search[ T_STRING ] = T_STRING;
5966

60-
if ( $this->tokens[ $nextToken ]['code'] !== T_CONSTANT_ENCAPSED_STRING ) {
61-
$message = 'Constant name, as a string, should be used along with `%s()`.';
62-
$data = [ $this->tokens[ $stackPtr ]['content'] ];
63-
$this->phpcsFile->addError( $message, $nextToken, 'NotCheckingConstantName', $data );
67+
$has_only_tstring = $this->phpcsFile->findNext( $search, $param['start'], $param['end'] + 1, true );
68+
if ( $has_only_tstring !== false ) {
69+
// Came across something other than a T_STRING token. Ignore.
6470
return;
6571
}
72+
73+
$tstring_token = $this->phpcsFile->findNext( T_STRING, $param['start'], $param['end'] + 1 );
74+
75+
$message = 'Constant name, as a string, should be used along with `%s()`.';
76+
$data = [ $this->tokens[ $stackPtr ]['content'] ];
77+
$this->phpcsFile->addError( $message, $tstring_token, 'NotCheckingConstantName', $data );
6678
}
6779

6880
}

WordPressVIPMinimum/Tests/Constants/ConstantStringUnitTest.inc

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,30 @@ 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, true ); // 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[1], $value ); // OK.
15+
}
16+
17+
define( __NAMESPACE__ . '\PLUGIN_URL', \plugins_url( '/', __FILE__ ) ); // OK.
18+
if ( defined( __NAMESPACE__ . '\\LOADED' ) ) {} // OK.
19+
20+
if ( defined( $obj->constant_name_property ) === false ) { // OK.
21+
define( $variable_containing_constant_name, $constant_value ); // OK.
22+
}
23+
24+
if ( defined( MY_PREFIX . '_CONSTANT_NAME' ) === false ) { // OK.
25+
define( 'PREFIX_' . $variable_part, $constant_value ); // OK.
26+
}
27+
28+
if ( ! defined($generator->get()) { // OK.
29+
define( $generator->getLast(), 'value'); // OK.
30+
}
31+
32+
$defined = defined(); // OK, ignore.
33+
$defined = defined( /*comment*/ ); // OK, ignore.

0 commit comments

Comments
 (0)