You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Casting preg_match() return value breaks array shapes for $matches by-ref parameter #11262
related with phpstan/phpstan-src#3175, IMO such hack should not be needed as the return type is always 1|false and thus == 1 is strictly equal to === 1
changed the title [-]Casting preg_match() return value to bool breaks array shapes for $matches by-ref parameter[/-][+]Casting preg_match() return value breaks array shapes for $matches by-ref parameter[/+]on Jul 1, 2024
related with phpstan/phpstan-src#3175, IMO such hack should not be needed as the return type is always 1|false and thus == 1 is strictly equal to === 1
@mvorisekpreg_match returns 1|0|false .. I don't think its as easy implemented as you think.
but maybe you want to give it a try and see how it goes
@@ @@-17: Offset 'background_imagee' does not exist on array{0: string, background_image: string, 1: string}.+17: Offset 'background_imagee' does not exist on array{0: string, background_image: non-empty-string, 1: non-empty-string}.
Full report
Line
Error
17
Offset 'background_imagee' does not exist on array{0: string, background_image: non-empty-string, 1: non-empty-string}.
In my PHP code I was needing to add this condition '' !== $matches['background_image'] as in the following if statement:
if (
is_string( $style )
&&
1 === preg_match( '/background(?:-image)?\s*:[^;]*?url\(\s*[\'"]?\s*(?<background_image>.+?)\s*[\'"]?\s*\)/', $style, $matches )
&&
'' !== $matches['background_image'] // PHPStan should ideally know that this is a non-empty string based on the `.+?` regular expression. See <https://github.com/phpstan/phpstan/issues/11222>.
&&
! $this->is_data_url( $matches['background_image'] )
) {
Ah wait, it's working because I changed it to check the return value of preg_match() to be equal to 1. It is still broken if I try (bool) preg_match(...).
Activity
mvorisek commentedon Jul 1, 2024
related with phpstan/phpstan-src#3175, IMO such hack should not be needed as the return type is always
1|falseand thus== 1is strictly equal to=== 1ondrejmirtes commentedon Jul 1, 2024
phpstan-strict-rules doesn't require you to cast to bool. You can do this instead: https://phpstan.org/r/7b3e4faf-28d9-426f-96de-42a65f2a8416 (typo on purpose to verify the inference works)
But otherwise yeah, this is a bug worth fixing.
[-]Casting preg_match() return value to bool breaks array shapes for $matches by-ref parameter[/-][+]Casting preg_match() return value breaks array shapes for $matches by-ref parameter[/+]staabm commentedon Jul 2, 2024
I think we have the same problem for other functions
https://phpstan.org/r/1984afb8-1492-4c1e-b4d6-33154eb14998
staabm commentedon Jul 2, 2024
@mvorisek
preg_matchreturns1|0|false.. I don't think its as easy implemented as you think.but maybe you want to give it a try and see how it goes
mvorisek commentedon Jul 2, 2024
I mean "truthy context" should be implied also by
expr == 1or(bool) expr.phpstan-bot commentedon Jul 6, 2024
@westonruter After the latest push in 1.12.x, PHPStan now reports different result with your code snippet:
phpstan-bot commentedon Jul 6, 2024
@westonruter After the latest push in 1.12.x, PHPStan now reports different result with your code snippet:
phpstan-bot commentedon Jul 6, 2024
@ondrejmirtes After the latest push in 1.12.x, PHPStan now reports different result with your code snippet:
phpstan-bot commentedon Jul 19, 2024
@westonruter After the latest push in 1.12.x, PHPStan now reports different result with your code snippet:
Full report
Offset 'background_image' might not exist on array{0?: string, background_image?: non-empty-string, 1?: non-empty-string}.phpstan-bot commentedon Jul 19, 2024
@westonruter After the latest push in 1.12.x, PHPStan now reports different result with your code snippet:
Full report
Offset 'background_image' might not exist on array{0?: string, background_image?: non-empty-string, 1?: non-empty-string}.phpstan-bot commentedon Jul 19, 2024
@ondrejmirtes After the latest push in 1.12.x, PHPStan now reports different result with your code snippet:
Full report
Offset 'background_imagee' does not exist on array{0: string, background_image: non-empty-string, 1: non-empty-string}.westonruter commentedon Jul 24, 2024
Fixed as of v1.11.8
Seldaek commentedon Jul 24, 2024
Are you sure? I can still repro your error from first post, and as far as I know this issue was not addressed
westonruter commentedon Jul 24, 2024
In my PHP code I was needing to add this condition
'' !== $matches['background_image']as in the followingifstatement:No longer needed as of the latest version: WordPress/performance#1395
westonruter commentedon Jul 24, 2024
Ah wait, it's working because I changed it to check the return value of
preg_match()to be equal to1. It is still broken if I try(bool) preg_match(...).phpstan-bot commentedon Sep 1, 2024
@staabm After the latest push in 1.12.x, PHPStan now reports different result with your code snippet:
staabm commentedon Sep 3, 2024
@ondrejmirtes the repro-snippet of this issue was fixed with the recent cast-narrowing improvements (see phpstan-bot comment above).
I have copied the only left repro from the description into #11293, as it is very similar to the mentioned referenced issue.
this means we can close here IMO.
github-actions commentedon Oct 7, 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.