Updated Rector to commit 717e3e00ca93c371f8633f6ea17ea1b5e686657a

717e3e00ca [Php81] Remove @readonly doc on transformation to native readonly on ReadOnlyPropertyRector (#5789)
This commit is contained in:
Tomas Votruba 2024-04-03 05:38:25 +00:00
parent 9f2e48fa07
commit 717507e265
3 changed files with 54 additions and 3 deletions

View File

@ -15,6 +15,10 @@ use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property; use PhpParser\Node\Stmt\Property;
use PhpParser\NodeTraverser; use PhpParser\NodeTraverser;
use PHPStan\Analyser\Scope; use PHPStan\Analyser\Scope;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\NodeAnalyzer\ParamAnalyzer; use Rector\NodeAnalyzer\ParamAnalyzer;
use Rector\NodeManipulator\PropertyFetchAssignManipulator; use Rector\NodeManipulator\PropertyFetchAssignManipulator;
use Rector\NodeManipulator\PropertyManipulator; use Rector\NodeManipulator\PropertyManipulator;
@ -60,13 +64,25 @@ final class ReadOnlyPropertyRector extends AbstractScopeAwareRector implements M
* @var \Rector\PhpParser\Node\BetterNodeFinder * @var \Rector\PhpParser\Node\BetterNodeFinder
*/ */
private $betterNodeFinder; private $betterNodeFinder;
public function __construct(PropertyManipulator $propertyManipulator, PropertyFetchAssignManipulator $propertyFetchAssignManipulator, ParamAnalyzer $paramAnalyzer, VisibilityManipulator $visibilityManipulator, BetterNodeFinder $betterNodeFinder) /**
* @readonly
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
*/
private $phpDocInfoFactory;
/**
* @readonly
* @var \Rector\Comments\NodeDocBlock\DocBlockUpdater
*/
private $docBlockUpdater;
public function __construct(PropertyManipulator $propertyManipulator, PropertyFetchAssignManipulator $propertyFetchAssignManipulator, ParamAnalyzer $paramAnalyzer, VisibilityManipulator $visibilityManipulator, BetterNodeFinder $betterNodeFinder, PhpDocInfoFactory $phpDocInfoFactory, DocBlockUpdater $docBlockUpdater)
{ {
$this->propertyManipulator = $propertyManipulator; $this->propertyManipulator = $propertyManipulator;
$this->propertyFetchAssignManipulator = $propertyFetchAssignManipulator; $this->propertyFetchAssignManipulator = $propertyFetchAssignManipulator;
$this->paramAnalyzer = $paramAnalyzer; $this->paramAnalyzer = $paramAnalyzer;
$this->visibilityManipulator = $visibilityManipulator; $this->visibilityManipulator = $visibilityManipulator;
$this->betterNodeFinder = $betterNodeFinder; $this->betterNodeFinder = $betterNodeFinder;
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->docBlockUpdater = $docBlockUpdater;
} }
public function getRuleDefinition() : RuleDefinition public function getRuleDefinition() : RuleDefinition
{ {
@ -170,8 +186,28 @@ CODE_SAMPLE
if ($attributeGroups !== []) { if ($attributeGroups !== []) {
$property->setAttribute(AttributeKey::ORIGINAL_NODE, null); $property->setAttribute(AttributeKey::ORIGINAL_NODE, null);
} }
$this->removeReadOnlyDoc($property);
return $property; return $property;
} }
/**
* @param \PhpParser\Node\Stmt\Property|\PhpParser\Node\Param $node
*/
private function removeReadOnlyDoc($node) : void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$readonlyDoc = $phpDocInfo->getByName('readonly');
if (!$readonlyDoc instanceof PhpDocTagNode) {
return;
}
if (!$readonlyDoc->value instanceof GenericTagValueNode) {
return;
}
if ($readonlyDoc->value->value !== '') {
return;
}
$phpDocInfo->removeByName('readonly');
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
}
private function refactorParam(Class_ $class, ClassMethod $classMethod, Param $param, Scope $scope) : ?\PhpParser\Node\Param private function refactorParam(Class_ $class, ClassMethod $classMethod, Param $param, Scope $scope) : ?\PhpParser\Node\Param
{ {
if (!$this->visibilityManipulator->hasVisibility($param, Visibility::PRIVATE)) { if (!$this->visibilityManipulator->hasVisibility($param, Visibility::PRIVATE)) {
@ -197,6 +233,7 @@ CODE_SAMPLE
$param->setAttribute(AttributeKey::ORIGINAL_NODE, null); $param->setAttribute(AttributeKey::ORIGINAL_NODE, null);
} }
$this->visibilityManipulator->makeReadonly($param); $this->visibilityManipulator->makeReadonly($param);
$this->removeReadOnlyDoc($param);
return $param; return $param;
} }
private function isPromotedPropertyAssigned(Class_ $class, Param $param) : bool private function isPromotedPropertyAssigned(Class_ $class, Param $param) : bool

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api * @api
* @var string * @var string
*/ */
public const PACKAGE_VERSION = '9b4ad93acd680c5da9a3bab4d5e1f46ad5905b7f'; public const PACKAGE_VERSION = '717e3e00ca93c371f8633f6ea17ea1b5e686657a';
/** /**
* @api * @api
* @var string * @var string
*/ */
public const RELEASE_DATE = '2024-04-03 11:59:45'; public const RELEASE_DATE = '2024-04-03 07:35:59';
/** /**
* @var int * @var int
*/ */

View File

@ -266,6 +266,20 @@ final class PhpDocInfo
}); });
return $hasChanged; return $hasChanged;
} }
public function removeByName(string $tagName) : bool
{
$tagName = '@' . \ltrim($tagName, '@');
$hasChanged = \false;
$phpDocNodeTraverser = new PhpDocNodeTraverser();
$phpDocNodeTraverser->traverseWithCallable($this->phpDocNode, '', static function (Node $node) use($tagName, &$hasChanged) : ?int {
if ($node instanceof PhpDocTagNode && $node->name === $tagName) {
$hasChanged = \true;
return PhpDocNodeTraverser::NODE_REMOVE;
}
return null;
});
return $hasChanged;
}
public function addTagValueNode(PhpDocTagValueNode $phpDocTagValueNode) : void public function addTagValueNode(PhpDocTagValueNode $phpDocTagValueNode) : void
{ {
if ($phpDocTagValueNode instanceof DoctrineAnnotationTagValueNode) { if ($phpDocTagValueNode instanceof DoctrineAnnotationTagValueNode) {