Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Handle deprecation notices softer
  • Loading branch information
Seldaek committed May 31, 2015
1 parent e64470c commit 1753c27
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Composer/Console/Application.php
Expand Up @@ -65,7 +65,6 @@ public function __construct()
date_default_timezone_set(@date_default_timezone_get());
}

ErrorHandler::register();
parent::__construct('Composer', Composer::VERSION);
}

Expand All @@ -89,6 +88,7 @@ public function run(InputInterface $input = null, OutputInterface $output = null
public function doRun(InputInterface $input, OutputInterface $output)
{
$this->io = new ConsoleIO($input, $output, $this->getHelperSet());
ErrorHandler::register($this->io);

if (PHP_VERSION_ID < 50302) {
$this->getIO()->writeError('<warning>Composer only officially supports PHP 5.3.2 and above, you will most likely encounter problems with your PHP '.PHP_VERSION.', upgrading is strongly recommended.</warning>');
Expand Down
24 changes: 22 additions & 2 deletions src/Composer/Util/ErrorHandler.php
Expand Up @@ -12,13 +12,17 @@

namespace Composer\Util;

use Composer\IO\IOInterface;

/**
* Convert PHP errors into exceptions
*
* @author Artem Lopata <biozshock@gmail.com>
*/
class ErrorHandler
{
private static $io;

/**
* Error handler
*
Expand All @@ -42,16 +46,32 @@ public static function handle($level, $message, $file, $line)
"\na legitimately suppressed error that you were not supposed to see.";
}

throw new \ErrorException($message, 0, $level, $file, $line);
if ($level !== E_DEPRECATED && $level !== E_USER_DEPRECATED) {
throw new \ErrorException($message, 0, $level, $file, $line);
}

if (self::$io) {
self::$io->writeError('<warning>Deprecation Notice: '.$message.' in '.$file.':'.$line.'</warning>');
if (self::$io->isVerbose()) {
self::$io->writeError('<warning>Stack trace:</warning>');
self::$io->writeError(array_filter(array_map(function ($a) {
if (isset($a['line'], $a['file'])) {
return '<warning> '.$a['file'].':'.$a['line'].'</warning>';
}
return null;
}, array_slice(debug_backtrace(), 2))));
}
}
}

/**
* Register error handler
*
* @static
*/
public static function register()
public static function register(IOInterface $io = null)
{
set_error_handler(array(__CLASS__, 'handle'));
self::$io = $io;
}
}

0 comments on commit 1753c27

Please sign in to comment.