From dc806846299a92155f385a336ff6048d29e10381 Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Sun, 20 Nov 2022 11:11:08 +0100 Subject: [PATCH] literal-string and non-falsy-string handling for bool --- src/Hooks/StrictEqualityHooks.php | 52 +++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/Hooks/StrictEqualityHooks.php b/src/Hooks/StrictEqualityHooks.php index d2abca1..5e3c871 100644 --- a/src/Hooks/StrictEqualityHooks.php +++ b/src/Hooks/StrictEqualityHooks.php @@ -122,6 +122,23 @@ private static function isEqualOrdered(Atomic $first_type, Atomic $second_type): return true; } + // insane comparisons + if ($first_type instanceof Atomic\TFalse && $second_type instanceof Atomic\TNonFalsyString) { + return true; + } + + if ($first_type instanceof Atomic\TFalse && $second_type instanceof Atomic\TLiteralString) { + if ((bool) $second_type->value === true) { + return true; + } + } + + if ($first_type instanceof Atomic\TTrue && $second_type instanceof Atomic\TLiteralString) { + if ((bool) $second_type->value === false) { + return true; + } + } + // array/objects are somewhat safe to compare against strings if (self::isTooComplicatedType($first_type) && $second_type instanceof Atomic\TString) { return true; @@ -157,12 +174,39 @@ private static function isUnionCompatibleType(array $left_type_atomics, array $r private static function isUnionStringEqualOrdered(array $first_types, array $second_types): bool { foreach ($first_types as $atomic_type) { + if ($atomic_type instanceof Atomic\TNonFalsyString) { + $with_false = true; + $with_true = false; + $with_null = true; + continue; + } + + if ($atomic_type instanceof Atomic\TLiteralString) { + if ((bool) $atomic_type->value === true) { + $with_false = true; + $with_true = false; + } else { + $with_false = false; + $with_true = true; + } + + $with_null = false; + if ($atomic_type->value !== '') { + $with_null = true; + } + continue; + } + if ($atomic_type instanceof Atomic\TNonEmptyString) { + $with_false = false; + $with_true = false; $with_null = true; continue; } if ($atomic_type instanceof Atomic\TString) { + $with_false = false; + $with_true = false; $with_null = false; continue; } @@ -175,6 +219,14 @@ private static function isUnionStringEqualOrdered(array $first_types, array $sec continue; } + if ($with_true === true && $atomic_type instanceof Atomic\TTrue) { + continue; + } + + if ($with_false === true && $atomic_type instanceof Atomic\TFalse) { + continue; + } + if ($with_null === true && $atomic_type instanceof Atomic\TNull) { continue; }