Skip to content

Commit 6edffc8

Browse files
Create the HookPriorityLimit Sniff (#75)
* Create the HookPriorityLimit Sniff It checks if `PHP_INT_MAX` or `PHP_INT_MIN` are added used for `add_filter` or `add_action`. * Convert errors to warnings * Update documentation * Rename the sniff in `HookPrioritySniff` * Simplify the code Use the utility method `PassedParameters::getParameter()` in order to get rid of a lot of custom code. * Update the sniff name in the readme
1 parent ecb838a commit 6edffc8

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the "php-coding-standards" package.
5+
*
6+
* Copyright (c) 2023 Inpsyde GmbH
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
27+
declare(strict_types=1);
28+
29+
namespace Inpsyde\Sniffs\CodeQuality;
30+
31+
use PHP_CodeSniffer\Files\File;
32+
use PHP_CodeSniffer\Sniffs\Sniff;
33+
use PHPCSUtils\Utils\PassedParameters;
34+
35+
class HookPrioritySniff implements Sniff
36+
{
37+
/**
38+
* @return list<int>
39+
*/
40+
public function register(): array
41+
{
42+
return [T_STRING];
43+
}
44+
45+
/**
46+
* @param File $phpcsFile
47+
* @param int $stackPtr
48+
* @return void
49+
*
50+
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration
51+
*/
52+
public function process(File $phpcsFile, $stackPtr): void
53+
{
54+
// phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration
55+
56+
$tokens = $phpcsFile->getTokens();
57+
$functionName = $tokens[$stackPtr]['content'] ?? '';
58+
59+
if ($functionName !== 'add_filter' && $functionName !== 'add_action') {
60+
return;
61+
}
62+
63+
$parameter = PassedParameters::getParameter($phpcsFile, $stackPtr, 3, 'priority');
64+
$parameter = $parameter['clean'] ?? '';
65+
66+
if ($parameter === 'PHP_INT_MAX' && $functionName === 'add_filter') {
67+
$phpcsFile->addWarning(
68+
'Found PHP_INT_MAX used as hook priority. '
69+
. 'This makes it hard, if not impossible to reliably filter the callback output.',
70+
$stackPtr,
71+
'HookPriority'
72+
);
73+
return;
74+
}
75+
76+
if ($parameter === 'PHP_INT_MIN') {
77+
$phpcsFile->addWarning(
78+
'Found PHP_INT_MIN used as hook priority. '
79+
. 'This makes it hard, if not impossible to reliably remove the callback.',
80+
$stackPtr,
81+
'HookPriority'
82+
);
83+
}
84+
}
85+
}

inpsyde-custom-sniffs.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ Analyses closures attached to WordPress hooks ensuring that:
172172

173173
This sniff has no available configuration.
174174

175+
-----
176+
## Inpsyde.CodeQuality.HookPriority
177+
178+
Raises a warning if:
179+
180+
- `PHP_INT_MAX` is used for `add_filter`.
181+
- `PHP_INT_MIN` is used for `add_action` or `add_filter`.
175182
-----
176183

177184
## Inpsyde.CodeQuality.LineLength

tests/fixtures/hook-priority.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace Inpsyde\CodingStandard\Tests\Fixtures;
4+
5+
// @phpcsSniff CodeQuality.HookPriority
6+
add_action('foo', 'bar');
7+
8+
add_filter('foo', [ArrayObject::class, 'meh']);
9+
10+
add_filter(
11+
'foo',
12+
function() {
13+
return true;
14+
}
15+
);
16+
17+
add_action(
18+
'hook',
19+
function() {
20+
echo 'hello';
21+
}
22+
);
23+
24+
add_action('foo', 'bar', 10);
25+
add_action('foo', [ArrayObject::class, 'meh'], -500);
26+
27+
add_filter('foo', 'bar', 500);
28+
add_filter('foo', [ArrayObject::class, 'meh'], 20);
29+
30+
add_filter(
31+
'foo',
32+
function() {
33+
return true;
34+
},
35+
-500
36+
);
37+
38+
add_action(
39+
'hook',
40+
function() {
41+
echo 'hello';
42+
},
43+
9999
44+
);
45+
46+
// @phpcsWarningCodeOnNextLine HookPriority
47+
add_filter('foo', 'foo', PHP_INT_MIN);
48+
// @phpcsWarningCodeOnNextLine HookPriority
49+
add_filter('foo', [ArrayObject::class, 'meh'], PHP_INT_MIN);
50+
// @phpcsWarningCodeOnNextLine HookPriority
51+
add_filter(
52+
'foo',
53+
function() {
54+
return true;
55+
},
56+
PHP_INT_MIN
57+
);
58+
59+
// @phpcsWarningCodeOnNextLine HookPriority
60+
add_action('foo', 'foo', PHP_INT_MIN);
61+
// @phpcsWarningCodeOnNextLine HookPriority
62+
add_action('foo', [ArrayObject::class, 'meh'], PHP_INT_MIN);
63+
// @phpcsWarningCodeOnNextLine HookPriority
64+
add_action(
65+
'hook',
66+
function() {
67+
echo 'hello';
68+
},
69+
PHP_INT_MIN
70+
);
71+
72+
// @phpcsWarningCodeOnNextLine HookPriority
73+
add_filter('foo', 'foo', PHP_INT_MAX);
74+
// @phpcsWarningCodeOnNextLine HookPriority
75+
add_filter('foo', [ArrayObject::class, 'meh'], PHP_INT_MAX);
76+
// @phpcsWarningCodeOnNextLine HookPriority
77+
add_filter('foo',
78+
function() {
79+
return true;
80+
},
81+
PHP_INT_MAX
82+
);
83+
84+
add_action('foo', 'foo', PHP_INT_MAX);
85+
add_action('foo', [ArrayObject::class, 'meh'], PHP_INT_MAX);
86+
add_action(
87+
'foo',
88+
function() {
89+
return true;
90+
},
91+
PHP_INT_MAX
92+
);

0 commit comments

Comments
 (0)