Skip to content

Commit

Permalink
Merge pull request #1741 from NielsdeBlaauw/1733-short-prefixes-error
Browse files Browse the repository at this point in the history
Fixes #1733 - Error on short prefixes
  • Loading branch information
jrfnl authored Jul 31, 2019
2 parents b56232b + 605d2f7 commit e22e842
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
26 changes: 26 additions & 0 deletions WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ class PrefixAllGlobalsSniff extends AbstractFunctionParameterSniff {
*/
const ERROR_MSG = '%s by a theme/plugin should start with the theme/plugin prefix. Found: "%s".';

/**
* Minimal number of characters the prefix needs in order to be valid.
*
* @since 2.2.0
*
* @link https://github.com/WordPress/WordPress-Coding-Standards/issues/1733 Issue 1733.
*
* @var int
*/
const MIN_PREFIX_LENGTH = 3;

/**
* Target prefixes.
*
Expand Down Expand Up @@ -955,6 +966,21 @@ private function validate_prefixes() {
continue;
}

$prefix_length = strlen( $prefix );
if ( function_exists( 'iconv_strlen' ) ) {
$prefix_length = iconv_strlen( $prefix, $this->phpcsFile->config->encoding );
}

if ( $prefix_length < self::MIN_PREFIX_LENGTH ) {
$this->phpcsFile->addError(
'The "%s" prefix is too short. Short prefixes are not unique enough and may cause name collisions with other code.',
0,
'ShortPrefixPassed',
array( $prefix )
);
continue;
}

// Validate the prefix against characters allowed for function, class, constant names etc.
if ( preg_match( '`^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\\\\]*$`', $prefix ) !== 1 ) {
$this->phpcsFile->addWarning(
Expand Down
25 changes: 25 additions & 0 deletions WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,28 @@ function acronym_lists_in_function_scope() {
}

// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[]

/*
* Bad: Issue https://github.com/WordPress/WordPress-Coding-Standards/issues/1733.
*
* Short prefixes are not allowed. The errors are triggered
* on LINE 1 for the unit-test, because it's the phpcs:set command that is
* wrong, not the implementing code.
*/
// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[] a
function a_do_something(){}

// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[] aa
function aa_do_something(){}

// The following line mimicks an empty prefix value.
// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[] ,
function aa_do_something(){}

// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[] 😊
function 😊_do_something(){}

// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[] 😊😊
function 😊😊_do_something(){}

// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[]
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function getErrorList( $testFile = 'PrefixAllGlobalsUnitTest.1.inc' ) {
switch ( $testFile ) {
case 'PrefixAllGlobalsUnitTest.1.inc':
return array(
1 => 2, // 1 x error for blacklisted prefix passed.
1 => 8, // 2 x error for blacklisted prefix passed. 4 x error for short prefixes. 2 x no prefix.
10 => 1,
18 => 1,
21 => 1,
Expand Down

0 comments on commit e22e842

Please sign in to comment.