Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,6 @@ parameters:
count: 1
path: src/Serializer/AbstractSerializer.php

-
message: "#^Cannot cast mixed to string\\.$#"
count: 1
path: src/Serializer/AbstractSerializer.php

-
message: "#^Call to method getResponse\\(\\) on an unknown class GuzzleHttp\\\\Exception\\\\RequestException\\.$#"
count: 1
Expand Down
5 changes: 5 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
<code>SentryProfile|null</code>
</MoreSpecificReturnType>
</file>
<file src="src/Serializer/AbstractSerializer.php">
<RedundantCast occurrences="1">
<code>(string) $value</code>
</RedundantCast>
</file>
<file src="src/Serializer/RepresentationSerializer.php">
<InvalidReturnStatement occurrences="1">
<code>$value</code>
Expand Down
22 changes: 12 additions & 10 deletions src/Serializer/AbstractSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,24 +212,22 @@ protected function serializeObject($object, int $_depth = 0, array $hashes = [])
/**
* Serializes the given value to a string.
*
* @param mixed $value The value to serialize
* @param string $value The value to serialize
*/
protected function serializeString($value): string
protected function serializeString(string $value): string
{
$value = (string) $value;

// we always guarantee this is coerced, even if we can't detect encoding
if ($currentEncoding = mb_detect_encoding($value, $this->mbDetectOrder)) {
$value = mb_convert_encoding($value, 'UTF-8', $currentEncoding);
$encoded = mb_convert_encoding($value, 'UTF-8', $currentEncoding) ?: '<encoding error>';
} else {
$value = mb_convert_encoding($value, 'UTF-8');
$encoded = mb_convert_encoding($value, 'UTF-8') ?: '<encoding error>';
}

if (mb_strlen($value) > $this->options->getMaxValueLength()) {
$value = mb_substr($value, 0, $this->options->getMaxValueLength() - 10, 'UTF-8') . ' {clipped}';
if (mb_strlen($encoded) > $this->options->getMaxValueLength()) {
$encoded = mb_substr($encoded, 0, $this->options->getMaxValueLength() - 10, 'UTF-8') . ' {clipped}';
}

return $value;
return $encoded;
}

/**
Expand Down Expand Up @@ -276,7 +274,11 @@ protected function serializeValue($value)
return 'Array of length ' . \count($value);
}

return $this->serializeString($value);
if (\is_string($value) || (\is_object($value) && method_exists($value, '__toString'))) {
return $this->serializeString((string) $value);
}

return null;
}

private function formatDate(\DateTimeInterface $date): string
Expand Down