From d12261121c8e196c158f5f8769743c571e6741a2 Mon Sep 17 00:00:00 2001 From: chaz6chez Date: Mon, 21 Oct 2024 10:46:19 +0800 Subject: [PATCH] =?UTF-8?q?feat=20Debugger=E5=A2=9E=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=E9=9D=99=E6=80=81=E5=AF=B9=E8=B1=A1=E7=9A=84=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Utils/Pool/Debugger.php | 26 +++++++++++++++++++------- tests/UtilsCase/Pool/DebuggerTest.php | 16 ++++++++++++++++ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/Utils/Pool/Debugger.php b/src/Utils/Pool/Debugger.php index c0215ca..088c2fe 100644 --- a/src/Utils/Pool/Debugger.php +++ b/src/Utils/Pool/Debugger.php @@ -13,15 +13,17 @@ class Debugger { - public const ERROR_TYPE_NON = 1; - public const ERROR_TYPE_NORMAL = 0; - public const ERROR_TYPE_STATIC_ARRAY = -1; + public const ERROR_TYPE_NON = 0; + public const ERROR_TYPE_NORMAL = -1; public const ERROR_TYPE_RESOURCE = -2; + public const ERROR_TYPE_STATIC_ARRAY = -101; + public const ERROR_TYPE_STATIC_OBJECT = -102; protected static array $_errorMap = [ - self::ERROR_TYPE_STATIC_ARRAY => 'static array', - self::ERROR_TYPE_RESOURCE => 'resource', - self::ERROR_TYPE_NORMAL => 'normal', + self::ERROR_TYPE_STATIC_OBJECT => 'static object', + self::ERROR_TYPE_STATIC_ARRAY => 'static array', + self::ERROR_TYPE_RESOURCE => 'resource', + self::ERROR_TYPE_NORMAL => 'normal', ]; /** @@ -108,13 +110,23 @@ public function cloneValidate(mixed $value, int $level = 0): Generator } // 静态属性 if ($property->isStatic()) { - switch ($type = gettype($v)) { + switch (gettype($v)) { // 静态数组不可控,所以返回异常 case 'array': + // weak map 临时保存避免生命周期内的重复检查 + static::$_seen->offsetSet($value, static::ERROR_TYPE_STATIC_ARRAY); throw new PoolDebuggerException( 'Value can not be cloned [static array]. ', static::ERROR_TYPE_STATIC_ARRAY ); + // 静态对象不可控,所以返回异常 + case 'object': + // weak map 临时保存避免生命周期内的重复检查 + static::$_seen->offsetSet($value, static::ERROR_TYPE_STATIC_OBJECT); + throw new PoolDebuggerException( + 'Value can not be cloned [static object]. ', + static::ERROR_TYPE_STATIC_OBJECT + ); // 资源不可拷贝,所以返回异常 case 'resource': // weak map 临时保存避免生命周期内的重复检查 diff --git a/tests/UtilsCase/Pool/DebuggerTest.php b/tests/UtilsCase/Pool/DebuggerTest.php index a29c1ca..c8144c1 100644 --- a/tests/UtilsCase/Pool/DebuggerTest.php +++ b/tests/UtilsCase/Pool/DebuggerTest.php @@ -334,6 +334,22 @@ public function testCloneValidateWithObjectStaticPropertyException() $this->assertEquals('Value can not be cloned [static array]. ', $e->getMessage()); } + // object + $object = new class () { + public static $object = null; + + public function __construct() + { + self::$object = new stdClass(); + } + }; + try { + Debugger::validate($object); + } catch (PoolDebuggerException $e) { + $this->assertEquals(Debugger::ERROR_TYPE_STATIC_OBJECT, $e->getCode()); + $this->assertEquals('Value can not be cloned [static object]. ', $e->getMessage()); + } + // resource $object = new class () { public static $resource = null;