Passed
Push — master ( 8b4244...ea75f3 )
by stéphane
03:50
created

Node2PHP::getObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Dallgoot\Yaml;
4
5
use Dallgoot\Yaml\{Yaml as Y, Regex as R};
6
7
/**
8
 * TODO
9
 *
10
 * @author  Stéphane Rebai <[email protected]>
11
 * @license Apache 2.0
12
 * @link    TODO : url to specific online doc
13
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
14
final class Node2PHP
15
{
16
17
    /**
18
     * Returns the correct PHP datatype for the value of the current Node
19
     *
20
     * @param Node $n a Node object to be evaluated as PHP type.
21
     * @return mixed  The value as PHP type : scalar, array or Compact, DateTime
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
22
     * @throws \Exception if occurs in self::getScalar or self::getCompact
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
23
     */
24
    public static function get(Node $n)
25
    {
26
        if (is_null($n->value)) return null;
27
        if ($n->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar($n->value);
0 ignored issues
show
Bug introduced by
It seems like $n->value can also be of type Dallgoot\Yaml\Node and Dallgoot\Yaml\NodeList; however, parameter $v of Dallgoot\Yaml\Node2PHP::getScalar() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
        if ($n->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar(/** @scrutinizer ignore-type */ $n->value);
Loading history...
28
        if ($n->type & Y::JSON) {
29
            return $n->value;
30
        }
31
        $expected = [Y::QUOTED => trim($n->value, "\"'"),
0 ignored issues
show
Bug introduced by
It seems like $n->value can also be of type Dallgoot\Yaml\Node and Dallgoot\Yaml\NodeList; however, parameter $str of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
        $expected = [Y::QUOTED => trim(/** @scrutinizer ignore-type */ $n->value, "\"'"),
Loading history...
32
                     Y::RAW    => strval($n->value)];
33
        return $expected[$n->type] ?? null;
34
    }
35
36
    /**
37
     * Returns the correct PHP type according to the string value
38
     *
39
     * @param string $v a string value
40
     *
41
     * @return mixed The value with appropriate PHP type
42
     * @throws \Exception if happens in R::isDate or R::isNumber
43
     */
44
    private static function getScalar(string $v)
0 ignored issues
show
Coding Style introduced by
Private method name "Node2PHP::getScalar" must be prefixed with an underscore
Loading history...
45
    {
46
        if (R::isDate($v))   return date_create($v);
47
        if (R::isNumber($v)) return self::getNumber($v);
48
        $types = ['yes'   => true,
49
                    'no'    => false,
50
                    'true'  => true,
51
                    'false' => false,
52
                    'null'  => null,
53
                    '.inf'  => INF,
54
                    '-.inf' => -INF,
55
                    '.nan'  => NAN
56
        ];
57
        return array_key_exists(strtolower($v), $types) ? $types[strtolower($v)] : strval($v);
58
    }
59
60
    /**
61
     * Returns the correct PHP type according to the string value
62
     *
63
     * @param string $v a string value
64
     *
65
     * @return int|float   The scalar value with appropriate PHP type
66
     */
67
    private static function getNumber(string $v)
0 ignored issues
show
Coding Style introduced by
Private method name "Node2PHP::getNumber" must be prefixed with an underscore
Loading history...
68
    {
69
        if (preg_match("/^(0o\d+)$/i", $v))      return intval(base_convert($v, 8, 10));
70
        if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10));
71
        return is_bool(strpos($v, '.')) ? intval($v) : floatval($v);
0 ignored issues
show
introduced by
The condition is_bool(strpos($v, '.')) is always false.
Loading history...
72
    }
73
}