Skip to content

Commit 5e3018c

Browse files
committed
Rearrange object conversion for smaller complexity
1 parent 8393a31 commit 5e3018c

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/Encoder/ObjectEncoder.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function encode($value, $depth, array $options, callable $encode)
4242

4343
/**
4444
* Encodes the object as string according to encoding options.
45-
* @param object $object Object to convert to code
45+
* @param object $object Object to encode as PHP
4646
* @param array $options List of encoder options
4747
* @param callable $encode Callback used to encode values
4848
* @return string The object encoded as string
@@ -57,6 +57,22 @@ private function encodeObject($object, array $options, callable $encode)
5757
return sprintf('\\%s::__set_state(%s)', get_class($object), $encode($this->getObjectState($object)));
5858
}
5959

60+
return $this->encodeObjectArray($object, $options, $encode);
61+
}
62+
63+
/**
64+
* Encodes the object into one of the array formats.
65+
* @param object $object Object to encode as PHP
66+
* @param array $options List of encoder options
67+
* @param callable $encode Callback used to encode values
68+
* @return string The object encoded as string
69+
*/
70+
private function encodeObjectArray($object, array $options, callable $encode)
71+
{
72+
if (!in_array($options['object.format'], ['array', 'vars', 'iterate'])) {
73+
throw new \RuntimeException('Invalid object encoding format: ' . $options['object.format']);
74+
}
75+
6076
$output = $encode($this->getObjectArray($object, $options['object.format']));
6177

6278
if ($options['object.cast']) {
@@ -79,15 +95,15 @@ private function getObjectArray($object, $format)
7995
return (array) $object;
8096
} elseif ($format === 'vars') {
8197
return get_object_vars($object);
82-
} elseif ($format === 'iterate') {
83-
$array = [];
84-
foreach ($object as $key => $value) {
85-
$array[$key] = $value;
86-
}
87-
return $array;
8898
}
8999

90-
throw new \RuntimeException('Invalid object encoding format: ' . $format);
100+
$array = [];
101+
102+
foreach ($object as $key => $value) {
103+
$array[$key] = $value;
104+
}
105+
106+
return $array;
91107
}
92108

93109
/**

tests/tests/EncodingTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,20 @@ public function testIgnoredArrayRecursion()
190190
);
191191
}
192192

193+
public function testMaxDeathOnNoRecursionDetection()
194+
{
195+
$foo = [1];
196+
$foo[1] = & $foo;
197+
198+
$encoder = new PHPEncoder([
199+
'recursion.detect' => false,
200+
'recursion.max' => 5
201+
]);
202+
203+
$this->setExpectedException('RuntimeException');
204+
$encoder->encode($foo);
205+
}
206+
193207
private function assertEncode($value, $string, PHPEncoder $encoder, $initial = null)
194208
{
195209
$output = $encoder->encode(func_num_args() < 4 ? $value : $initial);

0 commit comments

Comments
 (0)