Skip to content

Commit

Permalink
Fix Utils::getClass() with PHP8 (#1563)
Browse files Browse the repository at this point in the history
  • Loading branch information
villfa committed Jul 14, 2021
1 parent 42384f2 commit 63e1950
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Monolog/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ public static function getClass(object $object): string
{
$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
Expand Down
19 changes: 19 additions & 0 deletions tests/Monolog/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@

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
Expand Down

0 comments on commit 63e1950

Please sign in to comment.