Skip to content

Commit

Permalink
Merge branch '5.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Apr 4, 2024
2 parents 7d536b2 + c409c34 commit b9c7cad
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 32 deletions.
8 changes: 1 addition & 7 deletions .psalm/baseline.xml
@@ -1,8 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.23.1@8471a896ccea3526b26d082f4461eeea467f10a4">
<file src="src/Exporter.php">
<InvalidArgument>
<code><![CDATA[$value]]></code>
</InvalidArgument>
</file>
</files>
<files psalm-version="5.23.1@8471a896ccea3526b26d082f4461eeea467f10a4"/>
4 changes: 3 additions & 1 deletion infection.json5.dist
Expand Up @@ -6,7 +6,9 @@
]
},
"mutators": {
"@default": true
"@default": true,
"FunctionCallRemoval": false,
"MethodCallRemoval": false
},
"minMsi": 100,
"minCoveredMsi": 100
Expand Down
27 changes: 11 additions & 16 deletions src/Exporter.php
Expand Up @@ -17,6 +17,7 @@
use function ini_get;
use function ini_set;
use function is_array;
use function is_bool;
use function is_float;
use function is_object;
use function is_resource;
Expand Down Expand Up @@ -172,7 +173,7 @@ public function toArray(mixed $value): array
// private $propertyName => "\0ClassName\0propertyName"
// protected $propertyName => "\0*\0propertyName"
// public $propertyName => "propertyName"
if (preg_match('/^\0.+\0(.+)$/', (string) $key, $matches)) {
if (preg_match('/\0.+\0(.+)/', (string) $key, $matches)) {
$key = $matches[1];
}

Expand Down Expand Up @@ -207,12 +208,8 @@ private function recursiveExport(mixed &$value, int $indentation = 0, ?Recursion
return 'null';
}

if ($value === true) {
return 'true';
}

if ($value === false) {
return 'false';
if (is_bool($value)) {
return $value ? 'true' : 'false';
}

if (is_float($value)) {
Expand All @@ -226,7 +223,7 @@ private function recursiveExport(mixed &$value, int $indentation = 0, ?Recursion
if (is_resource($value)) {
return sprintf(
'resource(%d) of type (%s)',
$value,
(int) $value,
get_resource_type($value),
);
}
Expand Down Expand Up @@ -275,17 +272,15 @@ private function exportFloat(float $value): string

ini_set('precision', '-1');

try {
$valueStr = (string) $value;
$valueAsString = (string) $value;

if ((string) (int) $value === $valueStr) {
return $valueStr . '.0';
}
ini_set('precision', $precisionBackup);

return $valueStr;
} finally {
ini_set('precision', $precisionBackup);
if ((string) (int) $value === $valueAsString) {
return $valueAsString . '.0';
}

return $valueAsString;
}

private function exportString(string $value): string
Expand Down
21 changes: 13 additions & 8 deletions tests/ExporterTest.php
Expand Up @@ -20,6 +20,7 @@
use function mb_language;
use function preg_replace;
use function range;
use function str_repeat;
use Error;
use Exception;
use PHPUnit\Framework\Attributes\CoversClass;
Expand Down Expand Up @@ -296,14 +297,18 @@ public static function shortenedExportProvider(): array
'float 1 - 2 / 3' => [1 - 2 / 3, '0.33333333333333337'],
'numeric string' => ['1', "'1'"],
// \n\r and \r is converted to \n
'multilinestring' => ["this\nis\na\nvery\nvery\nvery\nvery\nvery\nvery\rlong\n\rtext", "'this\\nis\\na\\nvery\\nvery\\nvery...\\rtext'"],
'empty stdClass' => [new stdClass, 'stdClass Object ()'],
'not empty stdClass' => [$obj, 'stdClass Object (...)'],
'empty array' => [[], '[]'],
'not empty array' => [$array, '[...]'],
'enum' => [ExampleEnum::Value, 'SebastianBergmann\Exporter\ExampleEnum Enum (Value)'],
'backed enum (string)' => [ExampleStringBackedEnum::Value, 'SebastianBergmann\Exporter\ExampleStringBackedEnum Enum (Value, \'value\')'],
'backen enum (integer)' => [ExampleIntegerBackedEnum::Value, 'SebastianBergmann\Exporter\ExampleIntegerBackedEnum Enum (Value, 0)'],
'38 single-byte characters' => [str_repeat('A', 38), '\'' . str_repeat('A', 38) . '\''],
'39 single-byte characters' => [str_repeat('A', 39), '\'' . str_repeat('A', 29) . '...' . str_repeat('A', 6) . '\''],
'38 multi-byte characters' => [str_repeat('🧪', 38), '\'' . str_repeat('🧪', 38) . '\''],
'39 multi-byte characters' => [str_repeat('🧪', 39), '\'' . str_repeat('🧪', 29) . '...' . str_repeat('🧪', 6) . '\''],
'multi-line string' => ["this\nis\na\nvery\nvery\nvery\nvery\nvery\nvery\rlong\n\rtext", "'this\\nis\\na\\nvery\\nvery\\nvery...\\rtext'"],
'empty stdClass' => [new stdClass, 'stdClass Object ()'],
'not empty stdClass' => [$obj, 'stdClass Object (...)'],
'empty array' => [[], '[]'],
'not empty array' => [$array, '[...]'],
'enum' => [ExampleEnum::Value, 'SebastianBergmann\Exporter\ExampleEnum Enum (Value)'],
'backed enum (string)' => [ExampleStringBackedEnum::Value, 'SebastianBergmann\Exporter\ExampleStringBackedEnum Enum (Value, \'value\')'],
'backen enum (integer)' => [ExampleIntegerBackedEnum::Value, 'SebastianBergmann\Exporter\ExampleIntegerBackedEnum Enum (Value, 0)'],
];
}

Expand Down

0 comments on commit b9c7cad

Please sign in to comment.