Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UX: Display deprecations to end-user #5674

Merged
merged 1 commit into from May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/Console/Application.php
Expand Up @@ -22,6 +22,7 @@
use PhpCsFixer\Console\SelfUpdate\NewVersionChecker;
use PhpCsFixer\PharChecker;
use PhpCsFixer\ToolInfo;
use PhpCsFixer\Utils;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Command\ListCommand;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -83,16 +84,39 @@ public function doRun(InputInterface $input, OutputInterface $output)
? $output->getErrorOutput()
: ($input->hasParameterOption('--format', true) && 'txt' !== $input->getParameterOption('--format', null, true) ? null : $output)
;

if (null !== $stdErr) {
$warningsDetector = new WarningsDetector($this->toolInfo);
$warningsDetector->detectOldVendor();
$warningsDetector->detectOldMajor();
foreach ($warningsDetector->getWarnings() as $warning) {
$stdErr->writeln(sprintf($stdErr->isDecorated() ? '<bg=yellow;fg=black;>%s</>' : '%s', $warning));
$warnings = $warningsDetector->getWarnings();

if ($warnings) {
foreach ($warnings as $warning) {
$stdErr->writeln(sprintf($stdErr->isDecorated() ? '<bg=yellow;fg=black;>%s</>' : '%s', $warning));
}
$stdErr->writeln('');
}
}

$result = parent::doRun($input, $output);

if (
null !== $stdErr
&& $output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE
) {
$triggeredDeprecations = array_unique(Utils::getTriggeredDeprecations());
sort($triggeredDeprecations);
if ($triggeredDeprecations) {
$stdErr->writeln('');
$stdErr->writeln($stdErr->isDecorated() ? '<bg=yellow;fg=black;>Detected deprecations in use:</>' : 'Detected deprecations in use:');
foreach ($triggeredDeprecations as $deprecation) {
$stdErr->writeln(sprintf('- %s', $deprecation));
}
}
}

return parent::doRun($input, $output);
return $result;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Utils.php
Expand Up @@ -24,6 +24,8 @@
*/
final class Utils
{
private static $deprecations = [];

/**
* Calculate a bitmask for given constant names.
*
Expand Down Expand Up @@ -195,6 +197,12 @@ public static function triggerDeprecation($message, $exceptionClass = \RuntimeEx
throw new $exceptionClass("{$message} This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
}

self::$deprecations[] = $message;
@trigger_error($message, E_USER_DEPRECATED);
}

public static function getTriggeredDeprecations()
{
return self::$deprecations;
}
}