Application.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Console;
  13. use PhpCsFixer\Console\Command\CheckCommand;
  14. use PhpCsFixer\Console\Command\DescribeCommand;
  15. use PhpCsFixer\Console\Command\FixCommand;
  16. use PhpCsFixer\Console\Command\HelpCommand;
  17. use PhpCsFixer\Console\Command\ListFilesCommand;
  18. use PhpCsFixer\Console\Command\ListSetsCommand;
  19. use PhpCsFixer\Console\Command\SelfUpdateCommand;
  20. use PhpCsFixer\Console\SelfUpdate\GithubClient;
  21. use PhpCsFixer\Console\SelfUpdate\NewVersionChecker;
  22. use PhpCsFixer\PharChecker;
  23. use PhpCsFixer\ToolInfo;
  24. use PhpCsFixer\Utils;
  25. use Symfony\Component\Console\Application as BaseApplication;
  26. use Symfony\Component\Console\Command\ListCommand;
  27. use Symfony\Component\Console\Input\InputInterface;
  28. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. /**
  31. * @author Fabien Potencier <fabien@symfony.com>
  32. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  33. *
  34. * @internal
  35. */
  36. final class Application extends BaseApplication
  37. {
  38. public const NAME = 'PHP CS Fixer';
  39. public const VERSION = '3.56.2';
  40. public const VERSION_CODENAME = '15 Keys Accelerate';
  41. private ToolInfo $toolInfo;
  42. public function __construct()
  43. {
  44. parent::__construct(self::NAME, self::VERSION);
  45. $this->toolInfo = new ToolInfo();
  46. // in alphabetical order
  47. $this->add(new DescribeCommand());
  48. $this->add(new CheckCommand($this->toolInfo));
  49. $this->add(new FixCommand($this->toolInfo));
  50. $this->add(new ListFilesCommand($this->toolInfo));
  51. $this->add(new ListSetsCommand());
  52. $this->add(new SelfUpdateCommand(
  53. new NewVersionChecker(new GithubClient()),
  54. $this->toolInfo,
  55. new PharChecker()
  56. ));
  57. }
  58. public static function getMajorVersion(): int
  59. {
  60. return (int) explode('.', self::VERSION)[0];
  61. }
  62. public function doRun(InputInterface $input, OutputInterface $output): int
  63. {
  64. $stdErr = $output instanceof ConsoleOutputInterface
  65. ? $output->getErrorOutput()
  66. : ($input->hasParameterOption('--format', true) && 'txt' !== $input->getParameterOption('--format', null, true) ? null : $output);
  67. if (null !== $stdErr) {
  68. $warningsDetector = new WarningsDetector($this->toolInfo);
  69. $warningsDetector->detectOldVendor();
  70. $warningsDetector->detectOldMajor();
  71. $warnings = $warningsDetector->getWarnings();
  72. if (\count($warnings) > 0) {
  73. foreach ($warnings as $warning) {
  74. $stdErr->writeln(sprintf($stdErr->isDecorated() ? '<bg=yellow;fg=black;>%s</>' : '%s', $warning));
  75. }
  76. $stdErr->writeln('');
  77. }
  78. }
  79. $result = parent::doRun($input, $output);
  80. if (
  81. null !== $stdErr
  82. && $output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE
  83. ) {
  84. $triggeredDeprecations = Utils::getTriggeredDeprecations();
  85. if (\count($triggeredDeprecations) > 0) {
  86. $stdErr->writeln('');
  87. $stdErr->writeln($stdErr->isDecorated() ? '<bg=yellow;fg=black;>Detected deprecations in use:</>' : 'Detected deprecations in use:');
  88. foreach ($triggeredDeprecations as $deprecation) {
  89. $stdErr->writeln(sprintf('- %s', $deprecation));
  90. }
  91. }
  92. }
  93. return $result;
  94. }
  95. /**
  96. * @internal
  97. */
  98. public static function getAbout(bool $decorated = false): string
  99. {
  100. $longVersion = sprintf('%s <info>%s</info>', self::NAME, self::VERSION);
  101. $commit = '@git-commit@';
  102. $versionCommit = '';
  103. if ('@'.'git-commit@' !== $commit) { /** @phpstan-ignore-line as `$commit` is replaced during phar building */
  104. $versionCommit = substr($commit, 0, 7);
  105. }
  106. $about = implode('', [
  107. $longVersion,
  108. $versionCommit ? sprintf(' <info>(%s)</info>', $versionCommit) : '', // @phpstan-ignore-line to avoid `Ternary operator condition is always true|false.`
  109. self::VERSION_CODENAME ? sprintf(' <info>%s</info>', self::VERSION_CODENAME) : '', // @phpstan-ignore-line to avoid `Ternary operator condition is always true|false.`
  110. ' by <comment>Fabien Potencier</comment>, <comment>Dariusz Ruminski</comment> and <comment>contributors</comment>.',
  111. ]);
  112. if (false === $decorated) {
  113. return strip_tags($about);
  114. }
  115. return $about;
  116. }
  117. /**
  118. * @internal
  119. */
  120. public static function getAboutWithRuntime(bool $decorated = false): string
  121. {
  122. $about = self::getAbout(true)."\nPHP runtime: <info>".PHP_VERSION.'</info>';
  123. if (false === $decorated) {
  124. return strip_tags($about);
  125. }
  126. return $about;
  127. }
  128. public function getLongVersion(): string
  129. {
  130. return self::getAboutWithRuntime(true);
  131. }
  132. protected function getDefaultCommands(): array
  133. {
  134. return [new HelpCommand(), new ListCommand()];
  135. }
  136. }