Completed
Push — master ( c7418d...8b4244 )
by stéphane
04:44
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::COMPACT_MAPPING | Y::COMPACT_SEQUENCE))
29
        //     return self::getCompact(substr($n->value, 1, -1), $n->type);
30
        if ($n->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE| Y::JSON)) {
31
            return $n->value;
32
        }
33
        $expected = [//Y::JSON   => $n->value, //json_decode($n->value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR),
34
                     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

34
                     Y::QUOTED => trim(/** @scrutinizer ignore-type */ $n->value, "\"'"),
Loading history...
35
                     Y::RAW    => strval($n->value)];
36
        return $expected[$n->type] ?? null;
37
    }
38
39
    /**
40
     * Returns the correct PHP type according to the string value
41
     *
42
     * @param string $v a string value
43
     *
44
     * @return mixed The value with appropriate PHP type
45
     * @throws \Exception if happens in R::isDate or R::isNumber
46
     */
47
    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...
48
    {
49
        if (R::isDate($v))   return date_create($v);
50
        if (R::isNumber($v)) return self::getNumber($v);
51
        $types = ['yes'   => true,
52
                    'no'    => false,
53
                    'true'  => true,
54
                    'false' => false,
55
                    'null'  => null,
56
                    '.inf'  => INF,
57
                    '-.inf' => -INF,
58
                    '.nan'  => NAN
59
        ];
60
        return array_key_exists(strtolower($v), $types) ? $types[strtolower($v)] : strval($v);
61
    }
62
63
    /**
64
     * Returns the correct PHP type according to the string value
65
     *
66
     * @param string $v a string value
67
     *
68
     * @return int|float   The scalar value with appropriate PHP type
69
     */
70
    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...
71
    {
72
        if (preg_match("/^(0o\d+)$/i", $v))      return intval(base_convert($v, 8, 10));
73
        if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10));
74
        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...
75
    }
76
77
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $objOr should have a doc-comment as per coding-style.
Loading history...
78
     * Returns a Compact object representing the inline object/array provided as string
79
     *
80
     * @param string  $mappingOrSeqString The mapping or sequence string
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $mappingOrSeqString does not match actual variable name $objOr
Loading history...
81
     * @param integer $type               The type
82
     *
83
     * @return Compact The compact object equivalent to $mappingOrString
84
     * @throws \Exception if raised by self::getScalar
85
     */
86
    private static function getCompact(string $objOr, int $type):object
0 ignored issues
show
Unused Code introduced by
The parameter $objOr is not used and could be removed. ( Ignorable by Annotation )

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

86
    private static function getCompact(/** @scrutinizer ignore-unused */ string $objOr, int $type):object

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The method getCompact() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
Coding Style introduced by
Private method name "Node2PHP::getCompact" must be prefixed with an underscore
Loading history...
87
    {
88
        if ($type === Y::COMPACT_SEQUENCE) {
89
            return new Compact(self::getArray($objOrSeqString));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $objOrSeqString seems to be never defined.
Loading history...
Bug Best Practice introduced by
The method Dallgoot\Yaml\Node2PHP::getArray() is not static, but was called statically. ( Ignorable by Annotation )

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

89
            return new Compact(self::/** @scrutinizer ignore-call */ getArray($objOrSeqString));
Loading history...
90
        }
91
        if ($type === Y::COMPACT_MAPPING) {
92
            return new Compact(self::getObject($objOrSeqString));
93
        }
94
        throw new TypeError(__METHOD__.": paramater 'type' can only be YAML::COMPACT_SEQUENCE or YAML::COMPACT_MAPPING");
0 ignored issues
show
Bug introduced by
The type Dallgoot\Yaml\TypeError was not found. Did you mean TypeError? If so, make sure to prefix the type with \.
Loading history...
95
    }
96
97
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $sequenceString should have a doc-comment as per coding-style.
Loading history...
98
     * @todo : that's not robust enough, improve it.this should handle references present inside the string
99
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
100
    private function getArray($sequenceString)
0 ignored issues
show
Coding Style introduced by
Private method name "Node2PHP::getArray" must be prefixed with an underscore
Loading history...
101
    {
102
        $out = [];
103
        $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...
104
        foreach (array_map($f, explode(",", $sequenceString)) as $key => $value) {
105
            $out[$key] = $value;
106
        }
107
        return $out;
108
    }
109
110
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $objectString should have a doc-comment as per coding-style.
Loading history...
111
     * Gets the compact object.
112
     *
113
     * @todo : that's not robust enough, improve it.this should handle references present inside the string
114
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
115
    private static function getObject($objectString)
0 ignored issues
show
Coding Style introduced by
Private method name "Node2PHP::getObject" must be prefixed with an underscore
Loading history...
116
    {
117
        $out = new \StdClass;
118
        foreach (explode(',', $objectString) as $value) {
119
            list($keyName, $keyValue) = explode(':', $value);
120
            $out->{trim($keyName, '\'" ')} = self::getScalar(trim($keyValue));
121
        }
122
        return $out;
123
    }
124
}