Skip to content

Commit a4f1033

Browse files
committed
Report never-returned null of overriding methods in a closed project
Like the check without collected data, only nested types of a method overriding another one keep null. A method implementing Iterator::current() with @return Foo|null that never returns null was reported in an open project checking protected and public methods, but not in a closed project. Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
1 parent 5bc3b20 commit a4f1033

11 files changed

Lines changed: 237 additions & 6 deletions

‎src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public function processNode(Node $node, Scope&CollectedDataEmitter $scope): arra
8383
$functionDescription,
8484
$checkDescendantClass,
8585
$scope,
86+
false,
8687
);
8788
if ($result !== null) {
8889
$this->emitDeclaration($node, $scope, $result, false);

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class ChildClass extends ParentClass
6565
{
6666

6767
/**
68-
* @return Dog|null
68+
* @return Dog
6969
*/
7070
public function narrowedByChild()
7171
{
@@ -127,7 +127,7 @@ class MiddleClass extends GrandParent
127127
{
128128

129129
/**
130-
* @return Dog|int|null
130+
* @return Dog|int
131131
*/
132132
public function chain()
133133
{
@@ -232,3 +232,26 @@ class IncompatibleChild extends IncompatibleParent
232232
}
233233

234234
}
235+
236+
interface HasCurrent
237+
{
238+
239+
/**
240+
* @return mixed
241+
*/
242+
public function current();
243+
244+
}
245+
246+
class ImplementsCurrent implements HasCurrent
247+
{
248+
249+
/**
250+
* @return \stdClass
251+
*/
252+
public function current()
253+
{
254+
return new \stdClass();
255+
}
256+
257+
}

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class ChildClass extends ParentClass
6565
{
6666

6767
/**
68-
* @return Dog|null
68+
* @return Dog
6969
*/
7070
public function narrowedByChild()
7171
{
@@ -127,7 +127,7 @@ class MiddleClass extends GrandParent
127127
{
128128

129129
/**
130-
* @return Dog|int|null
130+
* @return Dog|int
131131
*/
132132
public function chain()
133133
{
@@ -232,3 +232,26 @@ class IncompatibleChild extends IncompatibleParent
232232
}
233233

234234
}
235+
236+
interface HasCurrent
237+
{
238+
239+
/**
240+
* @return mixed
241+
*/
242+
public function current();
243+
244+
}
245+
246+
class ImplementsCurrent implements HasCurrent
247+
{
248+
249+
/**
250+
* @return \stdClass
251+
*/
252+
public function current()
253+
{
254+
return new \stdClass();
255+
}
256+
257+
}

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class ChildClass extends ParentClass
6565
{
6666

6767
/**
68-
* @return Dog|null
68+
* @return Dog
6969
*/
7070
public function narrowedByChild()
7171
{
@@ -127,7 +127,7 @@ class MiddleClass extends GrandParent
127127
{
128128

129129
/**
130-
* @return Dog|int|null
130+
* @return Dog|int
131131
*/
132132
public function chain()
133133
{
@@ -232,3 +232,26 @@ class IncompatibleChild extends IncompatibleParent
232232
}
233233

234234
}
235+
236+
interface HasCurrent
237+
{
238+
239+
/**
240+
* @return mixed
241+
*/
242+
public function current();
243+
244+
}
245+
246+
class ImplementsCurrent implements HasCurrent
247+
{
248+
249+
/**
250+
* @return \stdClass
251+
*/
252+
public function current()
253+
{
254+
return new \stdClass();
255+
}
256+
257+
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,26 @@ class IncompatibleChild extends IncompatibleParent
232232
}
233233

234234
}
235+
236+
interface HasCurrent
237+
{
238+
239+
/**
240+
* @return mixed
241+
*/
242+
public function current();
243+
244+
}
245+
246+
class ImplementsCurrent implements HasCurrent
247+
{
248+
249+
/**
250+
* @return \stdClass
251+
*/
252+
public function current()
253+
{
254+
return new \stdClass();
255+
}
256+
257+
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,26 @@ class IncompatibleChild extends IncompatibleParent
232232
}
233233

234234
}
235+
236+
interface HasCurrent
237+
{
238+
239+
/**
240+
* @return mixed
241+
*/
242+
public function current();
243+
244+
}
245+
246+
class ImplementsCurrent implements HasCurrent
247+
{
248+
249+
/**
250+
* @return \stdClass
251+
*/
252+
public function current()
253+
{
254+
return new \stdClass();
255+
}
256+
257+
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,26 @@ class IncompatibleChild extends IncompatibleParent
232232
}
233233

234234
}
235+
236+
interface HasCurrent
237+
{
238+
239+
/**
240+
* @return mixed
241+
*/
242+
public function current();
243+
244+
}
245+
246+
class ImplementsCurrent implements HasCurrent
247+
{
248+
249+
/**
250+
* @return \stdClass
251+
*/
252+
public function current()
253+
{
254+
return new \stdClass();
255+
}
256+
257+
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,26 @@ class IncompatibleChild extends IncompatibleParent
232232
}
233233

234234
}
235+
236+
interface HasCurrent
237+
{
238+
239+
/**
240+
* @return mixed
241+
*/
242+
public function current();
243+
244+
}
245+
246+
class ImplementsCurrent implements HasCurrent
247+
{
248+
249+
/**
250+
* @return \stdClass|null
251+
*/
252+
public function current()
253+
{
254+
return new \stdClass();
255+
}
256+
257+
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,26 @@ class IncompatibleChild extends IncompatibleParent
232232
}
233233

234234
}
235+
236+
interface HasCurrent
237+
{
238+
239+
/**
240+
* @return mixed
241+
*/
242+
public function current();
243+
244+
}
245+
246+
class ImplementsCurrent implements HasCurrent
247+
{
248+
249+
/**
250+
* @return \stdClass|null
251+
*/
252+
public function current()
253+
{
254+
return new \stdClass();
255+
}
256+
257+
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,26 @@ class IncompatibleChild extends IncompatibleParent
232232
}
233233

234234
}
235+
236+
interface HasCurrent
237+
{
238+
239+
/**
240+
* @return mixed
241+
*/
242+
public function current();
243+
244+
}
245+
246+
class ImplementsCurrent implements HasCurrent
247+
{
248+
249+
/**
250+
* @return \stdClass|null
251+
*/
252+
public function current()
253+
{
254+
return new \stdClass();
255+
}
256+
257+
}

0 commit comments

Comments
 (0)