Skip to content

Commit 8157916

Browse files
committed
Do not require to have @return null in descendant classes because it's dumb
1 parent afcdbfc commit 8157916

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\Reflection\ParametersAcceptorSelector;
1010
use PHPStan\Rules\Rule;
1111
use PHPStan\Rules\RuleErrorBuilder;
12+
use PHPStan\Type\NullType;
1213
use PHPStan\Type\TypeCombinator;
1314
use PHPStan\Type\UnionType;
1415
use PHPStan\Type\VerbosityLevel;
@@ -38,8 +39,8 @@ public function processNode(Node $node, Scope $scope): array
3839
if (!$method instanceof MethodReflection) {
3940
throw new \PHPStan\ShouldNotHappenException();
4041
}
42+
$isFirstDeclaration = $method->getPrototype()->getDeclaringClass() === $method->getDeclaringClass();
4143
if (!$method->isPrivate()) {
42-
$isFirstDeclaration = $method->getPrototype()->getDeclaringClass() === $method->getDeclaringClass();
4344
if (!$isFirstDeclaration) {
4445
if (PHP_VERSION_ID < 70400 || !$this->checkPossibleCovariantMethodReturnType) {
4546
return [];
@@ -78,6 +79,15 @@ public function processNode(Node $node, Scope $scope): array
7879
}
7980

8081
$returnType = TypeCombinator::union(...$returnTypes);
82+
if (
83+
PHP_VERSION_ID >= 70400
84+
&& $this->checkPossibleCovariantMethodReturnType
85+
&& !$method->isPrivate()
86+
&& $returnType instanceof NullType
87+
&& !$isFirstDeclaration
88+
) {
89+
return [];
90+
}
8191

8292
$messages = [];
8393
foreach ($methodReturnType->getTypes() as $type) {

0 commit comments

Comments
 (0)