diff --git a/Inpsyde/Helpers.php b/Inpsyde/Helpers.php index 472da6e..de07329 100644 --- a/Inpsyde/Helpers.php +++ b/Inpsyde/Helpers.php @@ -217,6 +217,57 @@ public static function isHookClosure( return in_array(($tokens[$functionCall]['content'] ?? ''), $actions, true); } + /** + * @param File $file + * @param int $functionPosition + * @return bool + */ + public static function isHookFunction(File $file, int $functionPosition): bool + { + $tokens = $file->getTokens(); + if (($tokens[$functionPosition]['code'] ?? '') !== T_FUNCTION) { + return false; + } + + $findDocEnd = $file->findPrevious( + [T_WHITESPACE, T_FINAL, T_PUBLIC, T_ABSTRACT], + $functionPosition - 1, + null, + true, + null, + true + ); + + if (!$findDocEnd || ($tokens[$findDocEnd]['code'] ?? '') !== T_DOC_COMMENT_CLOSE_TAG) { + return false; + } + + $findDocStart = $file->findPrevious( + [T_DOC_COMMENT_OPEN_TAG], + $findDocEnd, + null, + false, + null, + true + ); + + if (!$findDocStart + || ($tokens[$findDocStart]['comment_closer'] ?? '') !== $findDocEnd + ) { + return false; + } + + $docTokens = self::filterTokensByType($findDocStart, $findDocEnd, $file, T_DOC_COMMENT_TAG); + + foreach ($docTokens as $token) { + if ($token['content'] === '@wp-hook') { + return true; + } + } + + return false; + } + /** * @param File $file * @param int $functionPosition diff --git a/Inpsyde/Sniffs/CodeQuality/ArgumentTypeDeclarationSniff.php b/Inpsyde/Sniffs/CodeQuality/ArgumentTypeDeclarationSniff.php index beb8931..5a971c2 100644 --- a/Inpsyde/Sniffs/CodeQuality/ArgumentTypeDeclarationSniff.php +++ b/Inpsyde/Sniffs/CodeQuality/ArgumentTypeDeclarationSniff.php @@ -41,6 +41,10 @@ public function register() */ public function process(File $file, $position) { + if (Helpers::isHookClosure($file, $position) || Helpers::isHookFunction($file, $position)) { + return; + } + $tokens = $file->getTokens(); $paramsStart = $tokens[$position]['parenthesis_opener'] ?? 0; @@ -61,11 +65,7 @@ public function process(File $file, $position) ); $type = $tokens[$typePosition] ?? null; - if ($type && in_array($type['code'], self::TYPE_CODES, true)) { - continue; - } - - if (!Helpers::isHookClosure($file, $position)) { + if ($type && !in_array($type['code'], self::TYPE_CODES, true)) { $file->addWarning('Argument type is missing', $position, 'NoArgumentType'); } }