/** * @param string $fileContent * @param int $lineNumber * @param int $linesBefore * @param int $linesAfter * @return string */ protected function getCodeSnippet($fileContent, $lineNumber, $linesBefore = 2, $linesAfter = 2) { if ($this->highlighter) { return $this->highlighter->getCodeSnippet($fileContent, $lineNumber, $linesBefore, $linesAfter); } else { return parent::getCodeSnippet($fileContent, $lineNumber, $linesBefore, $linesAfter); } }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $info = $this->fileInfo(); $num = $input->getOption('num'); $colors = new ConsoleColor(); $colors->addTheme('line_number', array('blue')); $highlighter = new Highlighter($colors); $contents = file_get_contents($info['file']); $output->page($highlighter->getCodeSnippet($contents, $info['line'], $num, $num), ShellOutput::OUTPUT_RAW); }
/** * Format the code represented by $reflector. * * @param \Reflector $reflector * * @return string formatted code */ public static function format(\Reflector $reflector) { if ($fileName = $reflector->getFileName()) { if (!is_file($fileName)) { throw new RuntimeException('Source code unavailable.'); } $file = file_get_contents($fileName); $start = $reflector->getStartLine(); $end = $reflector->getEndLine() - $start; $colors = new ConsoleColor(); $colors->addTheme('line_number', array('blue')); $highlighter = new Highlighter($colors); return $highlighter->getCodeSnippet($file, $start, 0, $end); // no need to escape this bad boy, since (for now) it's being output raw. // return OutputFormatter::escape(implode(PHP_EOL, $code)); return implode(PHP_EOL, $code); } else { throw new RuntimeException('Source code unavailable.'); } }
/** * Format the code represented by $reflector. * * @param \Reflector $reflector * @param null|string $colorMode (default: null) * * @return string formatted code */ public static function format(\Reflector $reflector, $colorMode = null) { $colorMode = $colorMode ?: Configuration::COLOR_MODE_AUTO; if ($fileName = $reflector->getFileName()) { if (!is_file($fileName)) { throw new RuntimeException('Source code unavailable.'); } $file = file_get_contents($fileName); $start = $reflector->getStartLine(); $end = $reflector->getEndLine() - $start; $factory = new ConsoleColorFactory($colorMode); $colors = $factory->getConsoleColor(); $highlighter = new Highlighter($colors); return $highlighter->getCodeSnippet($file, $start, 0, $end); // no need to escape this bad boy, since (for now) it's being output raw. // return OutputFormatter::escape(implode(PHP_EOL, $code)); return implode(PHP_EOL, $code); } else { throw new RuntimeException('Source code unavailable.'); } }
public function fixRemoteDir($remoteDir, OutputInterface $output, array $options = []) { $resolver = new OptionsResolver(); $resolver->setDefaults(['level' => 'symfony', 'dry-run' => true]); $options = $resolver->resolve($options); $this->getSshExec()->exec(strtr('php-cs-fixer fix %command_options% --level=%level% --no-interaction %dir%', ['%level%' => $options['level'], '%dir%' => $remoteDir, '%command_options%' => $options['dry-run'] ? '--dry-run --diff' : ''])); $returnStatus = $this->getSshExec()->getLastReturnStatus(); $this->getRemoteFilesystem()->mkdir($this->remotePhpcsStandardDir); $tmpDir = sprintf('%s/jarvis/phpcs_standard_dir', $this->cacheDir); $this->getLocalFilesystem()->mirror($this->localPhpcsStandardDir, $tmpDir); $this->getRemoteFilesystem()->syncLocalToRemote($tmpDir, $this->remotePhpcsStandardDir, ['delete' => true]); ob_start(); $this->getSshExec()->exec(strtr('phpcs %dir% --extensions=php --standard=%standard% --warning-severity=%warning-severity% --encoding=utf-8 --report=json', ['%dir%' => $remoteDir, '%standard%' => $this->remotePhpcsStandardDir, '%warning-severity%' => 0])); $jsonReport = ob_get_clean(); $json = new Json(); try { $data = $json->decode($jsonReport); } catch (\Seld\JsonLint\ParsingException $e) { $output->writeln(sprintf('<error>%s</error>', $jsonReport)); throw $e; } if ($data->totals->errors > 0) { $highlighter = new Highlighter(new ConsoleColor()); $output->writeln(sprintf('<error>%s errors detected</error>', $data->totals->errors)); foreach ($data->files as $filepath => $metadata) { if ($metadata->errors > 0) { foreach ($metadata->messages as $error) { $output->writeln(sprintf('<error>%s in "%s" at line %d</error>', strtr($error->message, [' use NULL() instead' => '']), strtr($filepath, [$remoteDir => '']), $error->line)); $fileContent = $this->getRemoteFilesystem()->getRemoteFileContent($filepath); $output->writeln($highlighter->getCodeSnippet($fileContent, $error->line), OutputInterface::OUTPUT_RAW); } } } $returnStatus += 1; } return $returnStatus; }
<?php use JakubOnderka\PhpConsoleColor\ConsoleColor; use JakubOnderka\PhpConsoleHighlighter\Highlighter; require __DIR__ . '/../vendor/autoload.php'; $highlighter = new Highlighter(new ConsoleColor()); $fileContent = file_get_contents(__FILE__); echo $highlighter->getCodeSnippet($fileContent, 3);
<?php use JakubOnderka\PhpConsoleColor\ConsoleColor; use JakubOnderka\PhpConsoleHighlighter\Highlighter; require __DIR__ . '/../vendor/autoload.php'; $highlighter = new Highlighter(new ConsoleColor()); $fileContent = file_get_contents(__FILE__); echo $highlighter->getWholeFile($fileContent);
<?php use JakubOnderka\PhpConsoleColor\ConsoleColor; use JakubOnderka\PhpConsoleHighlighter\Highlighter; require __DIR__ . '/../vendor/autoload.php'; $highlighter = new Highlighter(new ConsoleColor()); $fileContent = file_get_contents(__FILE__); echo $highlighter->getWholeFileWithLineNumbers($fileContent);
/** * @param string $filePath * @param int $lineNumber * @param int $linesBefore * @param int $linesAfter * @return string */ protected function getColoredCodeSnippet($filePath, $lineNumber, $linesBefore = 2, $linesAfter = 2) { if (!class_exists('\\JakubOnderka\\PhpConsoleHighlighter\\Highlighter') || !class_exists('\\JakubOnderka\\PhpConsoleColor\\ConsoleColor')) { return $this->getCodeSnippet($filePath, $lineNumber, $linesBefore, $linesAfter); } $colors = new ConsoleColor(); $colors->setForceStyle(true); $highlighter = new Highlighter($colors); $fileContent = file_get_contents($filePath); return $highlighter->getCodeSnippet($fileContent, $lineNumber, $linesBefore, $linesAfter); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $info = $this->fileInfo(); $num = $input->getOption('num'); $factory = new ConsoleColorFactory($this->colorMode); $colors = $factory->getConsoleColor(); $highlighter = new Highlighter($colors); $contents = file_get_contents($info['file']); $output->page($highlighter->getCodeSnippet($contents, $info['line'], $num, $num), ShellOutput::OUTPUT_RAW); }