Skip to content

Commit 33a3011

Browse files
authored
Fix new phpstan errors (#1835)
1 parent 9e3662d commit 33a3011

3 files changed

Lines changed: 17 additions & 15 deletions

File tree

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,6 @@ parameters:
285285
count: 1
286286
path: src/Serializer/AbstractSerializer.php
287287

288-
-
289-
message: "#^Cannot cast mixed to string\\.$#"
290-
count: 1
291-
path: src/Serializer/AbstractSerializer.php
292-
293288
-
294289
message: "#^Call to method getResponse\\(\\) on an unknown class GuzzleHttp\\\\Exception\\\\RequestException\\.$#"
295290
count: 1

psalm-baseline.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
<code>SentryProfile|null</code>
6767
</MoreSpecificReturnType>
6868
</file>
69+
<file src="src/Serializer/AbstractSerializer.php">
70+
<RedundantCast occurrences="1">
71+
<code>(string) $value</code>
72+
</RedundantCast>
73+
</file>
6974
<file src="src/Serializer/RepresentationSerializer.php">
7075
<InvalidReturnStatement occurrences="1">
7176
<code>$value</code>

src/Serializer/AbstractSerializer.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,24 +212,22 @@ protected function serializeObject($object, int $_depth = 0, array $hashes = [])
212212
/**
213213
* Serializes the given value to a string.
214214
*
215-
* @param mixed $value The value to serialize
215+
* @param string $value The value to serialize
216216
*/
217-
protected function serializeString($value): string
217+
protected function serializeString(string $value): string
218218
{
219-
$value = (string) $value;
220-
221219
// we always guarantee this is coerced, even if we can't detect encoding
222220
if ($currentEncoding = mb_detect_encoding($value, $this->mbDetectOrder)) {
223-
$value = mb_convert_encoding($value, 'UTF-8', $currentEncoding);
221+
$encoded = mb_convert_encoding($value, 'UTF-8', $currentEncoding) ?: '<encoding error>';
224222
} else {
225-
$value = mb_convert_encoding($value, 'UTF-8');
223+
$encoded = mb_convert_encoding($value, 'UTF-8') ?: '<encoding error>';
226224
}
227225

228-
if (mb_strlen($value) > $this->options->getMaxValueLength()) {
229-
$value = mb_substr($value, 0, $this->options->getMaxValueLength() - 10, 'UTF-8') . ' {clipped}';
226+
if (mb_strlen($encoded) > $this->options->getMaxValueLength()) {
227+
$encoded = mb_substr($encoded, 0, $this->options->getMaxValueLength() - 10, 'UTF-8') . ' {clipped}';
230228
}
231229

232-
return $value;
230+
return $encoded;
233231
}
234232

235233
/**
@@ -276,7 +274,11 @@ protected function serializeValue($value)
276274
return 'Array of length ' . \count($value);
277275
}
278276

279-
return $this->serializeString($value);
277+
if (\is_string($value) || (\is_object($value) && method_exists($value, '__toString'))) {
278+
return $this->serializeString((string) $value);
279+
}
280+
281+
return null;
280282
}
281283

282284
private function formatDate(\DateTimeInterface $date): string

0 commit comments

Comments
 (0)