Skip to content

Commit d8baf91

Browse files
committed
Fixes #1733 - Error on short prefixes
1 parent d00b44b commit d8baf91

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php

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

36+
/**
37+
* Minimal number of characters the prefix needs in order to be valid.
38+
*
39+
* @since 2.2.0
40+
*
41+
* @link https://github.com/WordPress/WordPress-Coding-Standards/issues/1733 Issue 1733.
42+
*
43+
* @var int
44+
*/
45+
const MIN_PREFIX_LENGTH = 3;
46+
3647
/**
3748
* Target prefixes.
3849
*
@@ -914,6 +925,16 @@ private function validate_prefixes() {
914925
continue;
915926
}
916927

928+
if ( function_exists( 'iconv_strlen' ) && iconv_strlen( $prefix, $this->phpcsFile->config->encoding ) < self::MIN_PREFIX_LENGTH ) {
929+
$this->phpcsFile->addError(
930+
'The "%s" prefix is too short. Short prefixes are not unique enough and may cause name collisions with other code.',
931+
0,
932+
'ShortPrefixPassed',
933+
array( $prefix )
934+
);
935+
continue;
936+
}
937+
917938
// Validate the prefix against characters allowed for function, class, constant names etc.
918939
if ( preg_match( '`^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\\\\]*$`', $prefix ) !== 1 ) {
919940
$this->phpcsFile->addWarning(

WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.1.inc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,28 @@ define(
425425
);
426426

427427
// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[]
428+
429+
/*
430+
* Bad: Issue https://github.com/WordPress/WordPress-Coding-Standards/issues/1733.
431+
*
432+
* Short prefixes are not allowed. The errors are triggered
433+
* on LINE 1 for the unit-test, because it's the phpcs:set command that is
434+
* wrong, not the implementing code.
435+
*/
436+
// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[] a
437+
function a_do_something(){}
438+
439+
// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[] aa
440+
function aa_do_something(){}
441+
442+
// The following line mimicks an empty prefix value.
443+
// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[] ,
444+
function aa_do_something(){}
445+
446+
// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[] 😊
447+
function 😊_do_something(){}
448+
449+
// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[] 😊😊
450+
function 😊😊_do_something(){}
451+
452+
// 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)