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 May 15, 2018
1 parent 785bbfa commit 758e1ea
Show file tree
Hide file tree
Showing 34 changed files with 121 additions and 96 deletions.
1 change: 1 addition & 0 deletions php-cs-fixer
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ $xdebug->check();
unset($xdebug);

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

__HALT_COMPILER();
5 changes: 3 additions & 2 deletions src/AbstractFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ public function configure(array $configuration = null)
$name = $option->getName();
if (array_key_exists($name, $configuration)) {
@trigger_error(sprintf(
'Option "%s" is deprecated and will be removed in %d.0. %s',
'Option "%s" for rule "%s" is deprecated and will be removed in version %d.0. %s',
$name,
Application::VERSION + 1,
$this->getName(),
(int) Application::VERSION + 1,
str_replace('`', '"', $option->getDeprecationMessage())
), E_USER_DEPRECATED);
}
Expand Down
19 changes: 19 additions & 0 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ final class Application extends BaseApplication
*/
private $toolInfo;

private $deprecationWarningsEnabled = false;

public function __construct()
{
error_reporting(-1);
Expand All @@ -61,6 +63,11 @@ public function __construct()
));
}

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

/**
* {@inheritdoc}
*/
Expand All @@ -71,6 +78,18 @@ public function doRun(InputInterface $input, OutputInterface $output)
: ($input->hasParameterOption('--format', true) && 'txt' !== $input->getParameterOption('--format', null, true) ? null : $output)
;
if (null !== $stdErr) {
if ($this->deprecationWarningsEnabled) {
$previousErrorHandler = set_error_handler(function ($severity, $message, $file, $line) use (&$previousErrorHandler, $stdErr) {
if ($severity & E_USER_DEPRECATED) {
$stdErr->writeln("<bg=yellow;fg=black;>{$message}</>");
}

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

$warningsDetector = new WarningsDetector($this->toolInfo);
$warningsDetector->detectOldVendor();
$warningsDetector->detectOldMajor();
Expand Down
41 changes: 21 additions & 20 deletions src/Console/ConfigurationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,25 +345,6 @@ static function (FixerInterface $fixer) {
throw new InvalidConfigurationException(sprintf('The rules contain risky fixers (%s), but they are not allowed to run. Perhaps you forget to use --allow-risky option?', implode(', ', $riskyFixers)));
}
}

foreach ($this->fixers as $fixer) {
if ($fixer instanceof DeprecatedFixerInterface) {
$successors = $fixer->getSuccessorsNames();
$message = sprintf(
'Fixer `%s` is deprecated%s',
$fixer->getName(),
[] === $successors
? ' and will be removed on next major version.'
: sprintf(', use %s instead.', Utils::naturalLanguageJoinWithBackticks($successors))
);

if (getenv('PHP_CS_FIXER_FUTURE_MODE')) {
throw new \RuntimeException($message.' This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.');
}

@trigger_error($message, E_USER_DEPRECATED);
}
}
}

return $this->fixers;
Expand Down Expand Up @@ -749,10 +730,12 @@ private function validateRules(array $rules)
/** @var string[] $configuredFixers */
$configuredFixers = array_keys($ruleSet->getRules());

$fixers = $this->createFixerFactory()->getFixers();

/** @var string[] $availableFixers */
$availableFixers = array_map(static function (FixerInterface $fixer) {
return $fixer->getName();
}, $this->createFixerFactory()->getFixers());
}, $fixers);

$unknownFixers = array_diff(
$configuredFixers,
Expand All @@ -774,6 +757,24 @@ private function validateRules(array $rules)

throw new InvalidConfigurationException(substr($message, 0, -2).'.');
}

