Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
ac0bbbb
DELME: temporally allow PHPCS 4.x tests to fail while they are not fixed
rodrigoprimo Aug 5, 2025
8a5da08
Tests: update to allow for running the tests on PHPCS 4.x
rodrigoprimo Aug 4, 2025
a56f78a
PR 2618 Security/EscapeOutput: add tests for namespaced names
rodrigoprimo Aug 21, 2025
276dcf5
PR 2579 ConstantsHelper::is_use_of_global_constant(): fix false posit…
rodrigoprimo Aug 15, 2025
2efc349
PR 2617 WP/AlternativeFunctions: add tests for namespaced names
rodrigoprimo Aug 21, 2025
eb1342d
PR 2633 CodeAnalysis/EscapedNotTranslated: add tests for namespaced n…
rodrigoprimo Oct 16, 2025
7446c1b
PR 2633 DateTime/CurrentTimeTimestamp: add tests for namespaced names
rodrigoprimo Oct 16, 2025
467cbd4
PR 2633 NamingConventions/ValidPostTypeSlug: add tests for namespaced…
rodrigoprimo Oct 16, 2025
a8dffd7
PR 2633 PHP/IniSet: add tests for namespaced names
rodrigoprimo Oct 16, 2025
a3058e4
PR 2633 Security/PluginMenuSlug: add tests for namespaced names
rodrigoprimo Oct 16, 2025
c311efe
PR 2633 PHP/PregQuoteDelimiter: add tests for namespaced names
rodrigoprimo Oct 16, 2025
94c99e8
PR 2633 PHP/StrictInArray: add tests for namespaced names
rodrigoprimo Oct 16, 2025
1d3fd6d
PR 2633 WP/Capabilities: move syntax error test to its own file
rodrigoprimo Oct 16, 2025
979179f
PR 2633 WP/Capabilities: add tests for namespaced names
rodrigoprimo Oct 16, 2025
b820889
PR 2633 WP/DeprecatedParameters: add tests for namespaced names
rodrigoprimo Oct 16, 2025
e8ce263
PR 2633 WP/DeprecatedParameterValues: rename test case file
rodrigoprimo Oct 16, 2025
bb05bab
PR 2633 WP/DeprecatedParameterValues: move syntax error test to its o…
rodrigoprimo Oct 16, 2025
5b56ad6
PR 2633 WP/DeprecatedParameterValues: add tests for namespaced names
rodrigoprimo Oct 16, 2025
b76a13e
PR 2633 WP/GetMetaSingle: add tests for namespaced names
rodrigoprimo Oct 16, 2025
554a0c4
PR 2633 WP/I18n: add tests for namespaced names
rodrigoprimo Oct 16, 2025
d1b83b2
PR 2643 WP/DeprecatedClasses: rename test case file
rodrigoprimo Nov 6, 2025
d9b07b9
PR 2643 WP/DeprecatedClasses: add tests for namespaced names
rodrigoprimo Nov 6, 2025
4f4f702
PR 2643 WP/ClassNameCase: add tests for namespaced names
rodrigoprimo Nov 6, 2025
9520851
DB/DirectDatabaseQuery: update for PHPCS 4.0
rodrigoprimo Aug 7, 2025
61f1e7a
WP/GlobalVariablesOverride: update for PHPCS 4.0
rodrigoprimo Aug 12, 2025
c98a12c
Security/NonceVerification: update for PHPCS 4.0
rodrigoprimo Aug 12, 2025
1d018ee
NamingConventions/ValidHookName: update for PHPCS 4.0
rodrigoprimo Aug 13, 2025
09209a7
AbstractFunctionRestrictions: update for PHPCS 4.0
rodrigoprimo Aug 13, 2025
5414158
NamingConventions/PrefixAllGlobals: update for PHPCS 4.0
rodrigoprimo Aug 13, 2025
137333a
AbstractClassRestrictions: update for PHPCS 4.0
rodrigoprimo Aug 13, 2025
d4c964c
PHP/NoSilencedErrors: update for PHPCS 4.0
rodrigoprimo Aug 13, 2025
ecfa4c8
PR 2626 ContextHelper::is_in_function_call(): add basic tests
rodrigoprimo Aug 14, 2025
313ac7b
ContextHelper::is_in_function_call(): update for PHPCS 4.0
rodrigoprimo Aug 14, 2025
afd4d4e
WP/CronInterval: update for PHPCS 4.0
rodrigoprimo Aug 14, 2025
ede01e0
ValidationHelper::is_validated(): update for PHPCS 4.0
rodrigoprimo Aug 14, 2025
9b4f1e2
ConstantsHelper::is_use_of_global_constant(): update for PHPCS 4.0
rodrigoprimo Aug 15, 2025
d05a952
WP/DiscouragedConstants: update for PHPCS 4.0
rodrigoprimo Aug 18, 2025
315f615
Security/EscapeOutput: update for PHPCS 4.0
rodrigoprimo Aug 19, 2025
bafdd19
DateTime/CurrentTimeTimestamp: update for PHPCS 4.0
rodrigoprimo Oct 20, 2025
5e178d6
CHECK Utils/I18nTextDomainFixer: update for PHPCS 4.0
rodrigoprimo Oct 21, 2025
10d46c7
Tests: fix testStdIn() methods to work with PHPCS 4.0
rodrigoprimo Oct 21, 2025
2d530c8
WIP ConstantHelper::is_use_of_global_constant(): add basic tests
rodrigoprimo Aug 15, 2025
f75ce3e
WIP ContextHelper::is_in_isset_or_empty(): add basic tests
rodrigoprimo Sep 24, 2025
b2d4177
WIP ContextHelper::is_in_array_comparison(): add basic tests
rodrigoprimo Sep 30, 2025
b0f3459
WIP ContextHelper::is_in_type_test(): add basic tests
rodrigoprimo Sep 30, 2025
25efeb2
WIP ContextHelper::is_token_namespaced(): add basic tests
rodrigoprimo Aug 18, 2025
ae903e8
WIP ContextHelper::is_token_namespaced(): update for PHPCS 4.0
rodrigoprimo Aug 18, 2025
ee4eff4
WIP SanitizationHelperTrait: add basic tests
rodrigoprimo Nov 5, 2025
8185e35
WIP ValidationHelper::is_validated(): add basic tests
rodrigoprimo Nov 5, 2025
eb41070
WPDBTrait::is_wpdb_method_call(): improve docblock description
rodrigoprimo Oct 21, 2025
28a7c8d
WIP WPDBTrait::is_wpdb_method_call(): add basic tests
rodrigoprimo Oct 2, 2025
fd92aab
WIP WPDBTrait::is_wpdb_method_call(): fix false positives for static …
rodrigoprimo Oct 28, 2025
bcc4c09
CHECK WPDBTrait::is_wpdb_method_call(): update for PHPCS 4.0
rodrigoprimo Aug 7, 2025
1ef5e53
ContextHelper::is_in_array_comparison(): update for PHPCS 4.0
rodrigoprimo Oct 28, 2025
0bb8135
WP/EnqueuedResourceParameters: add tests for namespaced names
rodrigoprimo Oct 15, 2025
a9ac920
DB/PreparedSQL: move intentional syntax error test to its own file
rodrigoprimo Aug 7, 2025
c151eae
DB/PreparedSQL: add tests for namespaced names
rodrigoprimo Aug 21, 2025
15ab301
DB/PreparedSQL: update for PHPCS 4.0
rodrigoprimo Aug 7, 2025
385a315
DB/PreparedSQLPlaceholders: add namespaced tests
rodrigoprimo Aug 21, 2025
1440d86
DB/PreparedSQLPlaceholders: fix false negatives for namespaced and me…
rodrigoprimo Nov 4, 2025
33ceded
DB/PreparedSQLPlaceholders: update for PHPCS 4.0
rodrigoprimo Aug 7, 2025
bf88d18
Security/ValidatedSanitizedInput: add tests for namespaced names
rodrigoprimo Nov 5, 2025
c38330e
Security/ValidatedSanitizedInput: updates for PHPCS 4.0
rodrigoprimo Dec 4, 2025
fc374e7
WP/CapitalPDangit: add tests for namespaced names
rodrigoprimo Nov 6, 2025
b9d0c49
ArrayWalkingFunctionsHelper::get_callback_parameter(): update for PHP…
rodrigoprimo Dec 4, 2025
1726154
SanitizationHelperTrait: update tests for PHPCS 4.0
rodrigoprimo Dec 4, 2025
169631e
WIP SanitizationHelperTrait: update for PHPCS 4.0
rodrigoprimo Nov 5, 2025
b5930a4
WIP WPHookHelper::get_hook_name_param(): add basic tests
rodrigoprimo Dec 4, 2025
a546bb6
WIP ArrayWalkingFunctionsHelper: add basic tests
rodrigoprimo Dec 4, 2025
f1d258c
WIP EscapingFunctionsTrait: add basic tests
rodrigoprimo Dec 4, 2025
74e826e
WIP FormattingFunctionsHelper::is_formatting_function(): add basic tests
rodrigoprimo Dec 4, 2025
2030d78
WIP PrintingFunctionsTrait::is_printing_function(): add basic tests
rodrigoprimo Dec 4, 2025
b85e9bd
WIP UnslashingFunctionsHelper::is_unslashing_function(): add basic tests
rodrigoprimo Dec 4, 2025
e948340
EscapingFunctionsTrait: update for PHPCS 4.0
rodrigoprimo Dec 4, 2025
0cbc1df
FormattingFunctionsHelper::is_formatting_function(): update for PHPCS…
rodrigoprimo Dec 4, 2025
33cab61
PrintingFunctionsTrait::is_printing_function(): update for PHPCS 4.0
rodrigoprimo Dec 4, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions .github/workflows/quicktest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,25 @@ jobs:
if: ${{ matrix.dependencies == 'stable' }}
run: composer lint

