Skip to content

Commit e7ba80b

Browse files
committed
Don't check param type for function w/ @wp-hook doc
1 parent b107246 commit e7ba80b

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

Inpsyde/Helpers.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,57 @@ public static function isHookClosure(
217217
return in_array(($tokens[$functionCall]['content'] ?? ''), $actions, true);
218218
}
219219

220+
/**
221+
* @param File $file
222+
* @param int $functionPosition
223+
* @return bool
224+
*/
225+
public static function isHookFunction(File $file, int $functionPosition): bool
226+
{
227+
$tokens = $file->getTokens();
228+
if (($tokens[$functionPosition]['code'] ?? '') !== T_FUNCTION) {
229+
return false;
230+
}
231+
232+
$findDocEnd = $file->findPrevious(
233+
[T_WHITESPACE, T_FINAL, T_PUBLIC, T_ABSTRACT],
234+
$functionPosition - 1,
235+
null,
236+
true,
237+
null,
238+
true
239+
);
240+
241+
if (!$findDocEnd || ($tokens[$findDocEnd]['code'] ?? '') !== T_DOC_COMMENT_CLOSE_TAG) {
242+
return false;
243+
}
244+
245+
$findDocStart = $file->findPrevious(
246+
[T_DOC_COMMENT_OPEN_TAG],
247+
$findDocEnd,
248+
null,
249+
false,
250+
null,
251+
true
252+
);
253+
254+
if (!$findDocStart
255+
|| ($tokens[$findDocStart]['comment_closer'] ?? '') !== $findDocEnd
256+
) {
257+
return false;
258+
}
259+
260+
$docTokens = self::filterTokensByType($findDocStart, $findDocEnd, $file, T_DOC_COMMENT_TAG);
261+
262+
foreach ($docTokens as $token) {
263+
if ($token['content'] === '@wp-hook') {
264+
return true;
265+
}
266+
}
267+
268+
return false;
269+
}
270+
220271
/**
221272
* @param File $file
222273
* @param int $functionPosition

Inpsyde/Sniffs/CodeQuality/ArgumentTypeDeclarationSniff.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public function register()
4141
*/
4242
public function process(File $file, $position)
4343
{
44+
if (Helpers::isHookClosure($file, $position) || Helpers::isHookFunction($file, $position)) {
45+
return;
46+
}
47+
4448
$tokens = $file->getTokens();
4549

4650
$paramsStart = $tokens[$position]['parenthesis_opener'] ?? 0;
@@ -61,11 +65,7 @@ public function process(File $file, $position)
6165
);
6266

6367
$type = $tokens[$typePosition] ?? null;
64-
if ($type && in_array($type['code'], self::TYPE_CODES, true)) {
65-
continue;
66-
}
67-
68-
if (!Helpers::isHookClosure($file, $position)) {
68+
if ($type && !in_array($type['code'], self::TYPE_CODES, true)) {
6969
$file->addWarning('Argument type is missing', $position, 'NoArgumentType');
7070
}
7171
}

0 commit comments

Comments
 (0)