Completed
Push — master ( 0a933f...8a8a33 )
by Ma
02:04
created

Node::setEndNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Autocomplete\Container\Trie;
3
4
use Autocomplete\Contract\Container\NodeInterface;
5
6
/**
7
 * @implements NodeInterface
8
 */
9
class Node implements NodeInterface
10
{
11
    /**
12
     * @var mixed
13
     */
14
    private $value;
15
    /**
16
     * @var Node
17
     */
18
    private $children = [];
19
    /**
20
     * boolean|string
21
     */
22
    private $endNode;
23
24
    public function __construct($value)
25
    {
26
        $this->value = $value;
27
        $this->endNode = false;
28
    }
29
30
    public function getEndNode()
31
    {
32
        return $this->endNode;
33
    }
34
35
    public function setEndNode($value)
36
    {
37
        $this->endNode = $value;
38
    }
39
40
    public function getChild($string)
41
    {
42
        foreach ($this->children as $child) {
0 ignored issues
show
Bug introduced by
The expression $this->children of type object<Autocomplete\Container\Trie\Node> is not traversable.
Loading history...
43
            if ($child->value == $string) {
44
                return $child;
45
            }
46
        }
47
        return false;
48
    }
49
50
    public function getClosest($prefix)
51
    {
52
        $child = $this;
53
        $prefix = str_split($prefix);
54
        foreach ($prefix as $char) {
55
            if ($child) {
56
                $child = $child->getChild($char);
57
            }
58
        }
59
        return $child;
60
    }
61
62
    public function getPostfix($prefix, $postFixes = [])
63
    {
64
        if ($this->endNode) {
65
            $postFixes[] = $this->endNode;
66
        }
67
        foreach ($this->children as $child) {
0 ignored issues
show
Bug introduced by
The expression $this->children of type object<Autocomplete\Container\Trie\Node> is not traversable.
Loading history...
68
            $postFixes = $child->getPostfix($prefix, $postFixes);
69
        }
70
        return $postFixes;
71
    }
72
73
    public function addChild($name)
74
    {
75
        if ($this->getChild($name)) {
76
            return $this->getChild($name);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getChild($name); (boolean) is incompatible with the return type declared by the interface Autocomplete\Contract\Co...NodeInterface::addChild of type Autocomplete\Contract\Container\Node.

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...
77
        }
78
        $child = new Node($name);
79
        $this->children[] = $child;
80
        return $child;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $child; (Autocomplete\Container\Trie\Node) is incompatible with the return type declared by the interface Autocomplete\Contract\Co...NodeInterface::addChild of type Autocomplete\Contract\Container\Node.

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...
81
    }
82
83
    public function addSuffix($suffix)
84
    {
85
        $method = new Method\AddSuffix;
86
        return $method->execute($suffix, $this);
87
    }
88
89
    public function hasWord($word)
90
    {
91
        $method = new Method\HasWord;
92
        return $method->execute($word, $this);
93
    }
94
95
    public function hasPrefix($prefix)
96
    {
97
        $method = new Method\HasPrefix;
98
        return $method->execute($prefix, $this);
99
    }
100
101
    public function graph(array &$arr, $level = 0)
102
    {
103
        $arr[$level][] = $this->value;
104
        $level++;
105
        foreach ($this->children as $child) {
0 ignored issues
show
Bug introduced by
The expression $this->children of type object<Autocomplete\Container\Trie\Node> is not traversable.
Loading history...
106
            $child->graph($arr, $level);
107
        }
108
    }
109
}
110