From 5c098e3472edb521739765678666da549bf699f7 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 11 Dec 2023 11:34:05 +0700 Subject: [PATCH] add docblock @return null --- .../Fixture/return_null.php.inc | 3 +++ ...eStandaloneNullTrueFalseReturnTypeRector.php | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/rules-tests/DowngradePhp82/Rector/FunctionLike/DowngradeStandaloneNullTrueFalseReturnTypeRector/Fixture/return_null.php.inc b/rules-tests/DowngradePhp82/Rector/FunctionLike/DowngradeStandaloneNullTrueFalseReturnTypeRector/Fixture/return_null.php.inc index 701279eb..075872fc 100644 --- a/rules-tests/DowngradePhp82/Rector/FunctionLike/DowngradeStandaloneNullTrueFalseReturnTypeRector/Fixture/return_null.php.inc +++ b/rules-tests/DowngradePhp82/Rector/FunctionLike/DowngradeStandaloneNullTrueFalseReturnTypeRector/Fixture/return_null.php.inc @@ -18,6 +18,9 @@ namespace Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeStandaloneNullTrueF final class ReturnNull { + /** + * @return null + */ public function run(): mixed { return null; diff --git a/rules/DowngradePhp82/Rector/FunctionLike/DowngradeStandaloneNullTrueFalseReturnTypeRector.php b/rules/DowngradePhp82/Rector/FunctionLike/DowngradeStandaloneNullTrueFalseReturnTypeRector.php index 7338ead4..8dc7b8c7 100644 --- a/rules/DowngradePhp82/Rector/FunctionLike/DowngradeStandaloneNullTrueFalseReturnTypeRector.php +++ b/rules/DowngradePhp82/Rector/FunctionLike/DowngradeStandaloneNullTrueFalseReturnTypeRector.php @@ -8,6 +8,9 @@ use PhpParser\Node\FunctionLike; use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\ClassMethod; +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; +use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; +use Rector\Comments\NodeDocBlock\DocBlockUpdater; use Rector\Core\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -19,6 +22,14 @@ */ final class DowngradeStandaloneNullTrueFalseReturnTypeRector extends AbstractRector { + public function __construct( + private readonly PhpDocInfoFactory $phpDocInfoFactory, + private readonly PhpDocTypeChanger $phpDocTypeChanger, + private readonly DocBlockUpdater $docBlockUpdater + ) + { + } + /** * @return array> */ @@ -79,6 +90,10 @@ public function refactor(Node $node): ?Node // todo: verify parent $node->returnType = new Identifier('mixed'); - return null; + + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); + $this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, new \PHPStan\Type\NullType()); + + return $node; } }