Skip to content

Commit

Permalink
Merge pull request #7175 from vimeo/muglug-fix-closure-comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Dec 16, 2021
2 parents 84d6777 + 89a1c5a commit f4b48ab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
40 changes: 12 additions & 28 deletions src/Psalm/Internal/Type/Comparator/AtomicTypeComparator.php
Expand Up @@ -165,40 +165,24 @@ public static function isContainedBy(
);
}

if ($container_type_part instanceof TClosure && $input_type_part instanceof TCallable) {
if (CallableTypeComparator::isContainedBy(
$codebase,
$input_type_part,
$container_type_part,
$atomic_comparison_result
) === false
) {
return false;
}

if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
}

return false;
}

if ($container_type_part instanceof TClosure) {
if (!$input_type_part instanceof TClosure) {
if ($input_type_part instanceof TCallable) {
if (CallableTypeComparator::isContainedBy(
$codebase,
$input_type_part,
$container_type_part,
$atomic_comparison_result
) === false
) {
return false;
}

if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
$atomic_comparison_result->type_coerced_from_mixed = true;
}

return false;
}

return CallableTypeComparator::isContainedBy(
$codebase,
$input_type_part,
$container_type_part,
$atomic_comparison_result
);
return false;
}

if ($container_type_part instanceof TCallable && $input_type_part instanceof TClosure) {
Expand Down
8 changes: 8 additions & 0 deletions tests/ClosureTest.php
Expand Up @@ -1043,6 +1043,14 @@ function (): void {};
false,
'7.4'
],
'closureInvalidArg' => [
'<?php
/** @param Closure(int): string $c */
function takesClosure(Closure $c): void {}
takesClosure(5);',
'error_message' => 'InvalidArgument',
],
];
}
}

0 comments on commit f4b48ab

Please sign in to comment.