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

Builder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A build() 0 10 2
1
<?php
2
3
namespace PHPAlgorithms\GraphTools\Graph\Connection;
4
5
use PHPAlgorithms\GraphTools\Abstracts\BuilderAbstract;
6
use PHPAlgorithms\GraphTools\Graph\Builder as GraphBuilder;
7
use PHPAlgorithms\GraphTools\Graph\Connection;
8
use PHPAlgorithms\GraphTools\Graph\Node;
9
10
/**
11
 * Class Builder
12
 * @package PHPAlgorithms\GraphTools\Graph\Connection
13
 * @method Node|null getFrom()
14
 * @method Node|null getTo()
15
 * @method float|null getWeight()
16
 * @method Builder setFrom(Node|null $from)
17
 * @method Builder setTo(Node|null $to)
18
 * @method Builder setWeight(float|null $weight)
19
 * @property Node|null $from
20
 * @property Node|null $to
21
 * @property float|null $weight
22
 */
23
class Builder extends BuilderAbstract
24
{
25
    /**
26
     * @var string[] $allowedGet
27
     */
28
    protected $allowedGet = array('from', 'to', 'weight');
29
30
    /**
31
     * @var string[] $allowedSet
32
     */
33
    protected $allowedSet = array('from', 'to', 'weight');
34
35
    /**
36
     * @var GraphBuilder|null $builder
37
     */
38
    private $builder;
39
40
    /**
41
     * @var Node $from
42
     */
43
    protected $from;
44
45
    /**
46
     * @var Node $to
47
     */
48
    protected $to;
49
50
    /**
51
     * @var float|null $weight
52
     */
53
    protected $weight;
54
55
    /**
56
     * Builder constructor.
57
     * @param GraphBuilder|null $builder
58
     */
59
    public function __construct(?GraphBuilder $builder = null)
60
    {
61
        $this->builder = $builder;
62
    }
63
64
    /**
65
     * @return Connection|GraphBuilder
66
     */
67
    public function build()
68
    {
69
        $connection = new Connection($this->from, $this->to, $this->weight);
70
71
        if (empty($this->builder)) {
72
            return $connection;
73
        }
74
75
        return $this->builder->addConnection($connection);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->builder->a...onnection($connection); (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...
76
    }
77
}
78