From 1786dcc624c78e54c7779ca8f3f6eb959bd7bb7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= Date: Tue, 4 Jan 2022 11:56:53 +0800 Subject: [PATCH] Added type hint for `Debug::getRefCount()` which only support object. (#4429) * Added type hint for `Debug::getRefCount()` which only support object. * Update CHANGELOG-2.2.md --- src/Debug.php | 5 ++++- tests/DebugTest.php | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 tests/DebugTest.php diff --git a/src/Debug.php b/src/Debug.php index 475fa2b..c50cfe7 100644 --- a/src/Debug.php +++ b/src/Debug.php @@ -13,7 +13,10 @@ class Debug { - public static function getRefCount($object): string + /** + * Get object's ref count. + */ + public static function getRefCount(object $object): string { ob_start(); debug_zval_dump($object); diff --git a/tests/DebugTest.php b/tests/DebugTest.php new file mode 100644 index 0000000..c5c9de2 --- /dev/null +++ b/tests/DebugTest.php @@ -0,0 +1,36 @@ +assertSame('1', Debug::getRefCount(new \stdClass())); + $obj = new \stdClass(); + $this->assertSame('2', Debug::getRefCount($obj)); + $obj2 = new \stdClass(); + $obj2->obj = $obj; + $this->assertSame('2', Debug::getRefCount($obj2)); + $this->assertSame('3', Debug::getRefCount($obj)); + $fun = static function () {}; + $this->assertSame('2', Debug::getRefCount($fun)); + $this->assertSame('1', Debug::getRefCount(function () {})); + } +}