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

DenormalizerContext::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 DenormalizerContext implements DenormalizerContextInterface
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;
32
33
    /**
34
     * @var array
35
     */
36
    private $attributes;
37
38
    /**
39
     * @param array|null                  $allowedAdditionalFields
40
     * @param string[]                    $groups
41
     * @param ServerRequestInterface|null $request
42
     * @param bool                        $resetMissingFields
43
     * @param array                       $attributes
44
     */
45 4
    public function __construct(
46
        array $allowedAdditionalFields = null,
47
        array $groups = [],
48
        ServerRequestInterface $request = null,
49
        bool $resetMissingFields = false,
50
        array $attributes = []
51
    ) {
52 4
        $this->allowedAdditionalFields = $allowedAdditionalFields;
53 4
        $this->groups = $groups;
0 ignored issues
show
Deprecated Code introduced by
The property Chubbyphp\Deserializatio...malizerContext::$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...
54 4
        $this->request = $request;
55
56 4
        if ($resetMissingFields) {
57 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...
58 1
                'resetMissingFields is broken by design, please do this your self by model or repository',
59 1
                E_USER_DEPRECATED
60
            );
61
        }
62
63 4
        $this->resetMissingFields = $resetMissingFields;
64 4
        $this->attributes = $attributes;
65 4
    }
66
67
    /**
68
     * @return array|null
69
     */
70 2
    public function getAllowedAdditionalFields()
71
    {
72 2
        return $this->allowedAdditionalFields;
73
    }
74
75
    /**
76
     * @deprecated
77
     *
78
     * @return string[]
79
     */
80 2
    public function getGroups(): array
81
    {
82 2
        return $this->groups;
0 ignored issues
show
Deprecated Code introduced by
The property Chubbyphp\Deserializatio...malizerContext::$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...
83
    }
84
85
    /**
86
     * @return ServerRequestInterface|null
87
     */
88 2
    public function getRequest()
89
    {
90 2
        return $this->request;
91
    }
92
93
    /**
94
     * @deprecated
95
     *
96
     * @return bool
97
     */
98 3
    public function isResetMissingFields(): bool
99
    {
100 3
        return $this->resetMissingFields;
101
    }
102
103
    /**
104
     * @return array
105
     */
106 3
    public function getAttributes(): array
107
    {
108 3
        return $this->attributes;
109
    }
110
111
    /**
112
     * @param string $name
113
     * @param mixed  $default
114
     *
115
     * @return mixed
116
     */
117 2
    public function getAttribute(string $name, $default = null)
118
    {
119 2
        if (isset($this->attributes[$name])) {
120 1
            return $this->attributes[$name];
121
        }
122
123 1
        return $default;
124
    }
125
126
    /**
127
     * @param string $name
128
     * @param mixed  $value
129
     *
130
     * @return DenormalizerContextInterface
131
     */
132 1
    public function withAttribute(string $name, $value): DenormalizerContextInterface
133
    {
134 1
        $context = clone $this;
135 1
        $context->attributes[$name] = $value;
136
137 1
        return $context;
138
    }
139
}
140