Explorar el Código

Fix Utils::getClass() with PHP8 (#1563)

Fabien Villepinte hace 4 años
padre
commit
63e195046c
Se han modificado 2 ficheros con 28 adiciones y 1 borrados
  1. 9 1
      src/Monolog/Utils.php
  2. 19 0
      tests/Monolog/UtilsTest.php

+ 9 - 1
src/Monolog/Utils.php

@@ -19,7 +19,15 @@ final class Utils
     {
         $class = \get_class($object);
 
-        return 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
+        if (false === ($pos = \strpos($class, "@anonymous\0"))) {
+            return $class;
+        }
+
+        if (false === ($parent = \get_parent_class($class))) {
+            return \substr($class, 0, $pos + 10);
+        }
+
+        return $parent . '@anonymous';
     }
 
     public static function substr(string $string, int $start, ?int $length = null): string

+ 19 - 0
tests/Monolog/UtilsTest.php

@@ -13,6 +13,25 @@ namespace Monolog;
 
 class UtilsTest extends \PHPUnit_Framework_TestCase
 {
+    /**
+     * @param string $expected
+     * @param object $object
+     * @dataProvider provideObjects
+     */
+    public function testGetClass($expected, $object)
+    {
+        $this->assertSame($expected, Utils::getClass($object));
+    }
+
+    public function provideObjects()
+    {
+        return [
+            ['stdClass', new \stdClass()],
+            ['class@anonymous', new class {}],
+            ['stdClass@anonymous', new class extends \stdClass {}],
+        ];
+    }
+
     /**
      * @param string $expected
      * @param string $input