1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix simplifyUnionType so classes with __toString aren’t confused with string

This commit is contained in:
Matthew Brown 2017-01-25 17:01:01 -07:00
parent ea63fd7ae3
commit 15e8c1b904
3 changed files with 45 additions and 15 deletions

View File

@ -789,6 +789,37 @@ abstract class FunctionLikeChecker extends SourceChecker implements StatementsSo
$return_type_location = new CodeLocation($this, $this->function, true);
}
$inferred_yield_types = [];
$inferred_return_types = EffectsAnalyser::getReturnTypes(
$this->function->getStmts(),
$inferred_yield_types,
true
);
$inferred_return_type = $inferred_return_types ? Type::combineTypes($inferred_return_types) : Type::getVoid();
$inferred_yield_type = $inferred_yield_types ? Type::combineTypes($inferred_yield_types) : null;
$inferred_generator_return_type = null;
if ($inferred_yield_type) {
$inferred_generator_return_type = $inferred_return_type;
$inferred_return_type = $inferred_yield_type;
}
if (!$return_type && !Config::getInstance()->add_void_docblocks && $inferred_return_type->isVoid()) {
return null;
}
$inferred_return_type = TypeChecker::simplifyUnionType(
ExpressionChecker::fleshOutTypes(
$inferred_return_type,
[],
$this->source->getFQCLN(),
''
),
$this->getFileChecker()
);
if (!$return_type && !$update_docblock && !$is_to_string) {
if ($this->function instanceof Closure) {
if (IssueBuffer::accepts(
@ -853,16 +884,6 @@ abstract class FunctionLikeChecker extends SourceChecker implements StatementsSo
if (!$return_type) {
if ($inferred_return_type && !$inferred_return_type->isMixed()) {
$inferred_return_type = TypeChecker::simplifyUnionType(
ExpressionChecker::fleshOutTypes(
$inferred_return_type,
[],
$this->source->getFQCLN(),
''
),
$this->getFileChecker()
);
FileChecker::addDocblockReturnType(
$this->source->getFileName(),
$this->function->getLine(),

View File

@ -105,11 +105,11 @@ class StatementsChecker extends SourceChecker implements StatementsSource
break;
}
/*
if (isset($context->vars_in_scope['$storage->return_type_location'])) {
var_dump($stmt->getLine() . ' ' . $context->vars_in_scope['$storage->return_type_location']);
///*
if (isset($context->vars_in_scope['$firstStmts'])) {
var_dump($stmt->getLine() . ' ' . $context->vars_in_scope['$firstStmts']);
}
*/
//*/
if ($stmt instanceof PhpParser\Node\Stmt\If_) {
IfChecker::analyze($this, $stmt, $context, $loop_context);

View File

@ -1435,7 +1435,16 @@ class TypeChecker
foreach ($union->types as $container_type_part) {
if ($type_part !== $container_type_part &&
TypeChecker::isAtomicContainedBy($type_part, $container_type_part, $file_checker)
TypeChecker::isAtomicContainedBy(
$type_part,
$container_type_part,
$file_checker,
false,
$has_scalar_match,
$type_coerced,
$to_string_cast
) &&
!$to_string_cast
) {
$is_contained_by_other = true;
break;