Skip to content

chore: PHP CS Fixer - update heredoc handling #61372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2025

Conversation

keradus
Copy link
Member

@keradus keradus commented Aug 9, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? no
Deprecations? no
Issues Fix CS
License MIT

Inspired while #61371 , but can be merged separately.

@PHPxMigration and @Symfony ruleset have different config for those rules. I assume the difference is coming from supporting pre-7.2 PHP version earlier in the codebase, where heredoc closure and trailing comma had to be on separated lines - but it's no longer the case.

Can we benefit from newer PHP syntax and incorporate those changes? (yes, trailing_comma_in_multiline, part of @Symfony, is already having this enabled!)
if so, i will also merge them into @Symfony ruleset itself afterwards.

with love by PHP Coding Standards Fixer

@carsonbot carsonbot added this to the 7.4 milestone Aug 9, 2025
@carsonbot carsonbot changed the title chore: PHP CS Fixer - update heredoc handling chore: PHP CS Fixer - update heredoc handling Aug 9, 2025
@nicolas-grekas
Copy link
Member

rebase needed

@nicolas-grekas
Copy link
Member

I made some handcrafted tweaks.
BTW, would it be possible to tell php-cs-fixer to stop removing whitespaces at the end of lines inside heredocs?

@xabbuh
Copy link
Member

xabbuh commented Aug 13, 2025

BTW, would it be possible to tell php-cs-fixer to stop removing whitespaces at the end of lines inside heredocs?

I think we should find a better way than having the need for trailing spaces in some tests as dealing with them is also a mess when you open these test files in your local IDE for doing other changes in that file and your IDE then strips all trailing whitespaces.

@keradus
Copy link
Member Author

keradus commented Aug 13, 2025

I made some handcrafted tweaks.

👍🏻
minor, but on such changes i would also recommend to start very first argument in new line. otherwise, argument 0 starts in line 0 (run-in), but argument 1 start in line 1(-ish) (like listed items)

BTW, would it be possible to tell php-cs-fixer to stop removing whitespaces at the end of lines inside heredocs?

do you have an example of Fixer doing that? I can't see it in this PR, and we tried hard to not remove trailing whitespace inside of string, as it would change the meaning of the code.
( I also enjoy suggestion of @xabbuh , and would love to have it this way as well - yet regardless of that, Fixer should not change string content)


separately, good to merge, I think ?

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Aug 13, 2025

See Fabbot's report in the CI failures: php-cs-fixer suggests many changes inside heredoc that we don't want.

@keradus
Copy link
Member Author

keradus commented Aug 13, 2025

ok, thanks for hint what you meant.
so it's not about heredoc rules. it's becuase Sf ruleset has no_trailing_whitespace_in_string enabled, whish is RISKY rule exactly of this reason.

to change the behaviour, we simply need to disable that rule for the project, but tbh - i would rather keep it enabled, as for 99% cases, removal of trailing whitespaces is expected, and as @xabbuh mentioned, having them is also fooling other tools like IDE

@nicolas-grekas
Copy link
Member

Ah that's good to know. This should be disabled to me. Strings are strings - aka not up for CS fixes.

@keradus
Copy link
Member Author

keradus commented Aug 13, 2025

that's where we have no_trailing_whitespace for non-string trailing whitespaces ;) [non-risky change].

i recommend to not block this PR due to no_trailing_... rules, and conclude on those trailing whitespaces separately.

@keradus
Copy link
Member Author

keradus commented Aug 13, 2025

from practice, most of cases i had strings i actually wanted to remove such spaces, but happy to go both ways and update the ruleset if concluded (or please, feel free to send such PR as well).

as alternative solution, we can change way we write such strings, eg:

--- a/src/Symfony/Component/Console/Tests/Helper/TableTest.php
+++ b/src/Symfony/Component/Console/Tests/Helper/TableTest.php
@@ -1832,20 +1832,20 @@ class TableTest extends TestCase
         ];
 
         yield 'Borderless style' => [
-            <<<EOTXT
-                 ============================== 
-                    ISBN: 99921-58-10-7         
-                   Title: Divine Comedy         
-                  Author: Dante Alighieri       
-                   Price: 9.95                  
-                 ============================== 
-                    ISBN: 9971-5-0210-0         
-                   Title: A Tale of Two Cities  
-                  Author: Charles Dickens       
-                   Price: 139.25                
-                 ============================== 
-
-                EOTXT,
+            implode("\n", [
+                ' ============================== ',
+                '    ISBN: 99921-58-10-7         ',
+                '   Title: Divine Comedy         ',
+                '  Author: Dante Alighieri       ',
+                '   Price: 9.95                  ',
+                ' ============================== ',
+                '    ISBN: 9971-5-0210-0         ',
+                '   Title: A Tale of Two Cities  ',
+                '  Author: Charles Dickens       ',
+                '   Price: 139.25                ',
+                ' ============================== ',
+                '',
+            ]),

benefit? no other tools (like IDE) will complain on trailing whitespaces.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disabled no_trailing_whitespace_in_string.
That'll remove a bunch of false-positives.
Thanks for pointing this exists :)

@nicolas-grekas
Copy link
Member

Thank you @keradus.

@nicolas-grekas nicolas-grekas merged commit 70db939 into symfony:7.4 Aug 13, 2025
9 of 12 checks passed
@keradus keradus deleted the 7.4_CS_2 branch August 13, 2025 16:53
@keradus
Copy link
Member Author

keradus commented Aug 13, 2025

@nicolas-grekas , would you like to re-visit no_trailing_whitespace_in_string ? it's recommended directly from PSR / PER-CS

@keradus
Copy link
Member Author

keradus commented Aug 13, 2025

@nicolas-grekas , also as you are on fire, please check PHP-CS-Fixer/PHP-CS-Fixer#8930

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants