Replacing lines with issues instead of replacing tokens #3735
-
Hello! I have dug a bit into the code and would like to ask to have a better idea of what would it take for me to implement this:
I've checked that CBF implements Report, so am wondering if I need to implement that interface and create a new class that bases on CBF (or perhaps, extend CBF for my purposes), or if there's an easier way to achieve this. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Code can be written in so many different ways that to replace lines is rarely the correct solution and I'd strongly advise against it. $foo = function_call($a, $b);
// versus (functionally the same):
$foo = function_call(
param_b : $b,
param_a : $a,
); If you really need to replace a whole "line", use the token stream to walk to the start of the line - As you would be touching multiple tokens, make sure to wrap the changes you are making in a changeset to prevent problems if your fix would conflict with other fixes being made.
The |
Beta Was this translation helpful? Give feedback.
-
Thank you for taking the time to reply rfnl! I am playing with fixing more complicated linting issues with AI. As you have outlined, replacing code has its risks, but I've found it can resolve specific problems pretty good on a consistent basis (as an example, escaping issues with WordPress coding standards), while others not so well. |
Beta Was this translation helpful? Give feedback.
Code can be written in so many different ways that to replace lines is rarely the correct solution and I'd strongly advise against it.
Think:
If you really need to replace a whole "line", use the token stream to walk to the start of the line -
$tokens[$ptr]['column'] === 1
- and replace all the tokens until you reach the end of the line -$tokens[$ptr]['line'] !== $tokens[($ptr + 1)]['line']
(or rather, replace the first token with your "replacement" and then replace the rest of the tokens with''
, which will effectively remove them).As you would be touching multiple …