Open montala-alinc opened 9 months ago
@montala-alinc While I agree this behaviour is unexpected, I'm not sure it is a bug in PHPCS.
PSR12 is pretty clear about how control structure braces should be formatted and as far as I can see, this is handled correctly by the sniffs, though it could be argued that the start of the HTML should be taken as the "start of the statement" for the indentation of the close brace, same as it is for the "four space indent" of the control structure body.
I'm not sure I agree though.
Is this something that PHPCS should address or is it simply considered bad practice?
I do feel that inlining braced control structures as per the code sample is bad practice (and quite unusual) and there are plenty of other ways to write this code which would comply with PSR12 without problems (and would in most cases make the code more readable too).
Here are some examples:
// Example 1: use a ternary.
<input type="text" name="username" id="username" class="stdwidth" <?php echo (!$login_autocomplete) ? 'autocomplete="off"' : ''; ?> value="<?php echo htmlspecialchars(getval("username", "")) ?>" <?php echo ($error != "") ? 'aria-describedby="LoginError"' : ''; ?>/>
// Example 2: use a ternary with the short echo tag.
<input type="text" name="username" id="username" class="stdwidth" <?= (!$login_autocomplete) ? 'autocomplete="off"' : ''; ?> value="<?= htmlspecialchars(getval("username", "")) ?>" <?= ($error != "") ? 'aria-describedby="LoginError"' : ''; ?>/>
// Example 3: verbosely declare variables before creating the HTML.
<?php
$autocomplete = '';
if (!$login_autocomplete) {
$autocomplete = 'autocomplete="off"';
}
$describedBy = '';
if ($error != "") {
$describedBy = 'aria-describedby="LoginError"';
}
$userName = htmlspecialchars(getval("username", ""));
?>
<input type="text" name="username" id="username" class="stdwidth" <?php echo $autocomplete ?> value="<?php echo $userName ?>" <?php echo $describedBy ?>/>
// Example 4: split the HTML output across several lines.
<input type="text" name="username" id="username" class="stdwidth"
<?php
if (!$login_autocomplete) {
?>
autocomplete="off"
<?php
}
?>
value="<?php echo htmlspecialchars(getval("username", "")) ?>"
<?php
if ($error != "") {
?>
aria-describedby="LoginError"
<?php
}
?>
/>
it could be argued that the start of the HTML should be taken as the "start of the statement" for the indentation of the close brace, same as it is for the "four space indent" of the control structure body.
Indeed, the unnecessary indentation was what caught my attention in the first place.
The code base is from around 2010 and is slowly being brought up on modern practices so I'm aware this is not typical code nowadays.
Describe the bug
Using PSR12 standard on files with HTML & PHP complex lines get processed resulting with weird indentation.
Code sample
Custom ruleset
To reproduce
Steps to reproduce the behavior:
test.php
file with the code sample abovephpcs.xml
file with the above configphpcs --report=diff test.php