1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-16 03:16:54 +01:00
PHP-Parser/lib/PhpParser/Node/Scalar/LNumber.php
Nikita Popov a2d7e8977a Use real properties for storing subnodes
Instead of storing subnodes in a subNodes dictionary, they are
now stored as simple properties. This requires declarating the
properties, assigning them in the constructor, overriding
the getSubNodeNames() method and passing NULL to the first argument
of the NodeAbstract constructor.

[Deprecated: It's still possible to use the old mode of operation
for custom nodes by passing an array of subnodes to the constructor.]

The only behavior difference this should cause is that getSubNodeNames()
will always return the original subnode names and skip any additional
properties that were dynamically added. E.g. this means that the
"namespacedName" node added by the NameResolver visitor is not treated
as a subnode, but as a dynamic property instead.

This change improves performance and memory usage.
2015-03-09 08:54:20 +01:00

62 lines
1.4 KiB
PHP

<?php
namespace PhpParser\Node\Scalar;
use PhpParser\Node\Scalar;
class LNumber extends Scalar
{
/** @var int Number value */
public $value;
/**
* Constructs an integer number scalar node.
*
* @param int $value Value of the number
* @param array $attributes Additional attributes
*/
public function __construct($value = 0, array $attributes = array()) {
parent::__construct(null, $attributes);
$this->value = $value;
}
public function getSubNodeNames() {
return array('value');
}
/**
* @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;
}
// 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;
}
}