Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rector\Tests\DowngradePhp81\Rector\FunctionLike\DowngradeNeverTypeDeclarationRector\Fixture;

use Rector\Tests\DowngradePhp81\Rector\FunctionLike\DowngradeNeverTypeDeclarationRector\Source\SomeParentVoid;

class WithParentIsVoid extends SomeParentVoid
{
public function run(): never
{
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp81\Rector\FunctionLike\DowngradeNeverTypeDeclarationRector\Fixture;

use Rector\Tests\DowngradePhp81\Rector\FunctionLike\DowngradeNeverTypeDeclarationRector\Source\SomeParentVoid;

class WithParentIsVoid extends SomeParentVoid
{
/**
* @return never
*/
public function run(): void
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp81\Rector\FunctionLike\DowngradeNeverTypeDeclarationRector\Source;

class SomeParentVoid
{
public function run(): void
{
}
}
17 changes: 14 additions & 3 deletions src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -65,8 +66,10 @@ public function __construct(
];
}

public function decorateReturn(ClassMethod|Function_|Closure|ArrowFunction $functionLike): void
{
public function decorateReturn(
ClassMethod|Function_|Closure|ArrowFunction $functionLike,
?Type $requireType = null
): void {
if (! $functionLike->returnType instanceof Node) {
return;
}
Expand Down Expand Up @@ -123,6 +126,14 @@ public function decorateReturn(ClassMethod|Function_|Closure|ArrowFunction $func
$functionLike->returnType = new FullyQualified($returnType->toString());
break;
}

if ($requireType instanceof NeverType && $returnType instanceof Identifier && $this->nodeNameResolver->isName(
$returnType,
'void'
)) {
$functionLike->returnType = new Identifier('void');
break;
}
}

if (! $this->isRequireReturnTypeWillChange($classReflection, $functionLike)) {
Expand Down Expand Up @@ -196,7 +207,7 @@ public function decorateReturnWithSpecificType(
return false;
}

$this->decorateReturn($functionLike);
$this->decorateReturn($functionLike, $requireType);
return true;
}

Expand Down