Passed
Pull Request — master (#11)
by Dominik
02:22
created

DenormalizationFieldMappingBuilder::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 0
cts 8
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Doctrine\Mapping;
6
7
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizer;
8
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface;
9
use Chubbyphp\Deserialization\Doctrine\Accessor\PropertyAccessor;
10
use Chubbyphp\Deserialization\Mapping\DenormalizationFieldMapping;
11
use Chubbyphp\Deserialization\Mapping\DenormalizationFieldMappingBuilderInterface;
12
use Chubbyphp\Deserialization\Mapping\DenormalizationFieldMappingInterface;
13
14 View Code Duplication
final class DenormalizationFieldMappingBuilder implements DenormalizationFieldMappingBuilderInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
{
16
    /**
17
     * @var string
18
     */
19
    private $name;
20
21
    /**
22
     * @var array
23
     */
24
    private $groups;
25
26
    /**
27
     * @var FieldDenormalizerInterface
28
     */
29
    private $fieldDenormalizer;
30
31
    private function __construct()
32
    {
33
    }
34
35
    /**
36
     * @param string $name
37
     *
38
     * @return DenormalizationFieldMappingBuilderInterface
39
     */
40
    public static function create(string $name): DenormalizationFieldMappingBuilderInterface
41
    {
42
        $self = new self();
43
        $self->name = $name;
44
        $self->groups = [];
45
        $self->fieldDenormalizer = new FieldDenormalizer(new PropertyAccessor($name));
46
47
        return $self;
48
    }
49
50
    /**
51
     * @param array $groups
52
     *
53
     * @return DenormalizationFieldMappingBuilderInterface
54
     */
55
    public function setGroups(array $groups): DenormalizationFieldMappingBuilderInterface
56
    {
57
        $this->groups = $groups;
58
59
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Deserializatio...tionFieldMappingBuilder) 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...
60
    }
61
62
    /**
63
     * @param FieldDenormalizerInterface $fieldDenormalizer
64
     *
65
     * @return DenormalizationFieldMappingBuilderInterface
66
     */
67
    public function setFieldDenormalizer(
68
        FieldDenormalizerInterface $fieldDenormalizer
69
    ): DenormalizationFieldMappingBuilderInterface {
70
        $this->fieldDenormalizer = $fieldDenormalizer;
71
72
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Deserializatio...tionFieldMappingBuilder) 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...
73
    }
74
75
    /**
76
     * @return DenormalizationFieldMappingInterface
77
     */
78
    public function getMapping(): DenormalizationFieldMappingInterface
79
    {
80
        return new DenormalizationFieldMapping($this->name, $this->groups, $this->fieldDenormalizer);
81
    }
82
}
83