diff --git a/config/set/downgrade-php82.php b/config/set/downgrade-php82.php index 707b6493..372883ad 100644 --- a/config/set/downgrade-php82.php +++ b/config/set/downgrade-php82.php @@ -5,8 +5,12 @@ use Rector\Config\RectorConfig; use Rector\Core\ValueObject\PhpVersion; use Rector\DowngradePhp82\Rector\Class_\DowngradeReadonlyClassRector; +use Rector\DowngradePhp82\Rector\FunctionLike\DowngradeStandaloneNullTrueFalseReturnTypeRector; return static function (RectorConfig $rectorConfig): void { $rectorConfig->phpVersion(PhpVersion::PHP_81); - $rectorConfig->rule(DowngradeReadonlyClassRector::class); + $rectorConfig->rules([ + DowngradeReadonlyClassRector::class, + DowngradeStandaloneNullTrueFalseReturnTypeRector::class + ]); }; diff --git a/rules/DowngradePhp82/Rector/FunctionLike/DowngradeStandaloneNullTrueFalseReturnTypeRector.php b/rules/DowngradePhp82/Rector/FunctionLike/DowngradeStandaloneNullTrueFalseReturnTypeRector.php index c096c609..946dc830 100644 --- a/rules/DowngradePhp82/Rector/FunctionLike/DowngradeStandaloneNullTrueFalseReturnTypeRector.php +++ b/rules/DowngradePhp82/Rector/FunctionLike/DowngradeStandaloneNullTrueFalseReturnTypeRector.php @@ -11,7 +11,9 @@ use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; +use PHPStan\Type\Constant\ConstantBooleanType; use PHPStan\Type\NullType; +use PHPStan\Type\Type; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; use Rector\Core\Rector\AbstractRector; @@ -85,22 +87,36 @@ public function refactor(Node $node): ?Node } if (! $node instanceof ClassMethod) { - $node->returnType = new Identifier('mixed'); - // in closure and arrow function can't add `@return null` docblock as they are Expr // that rely on Stmt $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); - $this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, new NullType()); + $this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, $this->resolveType($node->returnType)); + $node->returnType = new Identifier('mixed'); return $node; } + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); + $this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, $this->resolveType($node->returnType)); + // todo: verify parent $node->returnType = new Identifier('mixed'); - $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); - $this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, new NullType()); - return $node; } + + private function resolveType(Node $node): Type + { + $nodeName = $this->getName($node); + + if ($nodeName === 'null') { + return new NullType(); + } + + if ($nodeName === 'false') { + return new ConstantBooleanType(false); + } + + return new ConstantBooleanType(true); + } }