Completed
Push — master ( aba289...58e266 )
by Ventaquil
02:10
created

Builder::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace PHPAlgorithms\GraphTools\Graph\Node;
4
5
use PHPAlgorithms\GraphTools\Abstracts\BuilderAbstract;
6
use PHPAlgorithms\GraphTools\Graph\Builder as GraphBuilder;
7
use PHPAlgorithms\GraphTools\Graph\Node;
8
9
/**
10
 * Class Builder
11
 * @package PHPAlgorithms\GraphTools\Graph\Node
12
 * @method integer|null getX()
13
 * @method integer|null getY()
14
 * @method integer|null getZ()
15
 * @method Builder setX(integer|null $x)
16
 * @method Builder setY(integer|null $y)
17
 * @method Builder setZ(integer|null $z)
18
 * @property integer|null $x
19
 * @property integer|null $y
20
 * @property integer|null $z
21
 */
22
class Builder extends BuilderAbstract
23
{
24
    /**
25
     * @var string[] $allowedGet
26
     */
27
    protected $allowedGet = array('x', 'y', 'z');
28
29
    /**
30
     * @var string[] $allowedSet
31
     */
32
    protected $allowedSet = array('x', 'y', 'z');
33
34
    /**
35
     * @var GraphBuilder|null $builder
36
     */
37
    private $builder;
38
39
    /**
40
     * @var integer $x
41
     */
42
    protected $x;
43
44
    /**
45
     * @var integer $y
46
     */
47
    protected $y;
48
49
    /**
50
     * @var integer $z
51
     */
52
    protected $z;
53
54
    /**
55
     * Builder constructor.
56
     * @param GraphBuilder|null $builder
57
     */
58
    public function __construct(?GraphBuilder $builder = null)
59
    {
60
        $this->builder = $builder;
61
    }
62
63
    /**
64
     * @return Node|GraphBuilder
65
     */
66
    public function build()
67
    {
68
        $node = new Node($this->x, $this->y, $this->z);
69
70
        if (empty($this->builder)) {
71
            return $node;
72
        }
73
74
        return $this->builder->addNode($node);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->builder->addNode($node); (PHPAlgorithms\GraphTools\Graph\Builder) is incompatible with the return type declared by the interface PHPAlgorithms\GraphTools...BuilderInterface::build of type PHPAlgorithms\GraphTools...aces\BuildableInterface.

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