Skip to content

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

Merged
ondrejmirtes merged 2 commits intophpstan:1.8.xfrom
staabm:non-falsy-replace
Sep 1, 2022
Merged

infer non-falsy-string in string replace functions#1661
ondrejmirtes merged 2 commits intophpstan:1.8.xfrom
staabm:non-falsy-replace

Conversation

@staabm
Copy link
Copy Markdown
Contributor

@staabm staabm commented Aug 30, 2022

No description provided.

@staabm staabm force-pushed the non-falsy-replace branch from 0f79dd4 to 5156705 Compare August 30, 2022 07:18
@staabm staabm marked this pull request as ready for review August 30, 2022 07:26
@staabm
Copy link
Copy Markdown
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 staabm force-pushed the non-falsy-replace branch from afafe37 to c04fc8a Compare August 30, 2022 17:39
@ondrejmirtes ondrejmirtes merged commit a917100 into phpstan:1.8.x Sep 1, 2022
@ondrejmirtes
Copy link
Copy Markdown
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

Development

Successfully merging this pull request may close these issues.

2 participants