1

I'm trying to be more strict about my adherence to PSR-12 and I'm not sure how to handle a specific situation involving whitespace.

In the past, I have often used trailing whitespace after a PHP tag to ensure that there is space in the resulting HTML. For example:

<?= $address_line_1 ?><br>
<?= $address_line_2 ?><br>
<?= $city ?>,
<?= $state ?> 
<?= $postal_code ?>

Note the space after <?= $state ?>. Without this trailing whitespace, my code prints "NY63764" instead of "NY 63764". (Obviously, in this example, I would put the state and postal code on the same line, but there are frequent cases where doing so would push me past the 120 character character soft limit).

If the trailing space is incorrect, what is the correct strategy? The options I can think of include:

  • Exceed the 120 character line limit
  • Print a space (e.g. <?= $state . ' ' ?>)
  • Use a non-breaking space (i.e. <?= $state ?>&nbsp;)

Those seem like poor options. What am I missing?

Libertie
  • 141
  • 1
  • 10
  • On the other hand, have you tested something like `= $state ?> = $postal_code ?>` all on one line? I'm still reading the psr-12 spec and I'm not convinced all rules are with php mixed into html (some are, but some seem incompatible with that context). – Anthony Jan 15 '21 at 02:23
  • 1
    I would argue the reason against how you currently have it is in the psr-12 opening: "Like PSR-2, the intent of this specification is to *reduce cognitive friction when scanning code* from different authors." If someone were scanning that code, they would almost never notice that there is a whitespace at the end of the line, much less that it is whitespace that is intentional and being output. Again, I'd say the nicest alternative without doing `.' '.` would be to have them on one line like `= $state ?> = $postal_code ?>` – Anthony Jan 15 '21 at 02:31
  • Lastly: if you're wanting to avoid ` ` because they cause problems (they do and you should avoid them) consider replacing intentional whitespace outside of script tags with ` ` which is the html encoding for a regular, normal space (treated like a normal space, not like a non-breaking space), like `= $state ?> ` – Anthony Jan 15 '21 at 02:37
  • @Anthony, I agree that putting the outputs on a single line is ideal, but would you still do so if it pushed the line past the 120 character soft limit? I really prefer to break long lines up for readability. Your comment about it being mixed PHP and HTML is interesting and seems like a plausible exception to PSR-12. This issue came up for me because I am using VS Code with Intellephense and the code formatting is automatically removing my intentionally placed trailing whitespace. – Libertie Jan 15 '21 at 14:24
  • Well, you have to pick which rules you care more about when they conflict, and I'd say that exceeding 120 characters is better than trailing whitespace, since I can actually see the whitespace. But also remember that the PSR says : "The soft limit on line length MUST be 120 characters." as well as: "There MUST NOT be a hard limit on line length." That second rule (actually the first rule in the PSR itself) is likely made explicit and more strict (**MUST NOT**) because while 120 characters is ideal, it should never be considered "wrong" if a line goes over 120 characters. – Anthony Jan 15 '21 at 17:00
  • Also, again, I would argue that the standard is meant to focus on code-readability, so output is not likely meant to be held to the same strictness. I agree that super long lines (code or output) is harder to read and less approachable, but generally we understand that something that is being output (something inside quotes or HEREDOC or whatever) is not part of the logic and sometimes just runs long because it's long content. – Anthony Jan 15 '21 at 17:03
  • Thanks @Anthony, I appreciate your insights! – Libertie Jan 16 '21 at 03:35

0 Answers0