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

filter_var_array with a mixed argument results in a fatal error #9178

Closed
daniel-calderini opened this issue Apr 12, 2023 · 3 comments · Fixed by phpstan/phpstan-src#2338
Closed

Comments

@daniel-calderini
Copy link

Bug report

Starting with version 1.10.12, the following code snippet creates a fatal error:

function filter(mixed $data): array
{
	return filter_var_array(
		$data,
		[
			'id' => [
				'filter' => FILTER_VALIDATE_INT,
				'flags' => FILTER_REQUIRE_SCALAR,
			],
		
		],
		true,
	);
}

https://phpstan.org/r/432b5663-9303-4742-969b-f4ac0a168e68

Using version 1.10.11 (or lower), or using array type for the $data argument fixes the fatal error:

function filter(array $data): array
{
	return filter_var_array(
		$data,
		[
			'id' => [
				'filter' => FILTER_VALIDATE_INT,
				'flags' => FILTER_REQUIRE_SCALAR,
			],
		
		],
		true,
	);
}

https://phpstan.org/r/4906b331-8a8d-45c3-860b-2d7039afa990

@daniel-calderini daniel-calderini changed the title filter_var_array with a mixed filter_var_array with a mixed argument Apr 12, 2023
@daniel-calderini daniel-calderini changed the title filter_var_array with a mixed argument filter_var_array with a mixed argument results in a fatal error Apr 12, 2023
@ondrejmirtes
Copy link
Member

/cc @zonuexe Please send a PR with a fix.

Personally I'd fix the code like this to make it safe and not crash

diff --git a/src/Type/Php/FilterVarArrayDynamicReturnTypeExtension.php b/src/Type/Php/FilterVarArrayDynamicReturnTypeExtension.php
index ac57eb12c..b2ca7f42c 100644
--- a/src/Type/Php/FilterVarArrayDynamicReturnTypeExtension.php
+++ b/src/Type/Php/FilterVarArrayDynamicReturnTypeExtension.php
@@ -78,9 +78,6 @@ class FilterVarArrayDynamicReturnTypeExtension implements DynamicFunctionReturnT
 		$addEmpty = $addEmptyType === null || $addEmptyType->isTrue()->yes();
 
 		$valueTypesBuilder = ConstantArrayTypeBuilder::createEmpty();
-		$inputTypesMap = [];
-		$optionalKeys = [];
-
 		if ($filterArgType instanceof ConstantIntegerType) {
 			if ($inputConstantArrayType === null) {
 				$isList = $inputArrayType->isList()->yes();
@@ -148,6 +145,9 @@ class FilterVarArrayDynamicReturnTypeExtension implements DynamicFunctionReturnT
 				}
 			} else {
 				$optionalKeys = $filterKeysList;
+				if (count($inputArrayType->getArrays()) !== 1) {
+					return null;
+				}
 				$inputTypesMap = array_fill_keys($optionalKeys, $inputArrayType->getArrays()[0]->getItemType());
 			}
 		}

But it infers the call to be array|false|null. Maybe there's a better way. Thanks.

@zonuexe
Copy link
Contributor

zonuexe commented Apr 12, 2023

Thanks for the report, I'll start fixing it.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants