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

Hook callback false positives for mixed return type #130

Merged
merged 12 commits into from Nov 12, 2022
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -56,7 +56,7 @@
"test:cs": "phpcs",
"test:cs:fix": "phpcbf",
"test:phpstan": "phpstan analyze",
"test:phpunit": "phpunit",
"test:phpunit": "phpunit -d memory_limit=1G",
johnbillion marked this conversation as resolved.
Show resolved Hide resolved
szepeviktor marked this conversation as resolved.
Show resolved Hide resolved
"test:syntax": "parallel-lint bootstrap.php src/ tests/"
}
}
8 changes: 8 additions & 0 deletions tests/HookCallbackRuleTest.php
Expand Up @@ -116,6 +116,14 @@ public function testRule(): void
'Filter callback return statement is missing.',
99,
],
[
'Action callback returns mixed but should not return anything.',
102,
],
[
'Action callback returns mixed but should not return anything.',
105,
],
]
);
}
Expand Down
38 changes: 38 additions & 0 deletions tests/data/hook-callback.php
Expand Up @@ -98,6 +98,12 @@
// Filter callback return statement is missing.
add_filter('filter', __NAMESPACE__ . '\\no_return_value_untyped');

// Action callback returns mixed but should not return anything.
add_action('action', __NAMESPACE__ . '\\return_value_mixed');

// Action callback returns mixed but should not return anything.
add_action('action', __NAMESPACE__ . '\\return_value_implicit_mixed');

/**
* Incorrect usage that's handled by PHPStan:
*
Expand Down Expand Up @@ -214,6 +220,11 @@

// Various callback types
add_filter('filter', '__return_false');
add_filter('filter', __NAMESPACE__ . '\\return_value_mixed');
add_filter('filter', __NAMESPACE__ . '\\return_value_implicit_mixed');
add_filter('filter', __NAMESPACE__ . '\\return_value_mixed_union');
add_filter('filter', __NAMESPACE__ . '\\return_value_documented');
add_filter('filter', __NAMESPACE__ . '\\return_value_untyped');
add_filter('filter', __NAMESPACE__ . '\\return_value_typed');
add_filter('filter', new TestInvokableTyped(), 10, 2);
add_filter('filter', [new TestClassTyped, 'foo']);
Expand All @@ -240,6 +251,33 @@ function return_value_typed() : int {

function no_return_value_untyped( $value ) {}

/**
* @return mixed
*/
function return_value_mixed() {
return 123;
}

function return_value_implicit_mixed( $value ) {
return $value;
}

/**
* Return type documented as a union that includes mixed.
johnbillion marked this conversation as resolved.
Show resolved Hide resolved
*
* @return int|mixed
johnbillion marked this conversation as resolved.
Show resolved Hide resolved
*/
function return_value_mixed_union() {
return 123;
}

/**
* @return int
*/
function return_value_documented() {
return 123;
}

function return_value_untyped() {
return 123;
}
Expand Down