• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

krakjoe / uopz / 11996063889

24 Nov 2024 12:20PM UTC coverage: 95.723% (+0.2%) from 95.502%
11996063889

Pull #185

github

web-flow
Merge 971d748fa into dcb01f128
Pull Request #185: Support `exit()` function in PHP 8.4

940 of 982 relevant lines covered (95.72%)

11308.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.12
/src/class.c
1
/*
2
  +----------------------------------------------------------------------+
3
  | uopz                                                                 |
4
  +----------------------------------------------------------------------+
5
  | Copyright (c) Joe Watkins 2016-2021                                  |
6
  +----------------------------------------------------------------------+
7
  | This source file is subject to version 3.01 of the PHP license,      |
8
  | that is bundled with this package in the file LICENSE, and is        |
9
  | available through the world-wide-web at the following url:           |
10
  | http://www.php.net/license/3_01.txt                                  |
11
  | If you did not receive a copy of the PHP license and are unable to   |
12
  | obtain it through the world-wide-web, please send a note to          |
13
  | license@php.net so we can mail you a copy immediately.               |
14
  +----------------------------------------------------------------------+
15
  | Author: Joe Watkins <krakjoe@php.net>                                |
16
  +----------------------------------------------------------------------+
17
 */
18

19
#ifndef UOPZ_CLASS
20
#define UOPZ_CLASS
21

22
#include "php.h"
23
#include "uopz.h"
24

25
#include "util.h"
26
#include "class.h"
27

28
ZEND_EXTERN_MODULE_GLOBALS(uopz);
29

30
#define uopz_get_scope(e) ((e) ? zend_get_executed_scope() : EG(fake_scope))
31
#define uopz_set_scope(s) EG(fake_scope) = (s)
32

33
void uopz_set_mock(zend_string *clazz, zval *mock) { /* {{{ */
200✔
34
        zend_string *key = zend_string_tolower(clazz);
200✔
35

36
        if (zend_hash_update(&UOPZ(mocks), key, mock)) {
200✔
37
                zval_copy_ctor(mock);
200✔
38
        }
39

40
        zend_string_release(key);
200✔
41
} /* }}} */
200✔
42

43
void uopz_unset_mock(zend_string *clazz) { /* {{{ */
80✔
44
        zend_string *key = zend_string_tolower(clazz);
80✔
45
        
46
        if (!zend_hash_exists(&UOPZ(mocks), key)) {
80✔
47
                uopz_exception(
20✔
48
                        "the class provided (%s) has no mock set",
49
                        ZSTR_VAL(clazz));
50
                zend_string_release(key);
20✔
51
                return;
20✔
52
        }
53

54
        zend_hash_del(&UOPZ(mocks), key);
60✔
55
        zend_string_release(key);
60✔
56
} /* }}} */
57

58
int uopz_get_mock(zend_string *clazz, zval *return_value) { /* {{{ */
80✔
59
        zval *mock = NULL;
80✔
60
        zend_string *key = zend_string_tolower(clazz);
80✔
61
        
62
        if (!(mock = zend_hash_find(&UOPZ(mocks), key))) {
80✔
63
                zend_string_release(key);
20✔
64
                return FAILURE;
20✔
65
        }
66

67
        ZVAL_COPY(return_value, mock);
60✔
68
        zend_string_release(key);
60✔
69

70
        return SUCCESS;
30✔
71
} /* }}} */
72

73
int uopz_find_mock(zend_string *clazz, zend_object **object, zend_class_entry **mock) { /* {{{ */
826✔
74
        zend_string *key = zend_string_tolower(clazz);
826✔
75
        zval *found = zend_hash_find(&UOPZ(mocks), key);
826✔
76

77
        zend_string_release(key);
826✔
78

79
        if (!found) {
826✔
80
                return FAILURE;
343✔
81
        }
82

83
        if (Z_TYPE_P(found) == IS_STRING) {
140✔
84
                *mock = zend_fetch_class_by_name(Z_STR_P(found), NULL, ZEND_FETCH_CLASS_EXCEPTION);
80✔
85
                return *mock ? SUCCESS : FAILURE;
80✔
86
        } else {
87
                *mock = Z_OBJCE_P(found);
60✔
88
                if (object) {
60✔
89
                        *object = Z_OBJ_P(found);
60✔
90
                }
91
                return SUCCESS;
60✔
92
        }
93
} /* }}} */
94

95
void uopz_set_property(zval *object, zend_string *member, zval *value) { /* {{{ */
80✔
96
        zend_class_entry *scope = uopz_get_scope(0);
80✔
97
        zend_class_entry *ce = Z_OBJCE_P(object);
80✔
98
        zend_property_info *info;
60✔
99

100
        do {
60✔
101
                uopz_set_scope(ce);
120✔
102

103
                info = zend_get_property_info(ce, member, 1);
120✔
104
        
105
                if (info && info != ZEND_WRONG_PROPERTY_INFO) {
120✔
106
                        break;
30✔
107
                }
108

109
                ce = ce->parent;
60✔
110
        } while (ce);
60✔
111

112
        if (info && info != ZEND_WRONG_PROPERTY_INFO) {
80✔
113
                uopz_set_scope(info->ce);
60✔
114
        } else {
115
                uopz_set_scope(Z_OBJCE_P(object));
20✔
116
        }
117

118
        Z_OBJ_HT_P(object)
80✔
119
                ->write_property(Z_OBJ_P(object), member, value, NULL);
80✔
120

121
        uopz_set_scope(scope);
80✔
122
} /* }}} */
80✔
123

