Skip to content

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

Closed

Description

@mvorisek

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

Activity

  1. staabm commented on Jun 24, 2023

    @staabm
    Contributor

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

  2. VincentLanglet commented on Feb 5, 2024

    @VincentLanglet
    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

    ?

  3. ondrejmirtes commented on Feb 5, 2024

    @ondrejmirtes
    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.

  4. staabm commented on Feb 5, 2024

    @staabm
    Contributor

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

  5. mvorisek commented on Feb 5, 2024

    @mvorisek
    ContributorAuthor

    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

  6. VincentLanglet commented on Feb 5, 2024

    @VincentLanglet
    Contributor

    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 !

  7. staabm commented on Jul 3, 2024

    @staabm
    Contributor

    @ondrejmirtes this issue can be closed

  8. ondrejmirtes commented on Jul 3, 2024

    @ondrejmirtes
    Member

    Thanks

  9. github-actions commented on Aug 4, 2024

    @github-actions

    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.

  10. locked as resolved and limited conversation to collaborators on Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions