AlNiTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 124
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 124
loc 124
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getDataSet() 4 4 1
A getModelClass() 4 4 1
A testMakeRootInsert() 11 11 1
A testMakeRootUpdate() 11 11 1
A testPrependTo() 11 11 1
A testPrependToAnotherTree() 11 11 1
A testAppendTo() 11 11 1
A testInsertBefore() 11 11 1
A testInsertAfter() 11 11 1
A testInsertAfterAnotherTree() 11 11 1
A testDelete() 5 5 1
A testDeleteWithChildren() 6 6 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
 * @link https://github.com/paulzi/yii2-auto-tree
4
 * @copyright Copyright (c) 2015 PaulZi <[email protected]>
5
 * @license MIT (https://github.com/paulzi/yii2-auto-tree/blob/master/LICENSE)
6
 */
7
8
namespace paulzi\autotree\tests;
9
10
use paulzi\autotree\tests\models\NodeAlNi;
11
use Yii;
12
13
/**
14
 * @author PaulZi <[email protected]>
15
 * @group AlNi
16
 */
17 View Code Duplication
class AlNiTest extends AutoTreeTraitTestCase
0 ignored issues
show
Duplication introduced by
This class 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...
18
{
19
    /**
20
     * @inheritdoc
21
     */
22
    public function getDataSet()
23
    {
24
        return new \PHPUnit_Extensions_Database_DataSet_ArrayDataSet(require(__DIR__ . '/data/data-ni.php'));
25
    }
26
27
    public function getModelClass()
28
    {
29
        return NodeAlNi::className();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \paulzi\autotree\...\NodeAlNi::className(); (string) is incompatible with the return type of the parent method paulzi\autotree\tests\Au...TestCase::getModelClass of type yii\db\BaseActiveRecord|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
30
    }
31
32
    public function testMakeRootInsert()
33
    {
34
        $node = new NodeAlNi(['slug' => 'r']);
35
        $this->assertTrue($node->makeRoot()->save());
36
37
        $node->refresh();
38
        $this->assertEquals(null,       $node->parent_id);
39
        $this->assertEquals(0,          $node->lft);
40
        $this->assertEquals(2147483647, $node->rgt);
41
        $this->assertEquals(0,          $node->depth);
42
    }
43
44
    public function testMakeRootUpdate()
45
    {
46
        $node = NodeAlNi::findOne(9);
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNi::findOne() has too many arguments starting with 9.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
47
        $this->assertTrue($node->makeRoot()->save());
48
49
        $node->refresh();
50
        $this->assertEquals(null,       $node->parent_id);
51
        $this->assertEquals(0,          $node->lft);
52
        $this->assertEquals(2147483647, $node->rgt);
53
        $this->assertEquals(0,          $node->depth);
54
    }
55
56
    public function testPrependTo()
57
    {
58
        $node = new NodeAlNi(['slug' => 'new']);
59
        $this->assertTrue($node->prependTo(NodeAlNi::findOne(1))->save());
0 ignored issues
show
Bug introduced by
It seems like \paulzi\autotree\tests\models\NodeAlNi::findOne(1) can be null; however, prependTo() 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...
Unused Code introduced by
The call to NodeAlNi::findOne() has too many arguments starting with 1.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
60
61
        $node->refresh();
62
        $this->assertEquals(1, $node->parent_id);
63
        $this->assertEquals(1, $node->lft);
64
        $this->assertEquals(2, $node->rgt);
65
        $this->assertEquals(1, $node->depth);
66
    }
67
68
    public function testPrependToAnotherTree()
69
    {
70
        $node = NodeAlNi::findOne(30);
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNi::findOne() has too many arguments starting with 30.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
71
        $this->assertTrue($node->prependTo(NodeAlNi::findOne(4))->save());
0 ignored issues
show
Bug introduced by
It seems like \paulzi\autotree\tests\models\NodeAlNi::findOne(4) can be null; however, prependTo() 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...
Unused Code introduced by
The call to NodeAlNi::findOne() has too many arguments starting with 4.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
72
73
        $node->refresh();
74
        $this->assertEquals(4, $node->parent_id);
75
        $this->assertEquals(1091940810, $node->lft);
76
        $this->assertEquals(1419523107, $node->rgt);
77
        $this->assertEquals(2, $node->depth);
78
    }
79
80
    public function testAppendTo()
81
    {
82
        $node = NodeAlNi::findOne(10);
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNi::findOne() has too many arguments starting with 10.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
83
        $this->assertTrue($node->appendTo(NodeAlNi::findOne(18))->save());
0 ignored issues
show
Bug introduced by
It seems like \paulzi\autotree\tests\m...s\NodeAlNi::findOne(18) can be null; however, appendTo() 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...
Unused Code introduced by
The call to NodeAlNi::findOne() has too many arguments starting with 18.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
84
85
        $node->refresh();
86
        $this->assertEquals(18, $node->parent_id);
87
        $this->assertEquals(1000013, $node->lft);
88
        $this->assertEquals(1000024, $node->rgt);
89
        $this->assertEquals(4, $node->depth);
90
    }
91
92
    public function testInsertBefore()
93
    {
94
        $node = new NodeAlNi(['slug' => 'new']);
95
        $this->assertTrue($node->insertBefore(NodeAlNi::findOne(22))->save());
0 ignored issues
show
Bug introduced by
It seems like \paulzi\autotree\tests\m...s\NodeAlNi::findOne(22) can be null; however, insertBefore() 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...
Unused Code introduced by
The call to NodeAlNi::findOne() has too many arguments starting with 22.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
96
97
        $node->refresh();
98
        $this->assertEquals(9, $node->parent_id);
99
        $this->assertEquals(300000005, $node->lft);
100
        $this->assertEquals(300000006, $node->rgt);
101
        $this->assertEquals(3, $node->depth);
102
    }
103
104
    public function testInsertAfter()
105
    {
106
        $node = NodeAlNi::findOne(32);
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNi::findOne() has too many arguments starting with 32.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
107
        $this->assertTrue($node->insertAfter(NodeAlNi::findOne(30))->save());
0 ignored issues
show
Bug introduced by
It seems like \paulzi\autotree\tests\m...s\NodeAlNi::findOne(30) can be null; however, insertAfter() 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...
Unused Code introduced by
The call to NodeAlNi::findOne() has too many arguments starting with 30.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
108
109
        $node->refresh();
110
        $this->assertEquals(26, $node->parent_id);
111
        $this->assertEquals(2147483644, $node->lft);
112
        $this->assertEquals(2147483646, $node->rgt);
113
        $this->assertEquals(1, $node->depth);
114
    }
115
116
    public function testInsertAfterAnotherTree()
117
    {
118
        $node = NodeAlNi::findOne(26);
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNi::findOne() has too many arguments starting with 26.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
119
        $this->assertTrue($node->insertAfter(NodeAlNi::findOne(21))->save());
0 ignored issues
show
Bug introduced by
It seems like \paulzi\autotree\tests\m...s\NodeAlNi::findOne(21) can be null; however, insertAfter() 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...
Unused Code introduced by
The call to NodeAlNi::findOne() has too many arguments starting with 21.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
120
121
        $node->refresh();
122
        $this->assertEquals(9, $node->parent_id);
123
        $this->assertEquals(1031907726, $node->lft);
124
        $this->assertEquals(1784921473, $node->rgt);
125
        $this->assertEquals(3, $node->depth);
126
    }
127
128
    public function testDelete()
129
    {
130
        $this->assertEquals(1, NodeAlNi::findOne(30)->delete());
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNi::findOne() has too many arguments starting with 30.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
131
        $this->assertEquals(null, NodeAlNi::findOne(30));
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNi::findOne() has too many arguments starting with 30.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
132
    }
133
134
    public function testDeleteWithChildren()
135
    {
136
        $this->assertEquals(10, NodeAlNi::findOne(4)->deleteWithChildren());
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNi::findOne() has too many arguments starting with 4.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
137
        $this->assertEquals(null, NodeAlNi::findOne(24));
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNi::findOne() has too many arguments starting with 24.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
138
        $this->assertEquals(15, NodeAlNi::findOne(1)->deleteWithChildren());
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNi::findOne() has too many arguments starting with 1.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
139
    }
140
}