Skip to content

\K must reset matches, false positives since v2.1.11 #12792

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

Closed
mvorisek opened this issue Mar 24, 2025 · 2 comments · Fixed by phpstan/phpstan-src#3920
Closed

\K must reset matches, false positives since v2.1.11 #12792

mvorisek opened this issue Mar 24, 2025 · 2 comments · Fixed by phpstan/phpstan-src#3920

Comments

@mvorisek
Copy link
Contributor

mvorisek commented Mar 24, 2025

Bug report

phpstan/phpstan-src#3897 has broken our pipelines as we use \K heavily (https://github.com/atk4/data/actions/runs/14046848345/job/39329419333).

PHPStan can be hotfixed by assuming all regexes containing \K are "not yet understood". But understanding \K is not that dificult, so better variant of fix would be to implement the support right away.

/cc @staabm

Code snippet that reproduces the problem

https://phpstan.org/r/bcf6e7a9-64b3-4bbd-b7d7-fb1c44f0efc8

Expected output

no error

@staabm
Copy link
Contributor

staabm commented Mar 25, 2025

I have never used \K - feel free send a PR as it seems you know how it needs to behave

@mvorisek
Copy link
Contributor Author

mvorisek commented Mar 25, 2025

Citing the php docs:

\K can be used to reset the match start. For example, the pattern foo\Kbar matches "foobar", but reports that it has matched "bar". The use of \K does not interfere with the setting of captured substrings. For example, when the pattern (foo)\Kbar matches "foobar", the first substring is still set to "foo".

ref: https://www.php.net/manual/en/regexp.reference.escape.php

php repro: https://3v4l.org/PcEOS

To fix this issue, $matches[0] must be of type string (instead of non-empty-string) when \K escape sequence is present anywhere in the regex. Or only when "there is NOT at least something always matched after \K - a\K.* vs a\K.+".

Looking into the code it can be implemented in https://github.com/phpstan/phpstan-src/pull/3897/files#diff-2d99eb3989780111d33d57f098cc862bed1aa0da5984be26570e4d0d2304c091R106.

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 a pull request may close this issue.

2 participants