Skip to content

Commit

Permalink
literal-string and non-falsy-string handling for bool
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed Dec 1, 2022
1 parent b5a467c commit dc80684
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Hooks/StrictEqualityHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit dc80684

Please sign in to comment.