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

Fix #3102 - identify issues with possibly-mixed output

This commit is contained in:
Brown 2020-04-08 14:07:09 -04:00
parent a117c48bb2
commit b2e010b431
4 changed files with 38 additions and 10 deletions

View File

@ -1386,9 +1386,7 @@ class IfAnalyzer
}
foreach ($possibly_redefined_vars as $var => $type) {
if ($type->hasMixed()) {
$if_scope->possibly_redefined_vars[$var] = $type;
} elseif (isset($if_scope->possibly_redefined_vars[$var])) {
if (isset($if_scope->possibly_redefined_vars[$var])) {
$if_scope->possibly_redefined_vars[$var] = Type::combineUnionTypes(
$type,
$if_scope->possibly_redefined_vars[$var],
@ -1720,9 +1718,7 @@ class IfAnalyzer
}
foreach ($else_redefined_vars as $var => $type) {
if ($type->hasMixed()) {
$if_scope->possibly_redefined_vars[$var] = $type;
} elseif (isset($if_scope->possibly_redefined_vars[$var])) {
if (isset($if_scope->possibly_redefined_vars[$var])) {
$if_scope->possibly_redefined_vars[$var] = Type::combineUnionTypes(
$type,
$if_scope->possibly_redefined_vars[$var],

View File

@ -254,7 +254,7 @@ class ReturnAnalyzer
return null;
}
if ($stmt_type->hasMixed()) {
if ($stmt_type->isMixed()) {
if ($local_return_type->isVoid() || $local_return_type->isNever()) {
if (IssueBuffer::accepts(
new InvalidReturnStatement(
@ -325,6 +325,20 @@ class ReturnAnalyzer
if ($union_comparison_results->type_coerced) {
if ($union_comparison_results->type_coerced_from_mixed) {
if (!$union_comparison_results->type_coerced_from_as_mixed) {
if ($inferred_type->hasMixed()) {
if (IssueBuffer::accepts(
new MixedReturnStatement(
'Could not infer a return type',
new CodeLocation($source, $stmt->expr)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
return null;
}
if (IssueBuffer::accepts(
new MixedReturnTypeCoercion(
'The type \'' . $stmt_type->getId() . '\' is more general than the'
@ -334,8 +348,10 @@ class ReturnAnalyzer
),
$statements_analyzer->getSuppressedIssues()
)) {
return false;
// fall through
}
return null;
}
} else {
if (IssueBuffer::accepts(

View File

@ -974,7 +974,8 @@ class Union implements TypeNode
*/
return isset($this->types['mixed'])
&& !$this->types['mixed']->from_loop_isset
&& get_class($this->types['mixed']) === Type\Atomic\TMixed::class;
&& get_class($this->types['mixed']) === Type\Atomic\TMixed::class
&& count($this->types) === 1;
}
/**

View File

@ -1387,7 +1387,7 @@ class ArrayAssignmentTest extends TestCase
$out[$key] = 5;
return $out;
}'
]
],
];
}
@ -1604,6 +1604,21 @@ class ArrayAssignmentTest extends TestCase
(new A)->foo()[3] = 5;',
'error_message' => 'InvalidArrayAssignment',
],
'mergeIntWithMixed' => [
'<?php
class A {
private static array $cache = [];
public function getCachedMixed(array $cache, string $locale) : string {
if (!isset(self::$cache[$locale])) {
$cache[$locale] = 5;
}
return $cache[$locale];
}
}',
'error_message' => 'InvalidReturnStatement',
],
];
}
}