|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types=1); |
| 4 | +/* |
| 5 | + * This file is part of PHP CS Fixer. |
| 6 | + * |
| 7 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 8 | + * Dariusz Rumiński <dariusz.ruminski@gmail.com> |
| 9 | + * |
| 10 | + * This source file is subject to the MIT license that is bundled |
| 11 | + * with this source code in the file LICENSE. |
| 12 | + */ |
| 13 | +namespace PhpCsFixer\Console; |
| 14 | + |
| 15 | +use PhpCsFixer\Console\Command\CheckCommand; |
| 16 | +use PhpCsFixer\Console\Command\DescribeCommand; |
| 17 | +use PhpCsFixer\Console\Command\FixCommand; |
| 18 | +use PhpCsFixer\Console\Command\HelpCommand; |
| 19 | +use PhpCsFixer\Console\Command\InitCommand; |
| 20 | +use PhpCsFixer\Console\Command\ListFilesCommand; |
| 21 | +use PhpCsFixer\Console\Command\ListRulesCommand; |
| 22 | +use PhpCsFixer\Console\Command\ListSetsCommand; |
| 23 | +use PhpCsFixer\Console\Command\SelfUpdateCommand; |
| 24 | +use PhpCsFixer\Console\Command\WorkerCommand; |
| 25 | +use PhpCsFixer\Console\SelfUpdate\GithubClient; |
| 26 | +use PhpCsFixer\Console\SelfUpdate\NewVersionChecker; |
| 27 | +use PhpCsFixer\Future; |
| 28 | +use PhpCsFixer\PharChecker; |
| 29 | +use PhpCsFixer\Runner\Parallel\WorkerException; |
| 30 | +use PhpCsFixer\ToolInfo; |
| 31 | +use ECSPrefix202605\Symfony\Component\Console\Application as BaseApplication; |
| 32 | +use ECSPrefix202605\Symfony\Component\Console\Command\Command; |
| 33 | +use ECSPrefix202605\Symfony\Component\Console\Command\CompleteCommand; |
| 34 | +use ECSPrefix202605\Symfony\Component\Console\Command\DumpCompletionCommand; |
| 35 | +use ECSPrefix202605\Symfony\Component\Console\Command\ListCommand; |
| 36 | +use ECSPrefix202605\Symfony\Component\Console\Exception\CommandNotFoundException; |
| 37 | +use ECSPrefix202605\Symfony\Component\Console\Input\InputInterface; |
| 38 | +use ECSPrefix202605\Symfony\Component\Console\Output\ConsoleOutputInterface; |
| 39 | +use ECSPrefix202605\Symfony\Component\Console\Output\OutputInterface; |
| 40 | +/** |
| 41 | + * @author Fabien Potencier <fabien@symfony.com> |
| 42 | + * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> |
| 43 | + * |
| 44 | + * @internal |
| 45 | + * |
| 46 | + * @no-named-arguments Parameter names are not covered by the backward compatibility promise. |
| 47 | + */ |
| 48 | +final class Application extends BaseApplication |
| 49 | +{ |
| 50 | + public const NAME = 'PHP CS Fixer'; |
| 51 | + public const VERSION = '3.95.3'; |
| 52 | + public const VERSION_CODENAME = 'Adalbertus'; |
| 53 | + /** |
| 54 | + * @readonly |
| 55 | + * @var \PhpCsFixer\ToolInfo |
| 56 | + */ |
| 57 | + private $toolInfo; |
| 58 | + /** |
| 59 | + * @var \Symfony\Component\Console\Command\Command|null |
| 60 | + */ |
| 61 | + private $executedCommand; |
| 62 | + public function __construct() |
| 63 | + { |
| 64 | + parent::__construct(self::NAME, self::VERSION); |
| 65 | + $this->toolInfo = new ToolInfo(); |
| 66 | + // in alphabetical order |
| 67 | + $this->add(new CheckCommand($this->toolInfo)); |
| 68 | + $this->add(new DescribeCommand()); |
| 69 | + $this->add(new FixCommand($this->toolInfo)); |
| 70 | + $this->add(new InitCommand()); |
| 71 | + $this->add(new ListFilesCommand($this->toolInfo)); |
| 72 | + $this->add(new ListRulesCommand()); |
| 73 | + $this->add(new ListSetsCommand()); |
| 74 | + $this->add(new SelfUpdateCommand(new NewVersionChecker(new GithubClient()), $this->toolInfo, new PharChecker())); |
| 75 | + $this->add(new WorkerCommand($this->toolInfo)); |
| 76 | + } |
| 77 | + // polyfill for `add` method, as it is not available in Symfony 8.0 |
| 78 | + public function add(Command $command): ?Command |
| 79 | + { |
| 80 | + if (method_exists($this, 'addCommand')) { |
| 81 | + // @phpstan-ignore-line |
| 82 | + return $this->addCommand($command); |
| 83 | + } |
| 84 | + return parent::add($command); |
| 85 | + // @phpstan-ignore-line |
| 86 | + } |
| 87 | + public static function getMajorVersion(): int |
| 88 | + { |
| 89 | + return (int) explode('.', self::VERSION)[0]; |
| 90 | + } |
| 91 | + public function doRun(InputInterface $input, OutputInterface $output): int |
| 92 | + { |
| 93 | + $stdErr = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : ($input->hasParameterOption('--format', \true) && 'txt' !== $input->getParameterOption('--format', null, \true) ? null : $output); |
| 94 | + if (null !== $stdErr) { |
| 95 | + $warningsDetector = new \PhpCsFixer\Console\WarningsDetector($this->toolInfo); |
| 96 | + $warningsDetector->detectOldVendor(); |
| 97 | + $warningsDetector->detectOldMajor(); |
| 98 | + try { |
| 99 | + $commandName = $this->getCommandName($input); |
| 100 | + if (null === $commandName) { |
| 101 | + throw new CommandNotFoundException('No command name found.'); |
| 102 | + } |
| 103 | + $command = $this->find($commandName); |
| 104 | + if ($command instanceof CheckCommand || $command instanceof FixCommand) { |
| 105 | + $warningsDetector->detectHigherPhpVersion(); |
| 106 | + $warningsDetector->detectNonMonolithic(); |
| 107 | + } |
| 108 | + } catch (CommandNotFoundException $e) { |
| 109 | + // no-op |
| 110 | + } |
| 111 | + $warnings = $warningsDetector->getWarnings(); |
| 112 | + if (\count($warnings) > 0) { |
| 113 | + foreach ($warnings as $warning) { |
| 114 | + $stdErr->writeln(\sprintf($stdErr->isDecorated() ? '<bg=yellow;fg=black;>%s</>' : '%s', $warning)); |
| 115 | + } |
| 116 | + $stdErr->writeln(''); |
| 117 | + } |
| 118 | + } |
| 119 | + $result = parent::doRun($input, $output); |
| 120 | + if (null !== $stdErr && $output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
| 121 | + $triggeredDeprecations = Future::getTriggeredDeprecations(); |
| 122 | + if (\count($triggeredDeprecations) > 0) { |
| 123 | + $stdErr->writeln(''); |
| 124 | + $stdErr->writeln($stdErr->isDecorated() ? '<bg=yellow;fg=black;>Detected deprecations in use (they will stop working in next major release):</>' : 'Detected deprecations in use (they will stop working in next major release):'); |
| 125 | + foreach ($triggeredDeprecations as $deprecation) { |
| 126 | + $stdErr->writeln(\sprintf('- %s', $deprecation)); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + return $result; |
| 131 | + } |
| 132 | + /** |
| 133 | + * @internal |
| 134 | + */ |
| 135 | + public static function getAbout(bool $decorated = \false): string |
| 136 | + { |
| 137 | + $longVersion = \sprintf('%s <info>%s</info>', self::NAME, self::VERSION); |
| 138 | + // value of `$commitPlaceholderPossiblyEvaluated` will be changed during phar building, other value will not |
| 139 | + $commitPlaceholderPossiblyEvaluated = '@git-commit@'; |
| 140 | + $commitPlaceholder = implode('', ['@', 'git-commit@']); |
| 141 | + // do not replace with imploded value, as here we need to prevent phar builder to replace the placeholder |
| 142 | + $versionCommit = $commitPlaceholder !== $commitPlaceholderPossiblyEvaluated ? substr($commitPlaceholderPossiblyEvaluated, 0, 7) : ''; |
| 143 | + $about = implode('', [ |
| 144 | + $longVersion, |
| 145 | + $versionCommit ? \sprintf(' <info>(%s)</info>', $versionCommit) : '', |
| 146 | + // @phpstan-ignore-line to avoid `Ternary operator condition is always true|false.` |
| 147 | + self::VERSION_CODENAME ? \sprintf(' <info>%s</info>', self::VERSION_CODENAME) : '', |
| 148 | + // @phpstan-ignore-line to avoid `Ternary operator condition is always true|false.` |
| 149 | + ' by <comment>Fabien Potencier</comment>, <comment>Dariusz Ruminski</comment> and <comment>contributors</comment>.', |
| 150 | + ]); |
| 151 | + if (\false === $decorated) { |
| 152 | + return strip_tags($about); |
| 153 | + } |
| 154 | + return $about; |
| 155 | + } |
| 156 | + /** |
| 157 | + * @internal |
| 158 | + */ |
| 159 | + public static function getAboutWithRuntime(bool $decorated = \false): string |
| 160 | + { |
| 161 | + $about = self::getAbout(\true) . "\nPHP runtime: <info>" . \PHP_VERSION . '</info>'; |
| 162 | + if (\false === $decorated) { |
| 163 | + return strip_tags($about); |
| 164 | + } |
| 165 | + return $about; |
| 166 | + } |
| 167 | + public function getLongVersion(): string |
| 168 | + { |
| 169 | + return self::getAboutWithRuntime(\true); |
| 170 | + } |
| 171 | + protected function getDefaultCommands(): array |
| 172 | + { |
| 173 | + return [new HelpCommand(), new ListCommand(), new CompleteCommand(), new DumpCompletionCommand()]; |
| 174 | + } |
| 175 | + /** |
| 176 | + * @throws \Throwable |
| 177 | + */ |
| 178 | + protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output): int |
| 179 | + { |
| 180 | + $this->executedCommand = $command; |
| 181 | + return parent::doRunCommand($command, $input, $output); |
| 182 | + } |
| 183 | + protected function doRenderThrowable(\Throwable $e, OutputInterface $output): void |
| 184 | + { |
| 185 | + // Since parallel analysis utilises child processes, and they have their own output, |
| 186 | + // we need to capture the output of the child process to determine it there was an exception. |
| 187 | + // Default render format is not machine-friendly, so we need to override it for `worker` command, |
| 188 | + // in order to be able to easily parse exception data for further displaying on main process' side. |
| 189 | + if ($this->executedCommand instanceof WorkerCommand) { |
| 190 | + $output->writeln(WorkerCommand::ERROR_PREFIX . json_encode(['class' => \get_class($e), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), 'code' => $e->getCode(), 'trace' => $e->getTraceAsString()], 0)); |
| 191 | + return; |
| 192 | + } |
| 193 | + parent::doRenderThrowable($e, $output); |
| 194 | + if ($output->isVeryVerbose() && $e instanceof WorkerException) { |
| 195 | + $output->writeln('<comment>Original trace from worker:</comment>'); |
| 196 | + $output->writeln(''); |
| 197 | + $output->writeln($e->getOriginalTraceAsString()); |
| 198 | + $output->writeln(''); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments