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

Remove null-return on preg_replace() with valid patterns #3338

Merged
merged 1 commit into from
Sep 2, 2024

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Aug 22, 2024

Remove null-return on preg_replace() with valid patterns
Return benevolent union from preg_replace()

closes phpstan/phpstan#11547

requires #3339

@staabm staabm force-pushed the preg-repl branch 2 times, most recently from 20f79f5 to e434231 Compare August 22, 2024 13:53
@staabm
Copy link
Contributor Author

staabm commented Aug 22, 2024

//cc @thg2k

@thg2k
Copy link
Contributor

thg2k commented Aug 23, 2024

See the original issue. I was proven wrong: a perfectly valid regexp can still fail based on the input. So maybe if the regexp looks correct we return a benevolent union with null?

@staabm staabm marked this pull request as ready for review August 26, 2024 09:03
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@staabm
Copy link
Contributor Author

staabm commented Aug 26, 2024

failing build in phpdoc-parser repo is related to a baselined error which no longer happens after the PR (because benevolent)

@@ -18,7 +18,7 @@ public function replace(string $search, string $replacement, string $subject){
assertType('non-empty-string', str_replace($search, $replacement, $subject));
assertType('non-empty-string', str_ireplace($search, $replacement, $subject));

assertType('non-empty-string|null', preg_replace($search, $replacement, $subject));
assertType('(non-empty-string|null)', preg_replace($search, $replacement, $subject));

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems a preexisting problem, but it's wrong: preg_replace('/^a(b?)/', '\\1', 'a');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please open a new issue

@ondrejmirtes
Copy link
Member

Do we want this? Why it would be benevolent? Do we not want the user to always check for errors?

@staabm
Copy link
Contributor Author

staabm commented Sep 2, 2024

I think it was one of the requests of the initial issue. dropped benevolence now and reduced it to the bugfix only

@ondrejmirtes ondrejmirtes merged commit 0c9487d into phpstan:1.12.x Sep 2, 2024
488 of 499 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@staabm staabm deleted the preg-repl branch September 2, 2024 18:48
This pull request was closed.
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.

Improved return type of preg_replace
4 participants