Skip to content

Commit

Permalink
Apply docblock changes using DocBlockUpdater (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeniJaho authored Sep 18, 2023
1 parent be68b68 commit 4b7856a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Type\ObjectType;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Core\Rector\AbstractRector;
use RectorLaravel\NodeFactory\AppAssignFactory;
use RectorLaravel\ValueObject\ServiceNameTypeAndVariableName;
Expand All @@ -30,6 +31,7 @@ final class CallOnAppArrayAccessToStandaloneAssignRector extends AbstractRector

public function __construct(
private readonly AppAssignFactory $appAssignFactory,
private readonly DocBlockUpdater $docBlockUpdater,
) {
$this->serviceNameTypeAndVariableNames[] = new ServiceNameTypeAndVariableName(
'validator',
Expand Down Expand Up @@ -88,6 +90,8 @@ public function refactor(Node $node): Node|array|int|null
$methodCall->var
);

$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($assignExpression);

$methodCall->var = new Variable($serviceNameTypeAndVariableName->getVariableName());

// the nop is a workaround because the docs of the first node are somehow stripped away
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\ValueObject\Type\FullyQualifiedIdentifierTypeNode;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -38,7 +39,8 @@ class AddGenericReturnTypeToRelationsRector extends AbstractScopeAwareRector
private const RELATION_WITH_CHILD_METHODS = ['belongsTo', 'morphTo'];

public function __construct(
private readonly TypeComparator $typeComparator
private readonly TypeComparator $typeComparator,
private readonly DocBlockUpdater $docBlockUpdater,
) {
}

Expand Down Expand Up @@ -174,6 +176,8 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
$phpDocInfo->addTagValueNode(new ReturnTagValueNode($genericTypeNode, ''));
}

$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);

return $node;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\ValueObject\Type\FullyQualifiedIdentifierTypeNode;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -30,6 +31,11 @@ final class AddExtendsAnnotationToModelFactoriesRector extends AbstractRector

private const FACTORY_CLASS_NAME = 'Illuminate\Database\Eloquent\Factories\Factory';

public function __construct(
private readonly DocBlockUpdater $docBlockUpdater,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Adds the @extends annotation to Factories.', [
Expand Down Expand Up @@ -87,6 +93,8 @@ public function refactor(Node $node): ?Node

$this->addExtendsPhpDocTag($node, $stmt);

$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);

break;
}

Expand Down

0 comments on commit 4b7856a

Please sign in to comment.