Passed
Push — master ( d251e3...0f10a6 )
by Koen
03:54 queued 38s
created

Lua::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Lua.php
4
 *
5
 * @author Koen Vlaswinkel <[email protected]>
6
 * @since  20/12/2015 14:47
7
 */
8
9
namespace Vlaswinkel\Lua;
10
11
/**
12
 * Class Lua
13
 *
14
 * @package Vlaswinkel\Lua
15
 */
16
class Lua {
17
    public static $luaKeywords = [
18
        'and',
19
        'break',
20
        'do',
21
        'else',
22
        'elseif',
23
        'end',
24
        'false',
25
        'for',
26
        'function',
27
        'if',
28
        'in',
29
        'local',
30
        'nil',
31
        'not',
32
        'or',
33
        'repeat',
34
        'return',
35
        'then',
36
        'true',
37
        'until',
38
        'while',
39
    ];
40
41
    public static function serialize($data) {
42
        return LuaSerializer::encode($data);
43
    }
44
45
    public static function deserialize($data) {
46
        $parser = new LuaParser(new LuaTokenStream(new LuaInputStream($data)));
47
        return LuaToPhpConverter::convertToPhpValue($parser->parse());
1 ignored issue
show
Bug introduced by
It seems like $parser->parse() can be null; however, convertToPhpValue() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
48
    }
49
}