Completed
Push — master ( c120c5...a414f1 )
by Dominik
02:47
created

DenormalizingFieldMappingBuilder::setGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Mapping;
6
7
use Chubbyphp\Deserialization\Accessor\PropertyAccessor;
8
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizer;
9
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface;
10
11
final class DenormalizingFieldMappingBuilder implements DenormalizingFieldMappingBuilderInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    private $name;
17
18
    /**
19
     * @var array
20
     */
21
    private $groups;
22
23
    /**
24
     * @var FieldDenormalizerInterface
25
     */
26
    private $fieldDenormalizer;
27
28
    private function __construct()
29
    {
30
    }
31
32
    /**
33
     * @param string $name
34
     *
35
     * @return DenormalizingFieldMappingBuilderInterface
36
     */
37
    public static function create(string $name): DenormalizingFieldMappingBuilderInterface
38
    {
39
        $self = new self();
40
        $self->name = $name;
41
        $self->groups = [];
42
        $self->fieldDenormalizer = new FieldDenormalizer(new PropertyAccessor($name));
43
44
        return $self;
45
    }
46
47
    /**
48
     * @param array $groups
49
     *
50
     * @return DenormalizingFieldMappingBuilderInterface
51
     */
52
    public function setGroups(array $groups): DenormalizingFieldMappingBuilderInterface
53
    {
54
        $this->groups = $groups;
55
56
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Deserializatio...zingFieldMappingBuilder) is incompatible with the return type declared by the interface Chubbyphp\Deserializatio...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...
57
    }
58
59
    /**
60
     * @param FieldDenormalizerInterface $fieldDenormalizer
61
     *
62
     * @return DenormalizingFieldMappingBuilderInterface
63
     */
64
    public function setFieldDenormalizer(FieldDenormalizerInterface $fieldDenormalizer): DenormalizingFieldMappingBuilderInterface
65
    {
66
        $this->fieldDenormalizer = $fieldDenormalizer;
67
68
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Deserializatio...zingFieldMappingBuilder) is incompatible with the return type declared by the interface Chubbyphp\Deserializatio...e::setFieldDenormalizer 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...
69
    }
70
71
    /**
72
     * @return DenormalizingFieldMappingInterface
73
     */
74
    public function getMapping(): DenormalizingFieldMappingInterface
75
    {
76
        return new DenormalizingFieldMapping($this->name, $this->groups, $this->fieldDenormalizer);
77
    }
78
}
79