From d546cd98a90c7428550aae63f8351a3c5bc893de Mon Sep 17 00:00:00 2001 From: Antonio Mangiacapra <11173091+antonioeatgoat@users.noreply.github.com> Date: Tue, 17 Oct 2023 13:09:07 +0200 Subject: [PATCH] Simplify the code Use the utility method `PassedParameters::getParameter()` in order to get rid of a lot of custom code. --- .../Sniffs/CodeQuality/HookPrioritySniff.php | 38 ++++++------------- tests/fixtures/hook-priority.php | 18 ++++----- 2 files changed, 20 insertions(+), 36 deletions(-) diff --git a/Inpsyde/Sniffs/CodeQuality/HookPrioritySniff.php b/Inpsyde/Sniffs/CodeQuality/HookPrioritySniff.php index a889690..946670a 100644 --- a/Inpsyde/Sniffs/CodeQuality/HookPrioritySniff.php +++ b/Inpsyde/Sniffs/CodeQuality/HookPrioritySniff.php @@ -30,6 +30,7 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; +use PHPCSUtils\Utils\PassedParameters; class HookPrioritySniff implements Sniff { @@ -59,43 +60,26 @@ public function process(File $phpcsFile, $stackPtr): void return; } - $parameters = $this->stringParameters($phpcsFile, $stackPtr + 1); + $parameter = PassedParameters::getParameter($phpcsFile, $stackPtr, 3, 'priority'); + $parameter = $parameter['clean'] ?? ''; - if (in_array('PHP_INT_MAX', $parameters, true) && $functionName === 'add_filter') { + if ($parameter === 'PHP_INT_MAX' && $functionName === 'add_filter') { $phpcsFile->addWarning( - 'PHP_INT_MAX applied as filter priority.', + 'Found PHP_INT_MAX used as hook priority. ' + . 'This makes it hard, if not impossible to reliably filter the callback output.', $stackPtr, - 'HookPriorityLimit' + 'HookPriority' ); return; } - if (in_array('PHP_INT_MIN', $parameters, true)) { + if ($parameter === 'PHP_INT_MIN') { $phpcsFile->addWarning( - 'PHP_INT_MIN applied as filter or action priority.', + 'Found PHP_INT_MIN used as hook priority. ' + . 'This makes it hard, if not impossible to reliably remove the callback.', $stackPtr, - 'HookPriorityLimit' + 'HookPriority' ); } } - - private function stringParameters(File $phpcsFile, int $position): array - { - $tokens = $phpcsFile->getTokens(); - - $openingParenthesis = $tokens[$position]['parenthesis_opener'] ?? 0; - $closingParenthesis = $tokens[$position]['parenthesis_closer'] ?? 0; - $stringParameters = []; - - for ($i = (int)$openingParenthesis + 1; $i < $closingParenthesis; $i++) { - $tokenCode = $tokens[$i]['code'] ?? 0; - if ($tokenCode !== T_STRING) { - continue; - } - - $stringParameters[] = $tokens[$i]['content'] ?? ''; - } - - return $stringParameters; - } } diff --git a/tests/fixtures/hook-priority.php b/tests/fixtures/hook-priority.php index f811018..72f297f 100644 --- a/tests/fixtures/hook-priority.php +++ b/tests/fixtures/hook-priority.php @@ -43,11 +43,11 @@ function() { 9999 ); -// @phpcsWarningCodeOnNextLine HookPriorityLimit +// @phpcsWarningCodeOnNextLine HookPriority add_filter('foo', 'foo', PHP_INT_MIN); -// @phpcsWarningCodeOnNextLine HookPriorityLimit +// @phpcsWarningCodeOnNextLine HookPriority add_filter('foo', [ArrayObject::class, 'meh'], PHP_INT_MIN); -// @phpcsWarningCodeOnNextLine HookPriorityLimit +// @phpcsWarningCodeOnNextLine HookPriority add_filter( 'foo', function() { @@ -56,11 +56,11 @@ function() { PHP_INT_MIN ); -// @phpcsWarningCodeOnNextLine HookPriorityLimit +// @phpcsWarningCodeOnNextLine HookPriority add_action('foo', 'foo', PHP_INT_MIN); -// @phpcsWarningCodeOnNextLine HookPriorityLimit +// @phpcsWarningCodeOnNextLine HookPriority add_action('foo', [ArrayObject::class, 'meh'], PHP_INT_MIN); -// @phpcsWarningCodeOnNextLine HookPriorityLimit +// @phpcsWarningCodeOnNextLine HookPriority add_action( 'hook', function() { @@ -69,11 +69,11 @@ function() { PHP_INT_MIN ); -// @phpcsWarningCodeOnNextLine HookPriorityLimit +// @phpcsWarningCodeOnNextLine HookPriority add_filter('foo', 'foo', PHP_INT_MAX); -// @phpcsWarningCodeOnNextLine HookPriorityLimit +// @phpcsWarningCodeOnNextLine HookPriority add_filter('foo', [ArrayObject::class, 'meh'], PHP_INT_MAX); -// @phpcsWarningCodeOnNextLine HookPriorityLimit +// @phpcsWarningCodeOnNextLine HookPriority add_filter('foo', function() { return true;