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

Improved preg_match() output param type inference #7027

Merged
merged 8 commits into from Nov 30, 2021

Conversation

rarila
Copy link
Contributor

@rarila rarila commented Nov 30, 2021

This PR adds a stub for preg_match() that takes flags into account.
Fixes #6914

For reference:

  • Return Values

    • 1 if the pattern matches given subject
    • 0 if the pattern does not match given subject
    • false on failure
  • $matches with flag: 0

    preg_match('/(foo)(not)?(?<name>bar)/', 'foobarbaz', $matches);

    $matches:

    [
      0      => 'foobar',
      1      => 'foo',
      2      => '',
      'name' => 'bar',
      3      => 'bar',
    ]
    

    ➡️ array<array-key, string>

  • $matches with flag: PREG_OFFSET_CAPTURE = 256

    preg_match('/(foo)(not)?(?<name>bar)/', 'foobarbaz', $matches, PREG_OFFSET_CAPTURE);

    $matches:

    [
      0      => [0 => 'foobar', 1 =>  0],
      1      => [0 => 'foo',    1 =>  0],
      2      => [0 => '',       1 => -1],
      'name' => [0 => 'bar',    1 =>  3],
      3      => [0 => 'bar',    1 =>  3],
    ]
    

    ➡️ array<array-key, array{string, 0|positive-int}|array{'', -1}>
    ↪️ array<array-key, array{string, int}>

  • $matches with flag: PREG_UNMATCHED_AS_NULL = 512

    preg_match('/(foo)(not)?(?<name>bar)/', 'foobarbaz', $matches, PREG_UNMATCHED_AS_NULL);

    $matches:

    [
      0      => 'foobar',
      1      => 'foo',
      2      => null,
      'name' => 'bar',
      3      => 'bar',
    ]
    

    ➡️ array<array-key, string|null>

  • $matches with flag: PREG_OFFSET_CAPTURE | PREG_UNMATCHED_AS_NULL = 768

    preg_match('/(foo)(not)?(?<name>bar)/', 'foobarbaz', $matches, PREG_OFFSET_CAPTURE | PREG_UNMATCHED_AS_NULL);

    $matches:

    [
      0      => [0 => 'foobar', 1 =>  0],
      1      => [0 => 'foo',    1 =>  0],
      2      => [0 => null,     1 => -1],
      'name' => [0 => 'bar',    1 =>  3],
      3      => [0 => 'bar',    1 =>  3],
    ]
    

    ➡️ array<array-key, array{string, 0|positive-int}|array{null, -1}>
    ↪️ array<array-key, array{null|string, int}>

  • $matches on error
    On error an E_WARNING is emitted. $matches is not altered then. This case is not handled in this PR.

Legend:
↪️ = expression simplified by Psalm

dictionaries/CallMap.php Show resolved Hide resolved
tests/FunctionCallTest.php Show resolved Hide resolved
@weirdan weirdan added the release:feature The PR will be included in 'Features' section of the release notes label Nov 30, 2021
@weirdan weirdan changed the title Add stub for preg_match Improved preg_match() output param type inference Nov 30, 2021
@weirdan weirdan merged commit 28c4f86 into vimeo:master Nov 30, 2021
@weirdan
Copy link
Collaborator

weirdan commented Nov 30, 2021

Thanks!

@rarila rarila deleted the issue-6914 branch November 30, 2021 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:feature The PR will be included in 'Features' section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

preg_match stub/callmap
2 participants