No Description

DNumber.php 1.8KB

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