From 788da0680ec5118b453f597a328ee086a8cf3c2b Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Sun, 29 Mar 2020 10:19:09 -0400 Subject: [PATCH] Allow var_export userland implementation --- .../Statements/ExpressionAnalyzer.php | 57 +++++++++++++++++++ tests/AnnotationTest.php | 16 ++++++ 2 files changed, 73 insertions(+) diff --git a/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php index cdf8cfbea..7c034f2b9 100644 --- a/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php @@ -1536,6 +1536,63 @@ class ExpressionAnalyzer } } + if ($return_type instanceof Type\Atomic\TConditional && $evaluate) { + $all_conditional_return_types = []; + + foreach ($return_type->if_type->getAtomicTypes() as $if_atomic_type) { + $candidate = self::fleshOutAtomicType( + $codebase, + $if_atomic_type, + $self_class, + $static_class_type, + $parent_class, + $evaluate, + $final + ); + + if (is_array($candidate)) { + $all_conditional_return_types = array_merge( + $all_conditional_return_types, + $candidate + ); + } else { + $all_conditional_return_types[] = $candidate; + } + } + + foreach ($return_type->else_type->getAtomicTypes() as $else_atomic_type) { + $candidate = self::fleshOutAtomicType( + $codebase, + $else_atomic_type, + $self_class, + $static_class_type, + $parent_class, + $evaluate, + $final + ); + + if (is_array($candidate)) { + $all_conditional_return_types = array_merge( + $all_conditional_return_types, + $candidate + ); + } else { + $all_conditional_return_types[] = $candidate; + } + } + + foreach ($all_conditional_return_types as $i => $conditional_return_type) { + if ($conditional_return_type instanceof Type\Atomic\TVoid + && count($all_conditional_return_types) > 1 + ) { + $all_conditional_return_types[$i] = new Type\Atomic\TNull(); + $all_conditional_return_types[$i]->from_docblock = true; + } + } + + return $all_conditional_return_types; + } + return $return_type; } diff --git a/tests/AnnotationTest.php b/tests/AnnotationTest.php index 5e3246129..3624a7041 100644 --- a/tests/AnnotationTest.php +++ b/tests/AnnotationTest.php @@ -1211,6 +1211,22 @@ class AnnotationTest extends TestCase return true; }' ], + 'userlandVarExport' => [ + '