Skip to content

Commit

Permalink
Don't check param type for function w/ @wp-hook doc
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed Feb 5, 2018
1 parent b107246 commit e7ba80b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
51 changes: 51 additions & 0 deletions Inpsyde/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions Inpsyde/Sniffs/CodeQuality/ArgumentTypeDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
}
}
Expand Down

0 comments on commit e7ba80b

Please sign in to comment.