Skip to content

Commit 1c4ca0d

Browse files
committed
Keep fixed return types invariant across the class hierarchy before PHP 7.4
Without return type covariance, a method overriding one with a native return type declares the same native type and the narrower one in @return, and an overridden method is fixed only when the methods overriding it already declare the same native type. Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
1 parent 82f0506 commit 1c4ca0d

19 files changed

Lines changed: 2495 additions & 5 deletions

‎src/Rules/MissingReturnTypeFixRule.php‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ private function createFix(string $key): ?callable
158158
$nativeReturnInfos = [];
159159
$parentReturnTypes = [];
160160
$parentNativeReturnTypes = [];
161+
$overridingNativeReturnTypes = [];
161162
foreach ($traitMethod !== null ? $this->traitMethodRecords[$traitMethod] : [$key] as $contextKey) {
162163
$contextRecord = $this->records[$contextKey];
163164
if ($contextRecord['errors'] === []) {
@@ -218,13 +219,15 @@ private function createFix(string $key): ?callable
218219
}
219220

220221
$returnInfos[] = $overridingReturnInfo;
221-
if ($overridingNativeReturnInfo !== null) {
222-
$nativeReturnInfos[] = $overridingNativeReturnInfo;
222+
if ($overridingNativeType !== null) {
223+
$nativeReturnInfos[] = $this->createNativeReturnInfo($overridingNativeType);
224+
$overridingNativeReturnTypes[] = $overridingNativeType;
223225
continue;
224226
}
225227

226-
// PHP requires a native return type of the overriding method when the overridden method declares one
227-
if ($contextRecord['nativeReturnType'] === null && !$this->willDeclareNativeReturnType($overridingKey)) {
228+
// PHP requires a native return type of the overriding method when the overridden method declares one,
229+
// before PHP 7.4 the same one, which the fix of the overriding method does not know about
230+
if ($contextRecord['nativeReturnType'] === null && (!$this->fixer->supportsReturnCovariance() || !$this->willDeclareNativeReturnType($overridingKey))) {
228231
return null;
229232
}
230233

@@ -253,6 +256,7 @@ private function createFix(string $key): ?callable
253256
$classReflection,
254257
count($parentReturnTypes) === 0 ? null : TypeCombinator::intersect(...$parentReturnTypes),
255258
count($parentNativeReturnTypes) === 0 ? null : TypeCombinator::intersect(...$parentNativeReturnTypes),
259+
$overridingNativeReturnTypes,
256260
);
257261
}
258262

‎src/Rules/MissingReturnTypeFixer.php‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ public function isEnabled(): bool
131131
return $this->enabled;
132132
}
133133

134+
public function supportsReturnCovariance(): bool
135+
{
136+
return $this->phpVersion->supportsReturnCovariance();
137+
}
138+
134139
/**
135140
* @return ReturnInfo
136141
*/
@@ -300,7 +305,11 @@ public function findParentReturnTypes(ClassReflection $classReflection, string $
300305
* The return type of a method has to be within the return types of the methods it overrides,
301306
* see findParentReturnTypes(), when it is not, the method breaks their contract and is not fixed.
302307
*
308+
* Before PHP 7.4 return types are invariant: an overriding method declares the native type of the method
309+
* it overrides, and a method is fixed only when the native types of the methods overriding it are the same.
310+
*
303311
* @param Type|null $declaredNativeType the native return type the function declares
312+
* @param list<Type> $overridingNativeReturnTypes the native return types of the methods overriding the method
304313
* @return (callable(ClassMethod|Function_): (ClassMethod|Function_))|null
305314
*/
306315
public function createFix(
@@ -310,6 +319,7 @@ public function createFix(
310319
?ClassReflection $classReflection,
311320
?Type $parentReturnType = null,
312321
?Type $parentNativeReturnType = null,
322+
array $overridingNativeReturnTypes = [],
313323
): ?callable
314324
{
315325
$generalizedPhpDocType = $this->generalize($phpDocType);
@@ -323,10 +333,12 @@ public function createFix(
323333
return null;
324334
}
325335

336+
$supportsReturnCovariance = $this->phpVersion->supportsReturnCovariance();
326337
$nativeTypeNode = null;
327338
$nativeType = $declaredNativeType;
328339
if ($nativeType === null) {
329-
$nativeTypeNode = $this->createNativeTypeNode($nativeSourceType);
340+
// without return type covariance, the overriding method declares the same native type, the narrower type is in PHPDoc
341+
$nativeTypeNode = $this->createNativeTypeNode(!$supportsReturnCovariance && $parentNativeReturnType !== null ? $parentNativeReturnType : $nativeSourceType);
330342
if ($nativeTypeNode === null) {
331343
return null;
332344
}
@@ -341,6 +353,15 @@ public function createFix(
341353
return null;
342354
}
343355

356+
// without return type covariance, the overriding methods have to declare the same native type
357+
if (!$supportsReturnCovariance) {
358+
foreach ($overridingNativeReturnTypes as $overridingNativeReturnType) {
359+
if (!$overridingNativeReturnType->equals($nativeType)) {
360+
return null;
361+
}
362+
}
363+
}
364+
344365
$returnedPhpDocType = $this->getPhpDocType($phpDocType, $classReflection);
345366
$phpDocTypeNode = $returnedPhpDocType->toPhpDocNode();
346367
// the native type says everything, a new @return tag would only repeat it
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules;
4+
5+
class MissingReturnTypeFixPhp73Test extends MissingReturnTypeFixTestCase
6+
{
7+
8+
protected static function getPhpVersionSuffix(): string
9+
{
10+
return 'php73';
11+
}
12+
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters:
2+
phpVersion: 70300
3+
featureToggles:
4+
fixMissingReturnTypes: true
5+
fixer:
6+
preferSelf: true

0 commit comments

Comments
 (0)