From f7e9f7b2675aaf54f5babaac3e32112c2bae2292 Mon Sep 17 00:00:00 2001 From: Joe Watkins 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-- + +--INI-- +pcov.enabled = 1 +--ENV-- +PCOV_ENABLED=no +--FILE-- + +--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-- + +--INI-- +pcov.enabled = 0 +--ENV-- +PCOV_ENABLED=yes +--FILE-- + +--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-- + +--INI-- +pcov.enabled = 1 +--ENV-- +PCOV_ENABLED=off +--FILE-- + +--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-- + +--INI-- +pcov.enabled = 0 +--ENV-- +PCOV_ENABLED=on +--FILE-- + +--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-- + +--INI-- +pcov.enabled = 1 +--ENV-- +PCOV_ENABLED=0 +--FILE-- + +--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-- + +--INI-- +pcov.enabled = 0 +--ENV-- +PCOV_ENABLED=1 +--FILE-- + +--EXPECT-- +string(1) "1" +bool(true)