Test Setup Failed
Branch master (d251e3)
by Koen
02:30
created

LuaToPhpConverterTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 46.38 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 5
dl 32
loc 69
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testString() 0 7 1
A testNumber() 0 7 1
A testNil() 0 7 1
A testSimpleTable() 14 14 1
A testNestedTable() 18 18 1
A testEmptyTable() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * LuaToPhpConverterTest.php
4
 *
5
 * @author Koen Vlaswinkel <[email protected]>
6
 * @since  21/12/2015 17:50
7
 */
8
9
namespace Vlaswinkel\Lua\Tests;
10
11
use Vlaswinkel\Lua\LuaInputStream;
12
use Vlaswinkel\Lua\LuaParser;
13
use Vlaswinkel\Lua\LuaTokenStream;
14
use Vlaswinkel\Lua\LuaToPhpConverter;
15
16
class LuaToPhpConverterTest extends \PHPUnit_Framework_TestCase {
17
    public function testString() {
18
        $parser = new LuaParser(new LuaTokenStream(new LuaInputStream('"foo"')));
19
20
        $node = $parser->parse();
21
22
        $this->assertEquals("foo", LuaToPhpConverter::convertToPhpValue($node));
1 ignored issue
show
Bug introduced by
It seems like $node defined by $parser->parse() on line 20 can be null; however, Vlaswinkel\Lua\LuaToPhpC...er::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...
23
    }
24
25
    public function testNumber() {
26
        $parser = new LuaParser(new LuaTokenStream(new LuaInputStream('1337')));
27
28
        $node = $parser->parse();
29
30
        $this->assertEquals(1337, LuaToPhpConverter::convertToPhpValue($node));
1 ignored issue
show
Bug introduced by
It seems like $node defined by $parser->parse() on line 28 can be null; however, Vlaswinkel\Lua\LuaToPhpC...er::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...
31
    }
32
33
    public function testNil() {
34
        $parser = new LuaParser(new LuaTokenStream(new LuaInputStream('nil')));
35
36
        $node = $parser->parse();
37
38
        $this->assertEquals(null, LuaToPhpConverter::convertToPhpValue($node));
1 ignored issue
show
Bug introduced by
It seems like $node defined by $parser->parse() on line 36 can be null; however, Vlaswinkel\Lua\LuaToPhpC...er::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...
39
    }
40
41 View Code Duplication
    public function testSimpleTable() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
        $parser = new LuaParser(new LuaTokenStream(new LuaInputStream('{ foo = "bar" }')));
43
44
        $node = $parser->parse();
45
46
        $result = LuaToPhpConverter::convertToPhpValue($node);
1 ignored issue
show
Bug introduced by
It seems like $node defined by $parser->parse() on line 44 can be null; however, Vlaswinkel\Lua\LuaToPhpC...er::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...
47
48
        $this->assertEquals(
49
            [
50
                'foo' => 'bar',
51
            ],
52
            $result
53
        );
54
    }
55
56 View Code Duplication
    public function testNestedTable() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
        $parser = new LuaParser(new LuaTokenStream(new LuaInputStream('{ foo = { "bar" = { 1337 } } }')));
58
59
        $node = $parser->parse();
60
61
        $result = LuaToPhpConverter::convertToPhpValue($node);
1 ignored issue
show
Bug introduced by
It seems like $node defined by $parser->parse() on line 59 can be null; however, Vlaswinkel\Lua\LuaToPhpC...er::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...
62
63
        $this->assertEquals(
64
            [
65
                'foo' => [
66
                    'bar' => [
67
                        1337,
68
                    ],
69
                ],
70
            ],
71
            $result
72
        );
73
    }
74
75
    public function testEmptyTable() {
76
        $parser = new LuaParser(new LuaTokenStream(new LuaInputStream('{}')));
77
78
        $node = $parser->parse();
79
80
        $result = LuaToPhpConverter::convertToPhpValue($node);
1 ignored issue
show
Bug introduced by
It seems like $node defined by $parser->parse() on line 78 can be null; however, Vlaswinkel\Lua\LuaToPhpC...er::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...
81
82
        $this->assertEquals([], $result);
83
    }
84
}