Skip to content
Permalink
Browse files

Fix #2374 - refine error-suppressed output a little better

  • Loading branch information
muglug committed Nov 25, 2019
1 parent 1f07ea7 commit 5626058eff996d55eedf25f773a923fce6a55c1f
Showing with 24 additions and 1 deletion.
  1. +9 −1 src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php
  2. +15 −0 tests/IssetTest.php
@@ -434,6 +434,8 @@ public static function analyze(
}
}
$can_be_empty = true;
foreach ($assign_var->items as $offset => $assign_var_item) {
// $assign_var_item can be null e.g. list($a, ) = ['a', 'b']
if (!$assign_var_item) {
@@ -559,8 +561,12 @@ public static function analyze(
if ($assign_value_atomic_type instanceof Type\Atomic\TArray) {
$new_assign_type = clone $assign_value_atomic_type->type_params[1];
$can_be_empty = !$assign_value_atomic_type instanceof Type\Atomic\TNonEmptyArray;
} elseif ($assign_value_atomic_type instanceof Type\Atomic\TList) {
$new_assign_type = clone $assign_value_atomic_type->type_param;
$can_be_empty = !$assign_value_atomic_type instanceof Type\Atomic\TNonEmptyList;
} elseif ($assign_value_atomic_type instanceof Type\Atomic\ObjectLike) {
if ($assign_var_item->key
&& ($assign_var_item->key instanceof PhpParser\Node\Scalar\String_
@@ -584,6 +590,8 @@ public static function analyze(
$new_assign_type->possibly_undefined = false;
}
}
$can_be_empty = !$assign_value_atomic_type->sealed;
} elseif ($assign_value_atomic_type->hasArrayAccessInterface($codebase)) {
ForeachAnalyzer::getKeyValueParamsForTraversableObject(
$assign_value_atomic_type,
@@ -642,7 +650,7 @@ public static function analyze(
if ($list_var_id) {
$context->vars_in_scope[$list_var_id] = $new_assign_type ?: Type::getMixed();
if ($context->error_suppressing) {
if ($context->error_suppressing && ($offset || $can_be_empty)) {
$context->vars_in_scope[$list_var_id]->addType(new Type\Atomic\TNull);
}
}
@@ -644,6 +644,13 @@ function foo(string $s) : void {
echo isset($port) ? "cool" : "uncool";
}',
],
'listDestructuringErrorSuppress' => [
'<?php
function foo(string $s) : string {
@list($port, $starboard) = explode(":", $s);
return $port;
}',
],
];
}
@@ -686,6 +693,14 @@ public function test() : bool {
}',
'error_message' => 'InvalidArrayOffset',
],
'listDestructuringErrorSuppress' => [
'<?php
function foo(string $s) : string {
@list($port) = explode(":", $s, -1);
return $port;
}',
'error_message' => 'NullableReturnStatement',
],
];
}
}

0 comments on commit 5626058

Please sign in to comment.
You can’t perform that action at this time.