Need help with a rule creation for fpdf #9008
-
Hey ! i'm trying to create a rule that update fpdf lines: an exemple of what it was: Open(); $nom_BT = 'exemple.pdf'; $pdf->Output($nom_BT); ?>and what i want to replace : Open(); <-- that, or remove it, don't care $nom_BT = '/tmp/exemple.pdf'; $pdf->Output('F', $nom_BT); ?>i made a rule that works for those two lines: but i can't figured out how to do with this line my rule: Open(); $nom_BT = 'exemple.pdf'; $pdf->Output($nom_BT); CODE_SAMPLE , <<<'CODE_SAMPLE' $nom_BT = '/tmp/exemple.pdf'; $pdf->Output('F', $nom_BT); CODE_SAMPLE ), ] ); } public function getNodeTypes(): array { return [MethodCall::class, Assign::class]; } public function refactor(Node $node) { // delete `$pdf->Open();` <----- that's this part who didn work if ($node instanceof MethodCall && $this->getName($node->name) === 'Open') { return null; } // edit file path if ($node instanceof Assign && $node->var instanceof Variable && $this->getName($node->var) === 'nom_BT') { if ($node->expr instanceof String_) { $currentValue = $node->expr->value; // Vérifie si le chemin commence déjà par "/tmp/" if (!str_starts_with($currentValue, '/tmp/')) { $node->expr = new String_('/tmp/' . $currentValue); } } return $node; } // `$pdf->Output($nom_BT)` to`$pdf->Output('F', $nom_BT)` if ($node instanceof MethodCall && $this->getName($node->name) === 'Output') { if (count($node->args) === 1) { $node->args = [ new Arg(new String_('F')), $node->args[0] ]; } return $node; } return null; } } ---------- begin diff ---------- @@ @@ Open(); -$nom_BT = 'exemple.pdf'; -$pdf->Output($nom_BT); +$nom_BT = '/tmp/exemple.pdf'; +$pdf->Output('F', $nom_BT); ?>
Applied rules:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
(I don't know why the Layout i made is not effective, sorry, for me when i edit the message, it normal) |
Beta Was this translation helpful? Give feedback.
-
I think you can start by learning AST first, step by step, slowly, start with documentation and some refs |
Beta Was this translation helpful? Give feedback.
I think you can start by learning AST first, step by step, slowly, start with documentation and some refs