Skip to content

Commit 73e0dbb

Browse files
authored
[DowngradePhp81] Handle parent is void on DowngradeNeverTypeDeclarationRector (#311)
1 parent b4c47bf commit 73e0dbb

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp81\Rector\FunctionLike\DowngradeNeverTypeDeclarationRector\Fixture;
4+
5+
use Rector\Tests\DowngradePhp81\Rector\FunctionLike\DowngradeNeverTypeDeclarationRector\Source\SomeParentVoid;
6+
7+
class WithParentIsVoid extends SomeParentVoid
8+
{
9+
public function run(): never
10+
{
11+
}
12+
}
13+
14+
?>
15+
-----
16+
<?php
17+
18+
namespace Rector\Tests\DowngradePhp81\Rector\FunctionLike\DowngradeNeverTypeDeclarationRector\Fixture;
19+
20+
use Rector\Tests\DowngradePhp81\Rector\FunctionLike\DowngradeNeverTypeDeclarationRector\Source\SomeParentVoid;
21+
22+
class WithParentIsVoid extends SomeParentVoid
23+
{
24+
/**
25+
* @return never
26+
*/
27+
public function run(): void
28+
{
29+
}
30+
}
31+
32+
?>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp81\Rector\FunctionLike\DowngradeNeverTypeDeclarationRector\Source;
6+
7+
class SomeParentVoid
8+
{
9+
public function run(): void
10+
{
11+
}
12+
}

src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
1919
use PHPStan\Reflection\ClassReflection;
2020
use PHPStan\Type\MixedType;
21+
use PHPStan\Type\NeverType;
2122
use PHPStan\Type\ObjectType;
2223
use PHPStan\Type\ThisType;
2324
use PHPStan\Type\Type;
@@ -65,8 +66,10 @@ public function __construct(
6566
];
6667
}
6768

68-
public function decorateReturn(ClassMethod|Function_|Closure|ArrowFunction $functionLike): void
69-
{
69+
public function decorateReturn(
70+
ClassMethod|Function_|Closure|ArrowFunction $functionLike,
71+
?Type $requireType = null
72+
): void {
7073
if (! $functionLike->returnType instanceof Node) {
7174
return;
7275
}
@@ -123,6 +126,14 @@ public function decorateReturn(ClassMethod|Function_|Closure|ArrowFunction $func
123126
$functionLike->returnType = new FullyQualified($returnType->toString());
124127
break;
125128
}
129+
130+
if ($requireType instanceof NeverType && $returnType instanceof Identifier && $this->nodeNameResolver->isName(
131+
$returnType,
132+
'void'
133+
)) {
134+
$functionLike->returnType = new Identifier('void');
135+
break;
136+
}
126137
}
127138

128139
if (! $this->isRequireReturnTypeWillChange($classReflection, $functionLike)) {
@@ -196,7 +207,7 @@ public function decorateReturnWithSpecificType(
196207
return false;
197208
}
198209

199-
$this->decorateReturn($functionLike);
210+
$this->decorateReturn($functionLike, $requireType);
200211
return true;
201212
}
202213

0 commit comments

Comments
 (0)