Skip to content

Commit 2e06621

Browse files
committed
Callable with wider parameter type is supertype of callable with narrower parameter type
Amp-Thread-ID: https://ampcode.com/threads/T-01a19219-5899-7222-8fa0-6cb7e6fb7182
1 parent 091c5a7 commit 2e06621

5 files changed

Lines changed: 96 additions & 7 deletions

File tree

‎src/Type/CallableTypeHelper.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static function isParametersAcceptorSuperTypeOf(
7979
$isSuperType = $theirParameter->getType()->accepts($ourParameterType, $strictTypes);
8080
$isSuperType = new IsSuperTypeOfResult($isSuperType->result, $isSuperType->reasons);
8181
} else {
82-
$isSuperType = $theirParameter->getType()->isSuperTypeOf($ourParameterType);
82+
$isSuperType = $ourParameterType->isSuperTypeOf($theirParameter->getType());
8383
}
8484

8585
if ($isSuperType->maybe()) {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug11285;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
/**
10+
* @template T
11+
* @template U
12+
*/
13+
class Templated
14+
{
15+
16+
/**
17+
* @param callable(T|U): void $callback
18+
*/
19+
public function compare(callable $callback): void {
20+
21+
}
22+
}
23+
24+
class ClassA {}
25+
class ClassB {}
26+
27+
28+
class Test
29+
{
30+
/**
31+
* @param Templated<ClassA, ClassB>|Templated<ClassA, ClassA> $t
32+
*/
33+
public function broken(
34+
Templated $t,
35+
): void
36+
{
37+
$t->compare(
38+
static function (ClassA|ClassB $crate): void {
39+
assertType('Bug11285\\ClassA|Bug11285\\ClassB', $crate);
40+
}
41+
);
42+
}
43+
44+
/**
45+
* @param Templated<ClassA, ClassB> $t
46+
*/
47+
public function working(
48+
Templated $t,
49+
): void
50+
{
51+
$t->compare(
52+
static function (ClassA|ClassB $crate): void {
53+
assertType('Bug11285\\ClassA|Bug11285\\ClassB', $crate);
54+
}
55+
);
56+
}
57+
58+
}

‎tests/PHPStan/Type/CallableTypeTest.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ public static function dataIsSuperTypeOf(): array
4444
[
4545
new CallableType([new NativeParameterReflection('foo', false, new MixedType(), PassedByReference::createNo(), false, null)], new MixedType(), false),
4646
new CallableType([new NativeParameterReflection('foo', false, new IntegerType(), PassedByReference::createNo(), false, null)], new MixedType(), false),
47-
TrinaryLogic::createMaybe(),
47+
TrinaryLogic::createYes(),
4848
],
4949
[
5050
new CallableType([new NativeParameterReflection('foo', false, new IntegerType(), PassedByReference::createNo(), false, null)], new MixedType(), false),
5151
new CallableType([new NativeParameterReflection('foo', false, new MixedType(), PassedByReference::createNo(), false, null)], new MixedType(), false),
52-
TrinaryLogic::createYes(),
52+
TrinaryLogic::createMaybe(),
5353
],
5454
[
5555
new CallableType([
@@ -432,8 +432,8 @@ public function testIsSuperTypeOfKeepsLazyReasonsOfMaybeParameter(): void
432432
$accepting = new CallableType([new NativeParameterReflection('x', false, $acceptingParameterType, PassedByReference::createNo(), false, null)], new VoidType());
433433
$passed = new CallableType([new NativeParameterReflection('x', false, $passedParameterType, PassedByReference::createNo(), false, null)], new VoidType());
434434

435-
// the passed parameter's isSuperTypeOf() is maybe, with a lazy reason from its array{b: int} member
436-
$parameterResult = $passedParameterType->isSuperTypeOf($acceptingParameterType);
435+
// the accepting parameter's isSuperTypeOf() is maybe, with a lazy reason from the passed array{b: int} member
436+
$parameterResult = $acceptingParameterType->isSuperTypeOf($passedParameterType);
437437
$this->assertTrue($parameterResult->maybe());
438438
$this->assertNotSame([], $parameterResult->lazyReasons);
439439

@@ -442,7 +442,7 @@ public function testIsSuperTypeOfKeepsLazyReasonsOfMaybeParameter(): void
442442
$reasons = $result->getReasons();
443443
$this->assertCount(2, $reasons);
444444
$this->assertSame('Type array{a: int}|array{b: int} of parameter #1 $x of passed callable needs to be same or wider than parameter type array{a: int, b?: int} of accepting callable.', $reasons[0]);
445-
$this->assertStringStartsWith('Sealed array shapes array{b: int} and array{a: int, b?: int} cannot be intersected.', $reasons[1]);
445+
$this->assertStringStartsWith('Sealed array shapes array{a: int, b?: int} and array{b: int} cannot be intersected.', $reasons[1]);
446446
}
447447

448448
}

‎tests/PHPStan/Type/TypeCombinatorTest.php‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PHPStan\Generics\FunctionsAssertType\C;
2020
use PHPStan\PhpDoc\TypeStringResolver;
2121
use PHPStan\Reflection\Callables\SimpleImpurePoint;
22+
use PHPStan\Reflection\Php\DummyParameter;
2223
use PHPStan\Testing\PHPStanTestCase;
2324
use PHPStan\TrinaryLogic;
2425
use PHPStan\Type\Accessory\AccessoryArrayListType;
@@ -3244,6 +3245,36 @@ public static function dataUnion(): iterable
32443245
ConstantArrayType::class,
32453246
'array{}',
32463247
];
3248+
yield [
3249+
[
3250+
new CallableType([
3251+
new DummyParameter('callback', new ObjectType('stdClass'), false, null, false, null),
3252+
], new VoidType(), false),
3253+
new CallableType([
3254+
new DummyParameter('callback', new UnionType([
3255+
new ObjectType('stdClass'),
3256+
new ObjectType('Exception'),
3257+
]), false, null, false, null),
3258+
], new VoidType(), false),
3259+
],
3260+
CallableType::class,
3261+
'callable(Exception|stdClass): void',
3262+
];
3263+
yield [
3264+
[
3265+
new CallableType([
3266+
new DummyParameter('callback', new ObjectType('stdClass'), false, null, false, null),
3267+
], new VoidType(), false),
3268+
new CallableType([
3269+
new DummyParameter('callback', new UnionType([
3270+
new IntegerType(),
3271+
new ObjectType('Exception'),
3272+
]), false, null, false, null),
3273+
], new VoidType(), false),
3274+
],
3275+
UnionType::class,
3276+
'(callable(Exception|int): void)|(callable(stdClass): void)',
3277+
];
32473278
}
32483279

32493280
/**

‎turbo-ext/src/CallableTypeHelper.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class CallableTypeHelper
127127
if (UNEXPECTED(accepts.isUndef())) return zv::Val();
128128
isSuperType = resultFrom(accepts.raw());
129129
} else {
130-
isSuperType = callObject(theirParameterType.raw(), PT_LC("issupertypeof"), 1, ourParameterType.raw());
130+
isSuperType = callObject(ourParameterType.raw(), PT_LC("issupertypeof"), 1, theirParameterType.raw());
131131
}
132132
if (UNEXPECTED(isSuperType.isUndef())) return zv::Val();
133133

0 commit comments

Comments
 (0)