Skip to content

Commit acdd6c4

Browse files
committed
Restore BC by wrapping the to string in a try catch block
1 parent 8205de9 commit acdd6c4

1 file changed

Lines changed: 44 additions & 40 deletions

File tree

src/Twig/EasyAdminTwigExtension.php

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -187,63 +187,67 @@ public function fileSize(int $bytes): string
187187

188188
public function representAsString(mixed $value, string|callable|null $toStringMethod = null): string
189189
{
190-
if (null !== $toStringMethod) {
191-
if (\is_callable($toStringMethod)) {
192-
return $toStringMethod($value, $this->translator);
190+
try {
191+
if (null !== $toStringMethod) {
192+
if (\is_callable($toStringMethod)) {
193+
return $toStringMethod($value, $this->translator);
194+
}
195+
196+
$callable = [$value, $toStringMethod];
197+
if (!\is_callable($callable) || !method_exists($value, $toStringMethod)) {
198+
throw new \RuntimeException(sprintf('The method "%s()" does not exist or is not callable in the value of type "%s"', $toStringMethod, \is_object($value) ? $value::class : \gettype($value)));
199+
}
200+
201+
return \call_user_func($callable);
193202
}
194203

195-
$callable = [$value, $toStringMethod];
196-
if (!\is_callable($callable) || !method_exists($value, $toStringMethod)) {
197-
throw new \RuntimeException(sprintf('The method "%s()" does not exist or is not callable in the value of type "%s"', $toStringMethod, \is_object($value) ? $value::class : \gettype($value)));
204+
if (null === $value) {
205+
return '';
198206
}
199207

200-
return \call_user_func($callable);
201-
}
202-
203-
if (null === $value) {
204-
return '';
205-
}
208+
if (\is_string($value)) {
209+
return $value;
210+
}
206211

207-
if (\is_string($value)) {
208-
return $value;
209-
}
212+
if (is_numeric($value)) {
213+
return (string) $value;
214+
}
210215

211-
if (is_numeric($value)) {
212-
return (string) $value;
213-
}
216+
if (\is_bool($value)) {
217+
return $value ? 'true' : 'false';
218+
}
214219

215-
if (\is_bool($value)) {
216-
return $value ? 'true' : 'false';
217-
}
220+
if (\is_array($value)) {
221+
return sprintf('Array (%d items)', \count($value));
222+
}
218223

219-
if (\is_array($value)) {
220-
return sprintf('Array (%d items)', \count($value));
221-
}
224+
if (\is_object($value)) {
225+
if ($value instanceof TranslatableInterface) {
226+
return $value->trans($this->translator);
227+
}
222228

223-
if (\is_object($value)) {
224-
if ($value instanceof TranslatableInterface) {
225-
return $value->trans($this->translator);
226-
}
229+
if ($value instanceof \Stringable) {
230+
return (string) $value;
231+
}
227232

228-
if ($value instanceof \Stringable) {
229-
return (string) $value;
230-
}
233+
if (method_exists($value, 'getId')) {
234+
return sprintf(
235+
'%s #%s',
236+
// remove null bytes from class name (this happens in anonymous classes)
237+
str_replace("\0", '', $value::class),
238+
$value->getId()
239+
);
240+
}
231241

232-
if (method_exists($value, 'getId')) {
233242
return sprintf(
234243
'%s #%s',
235244
// remove null bytes from class name (this happens in anonymous classes)
236245
str_replace("\0", '', $value::class),
237-
$value->getId()
246+
hash('xxh32', (string) spl_object_id($value))
238247
);
239248
}
240-
241-
return sprintf(
242-
'%s #%s',
243-
// remove null bytes from class name (this happens in anonymous classes)
244-
str_replace("\0", '', $value::class),
245-
hash('xxh32', (string) spl_object_id($value))
246-
);
249+
} catch (\Throwable) {
250+
return '';
247251
}
248252

249253
return '';

0 commit comments

Comments
 (0)