Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReturnUnionTypeRector now skips nullable types, to prepare space for new nullable rule as off PHP 7.1+ #6105

Merged
merged 3 commits into from
Jul 2, 2024
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
Expand Up @@ -26,7 +26,7 @@ namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\
final class FalseBoolDocblock
{
/**
* @return array|false some description
* @return array|bool some description
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new rule to update sub type by docblock seems needed, to ensure not introduce phpstan notice

https://phpstan.org/r/49cbf30f-03e8-4b82-b47e-b649cadc7a34

Copy link
Member Author

@TomasVotruba TomasVotruba Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docblock changes are out of our scope since Rector 0.15. This will have to be dealt manualy.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will ok for 1 or 2 files, but for many files, imo, that's worth it, I think coding-style or code-quality set will match that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's beyond our scope. I'd leave that for manual review or custom set 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this seems rather rare case.

*/
public function run(): array|false
{
Expand Down

This file was deleted.

This file was deleted.

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\FixturePhp81;

final class SkipNullableHandledByAnotherRule
{
public function run($value)
{
if ($value) {
return null;
}

return false;
}
}

This file was deleted.

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\FixtureTrueInUnion;

final class SkipNullableHandledByAnotherRule
{
public function run($value)
{

if (rand(0, 1)) {
return false;
}

return null;
}
}

This file was deleted.

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

namespace Rector\Tests\TypeDeclaration\Rector\Closure\AddClosureUnionReturnTypeRector\Fixture;

final class SkipNullableArray
{
public function run()
{
function () {
if (rand(0, 1)) {
return [];
}

return null;
};
}
}
57 changes: 12 additions & 45 deletions rules/TypeDeclaration/NodeManipulator/AddUnionReturnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@

use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\PHPStanStaticTypeMapper\TypeMapper\UnionTypeMapper;
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
Expand All @@ -26,18 +21,23 @@
public function __construct(
private ReturnTypeInferer $returnTypeInferer,
private UnionTypeMapper $unionTypeMapper,
private PhpDocInfoFactory $phpDocInfoFactory,
private DocBlockUpdater $docBlockUpdater,
private ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard,
) {
}

/**
* @template TCallLike as ClassMethod|Function_|Closure
*
* @param TCallLike $node
* @return TCallLike|null
*/
public function add(ClassMethod|Function_|Closure $node, Scope $scope): ClassMethod|Function_|Closure|null
{
if ($node->stmts === null) {
return null;
}

// type is already known
if ($node->returnType instanceof Node) {
return null;
}
Expand All @@ -59,45 +59,12 @@ public function add(ClassMethod|Function_|Closure $node, Scope $scope): ClassMet
return null;
}

$this->mapStandaloneSubType($node, $inferReturnType);
// handled by another PHP 7.1 rule with broader scope
if ($returnType instanceof NullableType) {
return null;
}

$node->returnType = $returnType;
return $node;
}

private function mapStandaloneSubType(ClassMethod|Function_|Closure $node, UnionType $unionType): void
{
$value = null;

foreach ($unionType->getTypes() as $type) {
if ($type instanceof ConstantBooleanType) {
$value = $type->getValue() ? 'true' : 'false';
break;
}
}

if ($value === null) {
return;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$returnType = $phpDocInfo->getReturnTagValue();

if (! $returnType instanceof ReturnTagValueNode) {
return;
}

if (! $returnType->type instanceof BracketsAwareUnionTypeNode) {
return;
}

foreach ($returnType->type->types as $key => $type) {
if ($type instanceof IdentifierTypeNode && $type->__toString() === 'bool') {
$returnType->type->types[$key] = new IdentifierTypeNode($value);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);

break;
}
}
}
}