Passed
Push — master ( 265f32...0e6955 )
by Dominik
02:02 queued 12s
created

DenormalizerContextBuilder::setAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Denormalizer;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
9
final class DenormalizerContextBuilder implements DenormalizerContextBuilderInterface
10
{
11
    /**
12
     * @var array|null
13
     */
14
    private $allowedAdditionalFields;
15
16
    /**
17
     * @deprecated
18
     *
19
     * @var string[]
20
     */
21
    private $groups = [];
22
23
    /**
24
     * @var ServerRequestInterface|null
25
     */
26
    private $request;
27
28
    /**
29
     * @var bool
30
     */
31
    private $resetMissingFields = false;
32
33
    /**
34
     * @var array
35
     */
36
    private $attributes = [];
37
38 4
    private function __construct()
39
    {
40 4
    }
41
42
    /**
43
     * @return DenormalizerContextBuilderInterface
44
     */
45 4
    public static function create(): DenormalizerContextBuilderInterface
46
    {
47 4
        return new self();
48
    }
49
50
    /**
51
     * @param array|null $allowedAdditionalFields
52
     *
53
     * @return DenormalizerContextBuilderInterface
54
     */
55 1
    public function setAllowedAdditionalFields(
56
        array $allowedAdditionalFields = null
57
    ): DenormalizerContextBuilderInterface {
58 1
        $this->allowedAdditionalFields = $allowedAdditionalFields;
59
60 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Deserializatio...ormalizerContextBuilder) is incompatible with the return type declared by the interface Chubbyphp\Deserializatio...AllowedAdditionalFields 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...
61
    }
62
63
    /**
64
     * @deprecated
65
     *
66
     * @param string[] $groups
67
     *
68
     * @return DenormalizerContextBuilderInterface
69
     */
70 1
    public function setGroups(array $groups): DenormalizerContextBuilderInterface
71
    {
72 1
        $this->groups = $groups;
0 ignored issues
show
Deprecated Code introduced by
The property Chubbyphp\Deserializatio...ContextBuilder::$groups has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
73
74 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Deserializatio...ormalizerContextBuilder) 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...
75
    }
76
77
    /**
78
     * @param ServerRequestInterface|null $request
79
     *
80
     * @return DenormalizerContextBuilderInterface
81
     */
82 2
    public function setRequest(ServerRequestInterface $request = null): DenormalizerContextBuilderInterface
83
    {
84 2
        $this->request = $request;
85
86 2
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Deserializatio...ormalizerContextBuilder) is incompatible with the return type declared by the interface Chubbyphp\Deserializatio...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...
87
    }
88
89
    /**
90
     * @deprecated
91
     *
92
     * @param bool $resetMissingFields
93
     *
94
     * @return DenormalizerContextBuilderInterface
95
     */
96 1
    public function setResetMissingFields(bool $resetMissingFields): DenormalizerContextBuilderInterface
97
    {
98 1
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
99 1
            'setResetMissingFields is broken by design, please do this your self by model or repository',
100 1
            E_USER_DEPRECATED
101
        );
102
103 1
        $this->resetMissingFields = $resetMissingFields;
104
105 1
        return $this;
106
    }
107
108
    /**
109
     * @param array $attributes
110
     *
111
     * @return DenormalizerContextBuilderInterface
112
     */
113 1
    public function setAttributes(array $attributes): DenormalizerContextBuilderInterface
114
    {
115 1
        $this->attributes = $attributes;
116
117 1
        return $this;
118
    }
119
120
    /**
121
     * @return DenormalizerContextInterface
122
     */
123 3
    public function getContext(): DenormalizerContextInterface
124
    {
125 3
        return new DenormalizerContext(
126 3
            $this->allowedAdditionalFields,
127 3
            $this->groups,
0 ignored issues
show
Deprecated Code introduced by
The property Chubbyphp\Deserializatio...ContextBuilder::$groups has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
128 3
            $this->request,
129 3
            $this->resetMissingFields,
130 3
            $this->attributes
131
        );
132
    }
133
}
134