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

infer non-falsy-string in string replace functions #1661

Merged
merged 2 commits into from Sep 1, 2022

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Aug 30, 2022

No description provided.

@staabm staabm marked this pull request as ready for review August 30, 2022 07:26
@staabm
Copy link
Contributor Author

staabm commented Aug 30, 2022

I reproduced the build error locally with

<?php

function foo(float $f) {
	$s = (string) $f;
	\PHPStan\dumpType($s); // line 5
	$price = str_replace(',', '.', $s);
	\PHPStan\dumpType($price); // line 7
	if ($price == '0') {
		$price = 0.000001;
	}
}

which leads to

> php bin/phpstan analyze test.php  --debug

Note: Using configuration file /Users/staabm/workspace/phpstan-src/phpstan.neon.dist.
/Users/staabm/workspace/phpstan-src/test.php
 ------ -------------------------------------------------------------------------------------------
  Line   test.php
 ------ -------------------------------------------------------------------------------------------
  3      Function foo() has no return type specified.
  5      Dumped type: numeric-string
  7      Dumped type: non-falsy-string
  8      If condition is always false.
  8      Loose comparison using == between non-falsy-string and '0' will always evaluate to false.
  8      Loose comparison via "==" is not allowed.
         💡 Use strict comparison via "===" instead.
 ------ -------------------------------------------------------------------------------------------

 [ERROR] Found 6 errors

and reproduces with https://3v4l.org/6emcl

will work on a fix

staabm added a commit to staabm/phpstan-src that referenced this pull request Aug 30, 2022
@ondrejmirtes ondrejmirtes merged commit a917100 into phpstan:1.8.x Sep 1, 2022
@ondrejmirtes
Copy link
Member

Thank you.

@staabm staabm deleted the non-falsy-replace branch September 1, 2022 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants