Skip to content

Commit

Permalink
Output warnings for deprecated rules and options
Browse files Browse the repository at this point in the history
  • Loading branch information
julienfalque committed Nov 9, 2019
1 parent f2928db commit d3ed10a
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 10 deletions.
1 change: 1 addition & 0 deletions php-cs-fixer
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ $xdebug->check();
unset($xdebug);

$application = new Application();
$application->enableDeprecationWarnings();
$application->run();

__HALT_COMPILER();
4 changes: 2 additions & 2 deletions src/AbstractFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function configure(array $configuration = null)
throw new \InvalidArgumentException("{$message} This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
}

@trigger_error($message, E_USER_DEPRECATED);
Application::triggerDeprecation($message);

$configuration = [];
}
Expand All @@ -148,7 +148,7 @@ public function configure(array $configuration = null)
throw new \InvalidArgumentException("{$message} This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
}

@trigger_error($message, E_USER_DEPRECATED);
Application::triggerDeprecation($message);
}
}

Expand Down
50 changes: 49 additions & 1 deletion src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ final class Application extends BaseApplication
*/
private $toolInfo;

private $deprecationWarningsEnabled = false;

private $customErrorHandlerRegistered = false;

private static $printCurrentDeprecationNotice = false;

public function __construct()
{
if (!getenv('PHP_CS_FIXER_FUTURE_MODE')) {
Expand All @@ -63,6 +69,21 @@ public function __construct()
));
}

public function enableDeprecationWarnings()
{
$this->deprecationWarningsEnabled = true;
}

/**
* @param string $message
*/
public static function triggerDeprecation($message)
{
self::$printCurrentDeprecationNotice = true;
@trigger_error($message, E_USER_DEPRECATED);
self::$printCurrentDeprecationNotice = false;
}

/**
* {@inheritdoc}
*/
Expand All @@ -72,7 +93,24 @@ public function doRun(InputInterface $input, OutputInterface $output)
? $output->getErrorOutput()
: ($input->hasParameterOption('--format', true) && 'txt' !== $input->getParameterOption('--format', null, true) ? null : $output)
;

$previousErrorHandler = null;

if (null !== $stdErr) {
if ($this->deprecationWarningsEnabled && !$this->customErrorHandlerRegistered) {
$previousErrorHandler = set_error_handler(function ($severity, $message, $file, $line) use (&$previousErrorHandler, $stdErr) {
if (self::$printCurrentDeprecationNotice && $severity & E_USER_DEPRECATED) {
$stdErr->writeln("<bg=yellow;fg=black;>{$message}</>");
}

if (\is_callable($previousErrorHandler)) {
$previousErrorHandler($severity, $message, $file, $line);
}
});

$this->customErrorHandlerRegistered = true;
}

$warningsDetector = new WarningsDetector($this->toolInfo);
$warningsDetector->detectOldVendor();
$warningsDetector->detectOldMajor();
Expand All @@ -81,7 +119,17 @@ public function doRun(InputInterface $input, OutputInterface $output)
}
}

return parent::doRun($input, $output);
$exitCode = parent::doRun($input, $output);

if (null !== $previousErrorHandler) {
set_error_handler($previousErrorHandler);
} else {
restore_error_handler();
}

$this->customErrorHandlerRegistered = false;

return $exitCode;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Console/ConfigurationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public function getProgress()
throw new \InvalidArgumentException("{$message} This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
}

@trigger_error($message, E_USER_DEPRECATED);
Application::triggerDeprecation($message);
}

$this->progress = $progressType;
Expand Down Expand Up @@ -767,7 +767,7 @@ private function validateRules(array $rules)
throw new \RuntimeException("{$message} This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
}

@trigger_error($message, E_USER_DEPRECATED);
Application::triggerDeprecation($message);
}
}
}
Expand Down Expand Up @@ -919,7 +919,7 @@ private function resolveOptionBooleanValue($optionName)
throw new InvalidConfigurationException("{$message} This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
}

@trigger_error($message, E_USER_DEPRECATED);
Application::triggerDeprecation($message);

return false;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Fixer/Operator/BinaryOperatorSpacesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Console\Application;
use PhpCsFixer\Console\Command\HelpCommand;
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
Expand Down Expand Up @@ -524,7 +525,7 @@ private function resolveOldConfig(array $configuration)
throw new InvalidFixerConfigurationException($this->getName(), "{$message} This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
}

@trigger_error($message, E_USER_DEPRECATED);
Application::triggerDeprecation($message);

return $newConfig;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Fixer/Whitespace/NoExtraBlankLinesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\ConfigurationException\InvalidConfigurationException;
use PhpCsFixer\Console\Application;
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
Expand Down Expand Up @@ -323,7 +324,7 @@ protected function createConfigurationDefinition()
throw new InvalidConfigurationException("{$message} This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
}

@trigger_error($message, E_USER_DEPRECATED);
Application::triggerDeprecation($message);
$token = 'use_trait';

break;
Expand Down
3 changes: 2 additions & 1 deletion src/FixerConfiguration/FixerConfigurationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace PhpCsFixer\FixerConfiguration;

use PhpCsFixer\Console\Application;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand Down Expand Up @@ -67,7 +68,7 @@ public function resolve(array $options)
throw new InvalidOptionsException(sprintf('Aliased option %s/%s is passed multiple times.', $name, $alias));
}

@trigger_error(sprintf('Option "%s" is deprecated, use "%s" instead.', $alias, $name), E_USER_DEPRECATED);
Application::triggerDeprecation(sprintf('Option "%s" is deprecated, use "%s" instead.', $alias, $name));

$options[$name] = $options[$alias];
unset($options[$alias]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace PhpCsFixer\FixerConfiguration;

use PhpCsFixer\Console\Application;

/**
* @internal
*
Expand Down Expand Up @@ -88,7 +90,7 @@ function (FixerOptionInterface $option) {
throw new \RuntimeException("{$message}. This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
}

@trigger_error($message, E_USER_DEPRECATED);
Application::triggerDeprecation($message);

$options = [$this->root => $options];
}
Expand Down

0 comments on commit d3ed10a

Please sign in to comment.