Skip to content

Commit

Permalink
be less strict for generic string type
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed Aug 3, 2022
1 parent ad6477a commit 4cf9bae
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/Psalm/Internal/Analyzer/Statements/Expression/CastAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,14 @@ public static function castIntAttempt(
}

if ($atomic_type instanceof TString) {
if ($atomic_type instanceof TLiteralString && (int) $atomic_type->value !== 0) {
if ($atomic_type instanceof TLiteralString) {
$valid_ints[] = new TLiteralInt((int) $atomic_type->value);
} elseif ($atomic_type instanceof TNumericString) {
$castable_types[] = new TInt();
} else {
// any normal string
$valid_ints[] = new TLiteralInt(0);
// any normal string is technically $valid_int[] = new TLiteralInt(0);
// however we cannot be certain that it's not inferred, therefore less strict
$castable_types[] = new TInt();
}

continue;
Expand Down Expand Up @@ -404,14 +405,13 @@ public static function castIntAttempt(
}

foreach ($return_type->getAtomicTypes() as $sub_atomic_type) {
if ($sub_atomic_type instanceof TLiteralString
&& (int) $sub_atomic_type->value !== 0
) {
if ($sub_atomic_type instanceof TLiteralString) {
$valid_ints[] = new TLiteralInt((int) $sub_atomic_type->value);
} elseif ($sub_atomic_type instanceof TNumericString) {
$castable_types[] = new TInt();
} else {
$valid_ints[] = new TLiteralInt(0);
// see above, why this is less strict
$castable_types[] = new TInt();
}
}

Expand Down Expand Up @@ -545,13 +545,14 @@ public static function castFloatAttempt(
}

if ($atomic_type instanceof TString) {
if ($atomic_type instanceof TLiteralString && (float) $atomic_type->value !== 0.0) {
if ($atomic_type instanceof TLiteralString) {
$valid_floats[] = new TLiteralFloat((float) $atomic_type->value);
} elseif ($atomic_type instanceof TNumericString) {
$castable_types[] = new TFloat();
} else {
// any normal string
$valid_floats[] = new TLiteralFloat(0.0);
// any normal string is technically $valid_floats[] = new TLiteralFloat(0.0);
// however we cannot be certain that it's not inferred, therefore less strict
$castable_types[] = new TFloat();
}

continue;
Expand Down Expand Up @@ -621,14 +622,13 @@ public static function castFloatAttempt(
}

foreach ($return_type->getAtomicTypes() as $sub_atomic_type) {
if ($sub_atomic_type instanceof TLiteralString
&& (float) $sub_atomic_type->value !== 0.0
) {
if ($sub_atomic_type instanceof TLiteralString) {
$valid_floats[] = new TLiteralFloat((float) $sub_atomic_type->value);
} elseif ($sub_atomic_type instanceof TNumericString) {
$castable_types[] = new TFloat();
} else {
$valid_floats[] = new TLiteralFloat(0.0);
// see above, why this is less strict
$castable_types[] = new TFloat();
}
}

Expand Down

0 comments on commit 4cf9bae

Please sign in to comment.