Completed
Pull Request — master (#1148)
by Alexander
12:41
created

JsonSerializationVisitor::visitStaticProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer;
6
7
use JMS\Serializer\Exception\NotAcceptableException;
8
use JMS\Serializer\Exception\RuntimeException;
9
use JMS\Serializer\Metadata\ClassMetadata;
10
use JMS\Serializer\Metadata\PropertyMetadata;
11
use JMS\Serializer\Metadata\StaticPropertyMetadata;
12
use JMS\Serializer\Visitor\SerializationVisitorInterface;
13
14
final class JsonSerializationVisitor extends AbstractVisitor implements SerializationVisitorInterface
15
{
16
    /**
17
     * @var int
18
     */
19
    private $options = JSON_PRESERVE_ZERO_FRACTION;
20
21
    /**
22
     * @var array
23 180
     */
24
    private $dataStack = [];
25
    /**
26 180
     * @var \ArrayObject
27 180
     */
28 180
    private $data;
29
30 14
    public function __construct(
31
        int $options = JSON_PRESERVE_ZERO_FRACTION
32 14
    ) {
33
        $this->dataStack = [];
34
        $this->options = $options;
35 97
    }
36
37 97
    /**
38
     * {@inheritdoc}
39
     */
40 9
    public function visitNull($data, array $type)
41
    {
42 9
        return null;
43
    }
44
45 30
    /**
46
     * {@inheritdoc}
47 30
     */
48
    public function visitString(string $data, array $type)
49
    {
50 12
        return $data;
51
    }
52 12
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function visitBoolean(bool $data, array $type)
57
    {
58
        return $data;
59
    }
60 81
61
    /**
62 81
     * {@inheritdoc}
63
     */
64 81
    public function visitInteger(int $data, array $type)
65
    {
66 81
        return $data;
67
    }
68 81
69 81
    /**
70
     * {@inheritdoc}
71
     */
72 73
    public function visitDouble(float $data, array $type)
73 6
    {
74 6
        return $data;
75
    }
76
77 70
    /**
78 19
     * @param array $data
79
     * @param array $type
80 70
     *
81
     * @return array|\ArrayObject
82
     */
83
    public function visitArray(array $data, array $type)
84 81
    {
85 81
        \array_push($this->dataStack, $data);
86
87
        $rs = isset($type['params'][1]) ? new \ArrayObject() : [];
88 97
89
        $isList = isset($type['params'][0]) && !isset($type['params'][1]);
90 97
91 97
        $elType = $this->getElementType($type);
92 97
        foreach ($data as $k => $v) {
93
            try {
94 96
                $v = $this->navigator->accept($v, $elType);
95
            } catch (NotAcceptableException $e) {
96 96
                continue;
97 96
            }
98
99 96
            if ($isList) {
100 12
                $rs[] = $v;
101
            } else {
102 89
                $rs[$k] = $v;
103
            }
104
        }
105 90
106
        \array_pop($this->dataStack);
107
        return $rs;
108 90
    }
109 4
110 4
    public function startVisitingObject(ClassMetadata $metadata, object $data, array $type): void
111
    {
112
        \array_push($this->dataStack, $this->data);
113 90
        $this->data = true === $metadata->isMap ? new \ArrayObject() : [];
0 ignored issues
show
Documentation Bug introduced by
It seems like true === $metadata->isMa...ArrayObject() : array() can also be of type array. However, the property $data is declared as type ArrayObject. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
114 2
    }
115
116
    /**
117 89
     * @return array|\ArrayObject
118 9
     */
119
    public function endVisitingObject(ClassMetadata $metadata, object $data, array $type)
120
    {
121 9
        $rs = $this->data;
122 9
        $this->data = \array_pop($this->dataStack);
123
124
        if (true !== $metadata->isList && empty($rs)) {
125
            return new \ArrayObject();
126 82
        }
127
128 89
        return $rs;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function visitProperty(PropertyMetadata $metadata, $v): void
135
    {
136
        try {
137 1
            $v = $this->navigator->accept($v, $metadata->type);
138
        } catch (NotAcceptableException $e) {
139 1
            return;
140
        }
141
142
        if (true === $metadata->skipWhenEmpty && ($v instanceof \ArrayObject || \is_array($v)) && 0 === count($v)) {
143
            return;
144
        }
145
146
        if ($metadata->inline) {
147
            if (\is_array($v) || ($v instanceof \ArrayObject)) {
148
                // concatenate the two array-like structures
149
                // is there anything faster?
150
                foreach ($v as $key => $value) {
151 2
                    $this->data[$key] = $value;
152
                }
153 2
            }
154 2
        } else {
155
            $this->data[$metadata->serializedName] = $v;
156 150
        }
157
    }
158 150
159
    public function visitStaticProperty(string $name, $v): void
160 150
    {
161 150
        $this->visitProperty(new StaticPropertyMetadata('', $name, $v), $v);
162 148
    }
163
164 2
    /**
165 2
     * @deprecated Will be removed in 3.0
166
     *
167
     * Checks if some data key exists.
168
     */
169
    public function hasData(string $key): bool
170
    {
171
        return isset($this->data[$key]);
172
    }
173
174
    /**
175
     * @deprecated Use `::visitProperty(new StaticPropertyMetadata('', 'name', 'value'), 'value')` instead
176
     *
177
     * Allows you to replace existing data on the current object element.
178
     *
179
     * @param mixed $value This value must either be a regular scalar, or an array.
180
     *                                                       It must not contain any objects anymore.
181
     */
182
    public function setData(string $key, $value): void
183
    {
184
        $this->data[$key] = $value;
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    public function getResult($data)
191
    {
192
        $result = @json_encode($data, $this->options);
193
194
        switch (json_last_error()) {
195
            case JSON_ERROR_NONE:
196
                return $result;
197
198
            case JSON_ERROR_UTF8:
199
                throw new RuntimeException('Your data could not be encoded because it contains invalid UTF8 characters.');
200
201
            default:
202
                throw new RuntimeException(sprintf('An error occurred while encoding your data (error code %d).', json_last_error()));
203
        }
204
    }
205
}
206