Skip to content

Commit 5bc3b20

Browse files
committed
Do not fix too-wide return types of methods incompatible with their parents
When the return type of the overridden method does not accept the return type of the overriding one, narrowing the overriding method can make the incompatibility reported.
1 parent 531a677 commit 5bc3b20

12 files changed

Lines changed: 281 additions & 0 deletions

‎src/Rules/TooWideTypehints/TooWideReturnTypeFixer.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use PHPStan\Type\NullType;
3232
use PHPStan\Type\ParserNodeTypeToPHPStanType;
3333
use PHPStan\Type\Type;
34+
use PHPStan\Type\TypeUtils;
3435
use function array_diff;
3536
use function array_intersect;
3637
use function array_key_exists;
@@ -115,6 +116,23 @@ public function canNarrowNativeReturnType(?ClassReflection $classReflection, str
115116
return true;
116117
}
117118

119+
/**
120+
* Whether the return types of the methods the method overrides accept its return type.
121+
* When they do not, the hierarchy is already inconsistent and narrowing the method could make it incompatible.
122+
*/
123+
public function isCompatibleWithParentMethods(ClassReflection $classReflection, string $functionName, Type $returnType): bool
124+
{
125+
foreach ($this->parentMethodHelper->collectParentMethods($functionName, $classReflection) as [$parentMethod]) {
126+
foreach ($parentMethod->getVariants() as $variant) {
127+
if (!TypeUtils::resolveLateResolvableTypes($variant->getReturnType())->isSuperTypeOf($returnType)->yes()) {
128+
return false;
129+
}
130+
}
131+
}
132+
133+
return true;
134+
}
135+
118136
/**
119137
* @param list<Type> $members the members of the checked return type
120138
* @param bool $checkedPhpDoc whether the checked return type is the PHPDoc one

‎src/Rules/TooWideTypehints/TooWideTypeCheck.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ public function createReturnTypeFixPlan(
301301
$functionNode = $node->getClassMethod();
302302
$classReflection = $node->getClassReflection();
303303
$functionName = $functionNode->name->toString();
304+
if (!$this->returnTypeFixer->isCompatibleWithParentMethods($classReflection, $functionName, $functionReturnType)) {
305+
return null;
306+
}
304307
} else {
305308
$functionNode = $node->getFunction();
306309
$classReflection = null;

‎tests/PHPStan/Rules/TooWideTypehints/data/fix-too-wide-return-type/methods-closed-project-php73.php.fixed‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,29 @@ final class PhpDocPropertyChild extends PhpDocPropertyParent
206206
}
207207

208208
}
209+
210+
class IncompatibleParent
211+
{
212+
213+
/**
214+
* @return \Exception|\DateTimeImmutable
215+
*/
216+
public function get()
217+
{
218+
return rand(0, 1) ? new \Exception() : new \DateTimeImmutable();
219+
}
220+
221+
}
222+
223+
class IncompatibleChild extends IncompatibleParent
224+
{
225+
226+
/**
227+
* @return array<int>|\Exception
228+
*/
229+
public function get()
230+
{
231+
return [];
232+
}
233+
234+
}

‎tests/PHPStan/Rules/TooWideTypehints/data/fix-too-wide-return-type/methods-closed-project-php74.php.fixed‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,29 @@ final class PhpDocPropertyChild extends PhpDocPropertyParent
206206
}
207207

208208
}
209+
210+
class IncompatibleParent
211+
{
212+
213+
/**
214+
* @return \Exception|\DateTimeImmutable
215+
*/
216+
public function get()
217+
{
218+
return rand(0, 1) ? new \Exception() : new \DateTimeImmutable();
219+
}
220+
221+
}
222+
223+
class IncompatibleChild extends IncompatibleParent
224+
{
225+
226+
/**
227+
* @return array<int>|\Exception
228+
*/
229+
public function get()
230+
{
231+
return [];
232+
}
233+
234+
}

‎tests/PHPStan/Rules/TooWideTypehints/data/fix-too-wide-return-type/methods-closed-project-php82.php.fixed‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,29 @@ final class PhpDocPropertyChild extends PhpDocPropertyParent
206206
}
207207

