Completed
Pull Request — master (#923)
by Asmir
02:54
created

SerializationGraphNavigator::afterVisitingObject()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 5
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright 2016 Johannes M. Schmitt <[email protected]>
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
namespace JMS\Serializer;
22
23
use JMS\Serializer\Accessor\AccessorStrategyInterface;
24
use JMS\Serializer\Construction\ObjectConstructorInterface;
25
use JMS\Serializer\EventDispatcher\EventDispatcher;
26
use JMS\Serializer\EventDispatcher\EventDispatcherInterface;
27
use JMS\Serializer\EventDispatcher\ObjectEvent;
28
use JMS\Serializer\EventDispatcher\PreDeserializeEvent;
29
use JMS\Serializer\EventDispatcher\PreSerializeEvent;
30
use JMS\Serializer\Exception\CircularReferenceDetectedException;
31
use JMS\Serializer\Exception\ExcludedClassException;
32
use JMS\Serializer\Exception\ExpressionLanguageRequiredException;
33
use JMS\Serializer\Exception\InvalidArgumentException;
34
use JMS\Serializer\Exception\NotAcceptableException;
35
use JMS\Serializer\Exception\RuntimeException;
36
use JMS\Serializer\Exclusion\ExpressionLanguageExclusionStrategy;
37
use JMS\Serializer\Expression\ExpressionEvaluatorInterface;
38
use JMS\Serializer\Handler\HandlerRegistryInterface;
39
use JMS\Serializer\Metadata\ClassMetadata;
40
use Metadata\MetadataFactoryInterface;
41
42
/**
43
 * Handles traversal along the object graph.
44
 *
45
 * This class handles traversal along the graph, and calls different methods
46
 * on visitors, or custom handlers to process its nodes.
47
 *
48
 * @author Johannes M. Schmitt <[email protected]>
49
 */
50
final class SerializationGraphNavigator implements GraphNavigatorInterface
51
{
52
    /**
53
     * @var ExpressionLanguageExclusionStrategy
54
     */
55
    private $expressionExclusionStrategy;
56
57
    private $dispatcher;
58
    private $metadataFactory;
59
    private $handlerRegistry;
60
    /**
61
     * @var AccessorStrategyInterface
62
     */
63
    private $accessor;
64
65 317
    public function __construct(
66
        MetadataFactoryInterface $metadataFactory,
67
        HandlerRegistryInterface $handlerRegistry,
68
        AccessorStrategyInterface $accessor,
69
        EventDispatcherInterface $dispatcher = null,
70
        ExpressionEvaluatorInterface $expressionEvaluator = null
71
    )
72
    {
73 317
        $this->dispatcher = $dispatcher ?: new EventDispatcher();
74 317
        $this->metadataFactory = $metadataFactory;
75 317
        $this->handlerRegistry = $handlerRegistry;
76 317
        $this->accessor = $accessor;
77 317
        if ($expressionEvaluator) {
78 21
            $this->expressionExclusionStrategy = new ExpressionLanguageExclusionStrategy($expressionEvaluator);
79
        }
80 317
    }
81
82
    /**
83
     * Called for each node of the graph that is being traversed.
84
     *
85
     * @param mixed $data the data depends on the direction, and type of visitor
86
     * @param null|array $type array has the format ["name" => string, "params" => array]
87
     * @param Context|SerializationContext $context
88
     * @return mixed the return value depends on the direction, and type of visitor
89
     */
90 274
    public function accept($data, array $type = null, Context $context)
91
    {
92 274
        $visitor = $context->getVisitor();
93 274
        $shouldSerializeNull = $context->shouldSerializeNull();
94
95
        // If the type was not given, we infer the most specific type from the
96
        // input data in serialization mode.
97 274
        if (null === $type) {
98
99 254
            $typeName = \gettype($data);
100 254
            if ('object' === $typeName) {
101 180
                $typeName = \get_class($data);
102
            }
103
104 254
            $type = array('name' => $typeName, 'params' => array());
105
        }
106
        // If the data is null, we have to force the type to null regardless of the input in order to
107
        // guarantee correct handling of null values, and not have any internal auto-casting behavior.
108 142
        else if (null === $data) {
109 5
            $type = array('name' => 'NULL', 'params' => array());
110
        }
111
        // Sometimes data can convey null but is not of a null type.
112
        // Visitors can have the power to add this custom null evaluation
113 274
        if ($visitor instanceof NullAwareVisitorInterface && $visitor->isNull($data) === true) {
114
            $type = array('name' => 'NULL', 'params' => array());
115
        }
116
117 274
        switch ($type['name']) {
118 274
            case 'NULL':
119 38
                if (!$shouldSerializeNull) {
120 15
                    throw new NotAcceptableException();
121
                }
122 23
                return $visitor->visitNull($data, $type);
123
124 250
            case 'string':
125 156
                return $visitor->visitString((string)$data, $type);
126
127 246
            case 'int':
128 246
            case 'integer':
129 42
                return $visitor->visitInteger((int)$data, $type);
130
131 243
            case 'bool':
132 243
            case 'boolean':
133 13
                return $visitor->visitBoolean((bool)$data, $type);
134
135 238
            case 'double':
136 229
            case 'float':
137 20
                return $visitor->visitDouble((float)$data, $type);
138
139 229
            case 'array':
140 100
                $this->visiting = true;
0 ignored issues
show
Bug Best Practice introduced by
The property visiting does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
141 100
                return $visitor->visitArray((array)$data, $type);
142
143 191
            case 'resource':
144 1
                $msg = 'Resources are not supported in serialized data.';
145 1
                if (null !== $path = $context->getPath()) {
0 ignored issues
show
Bug introduced by
The method getPath() does not exist on JMS\Serializer\Context. It seems like you code against a sub-type of JMS\Serializer\Context such as JMS\Serializer\SerializationContext. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
                if (null !== $path = $context->/** @scrutinizer ignore-call */ getPath()) {
Loading history...
146 1
                    $msg .= ' Path: ' . $path;
147
                }
148
149 1
                throw new RuntimeException($msg);
150
151
            default:
152
153 190
                if (null !== $data) {
154 190
                    if ($context->isVisiting($data)) {
0 ignored issues
show
Bug introduced by
The method isVisiting() does not exist on JMS\Serializer\Context. It seems like you code against a sub-type of JMS\Serializer\Context such as JMS\Serializer\SerializationContext. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

154
                    if ($context->/** @scrutinizer ignore-call */ isVisiting($data)) {
Loading history...
155 4
                        throw new CircularReferenceDetectedException();
156
                    }
157 190
                    $context->startVisiting($data);
0 ignored issues
show
Bug introduced by
The method startVisiting() does not exist on JMS\Serializer\Context. It seems like you code against a sub-type of JMS\Serializer\Context such as JMS\Serializer\SerializationContext. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

157
                    $context->/** @scrutinizer ignore-call */ 
158
                              startVisiting($data);
Loading history...
158
                }
159
160
                // If we're serializing a polymorphic type, then we'll be interested in the
161
                // metadata for the actual type of the object, not the base class.
162 190
                if (class_exists($type['name'], false) || interface_exists($type['name'], false)) {
163 190
                    if (is_subclass_of($data, $type['name'], false)) {
164 4
                        $type = array('name' => \get_class($data), 'params' => array());
165
                    }
166
                }
167
168 190
                $format = $context->getFormat();
169
170
                // Trigger pre-serialization callbacks, and listeners if they exist.
171
                // Dispatch pre-serialization event before handling data to have ability change type in listener
172 190
                if ($this->dispatcher->hasListeners('serializer.pre_serialize', $type['name'], $format)) {
173 184
                    $this->dispatcher->dispatch('serializer.pre_serialize', $type['name'], $format, $event = new PreSerializeEvent($context, $data, $type));
174 184
                    $type = $event->getType();
175
                }
176
177
                // First, try whether a custom handler exists for the given type. This is done
178
                // before loading metadata because the type name might not be a class, but
179
                // could also simply be an artifical type.
180 190
                if (null !== $handler = $this->handlerRegistry->getHandler(GraphNavigatorInterface::DIRECTION_SERIALIZATION, $type['name'], $format)) {
181 47
                    $rs = \call_user_func($handler, $visitor, $data, $type, $context);
182 47
                    $context->stopVisiting($data);
0 ignored issues
show
Bug introduced by
The method stopVisiting() does not exist on JMS\Serializer\Context. It seems like you code against a sub-type of JMS\Serializer\Context such as JMS\Serializer\SerializationContext. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

182
                    $context->/** @scrutinizer ignore-call */ 
183
                              stopVisiting($data);
Loading history...
183
184 47
                    return $rs;
185
                }
186
187 168
                $exclusionStrategy = $context->getExclusionStrategy();
188
189
                /** @var $metadata ClassMetadata */
190 168
                $metadata = $this->metadataFactory->getMetadataForClass($type['name']);
191
192 166
                if ($metadata->usingExpression && $this->expressionExclusionStrategy === null) {
193 2
                    throw new ExpressionLanguageRequiredException("To use conditional exclude/expose in {$metadata->name} you must configure the expression language.");
194
                }
195
196 164
                if ($exclusionStrategy->shouldSkipClass($metadata, $context)) {
197 10
                    $context->stopVisiting($data);
198
199 10
                    throw new ExcludedClassException();
200
                }
201
202 160
                $context->pushClassMetadata($metadata);
203
204 160
                foreach ($metadata->preSerializeMethods as $method) {
205 2
                    $method->invoke($data);
206
                }
207
208 160
                $visitor->startVisitingObject($metadata, $data, $type, $context);
0 ignored issues
show
Unused Code introduced by
The call to JMS\Serializer\Serializa...::startVisitingObject() has too many arguments starting with $context. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

208
                $visitor->/** @scrutinizer ignore-call */ 
209
                          startVisitingObject($metadata, $data, $type, $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Unused Code introduced by
The call to JMS\Serializer\Deseriali...::startVisitingObject() has too many arguments starting with $context. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

208
                $visitor->/** @scrutinizer ignore-call */ 
209
                          startVisitingObject($metadata, $data, $type, $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
209 160
                foreach ($metadata->propertyMetadata as $propertyMetadata) {
210 159
                    if ($exclusionStrategy->shouldSkipProperty($propertyMetadata, $context)) {
211 16
                        continue;
212
                    }
213
214 159
                    if (null !== $this->expressionExclusionStrategy && $this->expressionExclusionStrategy->shouldSkipProperty($propertyMetadata, $context)) {
215 16
                        continue;
216
                    }
217
218 159
                    $v = $this->accessor->getValue($data, $propertyMetadata);
219
220 157
                    if (null === $v && $shouldSerializeNull !== true) {
221 22
                        continue;
222
                    }
223
224 155
                    $context->pushPropertyMetadata($propertyMetadata);
225 155
                    $visitor->visitProperty($propertyMetadata, $v);
226 154
                    $context->popPropertyMetadata();
227
                }
228
229 156
                $this->afterVisitingObject($metadata, $data, $type, $context, $format);
230
231 156
                return $visitor->endVisitingObject($metadata, $data, $type);
232
        }
233
    }
234
235 156
    private function afterVisitingObject(ClassMetadata $metadata, $object, array $type, SerializationContext $context, $format)
236
    {
237 156
        $context->stopVisiting($object);
238 156
        $context->popClassMetadata();
239
240 156
        foreach ($metadata->postSerializeMethods as $method) {
241 2
            $method->invoke($object);
242
        }
243
244 156
        if ($this->dispatcher->hasListeners('serializer.post_serialize', $metadata->name, $format)) {
245 2
            $this->dispatcher->dispatch('serializer.post_serialize', $metadata->name, $format, new ObjectEvent($context, $object, $type));
246
        }
247 156
    }
248
}
249