Skip to content

Commit

Permalink
Merge pull request #1079 from WordPress-Coding-Standards/feature/fix-…
Browse files Browse the repository at this point in the history
…1056-namespaced-constants

PrefixAllGlobals: Improve recognition of namespaced constants using `define()`
  • Loading branch information
GaryJones committed Aug 3, 2017
2 parents 594a119 + 3304604 commit 0b2401e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,11 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
return;
}

if ( strpos( $raw_content, '\\' ) !== false ) {
// Namespaced or unreachable constant.
return;
}

$data = array( 'Global constants defined' );
$error_code = 'NonPrefixedConstantFound';
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ const SOME_CONSTANT = 'value';
class Example {}
interface I_Example {}
trait T_Example {}

// Issue #1056.
define( __NAMESPACE__ . '\PLUGIN_FILE', __FILE__ );
12 changes: 12 additions & 0 deletions WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,15 @@ do_action( 'acronym-action' ); // OK.
apply_filters( 'acronym/filter', $var ); // OK.
do_action( "acronym-action-{$acronym_filter_var}" ); // OK.
apply_filters( 'acronym/filter-' . $acronym_filter_var ); // OK.

// Issue #1056.
define( 'SomeNameSpace\PLUGIN_FILE', __FILE__ ); // OK.
define( '\OtherNameSpace\PLUGIN_FILE', __FILE__ ); // OK.
// OK: unreachable constants.
define( __NAMESPACE__ . '\PLUGIN_FILE', __FILE__ );
define( '\PLUGIN_FILE', __FILE__ );

namespace Testing {
define( 'MY' . __NAMESPACE__, __FILE__ ); // Error, not actually namespaced.
define( 'MY\\' . __NAMESPACE__, __FILE__ ); // OK, even though strangely setup, the constant is in a namespace.
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function getErrorList( $testFile = 'PrefixAllGlobalsUnitTest.inc' ) {
230 => ( function_exists( '\array_column' ) ) ? 0 : 1,
234 => ( defined( '\E_DEPRECATED' ) ) ? 0 : 1,
238 => ( class_exists( '\IntlTimeZone' ) ) ? 0 : 1,
318 => 1,
);

case 'PrefixAllGlobalsUnitTest.1.inc':
Expand Down

0 comments on commit 0b2401e

Please sign in to comment.