Completed
Pull Request — master (#12)
by Dominik
07:37 queued 14s
created

NormalizerContextBuilder::setRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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|null
18
     */
19
    private $request;
20
21
    /**
22
     * @var object|string|null
23
     */
24
    private $role;
25
26 3
    private function __construct()
27
    {
28 3
    }
29
30
    /**
31
     * @return NormalizerContextBuilderInterface
32
     */
33 3
    public static function create(): NormalizerContextBuilderInterface
34
    {
35 3
        return new self();
36
    }
37
38
    /**
39
     * @param string[] $groups
40
     *
41
     * @return NormalizerContextBuilderInterface
42
     */
43 1
    public function setGroups(array $groups): NormalizerContextBuilderInterface
44
    {
45 1
        $this->groups = $groups;
46
47 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...
48
    }
49
50
    /**
51
     * @param ServerRequestInterface|null $request
52
     *
53
     * @return NormalizerContextBuilderInterface
54
     */
55 2
    public function setRequest(ServerRequestInterface $request = null): NormalizerContextBuilderInterface
56
    {
57 2
        $this->request = $request;
58
59 2
        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...
60
    }
61
62
    /**
63
     * @param object|string|null $role
64
     *
65
     * @return NormalizerContextBuilderInterface
66
     */
67
    public function setRole($role): NormalizerContextBuilderInterface
68
    {
69
        $this->role = $role;
70
71
        return $this;
72
    }
73
74
    /**
75
     * @return NormalizerContextInterface
76
     */
77 3
    public function getContext(): NormalizerContextInterface
78
    {
79 3
        return new NormalizerContext($this->groups, $this->request, $this->role);
80
    }
81
}
82