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 Apr 12, 2020
1 parent 99fc6f1 commit 1a9e0c7
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 13 deletions.
1 change: 1 addition & 0 deletions php-cs-fixer
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,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
3 changes: 2 additions & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace PhpCsFixer;

use PhpCsFixer\Console\Application;
use PhpCsFixer\Fixer\FixerInterface;

/**
Expand Down Expand Up @@ -46,7 +47,7 @@ public function __construct($name = 'default')
*/
public static function create()
{
@trigger_error(__METHOD__.' is deprecated since 2.17 and will be removed in 3.0.', E_USER_DEPRECATED);
Application::triggerDeprecation(__METHOD__.' is deprecated since 2.17 and will be removed in 3.0.');

return new static();
}
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;
}

/**
* @return int
*/
Expand All @@ -80,7 +101,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 @@ -89,7 +127,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 @@ -434,7 +434,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 @@ -771,7 +771,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 @@ -923,7 +923,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/ClassNotation/ClassAttributesSeparationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace PhpCsFixer\Fixer\ClassNotation;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\Console\Application;
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
Expand Down Expand Up @@ -195,7 +196,7 @@ protected function createConfigurationDefinition()
$deprecated = array_intersect($values, self::SUPPORTED_TYPES);
if (\count($deprecated) > 0) {
$message = 'A list of elements is deprecated, use a dictionary of `const|method|property` => `none|one` instead.';
@trigger_error($message, E_USER_DEPRECATED);
Application::triggerDeprecation($message);

return array_fill_keys($deprecated, self::SPACING_ONE);
}
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 @@ -525,7 +526,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/BlankLineBeforeStatementFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace PhpCsFixer\Fixer\Whitespace;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\Console\Application;
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
Expand Down Expand Up @@ -88,7 +89,7 @@ public function configure(array $configuration = null)

foreach ($this->configuration['statements'] as $key) {
if ('die' === $key) {
@trigger_error('Option "die" is deprecated, use "exit" instead.', E_USER_DEPRECATED);
Application::triggerDeprecation('Option "die" is deprecated, use "exit" instead.');
}

$this->fixTokenMap[$key] = self::$tokenMap[$key];
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 @@ -325,7 +326,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 @@ static 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 1a9e0c7

Please sign in to comment.