1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Allow var_export userland implementation

This commit is contained in:
Matthew Brown 2020-03-29 10:19:09 -04:00
parent 1439c90789
commit 788da0680e
2 changed files with 73 additions and 0 deletions

View File

@ -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;
}

View File

@ -1211,6 +1211,22 @@ class AnnotationTest extends TestCase
return true;
}'
],
'userlandVarExport' => [
'<?php
/**
* @template TReturnFlag as bool
* @param mixed $expression
* @param TReturnFlag $return
* @psalm-return (TReturnFlag is true ? string : void)
*/
function my_var_export($expression, bool $return = false) {
if ($return) {
return var_export($expression, true);
}
var_export($expression);
}'
],
];
}