|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +// phpcs:disable SlevomatCodingStandard.ControlStructures.DisallowYodaComparison.DisallowedYodaComparison |
| 4 | +// phpcs:disable SlevomatCodingStandard.Operators.DisallowEqualOperators.DisallowedEqualOperator |
| 5 | +// phpcs:disable SlevomatCodingStandard.Operators.DisallowEqualOperators.DisallowedNotEqualOperator |
| 6 | +// phpcs:disable Squiz.Functions.GlobalFunction.Found |
| 7 | +// phpcs:disable Squiz.Strings.DoubleQuoteUsage.NotRequired |
| 8 | + |
| 9 | +namespace EqualTypeNarrowing; |
| 10 | + |
| 11 | +use function PHPStan\Testing\assertType; |
| 12 | + |
| 13 | +/** |
| 14 | + * @param 0|0.0|1|''|'0'|'x'|array{}|bool|object|null $x |
| 15 | + * @param int|string|null $y |
| 16 | + * @param mixed $z |
| 17 | + */ |
| 18 | +function doNull($x, $y, $z): void |
| 19 | +{ |
| 20 | + if ($x == null) { |
| 21 | + assertType("0|0.0|''|array{}|false|null", $x); |
| 22 | + } else { |
| 23 | + assertType("1|'0'|'x'|object|true", $x); |
| 24 | + } |
| 25 | + if (null != $x) { |
| 26 | + assertType("1|'0'|'x'|object|true", $x); |
| 27 | + } else { |
| 28 | + assertType("0|0.0|''|array{}|false|null", $x); |
| 29 | + } |
| 30 | + |
| 31 | + if ($y == null) { |
| 32 | + assertType("0|''|null", $y); |
| 33 | + } else { |
| 34 | + assertType("int<min, -1>|int<1, max>|non-empty-string", $y); |
| 35 | + } |
| 36 | + |
| 37 | + if ($z == null) { |
| 38 | + assertType("0|0.0|''|array{}|false|null", $z); |
| 39 | + } else { |
| 40 | + assertType("mixed~0|0.0|''|array{}|false|null", $z); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * @param 0|0.0|1|''|'0'|'x'|array{}|bool|object|null $x |
| 46 | + * @param int|string|null $y |
| 47 | + * @param mixed $z |
| 48 | + */ |
| 49 | +function doFalse($x, $y, $z): void |
| 50 | +{ |
| 51 | + if ($x == false) { |
| 52 | + assertType("0|0.0|''|'0'|array{}|false|null", $x); |
| 53 | + } else { |
| 54 | + assertType("1|'x'|object|true", $x); |
| 55 | + } |
| 56 | + if (false != $x) { |
| 57 | + assertType("1|'x'|object|true", $x); |
| 58 | + } else { |
| 59 | + assertType("0|0.0|''|'0'|array{}|false|null", $x); |
| 60 | + } |
| 61 | + |
| 62 | + if ($y == false) { |
| 63 | + assertType("0|''|'0'|null", $y); |
| 64 | + } else { |
| 65 | + assertType("int<min, -1>|int<1, max>|non-falsy-string", $y); |
| 66 | + } |
| 67 | + |
| 68 | + if ($z == false) { |
| 69 | + assertType("0|0.0|''|'0'|array{}|false|null", $z); |
| 70 | + } else { |
| 71 | + assertType("mixed~0|0.0|''|'0'|array{}|false|null", $z); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * @param 0|0.0|1|''|'0'|'x'|array{}|bool|object|null $x |
| 77 | + * @param int|string|null $y |
| 78 | + * @param mixed $z |
| 79 | + */ |
| 80 | +function doTrue($x, $y, $z): void |
| 81 | +{ |
| 82 | + if ($x == true) { |
| 83 | + assertType("1|'x'|object|true", $x); |
| 84 | + } else { |
| 85 | + assertType("0|0.0|''|'0'|array{}|false|null", $x); |
| 86 | + } |
| 87 | + if (true != $x) { |
| 88 | + assertType("0|0.0|''|'0'|array{}|false|null", $x); |
| 89 | + } else { |
| 90 | + assertType("1|'x'|object|true", $x); |
| 91 | + } |
| 92 | + |
| 93 | + if ($y == true) { |
| 94 | + assertType("int<min, -1>|int<1, max>|non-falsy-string", $y); |
| 95 | + } else { |
| 96 | + assertType("0|''|'0'|null", $y); |
| 97 | + } |
| 98 | + |
| 99 | + if ($z == true) { |
| 100 | + assertType("mixed~0|0.0|''|'0'|array{}|false|null", $z); |
| 101 | + } else { |
| 102 | + assertType("0|0.0|''|'0'|array{}|false|null", $z); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * @param 0|0.0|1|''|'0'|'x'|array{}|bool|object|null $x |
| 108 | + * @param int|string|null $y |
| 109 | + * @param mixed $z |
| 110 | + */ |
| 111 | +function doEmptyString($x, $y, $z): void |
| 112 | +{ |
| 113 | + // PHP 7.x/8.x compatibility: Keep zero in both cases |
| 114 | + if ($x == '') { |
| 115 | + assertType("0|0.0|''|false|null", $x); |
| 116 | + } else { |
| 117 | + assertType("0|0.0|1|'0'|'x'|array{}|object|true", $x); |
| 118 | + } |
| 119 | + if ('' != $x) { |
| 120 | + assertType("0|0.0|1|'0'|'x'|array{}|object|true", $x); |
| 121 | + } else { |
| 122 | + assertType("0|0.0|''|false|null", $x); |
| 123 | + } |
| 124 | + |
| 125 | + if ($y == '') { |
| 126 | + assertType("0|''|null", $y); |
| 127 | + } else { |
| 128 | + assertType("int|non-empty-string", $y); |
| 129 | + } |
| 130 | + |
| 131 | + if ($z == '') { |
| 132 | + assertType("0|0.0|''|false|null", $z); |
| 133 | + } else { |
| 134 | + assertType("mixed~''|false|null", $z); |
| 135 | + } |
| 136 | +} |
0 commit comments