- name: Run the unit tests without code coverage
if: ${{ github.repository_owner != 'WordPress' || github.ref_name != 'develop' }}
run: composer run-tests
- name: Grab PHPCS version
id: phpcs_version
run: echo "VERSION=$(vendor/bin/phpcs --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> "$GITHUB_OUTPUT"

- name: Run the unit tests with code coverage
if: ${{ github.repository_owner == 'WordPress' && github.ref_name == 'develop' }}
run: composer coverage
- name: Run the unit tests without code coverage (PHPCS 3.x)
if: ${{ (github.repository_owner != 'WordPress' || github.ref_name != 'develop') && startsWith( steps.phpcs_version.outputs.VERSION, '3.' ) }}
run: composer run-tests-phpcs3

- name: Run the unit tests without code coverage (PHPCS 4.x)
if: ${{ (github.repository_owner != 'WordPress' || github.ref_name != 'develop') && startsWith( steps.phpcs_version.outputs.VERSION, '4.' ) }}
run: composer run-tests-phpcs4

- name: Run the unit tests with code coverage (PHPCS 3.x)
if: ${{ github.repository_owner == 'WordPress' && github.ref_name == 'develop' && startsWith( steps.phpcs_version.outputs.VERSION, '3.' ) }}
run: composer coverage-phpcs3

