From a143a19b8bcb3dc4d9b4a20c85776852c30642f2 Mon Sep 17 00:00:00 2001 From: omniError Date: Mon, 10 Nov 2014 18:05:18 -0600 Subject: [PATCH 1/5] [ticket/13306] add error level to collector https://tracker.phpbb.com/browse/PHPBB3-13306 PHPBB3-13306 --- phpBB/phpbb/error_collector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/error_collector.php b/phpBB/phpbb/error_collector.php index 7141f83174..d2a481fc6c 100644 --- a/phpBB/phpbb/error_collector.php +++ b/phpBB/phpbb/error_collector.php @@ -24,7 +24,7 @@ class error_collector function install() { - set_error_handler(array(&$this, 'error_handler')); + set_error_handler(array(&$this, 'error_handler'), error_reporting()); } function uninstall() From efd4b43c1bbea587a480c145d8a79066ae78d9a3 Mon Sep 17 00:00:00 2001 From: omniError Date: Tue, 11 Nov 2014 20:20:52 -0600 Subject: [PATCH 2/5] [ticket/13306] constructor sets error types https://tracker.phpbb.com/browse/PHPBB3-13306 PHPBB3-13306 --- phpBB/phpbb/error_collector.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/error_collector.php b/phpBB/phpbb/error_collector.php index d2a481fc6c..50587cc4ef 100644 --- a/phpBB/phpbb/error_collector.php +++ b/phpBB/phpbb/error_collector.php @@ -16,15 +16,27 @@ namespace phpbb; class error_collector { var $errors; + var $error_types; - function __construct() + /** + * Constructor. + * + * The variable $error_types may be set to a mask of PHP error types that + * the collector should keep, e.g. `E_ALL`. If unset, the current value of + * the error_reporting() function will be used to determine which errors + * the collector will keep. + * + * @param int|null $error_types + */ + function __construct($error_types = null) { $this->errors = array(); + $this->error_types = !empty($error_types) ? $error_types : error_reporting(); } function install() { - set_error_handler(array(&$this, 'error_handler'), error_reporting()); + set_error_handler(array(&$this, 'error_handler'), $this->error_types); } function uninstall() From 834a93f76c9b2efe9a4fa89200fe24f844df7479 Mon Sep 17 00:00:00 2001 From: omniError Date: Tue, 11 Nov 2014 20:23:13 -0600 Subject: [PATCH 3/5] [ticket/13306] allow 0 error types https://tracker.phpbb.com/browse/PHPBB3-13306 PHPBB3-13306 --- phpBB/phpbb/error_collector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/error_collector.php b/phpBB/phpbb/error_collector.php index 50587cc4ef..688bc0c422 100644 --- a/phpBB/phpbb/error_collector.php +++ b/phpBB/phpbb/error_collector.php @@ -31,7 +31,7 @@ class error_collector function __construct($error_types = null) { $this->errors = array(); - $this->error_types = !empty($error_types) ? $error_types : error_reporting(); + $this->error_types = ($error_types !== null) ? $error_types : error_reporting(); } function install() From 0a4ffb7cf818c399f4956773ff7cb8e5b0243fa3 Mon Sep 17 00:00:00 2001 From: omniError Date: Wed, 12 Nov 2014 16:01:49 -0600 Subject: [PATCH 4/5] [ticket/13306] move error_reporting call https://tracker.phpbb.com/browse/PHPBB3-13306 PHPBB3-13306 --- phpBB/phpbb/error_collector.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/error_collector.php b/phpBB/phpbb/error_collector.php index 688bc0c422..bf8efd1065 100644 --- a/phpBB/phpbb/error_collector.php +++ b/phpBB/phpbb/error_collector.php @@ -26,17 +26,18 @@ class error_collector * the error_reporting() function will be used to determine which errors * the collector will keep. * + * @see PHPBB3-13306 * @param int|null $error_types */ function __construct($error_types = null) { $this->errors = array(); - $this->error_types = ($error_types !== null) ? $error_types : error_reporting(); + $this->error_types = $error_types; } function install() { - set_error_handler(array(&$this, 'error_handler'), $this->error_types); + set_error_handler(array(&$this, 'error_handler'), ($this->error_types !== null) ? $this->error_types : error_reporting()); } function uninstall() From 8a9c2c055c7fbc199bad3e6fbfb0235a910b87e2 Mon Sep 17 00:00:00 2001 From: omniError Date: Wed, 12 Nov 2014 18:35:39 -0600 Subject: [PATCH 5/5] [ticket/13306] update test https://tracker.phpbb.com/browse/PHPBB3-13306 PHPBB3-13306 --- tests/error_collector_test.php | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/error_collector_test.php b/tests/error_collector_test.php index 1d8ef367f5..b92c4fa6bb 100644 --- a/tests/error_collector_test.php +++ b/tests/error_collector_test.php @@ -17,7 +17,7 @@ class phpbb_error_collector_test extends phpbb_test_case { public function test_collection() { - $collector = new \phpbb\error_collector; + $collector = new \phpbb\error_collector(E_ALL | E_STRICT); // php set_error_handler() default $collector->install(); // Cause a warning @@ -35,4 +35,32 @@ class phpbb_error_collector_test extends phpbb_test_case $this->assertStringStartsWith('Errno 2: Division by zero at ', $error_contents); $this->assertStringEndsWith(" line $line", $error_contents); } + + public function test_collection_with_mask() + { + $collector = new \phpbb\error_collector(E_ALL & ~E_NOTICE); // not collecting notices + $collector->install(); + + // Cause a warning + 1/0; $line = __LINE__; + + // Cause a notice + $array = array('ITEM' => 'value'); + $value = $array[ITEM]; $line2 = __LINE__; + + $collector->uninstall(); + + // The notice should not be collected + $this->assertEmpty($collector->errors[1]); + + list($errno, $msg_text, $errfile, $errline) = $collector->errors[0]; + $error_contents = $collector->format_errors(); + + $this->assertEquals($errno, 2); + + // Unfortunately $error_contents will contain the full path here, + // because the tests directory is outside of phpbb root path. + $this->assertStringStartsWith('Errno 2: Division by zero at ', $error_contents); + $this->assertStringEndsWith(" line $line", $error_contents); + } }