Skip to content

Commit 658c48f

Browse files
committed
WIP NamingConventions/PrefixAllGlobals: improve prefix validation
Add a new error when validating prefixes if a prefix is not a string or is an empty string. This is necessary in preparation for PHPCS 4.0. Starting with this version, array properties can have types other than string, and the sniff needs to be updated to handle non-string prefixes. Using the same error when the string is empty made sense as before the sniff would generate an error complaining about the length of the prefix. TODO: the value of the prefix is type casted to a string when displaying the error message. For example, `true` becomes `1`. I need to think if this is a problem or not. Also, if `true` is passed as the first and only element of an array property there seems to be an error in PHPCS. Check how PHPCSStandards/PHP_CodeSniffer#1172 and PHPCSStandards/PHP_CodeSniffer#1193 impact this commit.
1 parent b949c64 commit 658c48f

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,16 @@ private function validate_prefixes() {
12211221
$prefixes = array();
12221222
$ns_prefixes = array();
12231223
foreach ( $this->prefixes as $key => $prefix ) {
1224+
if ( is_string( $prefix ) === false || trim( $prefix ) === '' ) {
1225+
$this->phpcsFile->addError(
1226+
'The prefix must be a non-empty string. Found: "%s".',
1227+
0,
1228+
'InvalidPrefixPassed',
1229+
array( $prefix )
1230+
);
1231+
continue;
1232+
}
1233+
12241234
$prefixLC = strtolower( $prefix );
12251235

12261236
if ( isset( $this->prefix_blocklist[ $prefixLC ] ) ) {

0 commit comments

Comments
 (0)