Skip to content

Commit

Permalink
Updated Rector to commit a2f70054a7d976acd2c73b06789095b998829be6
Browse files Browse the repository at this point in the history
rectorphp/rector-src@a2f7005 [DocBlock] Update docblock contents right in the rule (#4999)
  • Loading branch information
TomasVotruba committed Sep 11, 2023
1 parent 35a66f9 commit 3c6c1c0
Show file tree
Hide file tree
Showing 28 changed files with 237 additions and 204 deletions.
24 changes: 2 additions & 22 deletions packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDoc\SpacelessPhpDocTagNode;
use Rector\BetterPhpDocParser\PhpDocNodeFinder\PhpDocNodeByTypeFinder;
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\ChangedPhpDocNodeVisitor;
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\Type\ShortenedIdentifierTypeNode;
Expand Down Expand Up @@ -80,10 +79,6 @@ final class PhpDocInfo
* @var \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode
*/
private $originalPhpDocNode;
/**
* @var bool
*/
private $hasChanged = \false;
public function __construct(PhpDocNode $phpDocNode, BetterTokenIterator $betterTokenIterator, StaticTypeMapper $staticTypeMapper, \PhpParser\Node $node, AnnotationNaming $annotationNaming, PhpDocNodeByTypeFinder $phpDocNodeByTypeFinder)
{
$this->phpDocNode = $phpDocNode;
Expand Down Expand Up @@ -341,26 +336,11 @@ public function getTemplateTagValueNodes() : array
* @deprecated Change doc block and print directly in the node instead
* @internal
* Should be handled by attributes of phpdoc node - if stard_and_end is missing in one of nodes, it has been changed
* Similar to missing original node in php-aprser
*
* @api
*/
public function markAsChanged() : void
{
$this->hasChanged = \true;
}
public function hasChanged() : bool
{
if ($this->isNewNode()) {
return \true;
}
if ($this->hasChanged) {
return \true;
}
// has a single node with missing start_end
$phpDocNodeTraverser = new PhpDocNodeTraverser();
$changedPhpDocNodeVisitor = new ChangedPhpDocNodeVisitor();
$phpDocNodeTraverser->addPhpDocNodeVisitor($changedPhpDocNodeVisitor);
$phpDocNodeTraverser->traverse($this->phpDocNode);
return $changedPhpDocNodeVisitor->hasChanged();
}
public function makeMultiLined() : void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ public function __construct(ClassAnnotationMatcher $classAnnotationMatcher)
*
* @param string[] $oldToNewClasses
*/
public function changeTypeInAnnotationTypes(Node $node, PhpDocInfo $phpDocInfo, array $oldToNewClasses) : void
public function changeTypeInAnnotationTypes(Node $node, PhpDocInfo $phpDocInfo, array $oldToNewClasses, bool &$hasChanged) : bool
{
$this->processAssertChoiceTagValueNode($oldToNewClasses, $phpDocInfo);
$this->processDoctrineRelationTagValueNode($node, $oldToNewClasses, $phpDocInfo);
$this->processSerializerTypeTagValueNode($oldToNewClasses, $phpDocInfo);
$this->processAssertChoiceTagValueNode($oldToNewClasses, $phpDocInfo, $hasChanged);
$this->processDoctrineRelationTagValueNode($node, $oldToNewClasses, $phpDocInfo, $hasChanged);
$this->processSerializerTypeTagValueNode($oldToNewClasses, $phpDocInfo, $hasChanged);
return $hasChanged;
}
/**
* @param array<string, string> $oldToNewClasses
*/
private function processAssertChoiceTagValueNode(array $oldToNewClasses, PhpDocInfo $phpDocInfo) : void
private function processAssertChoiceTagValueNode(array $oldToNewClasses, PhpDocInfo $phpDocInfo, bool &$hasChanged) : void
{
$assertChoiceDoctrineAnnotationTagValueNode = $phpDocInfo->findOneByAnnotationClass('Symfony\\Component\\Validator\\Constraints\\Choice');
if (!$assertChoiceDoctrineAnnotationTagValueNode instanceof DoctrineAnnotationTagValueNode) {
Expand Down Expand Up @@ -66,24 +67,25 @@ private function processAssertChoiceTagValueNode(array $oldToNewClasses, PhpDocI
$classNameStringNode->value = $newClass;
// trigger reprint
$classNameArrayItemNode->setAttribute(PhpDocAttributeKey::ORIG_NODE, null);
$hasChanged = \true;
break;
}
}
/**
* @param array<string, string> $oldToNewClasses
*/
private function processDoctrineRelationTagValueNode(Node $node, array $oldToNewClasses, PhpDocInfo $phpDocInfo) : void
private function processDoctrineRelationTagValueNode(Node $node, array $oldToNewClasses, PhpDocInfo $phpDocInfo, bool &$hasChanged) : void
{
$doctrineAnnotationTagValueNode = $phpDocInfo->getByAnnotationClasses(['Doctrine\\ORM\\Mapping\\OneToMany', 'Doctrine\\ORM\\Mapping\\ManyToMany', 'Doctrine\\ORM\\Mapping\\Embedded']);
if (!$doctrineAnnotationTagValueNode instanceof DoctrineAnnotationTagValueNode) {
return;
}
$this->processDoctrineToMany($doctrineAnnotationTagValueNode, $node, $oldToNewClasses);
$this->processDoctrineToMany($doctrineAnnotationTagValueNode, $node, $oldToNewClasses, $hasChanged);
}
/**
* @param array<string, string> $oldToNewClasses
*/
private function processSerializerTypeTagValueNode(array $oldToNewClasses, PhpDocInfo $phpDocInfo) : void
private function processSerializerTypeTagValueNode(array $oldToNewClasses, PhpDocInfo $phpDocInfo, bool &$hasChanged) : void
{
$doctrineAnnotationTagValueNode = $phpDocInfo->findOneByAnnotationClass('JMS\\Serializer\\Annotation\\Type');
if (!$doctrineAnnotationTagValueNode instanceof DoctrineAnnotationTagValueNode) {
Expand All @@ -99,6 +101,7 @@ private function processSerializerTypeTagValueNode(array $oldToNewClasses, PhpDo
}
$classNameStringNode->value = Strings::replace($classNameStringNode->value, '#\\b' . \preg_quote($oldClass, '#') . '\\b#', $newClass);
$classNameArrayItemNode->setAttribute(PhpDocAttributeKey::ORIG_NODE, null);
$hasChanged = \true;
}
$currentTypeArrayItemNode = $doctrineAnnotationTagValueNode->getValue('type');
if (!$currentTypeArrayItemNode instanceof ArrayItemNode) {
Expand All @@ -110,13 +113,14 @@ private function processSerializerTypeTagValueNode(array $oldToNewClasses, PhpDo
}
if ($currentTypeStringNode->value === $oldClass) {
$currentTypeStringNode->value = $newClass;
$hasChanged = \true;
}
}
}
/**
* @param array<string, string> $oldToNewClasses
*/
private function processDoctrineToMany(DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode, Node $node, array $oldToNewClasses) : void
private function processDoctrineToMany(DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode, Node $node, array $oldToNewClasses, bool &$hasChanged) : void
{
$classKey = $doctrineAnnotationTagValueNode->hasClassName('Doctrine\\ORM\\Mapping\\Embedded') ? 'class' : 'targetEntity';
$targetEntityArrayItemNode = $doctrineAnnotationTagValueNode->getValue($classKey);
Expand All @@ -136,6 +140,7 @@ private function processDoctrineToMany(DoctrineAnnotationTagValueNode $doctrineA
}
$targetEntityStringNode->value = $newClass;
$targetEntityArrayItemNode->setAttribute(PhpDocAttributeKey::ORIG_NODE, null);
$hasChanged = \true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ public function changeReturnType(FunctionLike $functionLike, PhpDocInfo $phpDocI
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($functionLike);
return \true;
}
public function changeParamType(FunctionLike $functionLike, PhpDocInfo $phpDocInfo, Type $newType, Param $param, string $paramName) : void
public function changeParamType(FunctionLike $functionLike, PhpDocInfo $phpDocInfo, Type $newType, Param $param, string $paramName) : bool
{
// better skip, could crash hard
if ($phpDocInfo->hasInvalidTag('@param')) {
return;
return \false;
}
if (!$this->newPhpDocFromPHPStanTypeGuard->isLegal($newType)) {
return;
return \false;
}
$phpDocTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($newType);
$paramTagValueNode = $phpDocInfo->getParamTagValueByName($paramName);
Expand All @@ -147,17 +147,18 @@ public function changeParamType(FunctionLike $functionLike, PhpDocInfo $phpDocIn
$currentType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($paramTagValueNode->type, $param);
// avoid overriding better type
if ($this->typeComparator->isSubtype($currentType, $newType)) {
return;
return \false;
}
if ($this->typeComparator->areTypesEqual($currentType, $newType)) {
return;
return \false;
}
$paramTagValueNode->type = $phpDocTypeNode;
} else {
$paramTagValueNode = $this->paramPhpDocNodeFactory->create($phpDocTypeNode, $param);
$phpDocInfo->addTagValueNode($paramTagValueNode);
}
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($functionLike);
return \true;
}
public function isAllowed(TypeNode $typeNode) : bool
{
Expand Down

This file was deleted.

27 changes: 0 additions & 27 deletions packages/Comments/NodeDocBlock/DocBlockUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,6 @@ public function __construct(PhpDocInfoPrinter $phpDocInfoPrinter)
{
$this->phpDocInfoPrinter = $phpDocInfoPrinter;
}
public function updateNodeWithPhpDocInfo(Node $node) : void
{
// nothing to change? don't save it
$phpDocInfo = $this->resolveChangedPhpDocInfo($node);
if (!$phpDocInfo instanceof PhpDocInfo) {
return;
}
$phpDoc = $this->printPhpDocInfoToString($phpDocInfo);
// make sure, that many separated comments are not removed
if ($phpDoc === '') {
$this->setCommentsAttribute($node);
return;
}
// this is needed to remove duplicated // commentsAsText
$node->setDocComment(new Doc($phpDoc));
}
public function updateRefactoredNodeWithPhpDocInfo(Node $node) : void
{
// nothing to change? don't save it
Expand All @@ -58,17 +42,6 @@ private function setCommentsAttribute(Node $node) : void
});
$node->setAttribute(AttributeKey::COMMENTS, $comments);
}
private function resolveChangedPhpDocInfo(Node $node) : ?PhpDocInfo
{
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
if (!$phpDocInfo instanceof PhpDocInfo) {
return null;
}
if (!$phpDocInfo->hasChanged()) {
return null;
}
return $phpDocInfo;
}
private function printPhpDocInfoToString(PhpDocInfo $phpDocInfo) : string
{
if ($phpDocInfo->isNewNode()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ public function __construct(ClassRenamePhpDocNodeVisitor $classRenamePhpDocNodeV
/**
* @param OldToNewType[] $oldToNewTypes
*/
public function renamePhpDocType(PhpDocInfo $phpDocInfo, array $oldToNewTypes) : void
public function renamePhpDocType(PhpDocInfo $phpDocInfo, array $oldToNewTypes) : bool
{
if ($oldToNewTypes === []) {
return;
return \false;
}
$phpDocNodeTraverser = new PhpDocNodeTraverser();
$phpDocNodeTraverser->addPhpDocNodeVisitor($this->classRenamePhpDocNodeVisitor);
$this->classRenamePhpDocNodeVisitor->setOldToNewTypes($oldToNewTypes);
$phpDocNodeTraverser->traverse($phpDocInfo->getPhpDocNode());
return $this->classRenamePhpDocNodeVisitor->hasChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ public function __construct(NameImportingPhpDocNodeVisitor $nameImportingPhpDocN
{
$this->nameImportingPhpDocNodeVisitor = $nameImportingPhpDocNodeVisitor;
}
public function importNames(PhpDocNode $phpDocNode, Node $node) : void
public function importNames(PhpDocNode $phpDocNode, Node $node) : bool
{
if ($phpDocNode->children === []) {
return;
return \false;
}
$this->nameImportingPhpDocNodeVisitor->setCurrentNode($node);
$phpDocNodeTraverser = new PhpDocNodeTraverser();
$phpDocNodeTraverser->addPhpDocNodeVisitor($this->nameImportingPhpDocNodeVisitor);
$phpDocNodeTraverser->traverse($phpDocNode);
return $this->nameImportingPhpDocNodeVisitor->hasChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ final class ClassRenamePhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
* @var OldToNewType[]
*/
private $oldToNewTypes = [];
/**
* @var bool
*/
private $hasChanged = \false;
public function __construct(StaticTypeMapper $staticTypeMapper, CurrentNodeProvider $currentNodeProvider, UseImportsResolver $useImportsResolver)
{
$this->staticTypeMapper = $staticTypeMapper;
Expand All @@ -56,6 +60,7 @@ public function beforeTraverse(Node $node) : void
if ($this->oldToNewTypes === []) {
throw new ShouldNotHappenException('Configure "$oldToNewClasses" first');
}
$this->hasChanged = \false;
}
public function enterNode(Node $node) : ?Node
{
Expand Down Expand Up @@ -91,6 +96,7 @@ public function enterNode(Node $node) : ?Node
// mirror attributes
$newTypeNode->setAttribute(PhpDocAttributeKey::PARENT, $parentType);
}
$this->hasChanged = \true;
return $newTypeNode;
}
return null;
Expand All @@ -102,6 +108,10 @@ public function setOldToNewTypes(array $oldToNewTypes) : void
{
$this->oldToNewTypes = $oldToNewTypes;
}
public function hasChanged() : bool
{
return $this->hasChanged;
}
private function resolveNamespacedName(IdentifierTypeNode $identifierTypeNode, PhpParserNode $phpParserNode, string $name) : string
{
if (\strncmp($name, '\\', \strlen('\\')) === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ final class NameImportingPhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
* @var PhpParserNode|null
*/
private $currentPhpParserNode;
/**
* @var bool
*/
private $hasChanged = \false;
public function __construct(ClassNameImportSkipper $classNameImportSkipper, UseNodesToAddCollector $useNodesToAddCollector, CurrentFileProvider $currentFileProvider, ReflectionProvider $reflectionProvider, IdentifierTypeMapper $identifierTypeMapper)
{
$this->classNameImportSkipper = $classNameImportSkipper;
Expand Down Expand Up @@ -99,15 +103,21 @@ public function enterNode(Node $node) : ?Node
}
public function setCurrentNode(PhpParserNode $phpParserNode) : void
{
$this->hasChanged = \false;
$this->currentPhpParserNode = $phpParserNode;
}
public function hasChanged() : bool
{
return $this->hasChanged;
}
private function processFqnNameImport(PhpParserNode $phpParserNode, IdentifierTypeNode $identifierTypeNode, FullyQualifiedObjectType $fullyQualifiedObjectType, File $file) : ?IdentifierTypeNode
{
$parentNode = $identifierTypeNode->getAttribute(PhpDocAttributeKey::PARENT);
if ($parentNode instanceof TemplateTagValueNode) {
// might break
return null;
}
// standardize to FQN
if (\strncmp($fullyQualifiedObjectType->getClassName(), '@', \strlen('@')) === 0) {
$fullyQualifiedObjectType = new FullyQualifiedObjectType(\ltrim($fullyQualifiedObjectType->getClassName(), '@'));
}
Expand All @@ -121,6 +131,7 @@ private function processFqnNameImport(PhpParserNode $phpParserNode, IdentifierTy
}
if ($this->shouldImport($newNode, $identifierTypeNode, $fullyQualifiedObjectType)) {
$this->useNodesToAddCollector->addUseImport($fullyQualifiedObjectType);
$this->hasChanged = \true;
return $newNode;
}
return null;
Expand Down
Loading

0 comments on commit 3c6c1c0

Please sign in to comment.