NodeCollection   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 6
Bugs 0 Features 1
Metric Value
c 6
b 0
f 1
dl 0
loc 85
wmc 13
lcom 1
cbo 3
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createCollection() 0 4 1
A __construct() 0 7 1
B add() 0 20 5
A remove() 0 10 2
A clear() 0 10 2
A checkUnlocked() 0 6 2
1
<?php
2
3
namespace Nayjest\Tree;
4
5
use InvalidArgumentException;
6
use Nayjest\Collection\Extended\ObjectCollection;
7
use Nayjest\Tree\Exception\LockedNodeException;
8
9
/**
10
 * Class NodeCollection.
11
 *
12
 * NodeCollection in addition to basic collection facilities
13
 * manages parent-child relationships and guarantees tree structure integrity.
14
 */
15
class NodeCollection extends ObjectCollection
16
{
17
    /**
18
     * @var ParentNodeInterface
19
     */
20
    protected $parentNode;
21
22
    public function __construct(
23
        ParentNodeInterface $parentNode,
24
        array $nodes = null
25
    ) {
26
        $this->parentNode = $parentNode;
27
        parent::__construct($nodes);
28
    }
29
30
    /**
31
     * Adds component to collection.
32
     *
33
     * If component is already in collection, it will not be added twice.
34
     *
35
     * @param ChildNodeInterface $item
36
     * @param bool               $prepend Pass true to add component to the beginning of an array.
37
     *
38
     * @return $this
39
     */
40
    public function add($item, $prepend = false)
41
    {
42
        if (!$item instanceof ChildNodeInterface) {
43
            $details = is_object($item) ? get_class($item) : gettype($item);
44
            throw new InvalidArgumentException(
45
                "NodeCollection accepts only objects implementing ChildNodeInterface, $details given."
46
            );
47
        }
48
        $old = $item->parent();
49
        if ($old === $this->parentNode) {
50
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Nayjest\Tree\NodeCollection) is incompatible with the return type of the parent method Nayjest\Collection\Extended\ObjectCollection::add of type Nayjest\Collection\CollectionWriteTrait.

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...
51
        } elseif ($old !== null) {
52
            $item->detach();
53
        }
54
        $this->checkUnlocked($item);
55
        parent::add($item, $prepend);
56
        $item->internalSetParent($this->parentNode);
57
58
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Nayjest\Tree\NodeCollection) is incompatible with the return type of the parent method Nayjest\Collection\Extended\ObjectCollection::add of type Nayjest\Collection\CollectionWriteTrait.

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...
59
    }
60
61
    /**
62
     * @param ChildNodeInterface $item
63
     *
64
     * @return $this
65
     */
66
    public function remove($item)
67
    {
68
        if ($item->parent() === $this->parentNode) {
69
            $this->checkUnlocked($item);
70
            $item->internalUnsetParent();
71
            parent::remove($item);
72
        }
73
74
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Nayjest\Tree\NodeCollection) is incompatible with the return type of the parent method Nayjest\Collection\Exten...bjectCollection::remove of type Nayjest\Collection\CollectionWriteTrait.

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...
75
    }
76
77
    public function clear()
78
    {
79
        /** @var ChildNodeInterface $item */
80
        foreach ($this->items() as $item) {
81
            $this->checkUnlocked($item);
82
            $item->internalUnsetParent();
83
        }
84
85
        return parent::clear();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return parent::clear(); (Nayjest\Tree\NodeCollection) is incompatible with the return type of the parent method Nayjest\Collection\Exten...ObjectCollection::clear of type Nayjest\Collection\CollectionWriteTrait.

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...
86
    }
87
88
    protected function createCollection(array $items)
89
    {
90
        return new ObjectCollection($items);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Nayjest\Coll...jectCollection($items); (Nayjest\Collection\Extended\ObjectCollection) is incompatible with the return type of the parent method Nayjest\Collection\Exten...ction::createCollection of type Nayjest\Collection\CollectionReadTrait.

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...
91
    }
92
93
    private function checkUnlocked(ChildNodeInterface $child)
94
    {
95
        if ($child->isLocked()) {
96
            throw new LockedNodeException();
97
        }
98
    }
99
}
100