Skip to content

Commit

Permalink
Only print deprecations relevant to CLI usage
Browse files Browse the repository at this point in the history
  • Loading branch information
julienfalque committed May 16, 2018
1 parent ecdb244 commit 11cd72a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 44 deletions.
13 changes: 3 additions & 10 deletions src/AbstractFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,7 @@ public function configure(array $configuration = null)
}

if (null === $configuration) {
if (getenv('PHP_CS_FIXER_FUTURE_MODE')) {
throw new \InvalidArgumentException('Parameter must not be `null`. This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.');
}

@trigger_error(
'Passing NULL to set default configuration is deprecated and will not be supported in 3.0, use an empty array instead.',
E_USER_DEPRECATED
);
Application::triggerDeprecation('Passing NULL to set default configuration is deprecated and will not be supported in 3.0, use an empty array instead.');

$configuration = [];
}
Expand All @@ -137,13 +130,13 @@ public function configure(array $configuration = null)

$name = $option->getName();
if (array_key_exists($name, $configuration)) {
@trigger_error(sprintf(
Application::triggerDeprecation(sprintf(
'Option "%s" for rule "%s" is deprecated and will be removed in version %d.0. %s',
$name,
$this->getName(),
(int) Application::VERSION + 1,
str_replace('`', '"', $option->getDeprecationMessage())
), E_USER_DEPRECATED);
));
}
}

Expand Down
18 changes: 17 additions & 1 deletion src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ final class Application extends BaseApplication

private $customErrorHandlerRegistered = false;

private static $printCurrentDeprecationNotice = false;

public function __construct()
{
error_reporting(-1);
Expand All @@ -70,6 +72,20 @@ public function enableDeprecationWarnings()
$this->deprecationWarningsEnabled = true;
}

/**
* @param string $message
*/
public static function triggerDeprecation($message)
{
if (getenv('PHP_CS_FIXER_FUTURE_MODE')) {
throw new \InvalidArgumentException("{$message} This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
}

self::$printCurrentDeprecationNotice = true;
@trigger_error($message, E_USER_DEPRECATED);
self::$printCurrentDeprecationNotice = false;
}

/**
* {@inheritdoc}
*/
Expand All @@ -85,7 +101,7 @@ public function doRun(InputInterface $input, OutputInterface $output)
if (null !== $stdErr) {
if ($this->deprecationWarningsEnabled && !$this->customErrorHandlerRegistered) {
$previousErrorHandler = set_error_handler(function ($severity, $message, $file, $line) use (&$previousErrorHandler, $stdErr) {
if ($severity & E_USER_DEPRECATED) {
if (self::$printCurrentDeprecationNotice && $severity & E_USER_DEPRECATED) {
$stdErr->writeln("<bg=yellow;fg=black;>{$message}</>");
}

Expand Down
26 changes: 3 additions & 23 deletions src/Console/ConfigurationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,14 +426,7 @@ public function getProgress()
implode('", "', $progressTypes)
));
} elseif (in_array($progressType, ['estimating', 'estimating-max', 'run-in'], true)) {
if (getenv('PHP_CS_FIXER_FUTURE_MODE')) {
throw new \InvalidArgumentException('Passing `estimating`, `estimating-max` or `run-in` is deprecated and will not be supported in 3.0, use `none` or `dots` instead. This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.');
}

@trigger_error(
'Passing `estimating`, `estimating-max` or `run-in` is deprecated and will not be supported in 3.0, use `none` or `dots` instead.',
E_USER_DEPRECATED
);
Application::triggerDeprecation('Passing `estimating`, `estimating-max` or `run-in` is deprecated and will not be supported in 3.0, use `none` or `dots` instead.');
}

$this->progress = $progressType;
Expand Down Expand Up @@ -766,13 +759,7 @@ private function validateRules(array $rules)
? 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);
Application::triggerDeprecation("Rule \"{$fixerName}\" is deprecated{$messageEnd}");
}
}
}
Expand Down Expand Up @@ -918,14 +905,7 @@ private function resolveOptionBooleanValue($optionName)
return false;
}

if (getenv('PHP_CS_FIXER_FUTURE_MODE')) {
throw new InvalidConfigurationException(sprintf('Expected "yes" or "no" for option "%s", got "%s". This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.', $optionName, $value));
}

@trigger_error(
sprintf('Expected "yes" or "no" for option "%s", other values are deprecated and support will be removed in 3.0. Got "%s", this implicitly set the option to "false".', $optionName, $value),
E_USER_DEPRECATED
);
Application::triggerDeprecation(sprintf('Expected "yes" or "no" for option "%s", other values are deprecated and support will be removed in 3.0. Got "%s", this implicitly set the option to "false".', $optionName, $value));

return false;
}
Expand Down
5 changes: 3 additions & 2 deletions 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 @@ -474,11 +475,11 @@ private function resolveOldConfig(array $configuration)
}
}

@trigger_error(sprintf(
Application::triggerDeprecation(sprintf(
'Given configuration is deprecated and will be removed in 3.0. Use configuration %s as replacement for %s.',
HelpCommand::toString($newConfig),
HelpCommand::toString($configuration)
), E_USER_DEPRECATED);
));

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 @@ -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 @@ -316,7 +317,7 @@ protected function createConfigurationDefinition()
->setNormalizer(static function (Options $options, $tokens) use ($that) {
foreach ($tokens as &$token) {
if ('useTrait' === $token) {
@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);
Application::triggerDeprecation("Token \"useTrait\" in option \"tokens\" for rule \"{$that->getName()}\" is deprecated and will be removed in 3.0, use \"use_trait\" instead.");

$token = 'use_trait';

Expand Down
10 changes: 3 additions & 7 deletions src/FixerConfiguration/FixerConfigurationResolverRootless.php
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 @@ -82,13 +84,7 @@ 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("{$message} This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
}

@trigger_error($message, E_USER_DEPRECATED);
Application::triggerDeprecation("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.");

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

0 comments on commit 11cd72a

Please sign in to comment.