- name: Run the unit tests with code coverage (PHPCS 4.x)
if: ${{ github.repository_owner == 'WordPress' && github.ref_name == 'develop' && startsWith( steps.phpcs_version.outputs.VERSION, '4.' ) }}
run: composer coverage-phpcs4

- name: Send coverage report to Codecov
if: ${{ success() && github.repository_owner == 'WordPress' && github.ref_name == 'develop' }}
Expand Down
58 changes: 44 additions & 14 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ concurrency:
cancel-in-progress: true

env:
PHPCS_DEV: '3.x-dev'
PHPCS_3_DEV: '3.x-dev'
PHPCS_4_DEV: '4.x-dev'
UTILS_DEV: 'dev-develop'
EXTRA_DEV: 'dev-develop'

Expand Down Expand Up @@ -52,25 +53,38 @@ jobs:

# Test against dev versions of all dependencies with select PHP versions for early detection of issues.
- php: '7.2'
dependencies: 'dev'
dependencies: 'phpcs-3-dev'
extensions: ''
coverage: false
- php: '7.2'
dependencies: 'phpcs-4-dev'
extensions: ''
coverage: false
- php: '8.1'
dependencies: 'dev'
dependencies: 'phpcs-3-dev'
extensions: ''
coverage: false
- php: '8.1'
dependencies: 'phpcs-4-dev'
extensions: ''
coverage: false
- php: '8.4'
dependencies: 'phpcs-3-dev'
extensions: ''
coverage: false
- php: '8.4'
dependencies: 'dev'
dependencies: 'phpcs-4-dev'
extensions: ''
coverage: false


