Skip to content

Commit

Permalink
Simplify the code
Browse files Browse the repository at this point in the history
Use the utility method `PassedParameters::getParameter()` in order to get rid of a lot of custom code.
  • Loading branch information
antonioeatgoat committed Oct 17, 2023
1 parent b9cdc6c commit d546cd9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 36 deletions.
38 changes: 11 additions & 27 deletions Inpsyde/Sniffs/CodeQuality/HookPrioritySniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHPCSUtils\Utils\PassedParameters;

class HookPrioritySniff implements Sniff
{
Expand Down Expand Up @@ -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;
}
}
18 changes: 9 additions & 9 deletions tests/fixtures/hook-priority.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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;
Expand Down

0 comments on commit d546cd9

Please sign in to comment.