Skip to content

Commit 4b06462

Browse files
committed
Fix too-wide return types of methods whose parents accept what they return
The parent of a method implementing Iterator<int, Foo>::current() with @return Foo|null returns Foo, which does not accept the declared type but accepts the narrowed one. Only a parent return type that does not overlap with the returned type blocks the fix. Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
1 parent a4f1033 commit 4b06462

12 files changed

Lines changed: 446 additions & 5 deletions

‎src/Rules/TooWideTypehints/TooWideReturnTypeFixer.php‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,15 @@ public function canNarrowNativeReturnType(?ClassReflection $classReflection, str
117117
}
118118

119119
/**
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.
120+
* Whether the return types of the methods the method overrides overlap with the type it returns.
121+
* A narrowed return type keeps what the method returns, so when they do not overlap,
122+
* narrowing the method would make it incompatible with its parents.
122123
*/
123-
public function isCompatibleWithParentMethods(ClassReflection $classReflection, string $functionName, Type $returnType): bool
124+
public function isCompatibleWithParentMethods(ClassReflection $classReflection, string $functionName, Type $returnedType): bool
124125
{
125126
foreach ($this->parentMethodHelper->collectParentMethods($functionName, $classReflection) as [$parentMethod]) {
126127
foreach ($parentMethod->getVariants() as $variant) {
127-
if (!TypeUtils::resolveLateResolvableTypes($variant->getReturnType())->isSuperTypeOf($returnType)->yes()) {
128+
if (TypeUtils::resolveLateResolvableTypes($variant->getReturnType())->isSuperTypeOf($returnedType)->no()) {
128129
return false;
129130
}
130131
}

‎src/Rules/TooWideTypehints/TooWideTypeCheck.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ public function createReturnTypeFixPlan(
301301
$functionNode = $node->getClassMethod();
302302
$classReflection = $node->getClassReflection();
303303
$functionName = $functionNode->name->toString();
304-
if (!$this->returnTypeFixer->isCompatibleWithParentMethods($classReflection, $functionName, $functionReturnType)) {
304+
if ($returnType !== null && !$this->returnTypeFixer->isCompatibleWithParentMethods($classReflection, $functionName, $returnType)) {
305305
return null;
306306
}
307307
} else {

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,47 @@ class ImplementsCurrent implements HasCurrent
255255
}
256256

257257
}
258+
259+
/**
260+
* @implements \Iterator<int, \stdClass>
261+
*/
262+
class GenericParentWithoutNull implements \Iterator
263+
{
264+
265+
/** @var list<\stdClass> */
266+
private $items = [];
267+
268+
/** @var int */
269+
private $i = 0;
270+
271+
/**
272+
* @return \stdClass
273+
*/
274+
#[\ReturnTypeWillChange]
275+
public function current()
276+
{
277+
return $this->items[$this->i];
278+
}
279+
280+
#[\ReturnTypeWillChange]
281+
public function key()
282+
{
283+
return $this->i;
284+
}
285+
286+
public function next(): void
287+
{
288+
$this->i++;
289+
}
290+
291+
public function rewind(): void
292+
{
293+
$this->i = 0;
294+
}
295+
296+
public function valid(): bool
297+
{
298+
return isset($this->items[$this->i]);
299+
}
300+
301+
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,47 @@ class ImplementsCurrent implements HasCurrent
255255
}
256256

257257
}
258+
259+
/**
260+
* @implements \Iterator<int, \stdClass>
261+
*/
262+
class GenericParentWithoutNull implements \Iterator
263+
{
264+
265+
/** @var list<\stdClass> */
266+
private $items = [];
267+
268+
/** @var int */
269+
private $i = 0;
270+
271+
/**
272+
* @return \stdClass
273+
*/
274+
#[\ReturnTypeWillChange]
275+
public function current()
276+
{
277+
return $this->items[$this->i];
278+
}
279+
280+
#[\ReturnTypeWillChange]
281+
public function key()
282+
{
283+
return $this->i;
284+
}
285+
286+
public function next(): void
287+
{
288+
$this->i++;
289+
}
290+
291+
public function rewind(): void
292+
{
293+
$this->i = 0;
294+
}
295+
296+
public function valid(): bool
297+
{
298+
return isset($this->items[$this->i]);
299+
}
300+
301+
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,47 @@ class ImplementsCurrent implements HasCurrent
255255
}
256256

257257
}
258+
259+
/**
260+
* @implements \Iterator<int, \stdClass>
261+
*/
262+
class GenericParentWithoutNull implements \Iterator
263+
{
264+
265+
/** @var list<\stdClass> */
266+
private $items = [];
267+
268+
/** @var int */
269+
private $i = 0;
270+
271+
/**
272+
* @return \stdClass
273+
*/
274+
#[\ReturnTypeWillChange]
275+
public function current()
276+
{
277+
return $this->items[$this->i];
278+
}
279+
280+
#[\ReturnTypeWillChange]
281+
public function key()
282+
{
283+
return $this->i;
284+
}
285+
286+
public function next(): void
287+
{
288+
$this->i++;
289+
}
290+
291+
public function rewind(): void
292+
{
293+
$this->i = 0;
294+
}
295+
296+
public function valid(): bool
297+
{
298+
return isset($this->items[$this->i]);
299+
}
300+
301+
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,47 @@ class ImplementsCurrent implements HasCurrent
255255
}
256256

257257
}
258+
259+
/**
260+
* @implements \Iterator<int, \stdClass>
261+
*/
262+
class GenericParentWithoutNull implements \Iterator
263+
{
264+
265+
/** @var list<\stdClass> */
266+
private $items = [];
267+
268+
/** @var int */
269+
private $i = 0;
270+
271+
/**
272+
* @return \stdClass
273+
*/
274+
#[\ReturnTypeWillChange]
275+
public function current()
276+
{
277+
return $this->items[$this->i];
278+
}
279+
280+
#[\ReturnTypeWillChange]
281+
public function key()
282+
{
283+
return $this->i;
284+
}
285+
286+
public function next(): void
287+
{
288+
$this->i++;
289+
}
290+
291+
public function rewind(): void
292+
{
293+
$this->i = 0;
294+
}
295+
296+
public function valid(): bool
297+
{
298+
return isset($this->items[$this->i]);
299+
}
300+
301+
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,47 @@ class ImplementsCurrent implements HasCurrent
255255
}
256256

