Skip to content

Commit

Permalink
ensure callbacks have the required number of params
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed Oct 17, 2022
1 parent 9218017 commit 2d447b4
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Psalm/Internal/Type/Comparator/UnionTypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Psalm\Internal\Type\TypeExpander;
use Psalm\Type\Atomic;
use Psalm\Type\Atomic\TArrayKey;
use Psalm\Type\Atomic\TCallable;
use Psalm\Type\Atomic\TClassConstant;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TIntRange;
Expand Down Expand Up @@ -134,6 +135,28 @@ public static function isContainedBy(
continue;
}

// if params are specified
if ($container_type_part instanceof TCallable && $container_type_part->params !== null && $input_type_part instanceof TCallable) {
$container_required_param_count = 0;
foreach ($container_type_part->params as $index => $container_param) {
if ($container_param->is_optional === false) {
$container_required_param_count = $index + 1;
}
}

$input_required_param_count = 0;
foreach ($input_type_part->params as $index => $input_param) {
if ($input_param->is_optional === false) {
$input_required_param_count = $index + 1;
}
}

// too few or too many non-optional params provided in callback
if ($container_required_param_count > count($input_type_part->params) || count($container_type_part->params) < $input_required_param_count) {
return false;
}
}

if ($union_comparison_result) {
$atomic_comparison_result = new TypeComparisonResult();
} else {
Expand Down

0 comments on commit 2d447b4

Please sign in to comment.