208208
}
209+
210+
class IncompatibleParent
211+
{
212+
213+
/**
214+
* @return \Exception|\DateTimeImmutable
215+
*/
216+
public function get()
217+
{
218+
return rand(0, 1) ? new \Exception() : new \DateTimeImmutable();
219+
}
220+
221+
}
222+
223+
class IncompatibleChild extends IncompatibleParent
224+
{
225+
226+
/**
227+
* @return array<int>|\Exception
228+
*/
229+
public function get()
230+
{
231+
return [];
232+
}
233+
234+
}

‎tests/PHPStan/Rules/TooWideTypehints/data/fix-too-wide-return-type/methods-open-project-checked-php73.php.fixed‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,29 @@ final class PhpDocPropertyChild extends PhpDocPropertyParent
206206
}
207207

208208
}
209+
210+
class IncompatibleParent
211+
{
212+
213+
/**
214+
* @return \Exception|\DateTimeImmutable
215+
*/
216+
public function get()
217+
{
218+
return rand(0, 1) ? new \Exception() : new \DateTimeImmutable();
219+
}
220+
221+
}
222+
223+
class IncompatibleChild extends IncompatibleParent
224+
{
225+
226+
/**
227+
* @return array<int>|\Exception
228+
*/
229+
public function get()
230+
{
231+
return [];
232+
}
233+
234+
}

‎tests/PHPStan/Rules/TooWideTypehints/data/fix-too-wide-return-type/methods-open-project-checked-php74.php.fixed‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,29 @@ final class PhpDocPropertyChild extends PhpDocPropertyParent
206206
}
207207

208208
}
209+
210+
class IncompatibleParent
211+
{
212+
213+
/**
214+
* @return \Exception|\DateTimeImmutable
215+
*/
216+
public function get()
217+
{
218+
return rand(0, 1) ? new \Exception() : new \DateTimeImmutable();
219+
}
220+
221+
}
222+
223+
class IncompatibleChild extends IncompatibleParent
224+
{
225+
226+
/**
227+
* @return array<int>|\Exception
228+
*/
229+
public function get()
230+
{
231+
return [];
232+
}
233+
234+
}

‎tests/PHPStan/Rules/TooWideTypehints/data/fix-too-wide-return-type/methods-open-project-checked-php82.php.fixed‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,29 @@ final class PhpDocPropertyChild extends PhpDocPropertyParent
206206
}
207207

208208
}
209+
210+
class IncompatibleParent
211+
{
212+
213+
/**
214+
* @return \Exception|\DateTimeImmutable
215+
*/
216+
public function get()
217+
{
218+
return rand(0, 1) ? new \Exception() : new \DateTimeImmutable();
219+
}
220+
221+
}
222+
223+
class IncompatibleChild extends IncompatibleParent
224+
{
225+
226+
/**
227+
* @return array<int>|\Exception
228+
*/
229+
public function get()
230+
{
231+
return [];
232+
}
233+
234+
}

‎tests/PHPStan/Rules/TooWideTypehints/data/fix-too-wide-return-type/methods-open-project-php73.php.fixed‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,29 @@ final class PhpDocPropertyChild extends PhpDocPropertyParent
206206
}
207207

208208
}
209+
210+
class IncompatibleParent
211+
{
212+
213+
/**
214+
* @return \Exception|\DateTimeImmutable
215+
*/
216+
public function get()
217+
{
218+
return rand(0, 1) ? new \Exception() : new \DateTimeImmutable();
219+
}
220+
221+
}
222+
223+
class IncompatibleChild extends IncompatibleParent
224+
{
225+
226+
/**
227+
* @return array<int>|\Exception
228+
*/
229+
public function get()
230+
{
231+
return [];
232+
}
233+
234+
}

‎tests/PHPStan/Rules/TooWideTypehints/data/fix-too-wide-return-type/methods-open-project-php74.php.fixed‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,29 @@ final class PhpDocPropertyChild extends PhpDocPropertyParent
206206
}
207207

208208
}
209+
210+
class IncompatibleParent
211+
{
212+
213+
/**
214+
* @return \Exception|\DateTimeImmutable
215+
*/
216+
public function get()
217+
{
218+
return rand(0, 1) ? new \Exception() : new \DateTimeImmutable();
219+
}
220+
221+
}
222+
223+
class IncompatibleChild extends IncompatibleParent
224+
{
225+
226+
/**
227+
* @return array<int>|\Exception
228+
*/
229+
public function get()
230+
{
231+
return [];
232+
}
233+
234+
}

0 commit comments

Comments
 (0)