LNumber::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace PhpParser\Node\Scalar;
4
5
use PhpParser\Node\Scalar;
6
7
class LNumber extends Scalar
8
{
9
    /** @var int Number value */
10
    public $value;
11
12
    /**
13
     * Constructs an integer number scalar node.
14
     *
15
     * @param int   $value      Value of the number
16
     * @param array $attributes Additional attributes
17
     */
18
    public function __construct($value, array $attributes = array()) {
19
        parent::__construct($attributes);
20
        $this->value = $value;
21
    }
22
23
    public function getSubNodeNames() {
24
        return array('value');
25
    }
26
27
    /**
28
     * @internal
29
     *
30
     * Parses an LNUMBER token (dec, hex, oct and bin notations) like PHP would.
31
     *
32
     * @param string $str A string number
33
     *
34
     * @return int The parsed number
35
     */
36
    public static function parse($str) {
37
        // handle plain 0 specially
38
        if ('0' === $str) {
39
            return 0;
40
        }
41
42
        // if first char is 0 (and number isn't 0) it's a special syntax
43
        if ('0' === $str[0]) {
44
            // hex
45 View Code Duplication
            if ('x' === $str[1] || 'X' === $str[1]) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
                return hexdec($str);
47
            }
48
49
            // bin
50 View Code Duplication
            if ('b' === $str[1] || 'B' === $str[1]) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
                return bindec($str);
52
            }
53
54
            // oct (intval instead of octdec to get proper cutting behavior with malformed numbers)
55
            return intval($str, 8);
56
        }
57
58
        // dec
59
        return (int) $str;
60
    }
61
}
62