Skip to content

Commit e22e842

Browse files
authored
Merge pull request #1741 from NielsdeBlaauw/1733-short-prefixes-error
Fixes #1733 - Error on short prefixes
2 parents b56232b + 605d2f7 commit e22e842

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ class PrefixAllGlobalsSniff extends AbstractFunctionParameterSniff {
3434
*/
3535
const ERROR_MSG = '%s by a theme/plugin should start with the theme/plugin prefix. Found: "%s".';
3636

37+
/**
38+
* Minimal number of characters the prefix needs in order to be valid.
39+
*
40+
* @since 2.2.0
41+
*
42+
* @link https://github.com/WordPress/WordPress-Coding-Standards/issues/1733 Issue 1733.
43+
*
44+
* @var int
45+
*/
46+
const MIN_PREFIX_LENGTH = 3;
47+
3748
/**
3849
* Target prefixes.
3950
*
@@ -955,6 +966,21 @@ private function validate_prefixes() {
955966
continue;
956967
}
957968

969+
$prefix_length = strlen( $prefix );
970+
if ( function_exists( 'iconv_strlen' ) ) {
971+
$prefix_length = iconv_strlen( $prefix, $this->phpcsFile->config->encoding );
972+
}
973+
974+
if ( $prefix_length < self::MIN_PREFIX_LENGTH ) {
975+
$this->phpcsFile->addError(
976+
'The "%s" prefix is too short. Short prefixes are not unique enough and may cause name collisions with other code.',
977+
0,
978+
'ShortPrefixPassed',
979+
array( $prefix )
980+
);
981+
continue;
982+
}
983+
958984
// Validate the prefix against characters allowed for function, class, constant names etc.
959985
if ( preg_match( '`^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\\\\]*$`', $prefix ) !== 1 ) {
960986
$this->phpcsFile->addWarning(

WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.1.inc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,3 +472,28 @@ function acronym_lists_in_function_scope() {
472472
}
473473

474474
// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[]
475+
476+
/*
477+
* Bad: Issue https://github.com/WordPress/WordPress-Coding-Standards/issues/1733.
478+
*
479+
* Short prefixes are not allowed. The errors are triggered
480+
* on LINE 1 for the unit-test, because it's the phpcs:set command that is
481+
* wrong, not the implementing code.
482+
*/
483+
// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[] a
484+
function a_do_something(){}
485+
486+
// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[] aa
487+
function aa_do_something(){}
488+
489+
// The following line mimicks an empty prefix value.
490+
// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[] ,
491+
function aa_do_something(){}
492+
493+
// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[] 😊
494+
function 😊_do_something(){}
495+
496+
// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[] 😊😊
497+
function 😊😊_do_something(){}
498+
499+
// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[]

WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function getErrorList( $testFile = 'PrefixAllGlobalsUnitTest.1.inc' ) {
3232
switch ( $testFile ) {
3333
case 'PrefixAllGlobalsUnitTest.1.inc':
3434
return array(
35-
1 => 2, // 1 x error for blacklisted prefix passed.
35+
1 => 8, // 2 x error for blacklisted prefix passed. 4 x error for short prefixes. 2 x no prefix.
3636
10 => 1,
3737
18 => 1,
3838
21 => 1,

0 commit comments

Comments
 (0)