Skip to content

Commit 8f014c5

Browse files
committed
Rearrange code for less complexity
1 parent f7b3469 commit 8f014c5

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

src/Encoder/ArrayEncoder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function encode($value, $depth, array $options, callable $encode)
5555
* @param callable $encode Callback used to encode values
5656
* @return string The PHP code representation for the array
5757
*/
58-
private function getFormattedArray(array $array, $depth, $options, $encode)
58+
private function getFormattedArray(array $array, $depth, array $options, callable $encode)
5959
{
6060
$lines = $this->getPairs($array, ' ', $options['array.omit'], $encode, $omitted);
6161

@@ -82,11 +82,11 @@ private function getInlineArray(array $lines, array $options)
8282

8383
if (preg_match('/[\r\n\t]/', $output)) {
8484
return false;
85-
} elseif ($options['array.inline'] !== true && strlen($output) > (int) $options['array.inline']) {
86-
return false;
85+
} elseif ($options['array.inline'] === true || strlen($output) <= (int) $options['array.inline']) {
86+
return $output;
8787
}
8888

89-
return $output;
89+
return false;
9090
}
9191

9292
/**

src/Encoder/FloatEncoder.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,32 @@ public function encode($value, $depth, array $options, callable $encode)
3232
return 'NAN';
3333
} elseif (is_infinite($value)) {
3434
return $value < 0 ? '-INF' : 'INF';
35-
} elseif ($options['float.integers'] && round($value) === $value) {
36-
return number_format($value, 0, '.', '');
3735
}
3836

39-
return $this->enforceType($this->getFloatString($value, $options['float.precision']));
37+
return $this->getFloat($value, $options['float.precision'], $options['float.integers']);
4038
}
4139

4240
/**
4341
* Converts the float value into string representation.
4442
* @param float $float Value to convert
45-
* @param integer|false $precision Number of decimals in the number
43+
* @param integer|false $precision Number of decimals in the number or false for default
44+
* @param boolean $useIntegers Whether to represent integer values as integers or not
4645
* @return string The given float value as a string
4746
*/
48-
private function getFloatString($float, $precision)
47+
private function getFloat($float, $precision, $useIntegers)
4948
{
50-
if ($precision === false) {
51-
return (string) $float;
49+
if ($useIntegers && round($float) === $float) {
50+
return number_format($float, 0, '.', '');
51+
} elseif ($precision === false) {
52+
$output = (string) $float;
53+
} else {
54+
$original = ini_get('precision');
55+
ini_set('precision', (int) $precision);
56+
$output = (string) $float;
57+
ini_set('precision', $original);
5258
}
5359

54-
$current = ini_get('precision');
55-
ini_set('precision', (int) $precision);
56-
$output = (string) $float;
57-
ini_set('precision', $current);
58-
59-
return $output;
60+
return $this->enforceType($output);
6061
}
6162

6263
/**

0 commit comments

Comments
 (0)