psalm-insane-comparison

A Psalm plugin to detect code susceptible to change behaviour with the introduction of PHP RFC: Saner string to number comparisons

Installation:

$ composer require --dev orklah/psalm-insane-comparison $ vendor/bin/psalm-plugin enable orklah/psalm-insane-comparison

Usage:

Run your usual Psalm command:

$ vendor/bin/psalm

Explanation:

Before PHP8, comparison between a non-empty-string and the literal int 0 resulted in true. This is no longer the case with the PHP RFC: Saner string to number comparisons.

$a = 'banana'; $b = 0; if($a == $b){ echo 'PHP 7 will display this'; } else{ echo 'PHP 8 will display this instead'; }

This plugin helps identify those case to check them before migrating.

You can solve this issue in a lot of ways:

use strict equality:

$a = 'banana'; $b = 0; if($a === $b){ echo 'This is impossible'; } else{ echo 'PHP 7 and 8 will both display this'; } use a cast to make both operands the same type:

$a = 'banana'; $b = 0; if((int)$a == $b){ echo 'PHP 7 and 8 will both display this'; } else{ echo 'This is impossible'; }

$a = 'banana'; $b = 0; if($a == (string)$b){ echo 'This is impossible'; } else{ echo 'PHP 7 and 8 will both display this'; } Make psalm understand you're working with positive-ints when the int operand is not a literal:

$a = 'banana'; /** @var positive-int $b */ if($a == $b){ echo 'This is impossible'; } else{ echo 'PHP 7 and 8 will both display this'; } Make psalm understand you're working with numeric-strings when the string operand is not a literal:

/** @var numeric-string $a */ $b = 0; if($a == $b){ echo 'PHP 7 and 8 will both display this depending on the value of $a'; } else{ echo 'PHP 7 and 8 will both display this depending on the value of $a'; }

版权声明:

1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。
2、网站不提供资料下载,如需下载请到原作者页面进行下载。
3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考学习用!
4、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。