Skip to content

Commit

Permalink
feat Debugger增加对静态对象的判断
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Oct 21, 2024
1 parent 792e06c commit d122611
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/Utils/Pool/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];

/**
Expand Down Expand Up @@ -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 临时保存避免生命周期内的重复检查
Expand Down
16 changes: 16 additions & 0 deletions tests/UtilsCase/Pool/DebuggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d122611

Please sign in to comment.