From fc36239be5a3abf0619fd24edd8e8bdc53ab5e24 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Thu, 10 Mar 2016 13:01:42 +0100 Subject: [PATCH] Remove internal LNumber::parse() method Combine it into ::fromString(), as they both do the same checks. --- lib/PhpParser/Node/Scalar/LNumber.php | 53 +++++++-------------------- 1 file changed, 13 insertions(+), 40 deletions(-) diff --git a/lib/PhpParser/Node/Scalar/LNumber.php b/lib/PhpParser/Node/Scalar/LNumber.php index 5fad153..a17a168 100644 --- a/lib/PhpParser/Node/Scalar/LNumber.php +++ b/lib/PhpParser/Node/Scalar/LNumber.php @@ -39,50 +39,23 @@ class LNumber extends Scalar * @return LNumber The constructed LNumber, including kind attribute */ public static function fromString($str, array $attributes = array()) { - if ($str === '0' || $str[0] !== '0') { + if ('0' !== $str[0] || '0' === $str) { $attributes['kind'] = LNumber::KIND_DEC; - } elseif ($str[1] === 'x' || $str[1] === 'X') { + return new LNumber((int) $str, $attributes); + } + + if ('x' === $str[1] || 'X' === $str[1]) { $attributes['kind'] = LNumber::KIND_HEX; - } elseif ($str[1] === 'b' || $str[1] === 'B') { + return new LNumber(hexdec($str), $attributes); + } + + if ('b' === $str[1] || 'B' === $str[1]) { $attributes['kind'] = LNumber::KIND_BIN; - } else { - $attributes['kind'] = LNumber::KIND_OCT; - } - return new self(self::parse($str), $attributes); - } - - /** - * @internal - * - * Parses an LNUMBER token (dec, hex, oct and bin notations) like PHP would. - * - * @param string $str A string number - * - * @return int The parsed number - */ - public static function parse($str) { - // handle plain 0 specially - if ('0' === $str) { - return 0; + return new LNumber(bindec($str), $attributes); } - // if first char is 0 (and number isn't 0) it's a special syntax - if ('0' === $str[0]) { - // hex - if ('x' === $str[1] || 'X' === $str[1]) { - return hexdec($str); - } - - // bin - if ('b' === $str[1] || 'B' === $str[1]) { - return bindec($str); - } - - // oct (intval instead of octdec to get proper cutting behavior with malformed numbers) - return intval($str, 8); - } - - // dec - return (int) $str; + // use intval instead of octdec to get proper cutting behavior with malformed numbers + $attributes['kind'] = LNumber::KIND_OCT; + return new LNumber(intval($str, 8), $attributes); } }