summaryrefslogtreecommitdiffstats
path: root/upstream.patch
blob: e07990ce2be2274b091bd8707699150824ee16b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
From f7e9f7b2675aaf54f5babaac3e32112c2bae2292 Mon Sep 17 00:00:00 2001
From: Joe Watkins <krakjoe@php.net>
Date: Mon, 15 Sep 2025 10:28:07 +0200
Subject: [PATCH] PCOV_ENABLED env support

---
 pcov.c         | 71 +++++++++++++++++++++++++++++++++++++++++++++-----
 tests/008.phpt | 16 ++++++++++++
 tests/009.phpt | 16 ++++++++++++
 tests/010.phpt | 16 ++++++++++++
 tests/011.phpt | 16 ++++++++++++
 tests/012.phpt | 16 ++++++++++++
 tests/013.phpt | 16 ++++++++++++
 7 files changed, 161 insertions(+), 6 deletions(-)
 create mode 100644 tests/008.phpt
 create mode 100644 tests/009.phpt
 create mode 100644 tests/010.phpt
 create mode 100644 tests/011.phpt
 create mode 100644 tests/012.phpt
 create mode 100644 tests/013.phpt

diff --git a/pcov.c b/pcov.c
index ba58263..06e60ee 100644
--- a/pcov.c
+++ b/pcov.c
@@ -57,8 +57,56 @@
 #define GC_SET_REFCOUNT(ref, rc) (GC_REFCOUNT(ref) = (rc))
 #endif
 
+static zend_always_inline bool php_pcov_api_enabled(void) {
+	const char* env = getenv("PCOV_ENABLED");
+
+	if (env) {
+		size_t length = strlen(env);
+
+		if (length == 1) {
+			switch (*env) {
+				case '1':
+				case 'y': /* short yes */
+					return true;
+				case '0':
+				case 'n': /* short no */
+					return false;
+			}
+			/* all other characters are assumed false */
+			return false;
+		}
+
+		if (length == 2) {
+			if (strncasecmp(env, ZEND_STRL("on")) == SUCCESS) {
+				return true;
+			} else if (strncasecmp(env, ZEND_STRL("no")) == SUCCESS) {
+				return false;
+			}
+
+			/* all other strings are assumed false */
+			return false;
+		}
+
+		if (length == 3) {
+			if (strncasecmp(env, ZEND_STRL("off")) == SUCCESS) {
+				return false;
+			} else if (strncasecmp(env, ZEND_STRL("yes")) == SUCCESS) {
+				return true;
+			}
+
+			/* all other strings are assumed false */
+			return false;
+		}
+
+		/* all other lengths are assumed false */
+		return false;
+	}
+
+	return INI_BOOL("pcov.enabled");
+}
+
 #define PHP_PCOV_API_ENABLED_GUARD() do { \
-	if (!INI_BOOL("pcov.enabled")) { \
+	if (!php_pcov_api_enabled()) { \
 		return; \
 	} \
 } while (0);
@@ -339,7 +387,7 @@ PHP_MINIT_FUNCTION(pcov)
 
 	REGISTER_INI_ENTRIES();
 
