/** * @param OutputInterface $output * @param string $name */ private function describeRule(OutputInterface $output, $name) { $fixers = $this->getFixers(); if (!isset($fixers[$name])) { throw new DescribeNameNotFoundException($name, 'rule'); } /** @var FixerInterface $fixer */ $fixer = $fixers[$name]; if ($fixer instanceof DefinedFixerInterface) { $definition = $fixer->getDefinition(); } else { $definition = new ShortFixerDefinition('Description is not availble.'); } $output->writeln(sprintf('<info>Description of</info> %s <info>rule</info>.', $name)); $output->writeln($definition->getSummary()); if ($definition->getDescription()) { $output->writeln($definition->getDescription()); } $output->writeln(''); if ($fixer->isRisky()) { $output->writeln('<error>Fixer applying this rule is risky.</error>'); if ($definition->getRiskyDescription()) { $output->writeln($definition->getRiskyDescription()); } $output->writeln(''); } if ($fixer instanceof ConfigurableFixerInterface) { $output->writeln('<comment>Fixer is configurable.</comment>'); if ($definition->getConfigurationDescription()) { $output->writeln($definition->getConfigurationDescription()); } if ($definition->getDefaultConfiguration()) { $output->writeln(sprintf('Default configuration: <comment>%s</comment>.', $this->arrayToText($definition->getDefaultConfiguration()))); } $output->writeln(''); } if ($definition->getCodeSamples()) { $output->writeln('Fixing examples:'); $differ = new SebastianBergmannDiffer(); $diffFormatter = new DiffConsoleFormatter($output->isDecorated(), sprintf('<comment> ---------- begin diff ----------</comment>%s%%s%s<comment> ----------- end diff -----------</comment>', PHP_EOL, PHP_EOL)); foreach ($definition->getCodeSamples() as $index => $codeSample) { $old = $codeSample->getCode(); $tokens = Tokens::fromCode($old); if ($fixer instanceof ConfigurableFixerInterface) { $fixer->configure($codeSample->getConfiguration()); } $fixer->fix(new StdinFileInfo(), $tokens); $new = $tokens->generateCode(); $diff = $differ->diff($old, $new); if (null === $codeSample->getConfiguration()) { $output->writeln(sprintf(' * Example #%d.', $index + 1)); } else { $output->writeln(sprintf(' * Example #%d. Fixing with configuration: <comment>%s</comment>.', $index + 1, $this->arrayToText($codeSample->getConfiguration()))); } $output->writeln($diffFormatter->format($diff, ' %s')); $output->writeln(''); } } if ($definition instanceof ShortFixerDefinition) { $output->writeln(sprintf('<question>This rule is not yet described, do you want to help us and describe it?</question>')); $output->writeln('Contribute at <comment>https://github.com/FriendsOfPHP/PHP-CS-Fixer</comment> !'); $output->writeln(''); } }