Passed
Push — master ( a1caf7...045368 )
by X
30s
created

FloatParser::parseExponentialPart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
1
<?php
2
/**
3
 * parser for PHP serialized float number
4
 */
5
namespace xKerman\Restricted;
6
7
/**
8
 * Parser for PHP serialized float number
9
 *
10
 * format: http://php.net/manual/en/language.types.float.php
11
 */
12
class FloatParser implements ParserInterface
13
{
14
    /**
15
     * parse given `$source` as PHP serialized float number
16
     *
17
     * @param Source $source parser input
18
     * @return array
19
     * @throws UnserializeFailedException
20
     */
21 29
    public function parse(Source $source)
22
    {
23 29
        $source->consume('d:');
24
25 26
        if ($source->peek() === 'N') {
26 1
            return $this->parseNan($source);
27
        }
28
29 25
        $parser = new OptionalSignParser();
30 25
        list($sign, $source) = $parser->parse($source);
31
32 25
        if ($source->peek() === 'I') {
33 3
            return $this->parseInf($source, $sign);
34
        }
35
36 22
        $num = $source->match('/\G((?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+|[0-9]+)(?:[eE][+-]?[0-9]+)?);/');
37 12
        return array(floatval($sign . $num), $source);
38
    }
39
40
    /**
41
     * parse given `$source` as NAN
42
     *
43
     * @param Source $source input
44
     * @return array
45
     * @throws UnserializeFailedException
46
     */
47 1
    private function parseNan($source)
48
    {
49 1
        $source->consume('NAN;');
50 1
        return array(NAN, $source);
51
    }
52
53
    /**
54
     * parse given `$source` as INF / -INF
55
     *
56
     * @param Source $source input
57
     * @param string $sign   '-', '+' or ''
58
     * @return array
59
     * @throws UnserializeFailedException
60
     */
61 3
    private function parseInf($source, $sign)
62
    {
63 3
        if (!in_array($sign, array('', '-'), true)) {
64 1
            return $source->triggerError();
65
        }
66
67 2
        $source->consume('INF;');
68
69 2
        if ($sign === '-') {
70 1
            return array(-INF, $source);
71
        }
72 1
        return array(INF, $source);
73
    }
74
}
75