PSR-12 declare(strict_types=1) and mixed with HTML should require semi-colon, causes fatal error with PHP CS Fixer

182 views
Skip to first unread message

Julian Wittorf

unread,
Mar 30, 2023, 9:29:53 AM3/30/23
to PHP Framework Interoperability Group
The title basically says it all but I'll list the issue I found so one can reproduce it:
  1. Copy the code example from https://www.php-fig.org/psr/psr-12/#3-declare-statements-namespace-and-import-statements into a file and DO NOT add optional semi-colon (although this is a part of the PHP manual https://www.php.net/manual/en/control-structures.declare.php)
  2. Run PHP CS Fixer on the file
  3. The code is now broken, because the fixer kind of thinks it's a function (call? declaration?) (but not really either), see code example below.
  4. This FatalError could simply be prevent if the semi-colon is required in PSR-12.
Broken/resulted code due to the missing semi-colon:
<?php declare(strict_types=1) {
    ;
} ?>
<!DOCTYPE html>
...

I'm aware the PHP-FIG can't respond to all the tools out there but I'm just trying to make the point, that a coding standard must not make recommendations, that don't follow the official language documentation and general coding best practices like "finish a statement/instruction properly with a terminating semicolon" which is mentioned in the manual as well: https://www.php.net/manual/en/language.basic-syntax.instruction-separation.php

Korvin Szanto

unread,
Mar 30, 2023, 10:43:19 AM3/30/23
to php...@googlegroups.com
Hi Julian, 
The best place to effect change in coding standards these days is in PER-CS since it can actually change in future releases: https://github.com/php-fig/per-coding-style

That said, I struggle to see how this isn't just an issue with CS Fixer. For example if you completely exclude CS Fixer the following is valid and parses fine:

```
<?php declare(strict_types=1) ?>
Hello Word
```

Restricting valid syntax in order to prevent unforeseeable bugs in third-party tools would require us to make guesses about what to disallow which would lead to a more restrictive spec without improving readability.

--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/3564160b-b997-4f6a-a3d8-9e61baa35659n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages