From 7d994b5de4db36c0e17c5c2e9b2f7478de82d320 Mon Sep 17 00:00:00 2001 From: Artem Lopata Date: Fri, 27 Jan 2012 10:29:07 +0200 Subject: [PATCH] Added ErrorHandler for handling E_NOTICE, E_WARNING, E_ERROR --- src/Composer/Console/Application.php | 2 + src/Composer/Util/ErrorHandler.php | 52 +++++++++++++++++++ tests/Composer/Test/Util/ErrorHandlerTest.php | 49 +++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 src/Composer/Util/ErrorHandler.php create mode 100644 tests/Composer/Test/Util/ErrorHandlerTest.php diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index 745d5510d..aeddf82ce 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -24,6 +24,7 @@ use Composer\Composer; use Composer\Factory; use Composer\IO\IOInterface; use Composer\IO\ConsoleIO; +use Composer\Util\ErrorHandler; /** * The console application that handles the commands @@ -39,6 +40,7 @@ class Application extends BaseApplication public function __construct() { + ErrorHandler::set(); parent::__construct('Composer', Composer::VERSION); } diff --git a/src/Composer/Util/ErrorHandler.php b/src/Composer/Util/ErrorHandler.php new file mode 100644 index 000000000..cb620e52f --- /dev/null +++ b/src/Composer/Util/ErrorHandler.php @@ -0,0 +1,52 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Util; + +/** + * Convert PHP E_NOTICE, E_WARNING into exceptions + * + * @author Artem Lopata + */ +class ErrorHandler +{ + /** + * Error handler + * + * @param int $errorNo Level of the error raised + * @param string $errorString Error message + * @param string $errorFile Filename that the error was raised in + * @param int $errorLine Line number the error was raised at + * + * @static + * @throws \ErrorException + */ + public static function handle($errorNo, $errorString, $errorFile, $errorLine) + { + //this allows error suppression in 3rd party code to work + if (!error_reporting()) { + return; + } + + throw new \ErrorException(sprintf('%s in %s:%d', $errorString, $errorFile, $errorLine), $errorNo); + } + + /** + * Set error handler + * + * @static + */ + public static function set() + { + set_error_handler(array(__CLASS__, 'handle')); + } +} diff --git a/tests/Composer/Test/Util/ErrorHandlerTest.php b/tests/Composer/Test/Util/ErrorHandlerTest.php new file mode 100644 index 000000000..6aebc5c15 --- /dev/null +++ b/tests/Composer/Test/Util/ErrorHandlerTest.php @@ -0,0 +1,49 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Util; + +use Composer\Util\ErrorHandler; +use Composer\Test\TestCase; + +/** + * ErrorHandler test case + * + * @author Artem Lopata + */ +class ErrorHandlerTest extends TestCase +{ + /** + * Test ErrorHandler handles notices + */ + public function testErrorHandlerCaptureNotice() + { + $this->setExpectedException('\ErrorException', 'Undefined index: baz in ' . __FILE__); + + ErrorHandler::set(); + + $array = array('foo' => 'bar'); + $array['baz']; + } + + /** + * Test ErrorHandler handles warnings + */ + public function testErrorHandlerCaptureWarning() + { + $this->setExpectedException('\ErrorException', 'array_merge(): Argument #2 is not an array in ' . __FILE__); + + ErrorHandler::set(); + + array_merge(array(), 'string'); + } +}