foreach ($fixers as $fixer) {
$fixerName = $fixer->getName();
if (isset($rules[$fixerName]) && $fixer instanceof DeprecatedFixerInterface) {
$successors = $fixer->getSuccessorsNames();
$messageEnd = [] === $successors
? sprintf(' and will be removed in version %d.0.', (int) Application::VERSION + 1)
: sprintf('. Use %s instead.', str_replace('`', '"', Utils::naturalLanguageJoinWithBackticks($successors)));

$message = "Rule \"{$fixerName}\" is deprecated{$messageEnd}";

if (getenv('PHP_CS_FIXER_FUTURE_MODE')) {
throw new \RuntimeException($message.' This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.');
}

@trigger_error($message, E_USER_DEPRECATED);
}
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Alias/RandomApiMigrationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,6 @@ protected function createConfigurationDefinition()
'srand' => 'mt_srand',
])
->getOption(),
]);
], $this->getName());
}
}
2 changes: 1 addition & 1 deletion src/Fixer/ClassNotation/OrderedClassElementsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ protected function createConfigurationDefinition()
->setAllowedValues($this->supportedSortAlgorithms)
->setDefault(self::SORT_NONE)
->getOption(),
]);
], $this->getName());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected function createConfigurationDefinition()
(new FixerOptionValidatorGenerator())->allowedValueIsSubsetOf($values),
])
->getOption(),
]);
], $this->getName());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/ClassNotation/VisibilityRequiredFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected function createConfigurationDefinition()
})
->setDefault(['property', 'method'])
->getOption(),
]);
], $this->getName());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,6 @@ protected function createConfigurationDefinition()
'yield',
])
->getOption(),
]);
], $this->getName());
}
}
2 changes: 1 addition & 1 deletion src/Fixer/PhpUnit/PhpUnitConstructFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected function createConfigurationDefinition()
'assertNotSame',
])
->getOption(),
]);
], $this->getName());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ protected function createConfigurationDefinition()
])
->setDefault(PhpUnitTargetVersion::VERSION_5_0) // @TODO 3.x: change to `VERSION_NEWEST`
->getOption(),
]);
], $this->getName());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/PhpUnit/PhpUnitStrictFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,6 @@ protected function createConfigurationDefinition()
'assertNotEquals',
])
->getOption(),
]);
], $this->getName());
}
}
2 changes: 1 addition & 1 deletion src/Fixer/Phpdoc/GeneralPhpdocAnnotationRemoveFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ protected function createConfigurationDefinition()
->setAllowedTypes(['array'])
->setDefault([])
->getOption(),
]);
], $this->getName());
}
}
2 changes: 1 addition & 1 deletion src/Fixer/Phpdoc/PhpdocNoAliasTagFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,6 @@ protected function createConfigurationDefinition()
'link' => 'see',
])
->getOption(),
]);
], $this->getName());
}
}
2 changes: 1 addition & 1 deletion src/Fixer/Phpdoc/PhpdocReturnSelfReferenceFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ protected function createConfigurationDefinition()
})
->setDefault($default)
->getOption(),
]);
], $this->getName());
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/Fixer/Whitespace/NoExtraBlankLinesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,19 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
*/
protected function createConfigurationDefinition()
{
$that = $this;

return new FixerConfigurationResolverRootless('tokens', [
(new FixerOptionBuilder('tokens', 'List of tokens to fix.'))
->setAllowedTypes(['array'])
->setAllowedValues([
(new FixerOptionValidatorGenerator())->allowedValueIsSubsetOf(self::$availableTokens),
])
->setNormalizer(static function (Options $options, $tokens) {
->setNormalizer(function (Options $options, $tokens) use ($that) {
foreach ($tokens as &$token) {
if ('useTrait' === $token) {
@trigger_error('Token "useTrait" is deprecated and will be removed in 3.0, use "use_trait" instead.', E_USER_DEPRECATED);
@trigger_error("Token \"useTrait\" in option \"tokens\" for rule \"{$that->getName()}\" is deprecated and will be removed in 3.0, use \"use_trait\" instead.", E_USER_DEPRECATED);

$token = 'use_trait';

break;
Expand All @@ -327,7 +330,7 @@ protected function createConfigurationDefinition()
})
->setDefault(['extra'])
->getOption(),
]);
], $this->getName());
}

private function fixByToken(Token $token, $index)
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Whitespace/NoSpacesAroundOffsetFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ protected function createConfigurationDefinition()
])
->setDefault($values)
->getOption(),
]);
], $this->getName());
}
}
21 changes: 12 additions & 9 deletions src/FixerConfiguration/FixerConfigurationResolverRootless.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,20 @@ final class FixerConfigurationResolverRootless implements FixerConfigurationReso
*/
private $root;

/**
* @var string
*/
private $fixerName;

