Skip to content

Replacing lines with issues instead of replacing tokens #3735

Answered by jrfnl
oscarssanchez asked this question in Q&A
Discussion options

You must be logged in to vote

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:

$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 - $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 …

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by oscarssanchez
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants