Skip to content

Commit e7b6352

Browse files
committed
refactor(core): 优化exists函数实现和代码结构
- 将逻辑运算符从 'or' 替换为 '||' 以提高可读性 - 重新排列ClosureBox类成员变量顺序 - 添加基本类型的exists函数重载(Int、Float、Bool) - 实现Variant类型的exists函数并处理null和undef情况 - 添加单元测试验证exists函数的基本功能
1 parent 7e54f15 commit e7b6352

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

include/phpx_helper.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,22 @@ static inline Bool empty(const Variant &v) {
9595
return !v.toBool();
9696
}
9797

98+
static inline bool exists(Int v) {
99+
return true;
100+
}
101+
102+
static inline bool exists(Float v) {
103+
return true;
104+
}
105+
106+
static inline bool exists(Bool v) {
107+
return true;
108+
}
109+
110+
static inline Bool exists(const Variant &v) {
111+
return !(v.isNull() || v.isUndef());
112+
}
113+
98114
static inline void move(Int v, zval *retval) {
99115
ZVAL_LONG(retval, v);
100116
}

src/core/base.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ bool empty(const Variant &v, const std::initializer_list<std::pair<Operation, co
523523

524524
bool exists(const Variant &v, const std::initializer_list<std::pair<Operation, const Variant>> &list) {
525525
Variant tmp = v;
526-
if (tmp.isNull() or tmp.isUndef()) {
526+
if (tmp.isNull() || tmp.isUndef()) {
527527
return false;
528528
}
529529

src/core/closure.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class ClosureBox : public Box {
2828
public:
2929
Object this_;
3030
ClosureFn fn_;
31-
zend_function *zf_;
3231
Array vars_;
32+
zend_function *zf_;
3333

3434
ClosureBox(const ClosureFn &fn, const Object &_this, const Array &vars, zend_function *zf) {
3535
fn_ = fn;

tests/src/exists.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44

55
using namespace php;
66

7+
TEST(exists, basic) {
8+
ASSERT_TRUE(exists(100L));
9+
ASSERT_TRUE(exists(100.1002));
10+
ASSERT_TRUE(exists(true));
11+
ASSERT_TRUE(exists(false));
12+
ASSERT_FALSE(exists(null));
13+
var v(undef());
14+
ASSERT_FALSE(exists(v));
15+
}
16+
717
TEST(exists, null) {
818
var v1;
919
ASSERT_FALSE(exists(v1, {}));

0 commit comments

Comments
 (0)