Skip to content

Commit

Permalink
Merge pull request #4 from kkmuffme/allow-literal-empty-string-casts
Browse files Browse the repository at this point in the history
allow casting '' to int/float
  • Loading branch information
orklah authored Dec 17, 2023
2 parents 02e6abb + b1b6b9f commit 3a17d50
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions hooks/StrictNumericCastAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ public static function afterExpressionAnalysis(
continue;
} elseif (
$previous_type instanceof TLiteralString &&
preg_match('#\d#', $previous_type->value[0] ?? '') // will probably have to inverse the check to forbid chars instead
is_numeric( $previous_type->value )
) {
//this is good too. It's not a numeric-string but this is actually more precise
continue;
} elseif (
$previous_type instanceof TLiteralString &&
$previous_type->value === ''
) {
// literal empty strings are safe as they work as expected
// common use case ''|numeric-string
continue;
} elseif (!$previous_type instanceof Type\Atomic\TString) {
//nothing to see here, it's not a string
Expand All @@ -60,7 +66,7 @@ public static function afterExpressionAnalysis(

if (IssueBuffer::accepts(
new StrictNumericCast(
'Unsafe cast from numeric to string. Consider documenting the string as numeric-string.',
'Unsafe cast from string to numeric. Consider documenting the string as numeric-string.',
new CodeLocation($statements_source, $expr)
),
$statements_source->getSuppressedIssues()
Expand Down

0 comments on commit 3a17d50

Please sign in to comment.