Skip to content

Commit

Permalink
WP.DiscouragedConstants: Trigger for const defined in the global na…
Browse files Browse the repository at this point in the history
…mespace

Distinguished between `const` defines in the global namespace and class constants.

To this end, two more utility functions I previously write for PHPCompatibility have been ported over.
  • Loading branch information
jrfnl committed Aug 5, 2017
1 parent 80faf1d commit cc8e2e9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
58 changes: 58 additions & 0 deletions WordPress/Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2220,4 +2220,62 @@ public function has_html_open_tag( $tag_name, $stackPtr = null, $content = false
return false;
}

/**
* Check whether a T_CONST token is a class constant declaration.
*
* @since 0.14.0
*
* @param int $stackPtr The position in the stack of the T_CONST token to verify.
*
* @return bool
*/
public function is_class_constant( $stackPtr ) {
if ( ! isset( $this->tokens[ $stackPtr ] ) || T_CONST !== $this->tokens[ $stackPtr ]['code'] ) {
return false;
}

// Note: traits can not declare constants.
$valid_scopes = array(
'T_CLASS' => true,
'T_ANON_CLASS' => true,
'T_INTERFACE' => true,
);

return $this->valid_direct_scope( $stackPtr, $valid_scopes );
}

/**
* Check whether the direct wrapping scope of a token is within a limited set of
* acceptable tokens.
*
* Used to check, for instance, if a T_CONST is a class constant.
*
* @since 0.14.0
*
* @param int $stackPtr The position in the stack of the token to verify.
* @param array $valid_scopes Array of token types.
* Keys should be the token types in string format
* to allow for newer token types.
* Value is irrelevant.
*
* @return bool
*/
protected function valid_direct_scope( $stackPtr, array $valid_scopes ) {
if ( empty( $this->tokens[ $stackPtr ]['conditions'] ) ) {
return false;
}

/*
* Check only the direct wrapping scope of the token.
*/
$conditions = array_keys( $this->tokens[ $stackPtr ]['conditions'] );
$ptr = array_pop( $conditions );

if ( ! isset( $this->tokens[ $ptr ] ) ) {
return false;
}

return isset( $valid_scopes[ $this->tokens[ $ptr ]['type'] ] );
}

}
9 changes: 8 additions & 1 deletion WordPress/Sniffs/WP/DiscouragedConstantsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class DiscouragedConstantsSniff extends AbstractFunctionParameterSniff {
T_IMPLEMENTS => true,
T_NEW => true,
T_FUNCTION => true,
T_CONST => true,
T_DOUBLE_COLON => true,
T_OBJECT_OPERATOR => true,
T_INSTANCEOF => true,
Expand Down Expand Up @@ -134,6 +133,14 @@ public function process_arbitrary_tstring( $stackPtr ) {
return;
}

if ( false !== $prev
&& T_CONST === $this->tokens[ $prev ]['code']
&& true === $this->is_class_constant( $prev )
) {
// Class constant of the same name.
return;
}

/*
* Deal with a number of variations of use statements.
*/
Expand Down
5 changes: 2 additions & 3 deletions WordPress/Tests/WP/DiscouragedConstantsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* The below are all ok.
*/
// Ok, not a constant.
// Ok, not a (global) constant.
namespace STYLESHEETPATH {}
namespace MY\OTHER\STYLESHEETPATH\NAMESPACE {}
use STYLESHEETPATH;
Expand All @@ -18,7 +18,6 @@ class ABC extends STYLESHEETPATH {}
class DEF implements STYLESHEETPATH {}
interface STYLESHEETPATH {}
trait STYLESHEETPATH {}
const STYLESHEETPATH = 'something';
$a = new STYLESHEETPATH;
$a = new STYLESHEETPATH();
function STYLESHEETPATH() {}
Expand All @@ -32,7 +31,6 @@ goto STYLESHEETPATH;
STYLESHEETPATH:
// Something.

// Ok, not a global constant.
echo \mynamespace\STYLESHEETPATH;
echo My_Class::STYLESHEETPATH;
echo $this->STYLESHEETPATH;
Expand Down Expand Up @@ -71,3 +69,4 @@ switch( STYLESHEETPATH ) {
}

define( 'STYLESHEETPATH', 'something' );
const STYLESHEETPATH = 'something';
11 changes: 6 additions & 5 deletions WordPress/Tests/WP/DiscouragedConstantsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function getErrorList() {
*/
public function getWarningList() {
return array(
50 => 1,
51 => 1,
52 => 1,
53 => 1,
54 => 1,
Expand All @@ -45,13 +47,12 @@ public function getWarningList() {
59 => 1,
60 => 1,
61 => 1,
62 => 1,
63 => 1,
65 => 1,
64 => 1,
66 => 1,
68 => 1,
69 => 1,
73 => 1,
67 => 1,
71 => 1,
72 => 1,
);
}

Expand Down

0 comments on commit cc8e2e9

Please sign in to comment.