From 5eea5cbc9ff08d529b14aab460effb6c22451b7e Mon Sep 17 00:00:00 2001 From: Martin Jonas Date: Mon, 11 Nov 2024 17:15:04 +0100 Subject: [PATCH] get_defined_vars() return type contains know variables --- conf/config.neon | 5 ++ ...DefinedVarsFunctionReturnTypeExtension.php | 55 +++++++++++++++++++ .../Analyser/nsrt/get-defined-vars.php | 11 ++++ 3 files changed, 71 insertions(+) create mode 100644 src/Type/Php/GetDefinedVarsFunctionReturnTypeExtension.php create mode 100644 tests/PHPStan/Analyser/nsrt/get-defined-vars.php diff --git a/conf/config.neon b/conf/config.neon index 7d1bf16616..8807c41beb 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -1427,6 +1427,11 @@ services: tags: - phpstan.broker.dynamicFunctionReturnTypeExtension + - + class: PHPStan\Type\Php\GetDefinedVarsFunctionReturnTypeExtension + tags: + - phpstan.broker.dynamicFunctionReturnTypeExtension + - class: PHPStan\Type\Php\GetParentClassDynamicFunctionReturnTypeExtension tags: diff --git a/src/Type/Php/GetDefinedVarsFunctionReturnTypeExtension.php b/src/Type/Php/GetDefinedVarsFunctionReturnTypeExtension.php new file mode 100644 index 0000000000..be0ec23d28 --- /dev/null +++ b/src/Type/Php/GetDefinedVarsFunctionReturnTypeExtension.php @@ -0,0 +1,55 @@ +getName() === 'get_defined_vars'; + } + + public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type + { + if ($scope->canAnyVariableExist()) { + return new ArrayType( + new StringType(), + new MixedType(), + ); + } + + $variables = array_values(array_filter( + $scope->getDefinedVariables(), + static fn ($variable) => $variable !== 'this', + )); + + $keys = array_map( + static fn ($variable) => new ConstantStringType($variable), + $variables, + ); + + $values = array_map( + static fn ($variable) => $scope->getVariableType($variable), + $variables, + ); + + return new ConstantArrayType($keys, $values); + } + +} diff --git a/tests/PHPStan/Analyser/nsrt/get-defined-vars.php b/tests/PHPStan/Analyser/nsrt/get-defined-vars.php new file mode 100644 index 0000000000..52ca042d3e --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/get-defined-vars.php @@ -0,0 +1,11 @@ +