From e2af5eeb51677f0b438de62bb60244b7168a0262 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20Tomulik?= <pawel@tomulik.pl>
Date: Mon, 20 Nov 2023 00:16:28 +0100
Subject: [PATCH] fix issue #5567

---
 src/Util/Exporter.php | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/src/Util/Exporter.php b/src/Util/Exporter.php
index 4cbe4141ff8..797f1418cb9 100644
--- a/src/Util/Exporter.php
+++ b/src/Util/Exporter.php
@@ -11,6 +11,7 @@
 
 use function is_array;
 use function is_scalar;
+use SebastianBergmann\RecursionContext\Context;
 
 /**
  * @internal This class is not covered by the backward compatibility promise for PHPUnit
@@ -26,7 +27,7 @@ public static function export(mixed $value, bool $exportObjects = false): string
         return '{enable export of objects to see this value}';
     }
 
-    private static function isScalarOrArrayOfScalars(mixed $value): bool
+    private static function isScalarOrArrayOfScalars(mixed &$value, Context $context = null): bool
     {
         if (is_scalar($value)) {
             return true;
@@ -36,8 +37,19 @@ private static function isScalarOrArrayOfScalars(mixed $value): bool
             return false;
         }
 
-        foreach ($value as $_value) {
-            if (!self::isScalarOrArrayOfScalars($_value)) {
+        if (!$context) {
+            $context = new Context;
+        }
+
+        if ($context->contains($value) !== false) {
+            return true;
+        }
+
+        $array = $value;
+        $context->add($value);
+
+        foreach ($array as &$_value) {
+            if (!self::isScalarOrArrayOfScalars($_value, $context)) {
                 return false;
             }
         }