Skip to content

Commit

Permalink
Look if userland prototype has ReturnTypeWillChange or not
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 9, 2023
1 parent 004c6a0 commit eb0c7a1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Rules/Methods/OverridingMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ public function processNode(Node $node, Scope $scope): array
&& $this->phpVersion->hasTentativeReturnTypes()
&& $realPrototype->getTentativeReturnType() !== null
&& !$this->hasReturnTypeWillChangeAttribute($node->getOriginalNode())
&& count($prototype->getDeclaringClass()->getNativeReflection()->getMethod($prototype->getName())->getAttributes('ReturnTypeWillChange')) === 0
) {

if (!$this->methodParameterComparisonHelper->isReturnTypeCompatible($realPrototype->getTentativeReturnType(), $methodVariant->getNativeReturnType(), true)) {
$messages[] = RuleErrorBuilder::message(sprintf(
'Return type %s of method %s::%s() is not covariant with tentative return type %s of method %s::%s().',
Expand Down
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,4 +682,27 @@ public function testBug10101(): void
]);
}

public function testBug9615(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$tipText = 'Make it covariant, or use the #[\ReturnTypeWillChange] attribute to temporarily suppress the error.';

$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-9615.php'], [
[
'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().',
19,
$tipText,
],
[
'Return type mixed of method Bug9615\ExpectComplaintsHere::getChildren() is not covariant with tentative return type RecursiveIterator|null of method RecursiveIterator<mixed,mixed>::getChildren().',
20,
$tipText,
],
]);
}

}
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-9615.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Bug9615;

class Filter extends \RecursiveFilterIterator {
#[\ReturnTypeWillChange]
public function accept() { return true; }

#[\ReturnTypeWillChange]
public function getChildren() { return null; }
}

class ThisShouldBeFine extends Filter {
public function accept() { return true; }
public function getChildren() { return null; }
}

class ExpectComplaintsHere extends \RecursiveFilterIterator {
public function accept() { return true; }
public function getChildren() { return null; }
}

0 comments on commit eb0c7a1

Please sign in to comment.