Completed
Push — master ( cc8038...edef00 )
by Bartko
01:27
created

AncestorQueryBuilder::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace StefanoTree\NestedSet\QueryBuilder;
6
7
use StefanoTree\NestedSet\Manipulator\ManipulatorInterface;
8
use StefanoTree\NestedSet\Utilities;
9
10
class AncestorQueryBuilder implements AncestorQueryBuilderInterface
11
{
12
    private $manipulator;
13
14
    private $excludeFirstNLevel = 0;
15
    private $excludeLastNLevel = 0;
16
17
    /**
18
     * @param ManipulatorInterface $manipulator
19
     */
20
    public function __construct(ManipulatorInterface $manipulator)
21
    {
22
        $this->manipulator = $manipulator;
23
    }
24
25
    public function get($nodeId, bool $nested = false): array
26
    {
27
        $result = $this->getManipulator()
28
            ->getAncestors($nodeId, $this->excludeFirstNLevel, $this->excludeLastNLevel);
29
30
        return $nested ?
31
            Utilities::flatToNested($result, $this->getManipulator()->getOptions()->getLevelColumnName()) : $result;
32
    }
33
34
    public function excludeFirstNLevel(int $count): AncestorQueryBuilderInterface
35
    {
36
        $this->excludeFirstNLevel = $count;
37
38
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (StefanoTree\NestedSet\Qu...er\AncestorQueryBuilder) is incompatible with the return type declared by the interface StefanoTree\NestedSet\Qu...ace::excludeFirstNLevel of type self.

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...
39
    }
40
41
    public function excludeLastNLevel(int $count): AncestorQueryBuilderInterface
42
    {
43
        $this->excludeLastNLevel = $count;
44
45
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (StefanoTree\NestedSet\Qu...er\AncestorQueryBuilder) is incompatible with the return type declared by the interface StefanoTree\NestedSet\Qu...face::excludeLastNLevel of type self.

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...
46
    }
47
48
    private function getManipulator(): ManipulatorInterface
49
    {
50
        return $this->manipulator;
51
    }
52
}
53