|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\TypeDeclaration\Rector\ClassMethod; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Identifier; |
| 9 | +use PhpParser\Node\Stmt\ClassMethod; |
| 10 | +use PhpParser\Node\Stmt\Function_; |
| 11 | +use PhpParser\Node\UnionType; |
| 12 | +use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; |
| 13 | +use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
| 14 | +use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; |
| 15 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; |
| 16 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
| 17 | +use Rector\Comments\NodeDocBlock\DocBlockUpdater; |
| 18 | +use Rector\Rector\AbstractRector; |
| 19 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 20 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 21 | + |
| 22 | +/** |
| 23 | + * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowBoolDocblockReturnTypeRector\NarrowBoolDocblockReturnTypeRectorTest |
| 24 | + */ |
| 25 | +final class NarrowBoolDocblockReturnTypeRector extends AbstractRector |
| 26 | +{ |
| 27 | + public function __construct( |
| 28 | + private readonly PhpDocInfoFactory $phpDocInfoFactory, |
| 29 | + private readonly DocBlockUpdater $docBlockUpdater, |
| 30 | + ) { |
| 31 | + } |
| 32 | + |
| 33 | + public function getRuleDefinition(): RuleDefinition |
| 34 | + { |
| 35 | + return new RuleDefinition( |
| 36 | + 'Narrow @return docblock "bool" to "false" or "true" when the native return type only allows one of them', |
| 37 | + [ |
| 38 | + new CodeSample( |
| 39 | + <<<'CODE_SAMPLE' |
| 40 | +class SomeClass |
| 41 | +{ |
| 42 | + /** |
| 43 | + * @return bool|string[] |
| 44 | + */ |
| 45 | + public function run(): false|array |
| 46 | + { |
| 47 | + return false; |
| 48 | + } |
| 49 | +} |
| 50 | +CODE_SAMPLE |
| 51 | + , |
| 52 | + <<<'CODE_SAMPLE' |
| 53 | +class SomeClass |
| 54 | +{ |
| 55 | + /** |
| 56 | + * @return false|string[] |
| 57 | + */ |
| 58 | + public function run(): false|array |
| 59 | + { |
| 60 | + return false; |
| 61 | + } |
| 62 | +} |
| 63 | +CODE_SAMPLE |
| 64 | + ), |
| 65 | + ], |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + public function getNodeTypes(): array |
| 70 | + { |
| 71 | + return [ClassMethod::class, Function_::class]; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param ClassMethod|Function_ $node |
| 76 | + */ |
| 77 | + public function refactor(Node $node): null|ClassMethod|Function_ |
| 78 | + { |
| 79 | + $constantBoolName = $this->matchNativeConstantBoolName($node); |
| 80 | + if ($constantBoolName === null) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + $phpDocInfo = $this->phpDocInfoFactory->createFromNode($node); |
| 85 | + if (! $phpDocInfo instanceof PhpDocInfo) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + $returnTagValueNode = $phpDocInfo->getReturnTagValue(); |
| 90 | + if (! $returnTagValueNode instanceof ReturnTagValueNode) { |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + if (! $returnTagValueNode->type instanceof UnionTypeNode) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + if (! $this->narrowBoolInUnion($returnTagValueNode->type, $constantBoolName)) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); |
| 103 | + |
| 104 | + return $node; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Returns "false" or "true" when the native return type allows exactly one of them, null otherwise. |
| 109 | + */ |
| 110 | + private function matchNativeConstantBoolName(ClassMethod|Function_ $node): ?string |
| 111 | + { |
| 112 | + $returnType = $node->returnType; |
| 113 | + if (! $returnType instanceof UnionType) { |
| 114 | + return null; |
| 115 | + } |
| 116 | + |
| 117 | + $hasFalse = false; |
| 118 | + $hasTrue = false; |
| 119 | + |
| 120 | + foreach ($returnType->types as $type) { |
| 121 | + if (! $type instanceof Identifier) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + $lowerName = $type->toLowerString(); |
| 126 | + if ($lowerName === 'bool') { |
| 127 | + // ambiguous, both values possible |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + if ($lowerName === 'false') { |
| 132 | + $hasFalse = true; |
| 133 | + } |
| 134 | + |
| 135 | + if ($lowerName === 'true') { |
| 136 | + $hasTrue = true; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + if ($hasFalse && ! $hasTrue) { |
| 141 | + return 'false'; |
| 142 | + } |
| 143 | + |
| 144 | + if ($hasTrue && ! $hasFalse) { |
| 145 | + return 'true'; |
| 146 | + } |
| 147 | + |
| 148 | + return null; |
| 149 | + } |
| 150 | + |
| 151 | + private function narrowBoolInUnion(UnionTypeNode $unionTypeNode, string $constantBoolName): bool |
| 152 | + { |
| 153 | + $hasChanged = false; |
| 154 | + |
| 155 | + foreach ($unionTypeNode->types as $key => $typeNode) { |
| 156 | + if ($typeNode instanceof IdentifierTypeNode && $typeNode->name === 'bool') { |
| 157 | + $unionTypeNode->types[$key] = new IdentifierTypeNode($constantBoolName); |
| 158 | + $hasChanged = true; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return $hasChanged; |
| 163 | + } |
| 164 | +} |
0 commit comments