Skip to content
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

[Php55] Handle crash on ([[:upper:]]+) regex on PregReplaceEModifierRector #3037

Merged
merged 6 commits into from
Nov 7, 2022

Conversation

samsonasik
Copy link
Member

@samsonasik samsonasik commented Nov 6, 2022

Given the following code:

class SkipNoModifier
{
    public function run()
    {
        echo preg_replace('([[:upper:]]+)', '_$0', 'FOO');
    }
}

It currently cause crash:

There was 1 error:

1) Rector\Tests\Php55\Rector\FuncCall\PregReplaceEModifierRector\PregReplaceEModifierRectorTest::test with data set #0 ('/Users/samsonasik/www/rector-...hp.inc')
PhpParser\Error: Syntax error, unexpected '$' on line 1

It should be skipped instead.

Fixes rectorphp/rector#7573

@samsonasik
Copy link
Member Author

Fixed 🎉 /cc @jtojnar

@samsonasik
Copy link
Member Author

The issue seems on [[:upper:]]+) detected as modifier.

@samsonasik samsonasik changed the title [Php55] Handle crash on no quote replace on PregReplaceEModifierRector [Php55] Handle crash on no ([[:upper:]]+) regex on PregReplaceEModifierRector Nov 6, 2022
@samsonasik samsonasik changed the title [Php55] Handle crash on no ([[:upper:]]+) regex on PregReplaceEModifierRector [Php55] Handle crash on ([[:upper:]]+) regex on PregReplaceEModifierRector Nov 6, 2022
@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba it is ready for review.

@@ -46,6 +46,10 @@ public function resolvePatternExpressionWithoutEIfFound(Expr $expr): Concat|Stri
return null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Won’t this not handle (…)e now?

Copy link
Member Author

@samsonasik samsonasik Nov 6, 2022

Choose a reason for hiding this comment

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

that seems require separate PR as the modifer can not only e:

echo preg_replace('{[[:upper:]]+}se', '_$0', 'FOO');

which seems needs extra check with e modifier which the change must only remove the e, to be:

echo preg_replace('{[[:upper:]]+}s', '_$0', 'FOO');

Also possibly other use cases for that.

Feel free to provide a different PR for solution for that if you found a solution first for that.

@@ -46,6 +46,10 @@ public function resolvePatternExpressionWithoutEIfFound(Expr $expr): Concat|Stri
return null;
}

if ($delimiter === '(' && $pattern[strlen($pattern) - 1] === ')') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, other brackets can be used as delimiters, to avoid having to escape the characters in the expression: https://www.php.net/manual/en/regexp.reference.delimiters.php

Copy link
Member Author

Choose a reason for hiding this comment

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

I add more brackets to handle that 7855361

@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba I think it is ready.

@TomasVotruba
Copy link
Member

Thank you 👍

@TomasVotruba TomasVotruba merged commit b7a6427 into main Nov 7, 2022
@TomasVotruba TomasVotruba deleted the handle-crash-no-quote branch November 7, 2022 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash in PregReplaceEModifierRector
3 participants