Skip to content

Commit

Permalink
Don’t modify object after unnecessary assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Aug 17, 2020
1 parent 7adc25c commit 11f170a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Psalm/Internal/Type/Comparator/ObjectComparator.php
Expand Up @@ -124,7 +124,7 @@ public static function isShallowlyContainedBy(
);
}

foreach ($intersection_input_types as $intersection_input_type) {
foreach ($intersection_input_types as $intersection_input_key => $intersection_input_type) {
$input_was_static = false;

if ($intersection_input_type instanceof TIterable) {
Expand Down Expand Up @@ -239,7 +239,10 @@ public static function isShallowlyContainedBy(
$input_type_is_interface = $codebase->interfaceExists($intersection_input_type_lower);
$container_type_is_interface = $codebase->interfaceExists($intersection_container_type_lower);

if ($allow_interface_equality && $container_type_is_interface) {
if ($allow_interface_equality
&& $container_type_is_interface
&& ($input_type_is_interface || !isset($intersection_container_types[$intersection_input_key]))
) {
continue 2;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/InterfaceTest.php
Expand Up @@ -686,6 +686,21 @@ function f3(H $f) : void {
$f->m()->m();
}'
],
'dontModifyAfterUnnecessaryAssertion' => [
'<?php
class A {}
interface I {}
/**
* @param A&I $a
* @return A&I
*/
function foo(I $a) {
/** @psalm-suppress RedundantConditionGivenDocblockType */
assert($a instanceof A);
return $a;
}'
],
];
}

Expand Down

0 comments on commit 11f170a

Please sign in to comment.