-
Notifications
You must be signed in to change notification settings - Fork 18
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
Automation to Place Tombstones and Clean-Up Dead Code #11
Comments
Maybe you can also add a Rector rule to remove tombstones, for example after deleting dead code we want to remove all tombstones (for performance reasons maybe) and uninstall |
@jawira You could easily do that with a regex ;) |
hi! currently implementing this library. for the first type of rector rule this is what I [and chatgpt] came up with: <?php
declare(strict_types=1);
namespace Utils\Rector\Rector;
use DateTimeImmutable;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use function array_unshift;
final class AddTombstones extends AbstractRector
{
private string $now;
public function __construct()
{
$this->now = (new DateTimeImmutable())->format('Y-m-d');
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'add tombstone method call in every class method',
[
new CodeSample(
<<<'CODE_SAMPLE'
function someFunction()
{
// Some code
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
function someFunction()
{
tombstone('2024-01-01');
// Some code
}
CODE_SAMPLE
),
]
);
}
public function getNodeTypes(): array
{
return [Function_::class, ClassMethod::class];
}
public function refactor(Node $node)
{
if ($this->getName($node) === '__construct') {
return null;
}
if ($node->stmts === null) { //interface
return null;
}
$tombstoneCall = new FuncCall(
new Name('tombstone'),
[new Arg(new String_($this->now))]
);
array_unshift($node->stmts, new Node\Stmt\Expression($tombstoneCall));
return $node;
}
} I'll do some testing and find out a way to fetch log results for the other rules:
I suppose |
Idea
I'd like to provide some automation to
Since Rector is the standard library for PHP code migrations now, I thought it would be a good thing to use the Rector platform, rather than building my own little code migrations tool.
In detail, it would need to provide 2 rectors:
1. Setting Tombstones
The first rector would add
tombstone()
function calls to each public method. There could be potentially additional filtering options, which public method to target.Result after rector was applied:
2. Deleting Dead Code
The second rector would take a result from the tombstone library (transfer format TBD), providing the information which methods are detected as "dead code". The rector would remove these dead methods from the code.
If this idea would be useful to you, give it a +1
The text was updated successfully, but these errors were encountered: