Skip to content

Commit 9787895

Browse files
committed
Revert "CallableTypeHelper - always use isSuperTypeOf() instead of accepts()"
This reverts commit eceb9f6.
1 parent 524a11e commit 9787895

File tree

4 files changed

+11
-67
lines changed

4 files changed

+11
-67
lines changed

src/Type/CallableTypeHelper.php

+2-21
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use PHPStan\Reflection\ParametersAcceptor;
66
use PHPStan\TrinaryLogic;
7-
use PHPStan\Type\Generic\TemplateMixedType;
87
use function array_merge;
98
use function sprintf;
109

@@ -60,16 +59,7 @@ public static function isParametersAcceptorSuperTypeOf(
6059
}
6160

6261
if ($treatMixedAsAny) {
63-
if (
64-
$ourParameterType instanceof MixedType
65-
&& !$ourParameterType instanceof TemplateMixedType
66-
) {
67-
$isSuperType = new AcceptsResult(TrinaryLogic::createYes(), []);
68-
} elseif ($ourParameterType instanceof BenevolentUnionType) {
69-
$isSuperType = $theirParameter->getType()->acceptsWithReason($ourParameterType, true);
70-
} else {
71-
$isSuperType = new AcceptsResult($theirParameter->getType()->isSuperTypeOf($ourParameterType), []);
72-
}
62+
$isSuperType = $theirParameter->getType()->acceptsWithReason($ourParameterType, true);
7363
} else {
7464
$isSuperType = new AcceptsResult($theirParameter->getType()->isSuperTypeOf($ourParameterType), []);
7565
}
@@ -95,16 +85,7 @@ public static function isParametersAcceptorSuperTypeOf(
9585

9686
$theirReturnType = $theirs->getReturnType();
9787
if ($treatMixedAsAny) {
98-
if (
99-
$theirReturnType instanceof MixedType
100-
&& !$theirReturnType instanceof TemplateMixedType
101-
) {
102-
$isReturnTypeSuperType = new AcceptsResult(TrinaryLogic::createYes(), []);
103-
} elseif ($theirReturnType instanceof BenevolentUnionType) {
104-
$isReturnTypeSuperType = $ours->getReturnType()->acceptsWithReason($theirReturnType, true);
105-
} else {
106-
$isReturnTypeSuperType = new AcceptsResult($ours->getReturnType()->isSuperTypeOf($theirReturnType), []);
107-
}
88+
$isReturnTypeSuperType = $ours->getReturnType()->acceptsWithReason($theirReturnType, true);
10889
} else {
10990
$isReturnTypeSuperType = new AcceptsResult($ours->getReturnType()->isSuperTypeOf($theirReturnType), []);
11091
}

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

+9-11
Original file line numberDiff line numberDiff line change
@@ -772,17 +772,15 @@ public function testDiscussion7124(): void
772772
}
773773

774774
$errors = $this->runAnalyse(__DIR__ . '/data/discussion-7124.php');
775-
$this->assertCount(5, $errors);
776-
$this->assertSame('Parameter #2 $callback of function array_filter expects callable(T): mixed, (callable(K): bool)|(callable(T): bool) given.', $errors[0]->getMessage());
777-
$this->assertSame(30, $errors[0]->getLine());
778-
$this->assertSame('Parameter #2 $callback of function Discussion7124\filter expects callable(bool, int=): bool, Closure(int, bool): bool given.', $errors[1]->getMessage());
779-
$this->assertSame(38, $errors[1]->getLine());
780-
$this->assertSame('Parameter #2 $callback of function Discussion7124\filter expects callable(bool, int=): bool, Closure(int): bool given.', $errors[2]->getMessage());
781-
$this->assertSame(45, $errors[2]->getLine());
782-
$this->assertSame('Parameter #2 $callback of function Discussion7124\filter expects callable(int): bool, Closure(bool): bool given.', $errors[3]->getMessage());
783-
$this->assertSame(52, $errors[3]->getLine());
784-
$this->assertSame('Parameter #2 $callback of function Discussion7124\filter expects callable(bool): bool, Closure(int): bool given.', $errors[4]->getMessage());
785-
$this->assertSame(59, $errors[4]->getLine());
775+
$this->assertCount(4, $errors);
776+
$this->assertSame('Parameter #2 $callback of function Discussion7124\filter expects callable(bool, int=): bool, Closure(int, bool): bool given.', $errors[0]->getMessage());
777+
$this->assertSame(38, $errors[0]->getLine());
778+
$this->assertSame('Parameter #2 $callback of function Discussion7124\filter expects callable(bool, int=): bool, Closure(int): bool given.', $errors[1]->getMessage());
779+
$this->assertSame(45, $errors[1]->getLine());
780+
$this->assertSame('Parameter #2 $callback of function Discussion7124\filter expects callable(int): bool, Closure(bool): bool given.', $errors[2]->getMessage());
781+
$this->assertSame(52, $errors[2]->getLine());
782+
$this->assertSame('Parameter #2 $callback of function Discussion7124\filter expects callable(bool): bool, Closure(int): bool given.', $errors[3]->getMessage());
783+
$this->assertSame(59, $errors[3]->getLine());
786784
}
787785

788786
public function testBug7214(): void

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

-5
Original file line numberDiff line numberDiff line change
@@ -2910,11 +2910,6 @@ public function testTrickyCallables(): void
29102910
'• Parameter #1 $key of passed callable is required but the parameter of accepting callable is optional. It might be called without it.
29112911
• Type int of parameter #1 $key of passed callable needs to be same or wider than parameter type int|string of accepting callable.',
29122912
],
2913-
[
2914-
'Parameter #1 $cb of method TrickyCallables\AcceptsFooParentCallable::sayHello() expects callable(TrickyCallables\FooParent): void, Closure(TrickyCallables\FooChild): void given.',
2915-
110,
2916-
'Type TrickyCallables\FooChild of parameter #1 $bar of passed callable needs to be same or wider than parameter type TrickyCallables\FooParent of accepting callable.',
2917-
],
29182913
]);
29192914
}
29202915

tests/PHPStan/Rules/Methods/data/tricky-callables.php

-30
Original file line numberDiff line numberDiff line change
@@ -82,33 +82,3 @@ function (TwoErrorsAtOnce $t): void {
8282
$filter = static fn (int $key): bool => true;
8383
$t->run($filter);
8484
};
85-
86-
class FooParent
87-
{
88-
89-
}
90-
91-
class FooChild extends FooParent
92-
{
93-
94-
}
95-
96-
class AcceptsFooParentCallable
97-
{
98-
99-
/**
100-
* @param callable(FooParent): void $cb
101-
*/
102-
public function sayHello(callable $cb): void
103-
{
104-
$cb(new FooParent());
105-
}
106-
107-
public function doFoo(): void
108-
{
109-
$hw = new self();
110-
$hw->sayHello(function (FooChild $bar): void {
111-
});
112-
}
113-
114-
}

0 commit comments

Comments
 (0)