From 86f57eee49e0beae650cbef6ff70841849cca6a0 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 11 Sep 2023 19:45:57 +0200 Subject: [PATCH] make use of doc block updater --- composer.json | 2 +- .../Class_/DowngradeAttributeToAnnotationRector.php | 7 ++++++- .../Enum_/DowngradeEnumToConstantListClassRector.php | 11 ++++++++--- .../Property/DowngradeReadonlyPropertyRector.php | 3 +++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 8d3cb3a9..58c64141 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "phpunit/phpunit": "^10.3", "rector/phpstan-rules": "^0.7", "rector/rector-generator": "^0.7.3", - "rector/rector-src": "dev-main", + "rector/rector-src": "dev-tv-narrow-abstract-10", "symplify/easy-ci": "^11.2", "symplify/easy-coding-standard": "^12.0", "symplify/phpstan-extensions": "^11.3", diff --git a/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php b/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php index 9f222428..62eddb4f 100644 --- a/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php +++ b/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php @@ -14,6 +14,7 @@ use PhpParser\Node\Stmt\Property; use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; +use Rector\Comments\NodeDocBlock\DocBlockUpdater; use Rector\Core\Contract\Rector\ConfigurableRectorInterface; use Rector\Core\Rector\AbstractRector; use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation; @@ -42,7 +43,8 @@ final class DowngradeAttributeToAnnotationRector extends AbstractRector implemen private bool $isDowngraded = false; public function __construct( - private readonly DoctrineAnnotationFactory $doctrineAnnotationFactory + private readonly DoctrineAnnotationFactory $doctrineAnnotationFactory, + private readonly DocBlockUpdater $docBlockUpdater, ) { } @@ -123,6 +125,8 @@ public function refactor(Node $node): ?Node $phpDocInfo->addPhpDocTagNode( new PhpDocTagNode('@' . $attributeToAnnotation->getTag(), new GenericTagValueNode('')) ); + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); + continue; } @@ -131,6 +135,7 @@ public function refactor(Node $node): ?Node $attributeToAnnotation->getTag() ); $phpDocInfo->addTagValueNode($doctrineAnnotation); + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); } } diff --git a/rules/DowngradePhp80/Rector/Enum_/DowngradeEnumToConstantListClassRector.php b/rules/DowngradePhp80/Rector/Enum_/DowngradeEnumToConstantListClassRector.php index 12076033..4f366226 100644 --- a/rules/DowngradePhp80/Rector/Enum_/DowngradeEnumToConstantListClassRector.php +++ b/rules/DowngradePhp80/Rector/Enum_/DowngradeEnumToConstantListClassRector.php @@ -19,6 +19,7 @@ use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ReflectionProvider; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; +use Rector\Comments\NodeDocBlock\DocBlockUpdater; use Rector\Core\Rector\AbstractRector; use Rector\DowngradePhp80\NodeAnalyzer\EnumAnalyzer; use Rector\NodeFactory\ClassFromEnumFactory; @@ -34,6 +35,7 @@ public function __construct( private readonly ClassFromEnumFactory $classFromEnumFactory, private readonly ReflectionProvider $reflectionProvider, private readonly EnumAnalyzer $enumAnalyzer, + private readonly DocBlockUpdater $docBlockUpdater, ) { } @@ -81,7 +83,7 @@ public function refactor(Node $node): Class_|ClassMethod|null } $hasChanged = false; - $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); + $classMethodPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); foreach ($node->params as $param) { if ($param->type instanceof Name) { @@ -110,7 +112,7 @@ public function refactor(Node $node): Class_|ClassMethod|null $hasChanged = true; - $this->decorateParamDocType($classLikeReflection, $param, $phpDocInfo, $isNullable); + $this->decorateParamDocType($classLikeReflection, $param, $classMethodPhpDocInfo, $isNullable, $node); } if ($hasChanged) { @@ -124,7 +126,8 @@ private function decorateParamDocType( ClassReflection $classReflection, Param $param, PhpDocInfo $phpDocInfo, - bool $isNullable + bool $isNullable, + ClassMethod $classMethod ): void { $constFetchNode = new ConstFetchNode('\\' . $classReflection->getName(), '*'); $constTypeNode = new ConstTypeNode($constFetchNode); @@ -134,6 +137,8 @@ private function decorateParamDocType( $paramTagValueNode = new ParamTagValueNode($paramTypeNode, false, $paramName, ''); $phpDocInfo->addTagValueNode($paramTagValueNode); + + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod); } private function refactorParamType(ClassReflection $classReflection, bool $isNullable, Param $param): void diff --git a/rules/DowngradePhp81/Rector/Property/DowngradeReadonlyPropertyRector.php b/rules/DowngradePhp81/Rector/Property/DowngradeReadonlyPropertyRector.php index 0c47d57c..fb783cef 100644 --- a/rules/DowngradePhp81/Rector/Property/DowngradeReadonlyPropertyRector.php +++ b/rules/DowngradePhp81/Rector/Property/DowngradeReadonlyPropertyRector.php @@ -9,6 +9,7 @@ use PhpParser\Node\Stmt\Property; use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; +use Rector\Comments\NodeDocBlock\DocBlockUpdater; use Rector\Core\Rector\AbstractRector; use Rector\Privatization\NodeManipulator\VisibilityManipulator; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -28,6 +29,7 @@ final class DowngradeReadonlyPropertyRector extends AbstractRector public function __construct( private readonly VisibilityManipulator $visibilityManipulator, + private readonly DocBlockUpdater $docBlockUpdater, ) { } @@ -103,5 +105,6 @@ private function addPhpDocTag(Property $property): void } $phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@' . self::TAGNAME, new GenericTagValueNode(''))); + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property); } }