Skip to content

Commit

Permalink
change int/float parser to a native PHP method
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed Mar 22, 2024
1 parent fc0ef3d commit 294a106
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/Psalm/Internal/Type/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
use function defined;
use function end;
use function explode;
use function filter_var;
use function get_class;
use function in_array;
use function is_int;
Expand All @@ -98,9 +99,11 @@
use function strlen;
use function strpos;
use function strtolower;
use function strtr;
use function substr;

use const FILTER_VALIDATE_FLOAT;
use const FILTER_VALIDATE_INT;

/**
* @psalm-suppress InaccessibleProperty Allowed during construction
* @internal
Expand Down Expand Up @@ -424,12 +427,14 @@ public static function getTypeFromTree(
return new TClassConstant($fq_classlike_name, $const_name, $from_docblock);
}

if (preg_match('/^\-?(0|[1-9][0-9]*)(\.[0-9]{1,})$/', $parse_tree->value)) {
return new TLiteralFloat((float) $parse_tree->value, $from_docblock);
$int = filter_var($parse_tree->value, FILTER_VALIDATE_INT);
if ($int !== false) {
return new TLiteralInt($int, $from_docblock);
}

if (preg_match('/^\-?(0|[1-9]([0-9_]*[0-9])?)$/', $parse_tree->value)) {
return new TLiteralInt((int) strtr($parse_tree->value, ['_' => '']), $from_docblock);
$float = filter_var($parse_tree->value, FILTER_VALIDATE_FLOAT);
if ($float !== false) {
return new TLiteralFloat($float, $from_docblock);
}

if (!preg_match('@^(\$this|\\\\?[a-zA-Z_\x7f-\xff][\\\\\-0-9a-zA-Z_\x7f-\xff]*)$@', $parse_tree->value)) {
Expand Down

0 comments on commit 294a106

Please sign in to comment.