Skip to content

Commit 6362375

Browse files
committed
Extract method to reduce complexity
1 parent 8f014c5 commit 6362375

File tree

1 file changed

+44
-23
lines changed

1 file changed

+44
-23
lines changed

src/PHPEncoder.php

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,25 @@ class PHPEncoder
4444
public function __construct(array $options = [], array $encoders = null)
4545
{
4646
$this->options = self::$defaultOptions;
47-
$this->encoders = $encoders !== null ? [] : [
48-
new Encoder\NullEncoder(),
49-
new Encoder\BooleanEncoder(),
50-
new Encoder\IntegerEncoder(),
51-
new Encoder\FloatEncoder(),
52-
new Encoder\StringEncoder(),
53-
new Encoder\ArrayEncoder(),
54-
new Encoder\GMPEncoder(),
55-
new Encoder\ObjectEncoder(),
56-
];
57-
58-
foreach ($encoders ?: [] as $encoder) {
59-
$this->addEncoder($encoder);
47+
$this->encoders = [];
48+
49+
if ($encoders === null) {
50+
$this->encoders = [
51+
new Encoder\NullEncoder(),
52+
new Encoder\BooleanEncoder(),
53+
new Encoder\IntegerEncoder(),
54+
new Encoder\FloatEncoder(),
55+
new Encoder\StringEncoder(),
56+
new Encoder\ArrayEncoder(),
57+
new Encoder\GMPEncoder(),
58+
new Encoder\ObjectEncoder(),
59+
];
60+
} else {
61+
foreach ($encoders as $encoder) {
62+
$this->addEncoder($encoder);
63+
}
6064
}
65+
6166
foreach ($options as $option => $value) {
6267
$this->setOption($option, $value);
6368
}
@@ -160,17 +165,9 @@ public function getAllOptions(array $overrides = [])
160165
* @return string The PHP code that represents the given value
161166
* @throws \RuntimeException On recursion or upon reaching maximum depth
162167
*/
163-
private function generate($value, $depth, array $options, $recursion = [])
168+
private function generate($value, $depth, array $options, array $recursion = [])
164169
{
165-
if ($options['recursion.detect']) {
166-
if (array_search($value, $recursion, true) !== false) {
167-
if ($options['recursion.ignore']) {
168-
$value = null;
169-
} else {
170-
throw new \RuntimeException('Recursion detected');
171-
}
172-
}
173-
170+
if ($this->detectRecursion($value, $options, $recursion)) {
174171
$recursion[] = $value;
175172
}
176173

@@ -185,6 +182,30 @@ private function generate($value, $depth, array $options, $recursion = [])
185182
return $this->encodeValue($value, $depth, $options, $callback);
186183
}
187184

185+
/**
186+
* Attempts to detect circular references in values.
187+
* @param mixed $value Value to try for circular reference
188+
* @param array $options List of encoder options
189+
* @param array $recursion Upper values in the encoding tree
190+
* @return boolean True if values should be recorded, false if not
191+
*/
192+
private function detectRecursion(& $value, array $options, array $recursion)
193+
{
194+
if ($options['recursion.detect']) {
195+
if (array_search($value, $recursion, true) !== false) {
196+
if ($options['recursion.ignore']) {
197+
$value = null;
198+
} else {
199+
throw new \RuntimeException('Recursion detected');
200+
}
201+
}
202+
203+
return true;
204+
}
205+
206+
return false;
207+
}
208+
188209
/**
189210
* Encodes the value using one of the encoders that supports the value type.
190211
* @param mixed $value Value to encode

0 commit comments

Comments
 (0)