Skip to content

Commit

Permalink
Updated Rector to commit cdc57233696fd680c8ebe3040b02c54d1da9c779
Browse files Browse the repository at this point in the history
rectorphp/rector-src@cdc5723 [TypeDeclaration] Change bool to false or true docblock when union has false/true standalone type on ReturnUnionTypeRector (#5543)
  • Loading branch information
TomasVotruba committed Feb 2, 2024
1 parent b0211bc commit 5c93328
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
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\Rector\AbstractScopeAwareRector;
Expand Down Expand Up @@ -38,11 +44,23 @@ final class ReturnUnionTypeRector extends AbstractScopeAwareRector implements Mi
* @var \Rector\PHPStanStaticTypeMapper\TypeMapper\UnionTypeMapper
*/
private $unionTypeMapper;
public function __construct(ReturnTypeInferer $returnTypeInferer, ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, UnionTypeMapper $unionTypeMapper)
/**
* @readonly
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
*/
private $phpDocInfoFactory;
/**
* @readonly
* @var \Rector\Comments\NodeDocBlock\DocBlockUpdater
*/
private $docBlockUpdater;
public function __construct(ReturnTypeInferer $returnTypeInferer, ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, UnionTypeMapper $unionTypeMapper, PhpDocInfoFactory $phpDocInfoFactory, DocBlockUpdater $docBlockUpdater)
{
$this->returnTypeInferer = $returnTypeInferer;
$this->classMethodReturnTypeOverrideGuard = $classMethodReturnTypeOverrideGuard;
$this->unionTypeMapper = $unionTypeMapper;
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->docBlockUpdater = $docBlockUpdater;
}
public function getRuleDefinition() : RuleDefinition
{
Expand Down Expand Up @@ -115,7 +133,39 @@ public function refactorWithScope(Node $node, Scope $scope) : ?Node
if (!$returnType instanceof Node) {
return null;
}
$this->mapStandaloneSubType($node, $inferReturnType);
$node->returnType = $returnType;
return $node;
}
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $node
*/
private function mapStandaloneSubType($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;
}
}
}
}
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'fa75fcf0d1445f90cff9199875aba352671795f9';
public const PACKAGE_VERSION = 'cdc57233696fd680c8ebe3040b02c54d1da9c779';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-02-02 06:52:25';
public const RELEASE_DATE = '2024-02-02 13:43:27';
/**
* @var int
*/
Expand Down

0 comments on commit 5c93328

Please sign in to comment.