257257
}
258+
259+
/**
260+
* @implements \Iterator<int, \stdClass>
261+
*/
262+
class GenericParentWithoutNull implements \Iterator
263+
{
264+
265+
/** @var list<\stdClass> */
266+
private $items = [];
267+
268+
/** @var int */
269+
private $i = 0;
270+
271+
/**
272+
* @return \stdClass
273+
*/
274+
#[\ReturnTypeWillChange]
275+
public function current()
276+
{
277+
return $this->items[$this->i];
278+
}
279+
280+
#[\ReturnTypeWillChange]
281+
public function key()
282+
{
283+
return $this->i;
284+
}
285+
286+
public function next(): void
287+
{
288+
$this->i++;
289+
}
290+
291+
public function rewind(): void
292+
{
293+
$this->i = 0;
294+
}
295+
296+
public function valid(): bool
297+
{
298+
return isset($this->items[$this->i]);
299+
}
300+
301+
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,47 @@ class ImplementsCurrent implements HasCurrent
255255
}
256256

257257
}
258+
259+
/**
260+
* @implements \Iterator<int, \stdClass>
261+
*/
262+
class GenericParentWithoutNull implements \Iterator
263+
{
264+
265+
/** @var list<\stdClass> */
266+
private $items = [];
267+
268+
/** @var int */
269+
private $i = 0;
270+
271+
/**
272+
* @return \stdClass
273+
*/
274+
#[\ReturnTypeWillChange]
275+
public function current()
276+
{
277+
return $this->items[$this->i];
278+
}
279+
280+
#[\ReturnTypeWillChange]
281+
public function key()
282+
{
283+
return $this->i;
284+
}
285+
286+
public function next(): void
287+
{
288+
$this->i++;
289+
}
290+
291+
public function rewind(): void
292+
{
293+
$this->i = 0;
294+
}
295+
296+
public function valid(): bool
297+
{
298+
return isset($this->items[$this->i]);
299+
}
300+
301+
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,47 @@ class ImplementsCurrent implements HasCurrent
255255
}
256256

257257
}
258+
259+
/**
260+
* @implements \Iterator<int, \stdClass>
261+
*/
262+
class GenericParentWithoutNull implements \Iterator
263+
{
264+
265+
/** @var list<\stdClass> */
266+
private $items = [];
267+
268+
/** @var int */
269+
private $i = 0;
270+
271+
/**
272+
* @return \stdClass|null
273+
*/
274+
#[\ReturnTypeWillChange]
275+
public function current()
276+
{
277+
return $this->items[$this->i];
278+
}
279+
280+
#[\ReturnTypeWillChange]
281+
public function key()
282+
{
283+
return $this->i;
284+
}
285+
286+
public function next(): void
287+
{
288+
$this->i++;
289+
}
290+
291+
public function rewind(): void
292+
{
293+
$this->i = 0;
294+
}
295+
296+
public function valid(): bool
297+
{
298+
return isset($this->items[$this->i]);
299+
}
300+
301+
}

0 commit comments

Comments
 (0)