Skip to content

Add RemoveMigrationDocBlockRector rector #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions src/Rector/RemoveMigrationDocBlockRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Rector;

use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use RectorLaravel\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class RemoveMigrationDocBlockRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove standard Laravel migration docblocks',
[
new CodeSample(
<<<'CODE_SAMPLE'
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// ...
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
public function up()
{
// ...
}
CODE_SAMPLE
),
]
);
}

public function getNodeTypes(): array
{
return [ClassMethod::class];
}

/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
$docComment = $node->getDocComment();
if ($docComment === null) {
return null;
}

// Additional safety: only process up() and down() methods in migrations
$methodName = $node->name->toString();
if (!in_array($methodName, ['up', 'down'])) {
return null;
}

// Check for standard migration docblocks
$patterns = [
'/\/\*\*\s*\n\s*\*\s*Run the migrations\.\s*(\n\s*\*\s*\n\s*\*\s*@return void\s*)?\n\s*\*\//',
'/\/\*\*\s*\n\s*\*\s*Reverse the migrations\.\s*(\n\s*\*\s*\n\s*\*\s*@return void\s*)?\n\s*\*\//'
];

$docText = $docComment->getText();
foreach ($patterns as $pattern) {
if (preg_match($pattern, $docText)) {
$node->setAttribute('comments', []);

return $node;
}
}

return null;
}
}