Skip to content

Commit

Permalink
Add more mixed-type bool subtraction tests
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored and ondrejmirtes committed Sep 23, 2024
1 parent 975486e commit a72f752
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tests/PHPStan/Analyser/nsrt/mixed-subtract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php // lint >= 8.0

namespace SubtractMixed;

use function PHPStan\Testing\assertType;

/**
* @param int|0.0|''|'0'|array{}|false|null $moreThenFalsy
*/
function subtract(mixed $m, $moreThenFalsy) {
if ($m !== true) {
assertType("mixed~true", $m);
assertType('bool', (bool) $m); // mixed could still contain something truthy
}
if ($m !== false) {
assertType("mixed~false", $m);
assertType('bool', (bool) $m); // mixed could still contain something falsy
}
if (!is_bool($m)) {
assertType('mixed~bool', $m);
assertType('bool', (bool) $m);
}
if (!is_array($m)) {
assertType('mixed~array', $m);
assertType('bool', (bool) $m);
}

if ($m) {
assertType("mixed~(0|0.0|''|'0'|array{}|false|null)", $m);
assertType('true', (bool) $m);
}
if (!$m) {
assertType("0|0.0|''|'0'|array{}|false|null", $m);
assertType('false', (bool) $m);
}
if (!$m) {
if (!is_int($m)) {
assertType("0.0|''|'0'|array{}|false|null", $m);
assertType('false', (bool)$m);
}
if (!is_bool($m)) {
assertType("0|0.0|''|'0'|array{}|null", $m);
assertType('false', (bool)$m);
}
}

if (!$m || is_int($m)) {
assertType("0.0|''|'0'|array{}|int|false|null", $m);
assertType('bool', (bool) $m);
}

if ($m !== $moreThenFalsy) {
assertType('mixed', $m);
assertType('bool', (bool) $m); // could be true
}
}

0 comments on commit a72f752

Please sign in to comment.