# Add extra build to test against PHPCS 4.
#- php: '7.4'
# dependencies: '4.0.x-dev as 3.99.99'

name: PHP ${{ matrix.php }} on PHPCS ${{ matrix.dependencies }}

continue-on-error: ${{ matrix.php == '8.5' }}
continue-on-error: ${{ matrix.php == '8.5' || matrix.dependencies == 'phpcs-4-dev' }}

steps:
- name: Checkout repository
Expand All @@ -83,7 +97,7 @@ jobs:
- name: Setup ini config
id: set_ini
run: |
if [ "${{ matrix.dependencies }}" != "dev" ]; then
if [ "${{ matrix.dependencies }}" != "phpcs-3-dev" ] && [ "${{ matrix.dependencies }}" != "phpcs-4-dev" ]; then
echo 'PHP_INI=error_reporting=E_ALL & ~E_DEPRECATED, display_errors=On, display_startup_errors=On' >> "$GITHUB_OUTPUT"
else
echo 'PHP_INI=error_reporting=-1, display_errors=On, display_startup_errors=On' >> "$GITHUB_OUTPUT"
Expand All @@ -97,11 +111,15 @@ jobs:
coverage: ${{ matrix.coverage && 'xdebug' || 'none' }}
tools: cs2pr

# Remove PHPCompatibility as it would (for now) prevent the tests from being able to run against PHPCS 4.x.
- name: 'Composer: remove PHPCompatibility'
run: composer remove --dev phpcompatibility/php-compatibility --no-update --no-interaction

- name: "Composer: set PHPCS dependencies for tests (dev)"
if: ${{ matrix.dependencies == 'dev' }}
if: ${{ matrix.dependencies == 'phpcs-3-dev' || matrix.dependencies == 'phpcs-4-dev' }}
run: >
composer require --no-update --no-scripts --no-interaction
squizlabs/php_codesniffer:"${{ env.PHPCS_DEV }}"
squizlabs/php_codesniffer:"${{ matrix.dependencies == 'phpcs-3-dev' && env.PHPCS_3_DEV || env.PHPCS_4_DEV }}"
phpcsstandards/phpcsutils:"${{ env.UTILS_DEV }}"
phpcsstandards/phpcsextra:"${{ env.EXTRA_DEV }}"

Expand All @@ -127,13 +145,25 @@ jobs:
if: ${{ matrix.dependencies == 'stable' }}
run: composer lint -- --checkstyle | cs2pr

- name: Run the unit tests without code coverage
if: ${{ matrix.coverage == false || github.repository_owner != 'WordPress' }}
run: composer run-tests
- name: Grab PHPCS version
id: phpcs_version
run: echo "VERSION=$(vendor/bin/phpcs --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> "$GITHUB_OUTPUT"

- name: Run the unit tests without code coverage (PHPCS 3.x)
if: ${{ startsWith( steps.phpcs_version.outputs.VERSION, '3.' ) && (matrix.coverage == false || github.repository_owner != 'WordPress') }}
run: composer run-tests-phpcs3

- name: Run the unit tests without code coverage (PHPCS 4.x)
if: ${{ startsWith( steps.phpcs_version.outputs.VERSION, '4.' ) && (matrix.coverage == false || github.repository_owner != 'WordPress') }}
run: composer run-tests-phpcs4

- name: Run the unit tests with code coverage (PHPCS 3.x)
if: ${{ startsWith( steps.phpcs_version.outputs.VERSION, '3.' ) && matrix.coverage == true && github.repository_owner == 'WordPress' }}
run: composer coverage-phpcs3

- name: Run the unit tests with code coverage
if: ${{ matrix.coverage == true && github.repository_owner == 'WordPress' }}
run: composer coverage
- name: Run the unit tests with code coverage (PHPCS 4.x)
if: ${{ startsWith( steps.phpcs_version.outputs.VERSION, '4.' ) && matrix.coverage == true && github.repository_owner == 'WordPress' }}
run: composer coverage-phpcs4

