plugin-php icon indicating copy to clipboard operation
plugin-php copied to clipboard

Single line conditions

Open arnoson opened this issue 3 years ago • 1 comments

When using simple conditions for early returns / guard statements the current formatting is quite verbose:

function test($arg1) {
  if (!$arg) return;

  for ($i = 0; $i < 10; $i++) {
    if ($i % 2 === 0) continue;
    // do something if the number is odd
  }
}

will be converted to:

function test($arg1) {
  if (!$arg) {
    return;
  }

  for ($i = 0; $i < 10; $i++) {
    if ($i % 2 === 0) {
      continue;
    }
    // do something if the number is odd
  }
}

It would be great if these single line conditions would be preserved. Alot of formatters for c-style languages like prettier for javascript or ClangFormat allow this, resulting in much more concise code.

arnoson avatar Nov 30 '22 09:11 arnoson

Hi @arnoson, thanks for reaching out about this. We chose to adhere to the PER-CS coding standard in this project as much as possible, which doesn't allow single line conditions: https://www.php-fig.org/per/coding-style/#5-control-structures That's why I don't think this is a change we should be making, but I'll leave the issue open for further discussion.

czosel avatar Dec 03 '22 08:12 czosel