Skip to content

Commit 0e10531

Browse files
committed
Fix accepting generic callable in CallableType and ClosureType
1 parent 5920c98 commit 0e10531

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/Type/CallableType.php

+6
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,14 @@ private function isSuperTypeOfInternal(Type $type, bool $treatMixedAsAny): IsSup
183183
return $isCallable;
184184
}
185185

186+
$parameterTypes = array_map(static fn ($parameter) => $parameter->getType(), $this->getParameters());
187+
186188
$variantsResult = null;
187189
foreach ($type->getCallableParametersAcceptors($scope) as $variant) {
190+
$variant = ParametersAcceptorSelector::selectFromTypes($parameterTypes, [$variant], false);
191+
if (!$variant instanceof CallableParametersAcceptor) {
192+
return IsSuperTypeOfResult::createNo([]);
193+
}
188194
$isSuperType = CallableTypeHelper::isParametersAcceptorSuperTypeOf($this, $variant, $treatMixedAsAny);
189195
if ($variantsResult === null) {
190196
$variantsResult = $isSuperType;

src/Type/ClosureType.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,14 @@ public function isSuperTypeOfWithReason(Type $type): IsSuperTypeOfResult
217217
private function isSuperTypeOfInternal(Type $type, bool $treatMixedAsAny): IsSuperTypeOfResult
218218
{
219219
if ($type instanceof self) {
220+
$parameterTypes = array_map(static fn ($parameter) => $parameter->getType(), $this->getParameters());
221+
$variant = ParametersAcceptorSelector::selectFromTypes($parameterTypes, [$type], false);
222+
if (!$variant instanceof CallableParametersAcceptor) {
223+
return IsSuperTypeOfResult::createNo([]);
224+
}
220225
return CallableTypeHelper::isParametersAcceptorSuperTypeOf(
221226
$this,
222-
$type,
227+
$variant,
223228
$treatMixedAsAny,
224229
);
225230
}

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -3424,4 +3424,13 @@ public function testBug4801(): void
34243424
$this->analyse([__DIR__ . '/data/bug-4801.php'], []);
34253425
}
34263426

3427+
public function testBug12691(): void
3428+
{
3429+
$this->checkThisOnly = false;
3430+
$this->checkNullables = true;
3431+
$this->checkUnionTypes = true;
3432+
$this->checkExplicitMixed = true;
3433+
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-12691.php'], []);
3434+
}
3435+
34273436
}

0 commit comments

Comments
 (0)