Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mixed return type for filter callbacks #131

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/HookCallbackRule.php
Expand Up @@ -19,6 +19,7 @@
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\VerbosityLevel;
use PHPStan\Type\VoidType;
use PHPStan\Type\MixedType;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\FuncCall>
Expand Down Expand Up @@ -206,8 +207,9 @@ protected function validateFilterReturnType(ParametersAcceptor $callbackAcceptor
{
$returnType = $callbackAcceptor->getReturnType();
$isVoidSuperType = $returnType->isSuperTypeOf(new VoidType());
$isMixedType = $returnType->equals(new MixedType());

if (! $isVoidSuperType->yes()) {
if ($isMixedType || $isVoidSuperType->no()) {
return;
}

Expand Down
5 changes: 5 additions & 0 deletions tests/data/hook-callback.php
Expand Up @@ -185,6 +185,7 @@
add_filter('filter', function($one = null, $two = null, $three = null) {
return 123;
});
add_filter('filter', 'return_mixed');

// Action callbacks must return void
add_action('action', function() {
Expand Down Expand Up @@ -248,6 +249,10 @@ function filter_variadic_typed( $one, ...$two ) : int {
return 123;
}

function return_mixed($value) : mixed {
return $value;
}

class TestInvokableTyped {
public function __invoke($one, $two) : int {
return 123;
Expand Down