Skip to content

Regression: regex matches after checking non-empty array #14270

Closed

Description

@ondrejmirtes

Bug report

What do you think about this? /cc @staabm

public function getDuration(string $path): void {
		preg_match('~^([a-z]+)\\:\\/\\/(.+)~', $path, $matches);
		$scheme = null;
		if ($matches !== []) {
			[, $scheme, $path] = $matches;
		}
	}

I think if the array is non-empty then the keys will exist, right? But PHPStan reports:

Offset 1 might not exist on non-empty-list{0?: string, 1?: non-empty-string, 2?: non-empty-string}.
Offset 2 might not exist on non-empty-list{0?: string, 1?: non-empty-string, 2?: non-empty-string}.

Code snippet that reproduces the problem

https://phpstan.org/r/9344ca07-6178-44d2-844b-b2b3903d2a2b

Expected output

No errors

Did PHPStan help you today? Did it make you happy in any way?

No response

Activity

  1. added this to the Easy fixes milestone on Mar 12, 2026
  2. ondrejmirtes commented on Mar 12, 2026

    @ondrejmirtes
    MemberAuthor

    Regression in 5b79cca6d4186fb25d807adb9ca80b84664f7ec2

  3. VincentLanglet commented on Mar 12, 2026

    @VincentLanglet
    Contributor

    Hi,

    This is not really a regression.

    This is a duplicate of #11602 in which we thought the issue was solved but it's just it that every might not exist on non-empty-list error (even if it was a false positive or a real error) was not reported.

    This was the section

    https://github.com/phpstan/phpstan/issues/11602: this was supposed to be fixed by https://github.com/phpstan/phpstan-src/pull/3899 but wasn't: the array shape was transformed into an intersection list&array shape, that's why the error wasn't reported anymore. (cc @staabm)
    => A way to see this is the fact it's reported with reportPossiblyNonexistentConstantArrayOffset [phpstan.org/r/42af1687-c88d-4369-8c0b-7d3d1a14fcaa](https://phpstan.org/r/42af1687-c88d-4369-8c0b-7d3d1a14fcaa)
    

    Currently a workaround is https://phpstan.org/r/3d5430ba-5821-4df1-b007-ad4094919527

  4. added a commit that references this issue on Mar 12, 2026
    024a65c
  5. phpstan-bot commented on Mar 12, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#5209

  6. added 2 commits that reference this issue on Mar 12, 2026
    024a65c
    8d36b11
  7. added a commit that references this issue on Apr 7, 2026
    7b2c4ca
  8. github-actions commented on Apr 13, 2026

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

  9. locked as resolved and limited conversation to collaborators on Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions