-
-
Notifications
You must be signed in to change notification settings - Fork 680
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
Minor indentation bug with template php code where statements have comments to the right. #8533
Comments
That known drawaback https://github.com/rectorphp/rector?tab=readme-ov-file#known-drawbacks You can skip the rule via configuration https://getrector.com/documentation/ignoring-rules-or-paths |
Hi @samsonasik |
@samsonasik Ref known drawbacks, are you referring to the use of the AST with issues with spaces or that it's a mix of HTML & PHP? I'm wondering if we can resolve the issue by having rector check if rules match a block or even at a file level and if there were no matching rules then it'd not apply the indentation change? |
When comparing content, php-parser print it first, and during print, the space maybe wiped on php-parser side, that unrelated with any rule, you can skip the path instead for that. |
@samsonasik ok I'll investigate it in the php-parser directly as I suspect it's an issue with it handling trailing comments. Adding a Is there any way to have rector check if there were matching rules in a block or file maybe and not update the code if none matched? I can't easily exclude these files as they too need rector rules applied. |
it probably was already handled before in the history: since we don't support html + php support since long time ago, I am not sure if that actually need to bring back, as the support should be in php-parser. but you may can try in /**
* @var string
* @see https://regex101.com/r/xP2MGa/1
*/
private const OPEN_TAG_SPACED_REGEX = '#^(?<open_tag_spaced>[^\S\r\n]+\<\?php)#m'; and add line after if (preg_replace(self::OPEN_TAG_SPACED_REGEX, '<?php', $ltrimOriginalFileContent) === $newContent) {
return;
} if that works for you, I will think if that can be back, while still will need @TomasVotruba decision. |
Just checked, it seems the open tag spaced regex not working on this case if bring back, so probably need special handling, but not sure if it worths the effort. |
If you're insterested to cover regex on this, you can create PR to https://github.com/rectorphp/rector-src
vendor/bin/phpunit tests/Issues/NoNamespaced |
@samsonasik thanks, appreciate your help with this, checking the rectorphp/rector-src repo out now to test with 👍 |
…anges Spaces removed from before opening tags in both the original and new content. Fixes: rectorphp/rector#8533
@samsonasik I've submitted a PR which resolves the issue by stripping the spaces before the original and new, what do you think? |
…anges (#5701) * fix: Strip left spaces from opening tags when comparing output for changes Spaces removed from before opening tags in both the original and new content. Fixes: rectorphp/rector#8533 * refactor: switched to ## delimiter for consistency Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com> * refactor: when comparing trimmed existing and new content, check before regex first as it can be a little slower * fix: when checking for changes, no need to ltrim the new content --------- Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
Bug Report
Hi, this is a bit of a weird one but on the legacy code I'm upgrading there's a a lot of comments after if statements etc. for inline php in HTML templates, such as:
Rector runs over the code fine but it changes the indentation of the closing tag for some reason.
It's to do with the comment being there as removing the comment in the code fixes it.
Resulting diff from running rector:
Minimal PHP Code Causing Issue
Examples with each type of comment that results in indentation change:
https://getrector.com/demo/4565e8a3-8ca6-458d-bb9f-a88dc83935ee
https://getrector.com/demo/a25d56d0-c6ad-43de-b5d6-29a389e3a7e6
https://getrector.com/demo/5f58a4fc-7888-4d5c-891b-4c8735085310
Example without comment where it doesn't change indentation:
https://getrector.com/demo/b1f7573b-445c-4666-b3ce-12141dca9b50
Above examples show it happening to if statements but tested and I believe all similar block statements have this issue, such as foreach: https://getrector.com/demo/1cf98379-d3e1-4310-90fe-ca75c009588f
Expected Behaviour
Is it possible to have Rector skip this change?
Has anyone else run into a similar issue?
I can resolve by removing the comments if needed but there's many throughout this project that'd need switching to either remove or show the comment above where it's needed.
I wonder if there's a way to have Rector skip changes like this where no other issue were spotted in that diff?
Thank you.
The text was updated successfully, but these errors were encountered: