Completed
Push — master ( 602f00...8ac133 )
by stéphane
07:27
created

Node2PHP   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 17

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCompact() 0 19 5
A get() 0 10 4
A getNumber() 0 5 4
A getScalar() 0 14 4
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
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $n should have a doc-comment as per coding-style.
Loading history...
18
     * Returns the correct PHP datatype for the value of the current Node
19
     *
20
     * @return mixed  The value as PHP type : scalar, array or Compact, DateTime
21
     */
22
    public static function get(Node $n)
23
    {
24
        if (is_null($n->value)) return null;
25
        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\NodeList and Dallgoot\Yaml\Node; 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

25
        if ($n->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar(/** @scrutinizer ignore-type */ $n->value);
Loading history...
26
        if ($n->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE))
27
            return self::getCompact(substr($n->value, 1, -1), $n->type);
0 ignored issues
show
Bug introduced by
It seems like $n->value can also be of type Dallgoot\Yaml\NodeList and Dallgoot\Yaml\Node; however, parameter $string of substr() 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
            return self::getCompact(substr(/** @scrutinizer ignore-type */ $n->value, 1, -1), $n->type);
Loading history...
28
        $expected = [Y::JSON   => json_decode($n->value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR),
0 ignored issues
show
Bug introduced by
It seems like $n->value can also be of type Dallgoot\Yaml\NodeList and Dallgoot\Yaml\Node; however, parameter $json of json_decode() 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

28
        $expected = [Y::JSON   => json_decode(/** @scrutinizer ignore-type */ $n->value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR),
Loading history...
29
                     Y::QUOTED => trim($n->value, "\"'"),
0 ignored issues
show
Bug introduced by
It seems like $n->value can also be of type Dallgoot\Yaml\NodeList and Dallgoot\Yaml\Node; 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

29
                     Y::QUOTED => trim(/** @scrutinizer ignore-type */ $n->value, "\"'"),
Loading history...
30
                     Y::RAW    => strval($n->value)];
31
        return $expected[$n->type] ?? null;
32
    }
33
34
    /**
35
     * Returns the correct PHP type according to the string value
36
     *
37
     * @param string $v a string value
38
     *
39
     * @return mixed The value with appropriate PHP type
40
     */
41
    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...
42
    {
43
        if (R::isDate($v))   return date_create($v);
44
        if (R::isNumber($v)) return self::getNumber($v);
45
        $types = ['yes'   => true,
46
                    'no'    => false,
47
                    'true'  => true,
48
                    'false' => false,
49
                    'null'  => null,
50
                    '.inf'  => INF,
51
                    '-.inf' => -INF,
52
                    '.nan'  => NAN
53
        ];
54
        return array_key_exists(strtolower($v), $types) ? $types[strtolower($v)] : strval($v);
55
    }
56
57
    /**
58
     * Returns the correct PHP type according to the string value
59
     *
60
     * @param string $v a string value
61
     *
62
     * @return int|float   The scalar value with appropriate PHP type
63
     */
64
    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...
65
    {
66
        if (preg_match("/^(0o\d+)$/i", $v))      return intval(base_convert($v, 8, 10));
67
        if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10));
68
        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...
69
    }
70
71
    /**
72
     * Returns a Compact object representing the inline object/array provided as string
73
     *
74
     * @param string  $mappingOrSeqString The mapping or sequence string
75
     * @param integer $type               The type
76
     *
77
     * @return Compact The compact object equivalent to $mappingOrString
78
     */
79
    private static function getCompact(string $mappingOrSeqString, int $type):object
0 ignored issues
show
Coding Style introduced by
Private method name "Node2PHP::getCompact" must be prefixed with an underscore
Loading history...
80
    {
81
        //TODO : this should handle references present inside the string
82
        $out = new Compact();
83
        if ($type === Y::COMPACT_SEQUENCE) {
84
            $f = function ($e) { return self::getScalar(trim($e));};
0 ignored issues
show
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
85
            //TODO : that's not robust enough, improve it
86
            foreach (array_map($f, explode(",", $mappingOrSeqString)) as $key => $value) {
87
                $out[$key] = $value;
88
            }
89
        }
90
        if ($type === Y::COMPACT_MAPPING) {
91
            //TODO : that's not robust enough, improve it
92
            foreach (explode(',', $mappingOrSeqString) as $value) {
93
                list($keyName, $keyValue) = explode(':', $value);
94
                $out->{trim($keyName, '\'" ')} = self::getScalar(trim($keyValue));
95
            }
96
        }
97
        return $out;
98
    }
99
}