-	if (INI_BOOL("pcov.enabled")) {
+	if (php_pcov_api_enabled()) {
 		zend_execute_ex_function   = zend_execute_ex;
 		zend_execute_ex            = php_pcov_execute_ex;
 	}
@@ -357,7 +405,7 @@ PHP_MINIT_FUNCTION(pcov)
  */
 PHP_MSHUTDOWN_FUNCTION(pcov)
 {
-	if (INI_BOOL("pcov.enabled")) {
+	if (php_pcov_api_enabled()) {
 		zend_execute_ex   = zend_execute_ex_function;
 	}
 
@@ -427,7 +475,7 @@ PHP_RINIT_FUNCTION(pcov)
 	ZEND_TSRMLS_CACHE_UPDATE();
 #endif
 
-	if (!INI_BOOL("pcov.enabled")) {
+	if (!php_pcov_api_enabled()) {
 		return SUCCESS;
 	}
 
@@ -464,7 +512,7 @@ PHP_RINIT_FUNCTION(pcov)
  */
 PHP_RSHUTDOWN_FUNCTION(pcov)
 {
-	if (!INI_BOOL("pcov.enabled") || CG(unclean_shutdown)) {
+	if (!php_pcov_api_enabled() || CG(unclean_shutdown)) {
 		return SUCCESS;
 	}
 
@@ -506,7 +554,7 @@ PHP_MINFO_FUNCTION(pcov)
 
 	php_info_print_table_header(2,
 		"PCOV support",
-		INI_BOOL("pcov.enabled")  ? "Enabled" : "Disabled");
+		php_pcov_api_enabled()  ? "Enabled" : "Disabled");
 	php_info_print_table_row(2,
 		"PCOV version",
 		PHP_PCOV_VERSION);
@@ -862,6 +910,16 @@ PHP_NAMED_FUNCTION(php_pcov_memory)
 	} while ((arena = arena->prev));
 } /* }}} */
 
+/* {{{ bool \pcov\enabled(void) */
+PHP_NAMED_FUNCTION(php_pcov_enabled)
+{
+	if (zend_parse_parameters_none() != SUCCESS) {
+		return;
+	}
+
+	RETURN_BOOL(php_pcov_api_enabled());
+} /* }}} */
+
 /* {{{ */
 ZEND_BEGIN_ARG_INFO_EX(php_pcov_collect_arginfo, 0, 0, 0)
 	ZEND_ARG_TYPE_INFO(0, type, IS_LONG, 0)
@@ -886,6 +944,7 @@ const zend_function_entry php_pcov_functions[] = {
 	ZEND_NS_FENTRY("pcov", clear,      php_pcov_clear,         php_pcov_clear_arginfo, 0)
 	ZEND_NS_FENTRY("pcov", waiting,    php_pcov_waiting,       php_pcov_no_arginfo, 0)
 	ZEND_NS_FENTRY("pcov", memory,     php_pcov_memory,        php_pcov_no_arginfo, 0)
+	ZEND_NS_FENTRY("pcov", enabled,    php_pcov_enabled,       php_pcov_no_arginfo, 0)
 	PHP_FE_END
 };
 /* }}} */
diff --git a/tests/008.phpt b/tests/008.phpt
new file mode 100644
index 0000000..0985bad
--- /dev/null
+++ b/tests/008.phpt
@@ -0,0 +1,16 @@
+--TEST--
+enable/disable (disable env as no)
+--SKIPIF--
+<?php if (!extension_loaded("pcov")) print "skip"; ?>
+--INI--
+pcov.enabled = 1
+--ENV--
+PCOV_ENABLED=no
+--FILE--
+<?php
+var_dump(\getenv("PCOV_ENABLED"));
+var_dump(\pcov\enabled());
+?>
+--EXPECT--
+string(2) "no"
+bool(false)
diff --git a/tests/009.phpt b/tests/009.phpt
new file mode 100644
index 0000000..b174b2f
--- /dev/null
+++ b/tests/009.phpt
@@ -0,0 +1,16 @@
+--TEST--
+enable/disable (enable env as yes)
+--SKIPIF--
+<?php if (!extension_loaded("pcov")) print "skip"; ?>
+--INI--
+pcov.enabled = 0
+--ENV--
+PCOV_ENABLED=yes
+--FILE--
+<?php
+var_dump(\getenv("PCOV_ENABLED"));
+var_dump(\pcov\enabled());
+?>
+--EXPECT--
+string(3) "yes"
+bool(true)
diff --git a/tests/010.phpt b/tests/010.phpt
new file mode 100644
index 0000000..a97e320
--- /dev/null
+++ b/tests/010.phpt
@@ -0,0 +1,16 @@
+--TEST--
+enable/disable (disable env as off)
+--SKIPIF--
+<?php if (!extension_loaded("pcov")) print "skip"; ?>
+--INI--
+pcov.enabled = 1
+--ENV--
+PCOV_ENABLED=off
+--FILE--
+<?php
+var_dump(\getenv("PCOV_ENABLED"));
+var_dump(\pcov\enabled());
+?>
+--EXPECT--
+string(3) "off"
+bool(false)
diff --git a/tests/011.phpt b/tests/011.phpt
new file mode 100644
index 0000000..8775c8c
--- /dev/null
+++ b/tests/011.phpt
@@ -0,0 +1,16 @@
+--TEST--
+enable/disable (enable env as on)
+--SKIPIF--
+<?php if (!extension_loaded("pcov")) print "skip"; ?>
+--INI--
+pcov.enabled = 0
+--ENV--
+PCOV_ENABLED=on
+--FILE--
+<?php
+var_dump(\getenv("PCOV_ENABLED"));
+var_dump(\pcov\enabled());
+?>
+--EXPECT--
+string(2) "on"
+bool(true)
diff --git a/tests/012.phpt b/tests/012.phpt
new file mode 100644
index 0000000..24fb998
--- /dev/null
+++ b/tests/012.phpt
@@ -0,0 +1,16 @@
+--TEST--
+enable/disable (disable env as 0)
+--SKIPIF--
+<?php if (!extension_loaded("pcov")) print "skip"; ?>
+--INI--
+pcov.enabled = 1
+--ENV--
+PCOV_ENABLED=0
+--FILE--
+<?php
+var_dump(\getenv("PCOV_ENABLED"));
+var_dump(\pcov\enabled());
+?>
+--EXPECT--
+string(1) "0"
+bool(false)
diff --git a/tests/013.phpt b/tests/013.phpt
new file mode 100644
index 0000000..f3c59c5
--- /dev/null
+++ b/tests/013.phpt
@@ -0,0 +1,16 @@
+--TEST--
+enable/disable (enable env as 1)
+--SKIPIF--
+<?php if (!extension_loaded("pcov")) print "skip"; ?>
+--INI--
+pcov.enabled = 0
+--ENV--
+PCOV_ENABLED=1
+--FILE--
+<?php
+var_dump(\getenv("PCOV_ENABLED"));
+var_dump(\pcov\enabled());
+?>
+--EXPECT--
+string(1) "1"
+bool(true)