Skip to content

Commit eb0c7a1

Browse files
committed
Look if userland prototype has ReturnTypeWillChange or not
1 parent 004c6a0 commit eb0c7a1

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/Rules/Methods/OverridingMethodRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ public function processNode(Node $node, Scope $scope): array
156156
&& $this->phpVersion->hasTentativeReturnTypes()
157157
&& $realPrototype->getTentativeReturnType() !== null
158158
&& !$this->hasReturnTypeWillChangeAttribute($node->getOriginalNode())
159+
&& count($prototype->getDeclaringClass()->getNativeReflection()->getMethod($prototype->getName())->getAttributes('ReturnTypeWillChange')) === 0
159160
) {
160-
161161
if (!$this->methodParameterComparisonHelper->isReturnTypeCompatible($realPrototype->getTentativeReturnType(), $methodVariant->getNativeReturnType(), true)) {
162162
$messages[] = RuleErrorBuilder::message(sprintf(
163163
'Return type %s of method %s::%s() is not covariant with tentative return type %s of method %s::%s().',

tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,4 +682,27 @@ public function testBug10101(): void
682682
]);
683683
}
684684

685+
public function testBug9615(): void
686+
{
687+
if (PHP_VERSION_ID < 80100) {
688+
$this->markTestSkipped('Test requires PHP 8.1.');
689+
}
690+
691+
$tipText = 'Make it covariant, or use the #[\ReturnTypeWillChange] attribute to temporarily suppress the error.';
692+
693+
$this->phpVersionId = PHP_VERSION_ID;
694+
$this->analyse([__DIR__ . '/data/bug-9615.php'], [
695+
[
696+
'Return type mixed of method Bug9615\ExpectComplaintsHere::accept() is not covariant with tentative return type bool of method FilterIterator<mixed,mixed,Traversable<mixed, mixed>>::accept().',
697+
19,
698+
$tipText,
699+
],
700+
[
701+
'Return type mixed of method Bug9615\ExpectComplaintsHere::getChildren() is not covariant with tentative return type RecursiveIterator|null of method RecursiveIterator<mixed,mixed>::getChildren().',
702+
20,
703+
$tipText,
704+
],
705+
]);
706+
}
707+
685708
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Bug9615;
4+
5+
class Filter extends \RecursiveFilterIterator {
6+
#[\ReturnTypeWillChange]
7+
public function accept() { return true; }
8+
9+
#[\ReturnTypeWillChange]
10+
public function getChildren() { return null; }
11+
}
12+
13+
class ThisShouldBeFine extends Filter {
14+
public function accept() { return true; }
15+
public function getChildren() { return null; }
16+
}
17+
18+
class ExpectComplaintsHere extends \RecursiveFilterIterator {
19+
public function accept() { return true; }
20+
public function getChildren() { return null; }
21+
}

0 commit comments

Comments
 (0)