/**
* @param string $root
* @param iterable<FixerOptionInterface> $options
* @param string $fixerName
*/
public function __construct($root, $options)
public function __construct($root, $options, $fixerName)
{
$this->resolver = new FixerConfigurationResolver($options);
$this->fixerName = $fixerName;

$names = array_map(
static function (FixerOptionInterface $option) {
Expand Down Expand Up @@ -75,17 +82,13 @@ function (FixerOptionInterface $option) {
$passedNames = array_keys($options);

if (!empty(array_diff($passedNames, $names))) {
$message = "Passing \"{$this->root}\" at the root of the configuration for rule \"{$this->fixerName}\" is deprecated and will not be supported in 3.0, use \"{$this->root}\" => array(...) option instead.";

if (getenv('PHP_CS_FIXER_FUTURE_MODE')) {
throw new \RuntimeException(sprintf(
'Passing "%1$s" at the root of the configuration is deprecated and will not be supported in 3.0, use "%1$s" => array(...) option instead. This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.',
$this->root
));
throw new \RuntimeException("{$message} This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
}

@trigger_error(sprintf(
'Passing "%1$s" at the root of the configuration is deprecated and will not be supported in 3.0, use "%1$s" => array(...) option instead.',
$this->root
), E_USER_DEPRECATED);
@trigger_error($message, E_USER_DEPRECATED);

$options = [$this->root => $options];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Console/ConfigurationResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ public function testWithEmptyRules()

/**
* @group legacy
* @expectedDeprecation Fixer `Vendor4/foo` is deprecated, use `testA` and `testB` instead.
* @expectedDeprecation Rule "Vendor4/foo" is deprecated. Use "testA" and "testB" instead.
*/
public function testDeprecatedFixerConfigured()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixer/Alias/RandomApiMigrationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testConfigureCheckReplacementType()

/**
* @group legacy
* @expectedDeprecation Passing "replacements" at the root of the configuration is deprecated and will not be supported in 3.0, use "replacements" => array(...) option instead.
* @expectedDeprecation Passing "replacements" at the root of the configuration for rule "random_api_migration" is deprecated and will not be supported in 3.0, use "replacements" => array(...) option instead.
*/
public function testLegacyConfigure()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ class Foo
*
* @group legacy
* @dataProvider provideConfigurationCases
* @expectedDeprecation Passing "order" at the root of the configuration is deprecated and will not be supported in 3.0, use "order" => array(...) option instead.
* @expectedDeprecation Passing "order" at the root of the configuration for rule "ordered_class_elements" is deprecated and will not be supported in 3.0, use "order" => array(...) option instead.
*/
public function testLegacyFixWithConfiguration(array $configuration, $expected)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ class Foo
*
* @group legacy
* @dataProvider provideConfigurationCases
* @expectedDeprecation Passing "elements" at the root of the configuration is deprecated and will not be supported in 3.0, use "elements" => array(...) option instead.
* @expectedDeprecation Passing "elements" at the root of the configuration for rule "single_class_element_per_statement" is deprecated and will not be supported in 3.0, use "elements" => array(...) option instead.
*/
public function testLegacyFixWithConfiguration(array $configuration, $expected)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixer/ClassNotation/VisibilityRequiredFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ public function testInvalidConfigurationValueForPHPVersion()
* @group legacy
* @requires PHP 7.1
* @dataProvider provideFixClassConstCases
* @expectedDeprecation Passing "elements" at the root of the configuration is deprecated and will not be supported in 3.0, use "elements" => array(...) option instead.
* @expectedDeprecation Passing "elements" at the root of the configuration for rule "visibility_required" is deprecated and will not be supported in 3.0, use "elements" => array(...) option instead.
*/
public function testLegacyFixClassConst($expected, $input)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testFix($expected, $input = null, $fixStatement = null)
*
* @group legacy
* @dataProvider provideFixCases
* @expectedDeprecation Passing "statements" at the root of the configuration is deprecated and will not be supported in 3.0, use "statements" => array(...) option instead.
* @expectedDeprecation Passing "statements" at the root of the configuration for rule "no_unneeded_control_parentheses" is deprecated and will not be supported in 3.0, use "statements" => array(...) option instead.
*/
public function testLegacyFix($expected, $input = null, $fixStatement = null)
{
Expand All @@ -87,7 +87,7 @@ public function testFix70($expected, $input = null, $fixStatement = null)
*
* @group legacy
* @dataProvider provideFix70Cases
* @expectedDeprecation Passing "statements" at the root of the configuration is deprecated and will not be supported in 3.0, use "statements" => array(...) option instead.
* @expectedDeprecation Passing "statements" at the root of the configuration for rule "no_unneeded_control_parentheses" is deprecated and will not be supported in 3.0, use "statements" => array(...) option instead.
* @requires PHP 7.0
*/
public function testLegacyFix70($expected, $input = null, $fixStatement = null)
Expand Down

0 comments on commit 758e1ea

Please sign in to comment.