124
void uopz_get_property(zval *object, zend_string *member, zval *value) { /* {{{ */
100✔
125
        zend_class_entry *scope = uopz_get_scope(0);
100✔
126
        zend_class_entry *ce = Z_OBJCE_P(object);
100✔
127
        zend_property_info *info;
80✔
128
        zval *prop, rv;
80✔
129

130
        do {
80✔
131
                uopz_set_scope(ce);
160✔
132

133
                info = zend_get_property_info(ce, member, 1);
160✔
134
        
135
                if (info && info != ZEND_WRONG_PROPERTY_INFO) {
160✔
136
                        break;
30✔
137
                }
138

139
                ce = ce->parent;
100✔
140
        } while (ce);
100✔
141

142
        if (info && info != ZEND_WRONG_PROPERTY_INFO) {
100✔
143
                uopz_set_scope(info->ce);
60✔
144
        } else {
145
                uopz_set_scope(Z_OBJCE_P(object));
40✔
146
        }
147

148
        prop = Z_OBJ_HT_P(object)->read_property(
100✔
149
                Z_OBJ_P(object), member, BP_VAR_R, NULL, &rv);
150

151
        uopz_set_scope(scope);
100✔
152

153
        ZVAL_COPY(value, prop);
100✔
154
} /* }}} */
100✔
155

156
void uopz_set_static_property(zend_class_entry *ce, zend_string *property, zval *value) { /* {{{ */
60✔
157
        zend_class_entry *scope = uopz_get_scope(0);
60✔
158
        zend_class_entry *seek = ce;
60✔
159
        zend_property_info *info;
40✔
160
        zval *prop;
40✔
161

162
        do {
40✔
163
                uopz_set_scope(seek);
80✔
164

165
                info = zend_get_property_info(seek, property, 1);
80✔
166
        
167
                if (info && info != ZEND_WRONG_PROPERTY_INFO) {
80✔
168
                        break;
20✔
169
                }
170

171
                seek = seek->parent;
40✔
172
        } while (seek);
40✔
173

174
        if (info && info != ZEND_WRONG_PROPERTY_INFO) {
60✔
175
                uopz_set_scope(info->ce);
40✔
176
        } else {
177
                uopz_set_scope(ce);
20✔
178
        }
179

180
        prop = zend_std_get_static_property(uopz_get_scope(0), property, 1);
60✔
181
        
182
        uopz_set_scope(scope);
60✔
183

184
        if (!prop) {
60✔
185
                uopz_exception(
20✔
186
                        "cannot set non-existent static property %s::%s", 
187
                        ZSTR_VAL(ce->name),
188
                        ZSTR_VAL(property));
189
                return;
20✔
190
        }
191

192
        zval_ptr_dtor(prop);
40✔
193
        ZVAL_COPY(prop, value);
40✔
194
} /* }}} */
195

196
void uopz_get_static_property(zend_class_entry *ce, zend_string *property, zval *value) { /* {{{ */
40✔
197
        zend_class_entry *scope = uopz_get_scope(0);
40✔
198
        zend_class_entry *seek = ce;
40✔
199
        zend_property_info *info;
30✔
200
        zval *prop;
30✔
201

202
        do {
30✔
203
                uopz_set_scope(seek);
60✔
204

205
                info = zend_get_property_info(seek, property, 1);
60✔
206
        
207
                if (info && info != ZEND_WRONG_PROPERTY_INFO) {
60✔
208
                        break;
20✔
209
                }
210

211
                seek = seek->parent;
20✔
212
        } while (seek);
20✔
213

214
        if (info && info != ZEND_WRONG_PROPERTY_INFO) {
40✔
215
                uopz_set_scope(info->ce);
40✔
216
        } else {
217
                uopz_set_scope(ce);
×
218
        }
219

220
        prop = zend_std_get_static_property(uopz_get_scope(0), property, 1);
40✔
221
        
222
        uopz_set_scope(scope);
40✔
223

224
        if (!prop) {
40✔
225
                return;
226
        }
227
        
228
        ZVAL_COPY(value, prop);
40✔
229
} /* }}} */
230

231
#endif        /* UOPZ_CLASS */
232

233
/*
234
 * Local variables:
235
 * tab-width: 4
236
 * c-basic-offset: 4
237
 * End:
238
 * vim600: noet sw=4 ts=4 fdm=marker
239
 * vim<600: noet sw=4 ts=4
240
 */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc