Browse Source

Merge remote-tracking branch 'biozshock/issue225'

Jordi Boggiano 13 years ago
parent
commit
a5c2c6c07e

+ 2 - 0
src/Composer/Console/Application.php

@@ -25,6 +25,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
@@ -40,6 +41,7 @@ class Application extends BaseApplication
 
     public function __construct()
     {
+        ErrorHandler::set();
         parent::__construct('Composer', Composer::VERSION);
     }
 

+ 52 - 0
src/Composer/Util/ErrorHandler.php

@@ -0,0 +1,52 @@
+<?php
+
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ *     Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * 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 <biozshock@gmail.com>
+ */
+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'));
+    }
+}

+ 49 - 0
tests/Composer/Test/Util/ErrorHandlerTest.php

@@ -0,0 +1,49 @@
+<?php
+
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ *     Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * 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 <biozshock@gmail.com>
+ */
+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');
+    }
+}