Is there a VSCode snippet to convert a PHP regular function to an arrow function? - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Monday, December 4, 2023

Is there a VSCode snippet to convert a PHP regular function to an arrow function?

Creating this question to answer myself, as I couldn't find it anywhere and I'm hoping it'll help someone else!

When writing PHP (e.g. Laravel) I often have an arrow function that I want to convert back to a regular function for debugging purposes or to add complexity to the return statement or closure process.

For example:

// Convert this:
collect($someArray)->map(fn($item) => $item->value);

// To this:
collect($someArray)->map(function ($item) {
  dd($item);
  return $item->value;
});

Once I'm done debugging and figure out the correct code I want to leave in there, I'll often want to put it back the way it was, like:

// Convert this:
collect($someArray)->map(function ($item) {
  // dd($item);
  return $item->correct_value;
});

// To this:
collect($someArray)->map(fn($item) => $item->correct_value);

I looked all over but couldn't find an existing VSCode snippet to accomplish this.



source https://stackoverflow.com/questions/77595463/is-there-a-vscode-snippet-to-convert-a-php-regular-function-to-an-arrow-function

No comments:

Post a Comment