Skip to content

Commit

Permalink
cs-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Oct 19, 2024
1 parent 2e4d43e commit 792e06c
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 64 deletions.
9 changes: 2 additions & 7 deletions src/Events/SwooleEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function add($fd, $flag, $func, $args = [])
// @codeCoverageIgnoreEnd
}
});
$res = is_int($res) ? (string)$res : false;
$res = is_int($res) ? (string) $res : false;
}

if ($res === false) {
Expand All @@ -95,7 +95,6 @@ public function add($fd, $flag, $func, $args = [])
return $timerId;
case EventInterface::EV_READ:
if (!\is_resource($fd)) {

return false;
}

Expand All @@ -104,7 +103,6 @@ public function add($fd, $flag, $func, $args = [])
: Event::add($fd, $func, null, SWOOLE_EVENT_READ);
case EventInterface::EV_WRITE:
if (!\is_resource($fd)) {

return false;
}

Expand Down Expand Up @@ -148,23 +146,20 @@ public function del($fd, $flag)
return false;
case EventInterface::EV_READ:
if (!\is_resource($fd)) {

return false;
}

if (!Event::isset($fd, SWOOLE_EVENT_WRITE)) {

return Event::del($fd);
}

return Event::set($fd, null, null, SWOOLE_EVENT_WRITE);
case EventInterface::EV_WRITE:
if (!\is_resource($fd)) {

return false;
}

if (!Event::isset($fd, SWOOLE_EVENT_READ)) {

return Event::del($fd);
}

Expand Down
44 changes: 26 additions & 18 deletions src/Utils/Pool/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

class Debugger
{
const ERROR_TYPE_NON = 1;
const ERROR_TYPE_NORMAL = 0;
const ERROR_TYPE_STATIC_ARRAY = -1;
const ERROR_TYPE_RESOURCE = -2;
public const ERROR_TYPE_NON = 1;
public const ERROR_TYPE_NORMAL = 0;
public const ERROR_TYPE_STATIC_ARRAY = -1;
public const ERROR_TYPE_RESOURCE = -2;

protected static array $_errorMap = [
self::ERROR_TYPE_STATIC_ARRAY => 'static array',
self::ERROR_TYPE_RESOURCE => 'resource',
self::ERROR_TYPE_NORMAL => 'normal'
self::ERROR_TYPE_NORMAL => 'normal',
];

/**
Expand Down Expand Up @@ -71,6 +71,7 @@ public static function validate(mixed $value): bool
{
$debugger = new static();
$res = $debugger->cloneValidate($value);

return $res->getReturn();
}

Expand All @@ -89,8 +90,9 @@ public function cloneValidate(mixed $value, int $level = 0): Generator
foreach ($value as $v) {
yield from $this->cloneValidate($v, $level - 1);
}

return true;
// 对象递归检查
// 对象递归检查
case 'object':
// 是否在调试容器中出现过
if (!static::$_seen->offsetExists($value)) {
Expand All @@ -110,18 +112,20 @@ public function cloneValidate(mixed $value, int $level = 0): Generator
// 静态数组不可控,所以返回异常
case 'array':
throw new PoolDebuggerException(
"Value can not be cloned [static array]. ", static::ERROR_TYPE_STATIC_ARRAY
'Value can not be cloned [static array]. ',
static::ERROR_TYPE_STATIC_ARRAY
);
// 资源不可拷贝,所以返回异常
// 资源不可拷贝,所以返回异常
case 'resource':
// weak map 临时保存避免生命周期内的重复检查
static::$_seen->offsetSet($value, static::ERROR_TYPE_RESOURCE);
throw new PoolDebuggerException(
"Value can not be cloned [resource]. ", static::ERROR_TYPE_RESOURCE
'Value can not be cloned [resource]. ',
static::ERROR_TYPE_RESOURCE
);
// 其他类型
// 使用生成器递归检查,避免内存溢出
// 使用throw=false忽略标量数据的抛出
// 其他类型
// 使用生成器递归检查,避免内存溢出
// 使用throw=false忽略标量数据的抛出
default:
yield from $this->cloneValidate($v, $level - 1);
break;
Expand All @@ -133,7 +137,8 @@ public function cloneValidate(mixed $value, int $level = 0): Generator
if (is_resource($v)) {
static::$_seen->offsetSet($value, static::ERROR_TYPE_RESOURCE);
throw new PoolDebuggerException(
"Value can not be cloned [resource]. ", static::ERROR_TYPE_RESOURCE
'Value can not be cloned [resource]. ',
static::ERROR_TYPE_RESOURCE
);
}
// 其他类型,其中非静态数组是安全的安全
Expand All @@ -146,6 +151,7 @@ public function cloneValidate(mixed $value, int $level = 0): Generator
}
// 不存在非法值
static::$_seen->offsetSet($value, static::ERROR_TYPE_NON);

return true;
}
// 如果生命周期内存在检查通过的则返回true
Expand All @@ -154,21 +160,23 @@ public function cloneValidate(mixed $value, int $level = 0): Generator
}
$info = static::$_errorMap[$errorType];
throw new PoolDebuggerException("Value can not be cloned [$info]. ", $errorType);
// 资源不可拷贝,返回异常
// 资源不可拷贝,返回异常
case 'resource':
throw new PoolDebuggerException(
"Value can not be cloned [resource]. ", static::ERROR_TYPE_RESOURCE
'Value can not be cloned [resource]. ',
static::ERROR_TYPE_RESOURCE
);
// 其他
// 其他
default:
// 允许内层
if ($level < 0) {
return true;
}
// 不允许外层
throw new PoolDebuggerException(
"Value can not be cloned [$type]. ", static::ERROR_TYPE_NORMAL
"Value can not be cloned [$type]. ",
static::ERROR_TYPE_NORMAL
);
}
}
}
}
5 changes: 4 additions & 1 deletion src/Utils/Pool/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

use WeakReference;
use Workbunny\WebmanCoroutine\Exceptions\PoolException;

use Workbunny\WebmanCoroutine\Exceptions\TimeoutException;

use function Workbunny\WebmanCoroutine\wait_for;

use Workerman\Worker;
Expand Down Expand Up @@ -86,6 +86,7 @@ public static function create(string $name, int $count, mixed $element, bool $cl
foreach (range(1, $count) as $i) {
static::append($name, $i, $element);
}

return self::$pools[$name];
}

Expand Down Expand Up @@ -208,6 +209,7 @@ public static function getIdle(string $name, int $timeout = -1): Pool
return $pool !== null;
}, $timeout);
$pool->setIdle(false);

return $pool;
}

Expand All @@ -224,6 +226,7 @@ public static function waitForIdle(string $name, \Closure $closure, int $timeout
{
try {
$pool = static::getIdle($name, $timeout);

return call_user_func($closure, $pool);
} finally {
if (isset($pool)) {
Expand Down
3 changes: 0 additions & 3 deletions src/Utils/Worker/AbstractWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

namespace Workbunny\WebmanCoroutine\Utils\Worker;

use Workbunny\WebmanCoroutine\Exceptions\WorkerException;
use Workbunny\WebmanCoroutine\Factory;
use Workbunny\WebmanCoroutine\Handlers\HandlerInterface;
use Workerman\Worker;

abstract class AbstractWorker extends Worker
Expand Down
5 changes: 1 addition & 4 deletions tests/EventsCase/SwooleEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ public function testAddTimer()
echo 'Timer triggered';
});
$this->assertEquals(1, $result);


}

public function testAddOnceTimer()
Expand Down Expand Up @@ -268,7 +266,6 @@ public function testAddAndDelRead()
// Event::set
$eventMock->shouldReceive('isset')->andReturn(false);


$result = $swooleEvent->del($stream2, EventInterface::EV_READ);
$this->assertTrue($result);
// false
Expand Down Expand Up @@ -371,7 +368,7 @@ public function testDestroy()
$this->assertTrue(is_int($id));
});
$eventMock->shouldReceive('listCoroutines')->andReturn([
1, 2
1, 2,
]);

$swooleEvent->destroy();
Expand Down
Loading

0 comments on commit 792e06c

Please sign in to comment.