Passed
Push — master ( 4d9341...54dd2f )
by Dominik
02:16
created

NormalizerContextBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 59
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 7 1
A setGroups() 0 6 1
A setRequest() 0 6 1
A getContext() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Normalizer;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
9
final class NormalizerContextBuilder implements NormalizerContextBuilderInterface
10
{
11
    /**
12
     * @var string[]
13
     */
14
    private $groups;
15
16
    /**
17
     * @var ServerRequestInterface
18
     */
19
    private $request;
20
21 2
    private function __construct()
22
    {
23 2
    }
24
25
    /**
26
     * @return NormalizerContextBuilderInterface
27
     */
28 2
    public static function create(): NormalizerContextBuilderInterface
29
    {
30 2
        $self = new self();
31 2
        $self->groups = [];
32
33 2
        return $self;
34
    }
35
36
    /**
37
     * @param string[] $groups
38
     *
39
     * @return NormalizerContextBuilderInterface
40
     */
41 1
    public function setGroups(array $groups): NormalizerContextBuilderInterface
42
    {
43 1
        $this->groups = $groups;
44
45 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Serialization\...ormalizerContextBuilder) is incompatible with the return type declared by the interface Chubbyphp\Serialization\...derInterface::setGroups 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
    /**
49
     * @param ServerRequestInterface $request
50
     *
51
     * @return NormalizerContextBuilderInterface
52
     */
53 1
    public function setRequest(ServerRequestInterface $request): NormalizerContextBuilderInterface
54
    {
55 1
        $this->request = $request;
56
57 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Serialization\...ormalizerContextBuilder) is incompatible with the return type declared by the interface Chubbyphp\Serialization\...erInterface::setRequest 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...
58
    }
59
60
    /**
61
     * @return NormalizerContextInterface
62
     */
63 2
    public function getContext(): NormalizerContextInterface
64
    {
65 2
        return new NormalizerContext($this->groups, $this->request);
66
    }
67
}
68