annotate vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/DNumber.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 5fb285c0d0e3
children
rev   line source
Chris@13 1 <?php declare(strict_types=1);
Chris@0 2
Chris@0 3 namespace PhpParser\Node\Scalar;
Chris@0 4
Chris@0 5 use PhpParser\Node\Scalar;
Chris@0 6
Chris@0 7 class DNumber extends Scalar
Chris@0 8 {
Chris@0 9 /** @var float Number value */
Chris@0 10 public $value;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Constructs a float number scalar node.
Chris@0 14 *
Chris@0 15 * @param float $value Value of the number
Chris@0 16 * @param array $attributes Additional attributes
Chris@0 17 */
Chris@13 18 public function __construct(float $value, array $attributes = []) {
Chris@0 19 parent::__construct($attributes);
Chris@0 20 $this->value = $value;
Chris@0 21 }
Chris@0 22
Chris@13 23 public function getSubNodeNames() : array {
Chris@13 24 return ['value'];
Chris@0 25 }
Chris@0 26
Chris@0 27 /**
Chris@0 28 * @internal
Chris@0 29 *
Chris@0 30 * Parses a DNUMBER token like PHP would.
Chris@0 31 *
Chris@0 32 * @param string $str A string number
Chris@0 33 *
Chris@0 34 * @return float The parsed number
Chris@0 35 */
Chris@13 36 public static function parse(string $str) : float {
Chris@0 37 // if string contains any of .eE just cast it to float
Chris@0 38 if (false !== strpbrk($str, '.eE')) {
Chris@0 39 return (float) $str;
Chris@0 40 }
Chris@0 41
Chris@0 42 // otherwise it's an integer notation that overflowed into a float
Chris@0 43 // if it starts with 0 it's one of the special integer notations
Chris@0 44 if ('0' === $str[0]) {
Chris@0 45 // hex
Chris@0 46 if ('x' === $str[1] || 'X' === $str[1]) {
Chris@0 47 return hexdec($str);
Chris@0 48 }
Chris@0 49
Chris@0 50 // bin
Chris@0 51 if ('b' === $str[1] || 'B' === $str[1]) {
Chris@0 52 return bindec($str);
Chris@0 53 }
Chris@0 54
Chris@0 55 // oct
Chris@0 56 // substr($str, 0, strcspn($str, '89')) cuts the string at the first invalid digit (8 or 9)
Chris@0 57 // so that only the digits before that are used
Chris@0 58 return octdec(substr($str, 0, strcspn($str, '89')));
Chris@0 59 }
Chris@0 60
Chris@0 61 // dec
Chris@0 62 return (float) $str;
Chris@0 63 }
Chris@13 64
Chris@13 65 public function getType() : string {
Chris@13 66 return 'Scalar_DNumber';
Chris@13 67 }
Chris@0 68 }