- name: Send coverage report to Codecov
if: ${{ success() && matrix.coverage == true && github.repository_owner == 'WordPress' }}
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ At this moment, WordPressCS offer the following tools:
- `old_text_domain`: an array with one or more (old) text domain names which need to be replaced;
- `new_text_domain`: the correct (new) text domain as a string.

**Note**: When using PHP_CodeSniffer 4.0+, this sniff will fix the text domain everywhere for plugins, but it will **not** fix the text domain in the CSS file header for themes, as PHP_CodeSniffer 4.0+ does not support scanning CSS files. Manual intervention will be required to update the `Text Domain` header in the theme's CSS file.


## Contributing

Expand Down
10 changes: 10 additions & 0 deletions Tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@
die( 1 );
}

// Alias the PHPCS 3.x test case to the PHPCS 4.x name.
if ( class_exists( 'PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest' ) === true
&& class_exists( 'PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase' ) === false
) {
class_alias(
'PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest',
'PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase'
);
}

/*
* Set the PHPCS_IGNORE_TEST environment variable to ignore tests from other standards.
*/
Expand Down
14 changes: 11 additions & 3 deletions WordPress/AbstractClassRestrictionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace WordPressCS\WordPress;

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\BackCompat\Helper;
use PHPCSUtils\Tokens\Collections;
use PHPCSUtils\Utils\GetTokensAsString;
use PHPCSUtils\Utils\Namespaces;
Expand Down Expand Up @@ -145,13 +146,20 @@ public function is_targetted_token( $stackPtr ) {

if ( \T_DOUBLE_COLON === $token['code'] ) {
$nameEnd = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true );
if ( \T_STRING !== $this->tokens[ $nameEnd ]['code'] ) {
if ( isset( Collections::nameTokens()[ $this->tokens[ $nameEnd ]['code'] ] ) === false ) {
// Hierarchy keyword or object stored in variable.
return false;
}

$nameStart = ( $this->phpcsFile->findPrevious( Collections::namespacedNameTokens(), ( $nameEnd - 1 ), null, true ) + 1 );
$classname = GetTokensAsString::noEmpties( $this->phpcsFile, $nameStart, $nameEnd );
$classname = $this->tokens[ $nameEnd ]['content'];
$nameStart = $nameEnd;

if ( \version_compare( Helper::getVersion(), '3.99.99', '<=' ) === true ) {
// For PHPCS 3.x, a namespaced class name is split over multiple tokens so it is necessary to combine then to get the class name.
$nameStart = ( $this->phpcsFile->findPrevious( Collections::namespacedNameTokens(), ( $nameEnd - 1 ), null, true ) + 1 );
$classname = GetTokensAsString::noEmpties( $this->phpcsFile, $nameStart, $nameEnd );
}

$classname = $this->get_namespaced_classname( $classname, ( $nameStart - 1 ) );
}

Expand Down
3 changes: 2 additions & 1 deletion WordPress/AbstractFunctionParameterSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public function getGroups() {
* @param int $stackPtr The position of the current token in the stack.
* @param string $group_name The name of the group which was matched.
* @param string $matched_content The token content (function name) which was matched
* in lowercase.
* in lowercase. For T_NAME_FULLY_QUALIFIED tokens,
* the leading backslash is removed.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
Expand Down
22 changes: 18 additions & 4 deletions WordPress/AbstractFunctionRestrictionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public function register() {

return array(
\T_STRING,
\T_NAME_FULLY_QUALIFIED,
);
}

Expand Down Expand Up @@ -201,10 +202,16 @@ public function process_token( $stackPtr ) {
return;
}

// Preliminary check. If the content of the T_STRING is not one of the functions we're
$content = $this->tokens[ $stackPtr ]['content'];

if ( \T_NAME_FULLY_QUALIFIED === $this->tokens[ $stackPtr ]['code'] ) {
$content = \ltrim( $content, '\\' );
}

// Preliminary check. If the content of the name token is not one of the functions we're
// looking for, we can bow out before doing the heavy lifting of checking whether
// this is a function call.
if ( preg_match( $this->prelim_check_regex, $this->tokens[ $stackPtr ]['content'] ) !== 1 ) {
if ( preg_match( $this->prelim_check_regex, $content ) !== 1 ) {
return;
}

Expand Down Expand Up @@ -286,7 +293,13 @@ public function is_targetted_token( $stackPtr ) {
* normal file processing.
*/
public function check_for_matches( $stackPtr ) {
$token_content = strtolower( $this->tokens[ $stackPtr ]['content'] );
$content = $this->tokens[ $stackPtr ]['content'];

if ( \T_NAME_FULLY_QUALIFIED === $this->tokens[ $stackPtr ]['code'] ) {
$content = \ltrim( $content, '\\' );
}

$token_content = strtolower( $content );
$skip_to = array();

foreach ( $this->groups as $groupName => $group ) {
Expand Down Expand Up @@ -319,7 +332,8 @@ public function check_for_matches( $stackPtr ) {
* @param int $stackPtr The position of the current token in the stack.
* @param string $group_name The name of the group which was matched.
* @param string $matched_content The token content (function name) which was matched
* in lowercase.
* in lowercase. For T_NAME_FULLY_QUALIFIED tokens,
* the leading backslash is removed.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
Expand Down
7 changes: 6 additions & 1 deletion WordPress/Helpers/ArrayWalkingFunctionsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static function get_functions() {
* @return bool
*/
public static function is_array_walking_function( $functionName ) {
return isset( self::$arrayWalkingFunctions[ strtolower( $functionName ) ] );
return isset( self::$arrayWalkingFunctions[ strtolower( ltrim( $functionName, '\\' ) ) ] );
}

/**
Expand All @@ -94,6 +94,11 @@ public static function get_callback_parameter( File $phpcsFile, $stackPtr ) {
}

$functionName = strtolower( $tokens[ $stackPtr ]['content'] );

if ( \T_NAME_FULLY_QUALIFIED === $tokens[ $stackPtr ]['code'] ) {
$functionName = ltrim( $functionName, '\\' );
}

if ( isset( self::$arrayWalkingFunctions[ $functionName ] ) === false ) {
return false;
}
Expand Down
18 changes: 15 additions & 3 deletions WordPress/Helpers/ConstantsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@
final class ConstantsHelper {

/**
* Determine whether an arbitrary T_STRING token is the use of a global constant.
* Determine whether an arbitrary T_STRING or T_NAME_FULLY_QUALIFIED token is the use of a
* global constant.
*
* @since 1.0.0
* @since 3.0.0 - Moved from the Sniff class to this class.
* - The method was changed to be `static`.
* - The `$phpcsFile` parameter was added.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the T_STRING token.
* @param int $stackPtr The position of the T_STRING or T_NAME_FULLY_QUALIFIED token.
*
* @return bool
*/
Expand All @@ -55,7 +56,9 @@ public static function is_use_of_global_constant( File $phpcsFile, $stackPtr ) {
}

// Is this one of the tokens this function handles ?
if ( \T_STRING !== $tokens[ $stackPtr ]['code'] ) {
if ( \T_STRING !== $tokens[ $stackPtr ]['code']
&& \T_NAME_FULLY_QUALIFIED !== $tokens[ $stackPtr ]['code']
) {
return false;
}

Expand All @@ -79,6 +82,7 @@ public static function is_use_of_global_constant( File $phpcsFile, $stackPtr ) {
\T_INSTANCEOF => true,
\T_INSTEADOF => true,
\T_GOTO => true,
\T_AS => true,
);
$tokens_to_ignore += Tokens::$ooScopeTokens;
$tokens_to_ignore += Collections::objectOperators();
Expand All @@ -90,6 +94,14 @@ public static function is_use_of_global_constant( File $phpcsFile, $stackPtr ) {
return false;
}

// If the token is a fully qualified name, ensure it does not include a namespace path.
if ( \T_NAME_FULLY_QUALIFIED === $tokens[ $stackPtr ]['code'] ) {
$trimmed = \ltrim( $tokens[ $stackPtr ]['content'], '\\' );
if ( \strpos( $trimmed, '\\' ) !== false ) {
return false;
}
}

if ( ContextHelper::is_token_namespaced( $phpcsFile, $stackPtr ) === true ) {
// Namespaced constant of the same name.
return false;
Expand Down
Loading
Loading