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

Regex matches should have array shape if the regex is known #9502

Closed
mvorisek opened this issue Jun 23, 2023 · 9 comments
Closed

Regex matches should have array shape if the regex is known #9502

mvorisek opened this issue Jun 23, 2023 · 9 comments

Comments

@mvorisek
Copy link
Contributor

mvorisek commented Jun 23, 2023

Feature request

If the regex is known/constant a very little trick is needed to obtain the returned array keys/shape: https://3v4l.org/sOXbn

Regexes are everywhere and thus the result shape would improve phpstan checks a lot.

https://phpstan.org/r/71ffe793-4349-4555-8385-a233d152178d

@staabm
Copy link
Contributor

staabm commented Jun 24, 2023

PHPStan already contains a regex AST parser, which could be used for a even more precise typing

@VincentLanglet
Copy link
Contributor

Hi @mvorisek @staabm,

As a first easy step shouldn't we remove the FALSE return type from preg_* call if the regex is valid ?
https://phpstan.org/r/a4de9bb6-e99d-438b-bcb1-17335d0b3608

Do you have in mind FALSE return value with a valid regex or could we create a dynamicReturnTypeExtension which

  • call @preg_match() on the constant values
  • if none is false, remove FALSE from the return value

?

@ondrejmirtes
Copy link
Member

The returned value can still be false even if the regex is valid. This has definitely been discussed somewhere on the issue tracker. There might even have been a PR.

@staabm
Copy link
Contributor

staabm commented Feb 5, 2024

Removing false was discussed in the past in some issue somewhere. A valid regex is not the only case where false is returned

@mvorisek
Copy link
Contributor Author

mvorisek commented Feb 5, 2024

The returned value can still be false even if the regex is valid. This has definitely been discussed somewhere on the issue tracker. There might even have been a PR.

false is returned for example for every invalid UTF-8 input when u PCRE modifier is used

@VincentLanglet
Copy link
Contributor

VincentLanglet commented Feb 5, 2024

The returned value can still be false even if the regex is valid. This has definitely been discussed somewhere on the issue tracker. There might even have been a PR.

Sorry, I looked for multiple issues and tried to find the discussion but didn't find it before asking it.

Thanks for the quick answer !

@staabm
Copy link
Contributor

staabm commented Jul 3, 2024

@ondrejmirtes this issue can be closed

@ondrejmirtes
Copy link
Member

Thanks

Copy link

github-actions bot commented Aug 4, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants