Closed micaherne closed 3 months ago
I'm not able to assign myself to this issue but I'll try and make a patch for it since it's quite likely to be my code that is causing it.
Thanks for reporting this @micaherne , I've assigned the issue o you, let us know if you need anything else.
Ciao :-)
What seems to be happening here is that the insertBoilerplate() method is actually inserting it in a way that fails the sniff so the moveBoilerplate() method is being called on the second round of fixing, and it's moving it to the wrong place.
As far as I can see this is to do with how the PHP tokeniser deals with the opening PHP tag. I wasn't aware of this but it seems that it grabs the first whitespace character after the <?php
bit.
$codes = ['<?php', '<?php ', "<?php\n", '<?php '];
foreach ($codes as $code) {
echo '|' . implode('|', array_map(fn($t) => $t[1], token_get_all($code))) . "|\n";
}
gives
|<?php|
|<?php |
|<?php
|
|<?php | |
The current code is assuming that the PHP tag content is either '<?php' or '<?php\n' so we need to check the content for whitespace and replace it if necessary.
The fix in BoilerplateCommentSniff can cause invalid PHP if there is a comment in the first line.
This code:
becomes this:
Because there is no whitespace after the php tag it is not recognised. Also clearly the boilerplate should be on the line after the opening tag.