Skip to content

Commit

Permalink
ensure callbacks have the required number of params
Browse files Browse the repository at this point in the history
Fix #8593
  • Loading branch information
kkmuffme committed Oct 17, 2022
1 parent 9218017 commit 25b01ce
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Psalm/Internal/Type/Comparator/UnionTypeComparator.php
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 All @@ -21,6 +22,10 @@
use function array_pop;
use function array_push;
use function array_reverse;
use function count;
use function is_array;

use const PHP_INT_MAX;

/**
* @internal
Expand Down Expand Up @@ -134,6 +139,39 @@ public static function isContainedBy(
continue;
}

// if params are specified
if ($container_type_part instanceof TCallable
&& is_array($container_type_part->params)
&& $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;
if (!is_array($input_type_part->params)) {
// it's not declared, there can be an arbitrary number of params
$input_all_param_count = PHP_INT_MAX;
} else {
$input_all_param_count = count($input_type_part->params);
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 > $input_all_param_count
|| 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 25b01ce

Please sign in to comment.