Context::setMetadataStack()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

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 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer\Context;
13
14
use Ivory\Serializer\Exclusion\ExclusionStrategy;
15
use Ivory\Serializer\Exclusion\ExclusionStrategyInterface;
16
use Ivory\Serializer\Mapping\MetadataInterface;
17
use Ivory\Serializer\Naming\IdenticalNamingStrategy;
18
use Ivory\Serializer\Naming\NamingStrategyInterface;
19
use Ivory\Serializer\Navigator\NavigatorInterface;
20
use Ivory\Serializer\Visitor\VisitorInterface;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25
class Context implements ContextInterface
26
{
27
    /**
28
     * @var NavigatorInterface
29
     */
30
    private $navigator;
31
32
    /**
33
     * @var VisitorInterface
34
     */
35
    private $visitor;
36
37
    /**
38
     * @var int
39
     */
40
    private $direction;
41
42
    /**
43
     * @var string
44
     */
45
    private $format;
46
47
    /**
48
     * @var bool
49
     */
50
    private $ignoreNull = false;
51
52
    /**
53
     * @var ExclusionStrategyInterface
54
     */
55
    private $exclusionStrategy;
56
57
    /**
58
     * @var NamingStrategyInterface
59
     */
60
    private $namingStrategy;
61
62
    /**
63
     * @var mixed[]
64
     */
65
    private $dataStack;
66
67
    /**
68
     * @var MetadataInterface[]
69
     */
70
    private $metadataStack;
71
72
    /**
73
     * @var mixed[]
74
     */
75
    private $options = [];
76
77
    /**
78
     * @param ExclusionStrategyInterface|null $exclusionStrategy
79
     * @param NamingStrategyInterface|null    $namingStrategy
80
     */
81 1216
    public function __construct(
82
        ExclusionStrategyInterface $exclusionStrategy = null,
83
        NamingStrategyInterface $namingStrategy = null
84
    ) {
85 1216
        $this->exclusionStrategy = $exclusionStrategy ?: new ExclusionStrategy();
86 1216
        $this->namingStrategy = $namingStrategy ?: new IdenticalNamingStrategy();
87 1216
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 1472
    public function initialize(NavigatorInterface $navigator, VisitorInterface $visitor, $direction, $format)
93
    {
94 736
        $this
95 1472
            ->setNavigator($navigator)
96 1472
            ->setVisitor($visitor)
97 1472
            ->setDirection($direction)
98 1472
            ->setFormat($format)
99 1472
            ->setDataStack([])
100 1472
            ->setMetadataStack([]);
101 1472
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 1472
    public function getNavigator()
107
    {
108 1472
        return $this->navigator;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 1472
    public function setNavigator(NavigatorInterface $navigator)
115
    {
116 1472
        $this->navigator = $navigator;
117
118 1472
        return $this;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 1456
    public function getVisitor()
125
    {
126 1456
        return $this->visitor;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 1472
    public function setVisitor(VisitorInterface $visitor)
133
    {
134 1472
        $this->visitor = $visitor;
135
136 1472
        return $this;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 1472
    public function getDirection()
143
    {
144 1472
        return $this->direction;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 1472
    public function setDirection($direction)
151
    {
152 1472
        $this->direction = $direction;
153
154 1472
        return $this;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function getFormat()
161
    {
162
        return $this->format;
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168 1472
    public function setFormat($format)
169
    {
170 1472
        $this->format = $format;
171
172 1472
        return $this;
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178 352
    public function isNullIgnored()
179
    {
180 352
        return $this->ignoreNull;
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function setIgnoreNull($ignoreNull)
187
    {
188
        $this->ignoreNull = $ignoreNull;
189
190
        return $this;
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196 1192
    public function getExclusionStrategy()
197
    {
198 1192
        return $this->exclusionStrategy;
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    public function setExclusionStrategy(ExclusionStrategyInterface $exclusionStrategy)
205
    {
206
        $this->exclusionStrategy = $exclusionStrategy;
207
208
        return $this;
209
    }
210
211
    /**
212
     * {@inheritdoc}
213
     */
214 1088
    public function getNamingStrategy()
215
    {
216 1088
        return $this->namingStrategy;
217
    }
218
219
    /**
220
     * {@inheritdoc}
221
     */
222
    public function setNamingStrategy(NamingStrategyInterface $namingStrategy)
223
    {
224
        $this->namingStrategy = $namingStrategy;
225
226
        return $this;
227
    }
228
229
    /**
230
     * {@inheritdoc}
231
     */
232 32
    public function getDataStack()
233
    {
234 32
        return $this->dataStack;
235
    }
236
237
    /**
238
     * {@inheritdoc}
239
     */
240 1472
    public function setDataStack(array $dataStack)
241
    {
242 1472
        $this->dataStack = $dataStack;
243
244 1472
        return $this;
245
    }
246
247
    /**
248
     * {@inheritdoc}
249
     */
250 92
    public function getMetadataStack()
251
    {
252 92
        return $this->metadataStack;
253
    }
254
255
    /**
256
     * {@inheritdoc}
257
     */
258 1472
    public function setMetadataStack(array $metadataStack)
259
    {
260 1472
        $this->metadataStack = $metadataStack;
261
262 1472
        return $this;
263
    }
264
265
    /**
266
     * {@inheritdoc}
267
     */
268 1192
    public function enterScope($data, MetadataInterface $metadata)
269
    {
270 1192
        $this->dataStack[] = $data;
271 1192
        $this->metadataStack[] = $metadata;
272
273 1192
        return $this;
274
    }
275
276
    /**
277
     * {@inheritdoc}
278
     */
279 1192
    public function leaveScope()
280
    {
281 1192
        array_pop($this->dataStack);
282 1192
        array_pop($this->metadataStack);
283
284 1192
        return $this;
285
    }
286
287
    /**
288
     * {@inheritdoc}
289
     */
290
    public function hasOptions()
291
    {
292
        return $this->options;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->options; (array) is incompatible with the return type declared by the interface Ivory\Serializer\Context...xtInterface::hasOptions of type boolean.

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...
293
    }
294
295
    /**
296
     * {@inheritdoc}
297
     */
298
    public function getOptions()
299
    {
300
        return $this->options;
301
    }
302
303
    /**
304
     * {@inheritdoc}
305
     */
306
    public function setOptions(array $options)
307
    {
308
        $this->options = [];
309
        $this->addOptions($options);
310
311
        return $this;
312
    }
313
314
    /**
315
     * {@inheritdoc}
316
     */
317
    public function addOptions(array $options)
318
    {
319
        foreach ($options as $option => $value) {
320
            $this->setOption($option, $value);
321
        }
322
323
        return $this;
324
    }
325
326
    /**
327
     * {@inheritdoc}
328
     */
329
    public function hasOption($option)
330
    {
331
        return array_key_exists($option, $this->options);
332
    }
333
334
    /**
335
     * {@inheritdoc}
336
     */
337
    public function getOption($option)
338
    {
339
        return $this->hasOption($option) ? $this->options[$option] : null;
340
    }
341
342
    /**
343
     * {@inheritdoc}
344
     */
345
    public function setOption($option, $value)
346
    {
347
        $this->options[$option] = $value;
348
349
        return $this;
350
    }
351
352
    /**
353
     * {@inheritdoc}
354
     */
355
    public function removeOption($option)
356
    {
357
        unset($this->options[$option]);
358
359
        return $this;
360
    }
361
}
362