Skip to content

Commit c059096

Browse files
committed
WP/DiscouragedConstants: update for PHPCS 4.0
The tokenization of (namespaced) "names" has changed in PHP 8.0, and this new tokenization will come into effect for PHP_CodeSniffer as of version 4.0.0. This commit adds handling for the new tokenization when checking the name of a given constants to see if it is one of the discouraged constants and when checking if `define()` was called.
1 parent 4289bfc commit c059096

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

WordPress/Sniffs/WP/DiscouragedConstantsSniff.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ final class DiscouragedConstantsSniff extends AbstractFunctionParameterSniff {
7272
* normal file processing.
7373
*/
7474
public function process_token( $stackPtr ) {
75-
if ( isset( $this->target_functions[ strtolower( $this->tokens[ $stackPtr ]['content'] ) ] ) ) {
75+
$content_lc = strtolower( $this->tokens[ $stackPtr ]['content'] );
76+
77+
if ( \T_NAME_FULLY_QUALIFIED === $this->tokens[ $stackPtr ]['code'] ) {
78+
$content_lc = \ltrim( $content_lc, '\\' );
79+
}
80+
81+
if ( isset( $this->target_functions[ $content_lc ] ) ) {
7682
// Disallow excluding function groups for this sniff.
7783
$this->exclude = array();
7884

@@ -84,7 +90,8 @@ public function process_token( $stackPtr ) {
8490
}
8591

8692
/**
87-
* Process an arbitrary T_STRING token to determine whether it is one of the target constants.
93+
* Process an arbitrary T_STRING or T_NAME_FULLY_QUALIFIED token to determine whether it is one
94+
* of the target constants.
8895
*
8996
* @since 0.14.0
9097
*
@@ -95,6 +102,10 @@ public function process_token( $stackPtr ) {
95102
public function process_arbitrary_tstring( $stackPtr ) {
96103
$content = $this->tokens[ $stackPtr ]['content'];
97104

105+
if ( \T_NAME_FULLY_QUALIFIED === $this->tokens[ $stackPtr ]['code'] ) {
106+
$content = \ltrim( $content, '\\' );
107+
}
108+
98109
if ( ! isset( $this->discouraged_constants[ $content ] ) ) {
99110
return;
100111
}

0 commit comments

Comments
 (0)