diff --git a/README.md b/README.md index 553ba74..4703fe3 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,8 @@ $ phpmnd wordpress --ignore-numbers=2,-1 --ignore-funcs=round,sleep --exclude=te The ``--allow-array-mapping`` option allow keys as strings when using "array" extension. +The ``--colour`` option forces colour to be used in the output. ``--no-colour`` disables colour. The default is on unless output is not a TTY or running under CI + The ``--exclude-file`` option will exclude a file from the code analysis. Multiple values are allowed. The ``--exclude-path`` option will exclude a path, which must be relative to the source, from the code analysis. Multiple values are allowed. diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index 4a6a024..63b71d2 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -143,6 +143,13 @@ protected function configure(): void 'Link to a file containing filenames to search', '' ) + ->addOption( + 'colour', + null, + InputOption::VALUE_NEGATABLE, + 'Show colours in the output', + '' + ) ; } @@ -161,11 +168,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int $progressBar = new ProgressBar($output, $finder->count()); $progressBar->start(); } + $colourRequested = $input->getOption('colour'); + $outputDecorator = new Printer\Colour(); + + if ($colourRequested === false || ($colourRequested === '' && $this->shouldDefaultToPlain())) { + $outputDecorator = new Printer\Plain(); + } $hintList = new HintList(); $detector = new Detector($this->createOption($input), $hintList); $fileReportList = new FileReportList(); + + $printer = new Printer\Console($outputDecorator); + $whitelist = $this->getFileOption($input->getOption('whitelist')); foreach ($finder as $file) { @@ -304,4 +320,32 @@ private function getResourceUsage() // php-timer ^2.0||^3.0 return Timer::resourceUsage(); } + + /** + * Defaults on plain output when the output is not a tty OR + * running under CI. + */ + private function shouldDefaultToPlain() + { + $ciChecks = [ + 'CI', + 'BUILD_NUMBER', + 'RUN_ID', + ]; + + foreach ($ciChecks as $check) { + if (getenv($check)) { + return true; + } + } + + if (function_exists('stream_isatty') && !stream_isatty(STDOUT)) { + return true; + } + if (function_exists('posix_isatty') && !posix_isatty(STDOUT)) { + return true; + } + + return false; + } } diff --git a/src/Printer/Colour.php b/src/Printer/Colour.php new file mode 100644 index 0000000..2f3f604 --- /dev/null +++ b/src/Printer/Colour.php @@ -0,0 +1,21 @@ +highlighter = new Highlighter(new ConsoleColor()); + } + + public function getLine(string $fileContents, int $lineNumber): string + { + return $this->highlighter->getCodeSnippet($fileContents, $lineNumber, 0, 0); + } +} diff --git a/src/Printer/Console.php b/src/Printer/Console.php index 793d5a3..6718352 100644 --- a/src/Printer/Console.php +++ b/src/Printer/Console.php @@ -13,6 +13,12 @@ class Console implements Printer { private const DEFAULT_LINE_LENGTH = 80; + private $decorator; + + public function __construct(Decorator $decorator) + { + $this->decorator = $decorator; + } public function printData(OutputInterface $output, FileReportList $fileReportList, HintList $hintList): void { @@ -21,9 +27,12 @@ public function printData(OutputInterface $output, FileReportList $fileReportLis $output->writeln(PHP_EOL . $separator . PHP_EOL); $total = 0; + foreach ($fileReportList->getFileReports() as $fileReport) { $entries = $fileReport->getEntries(); $total += count($entries); + $contents = $fileReport->getFile()->getContents(); + $contents = str_replace(["\r\n", "\r"], "\n", $contents); foreach ($entries as $entry) { $output->writeln(sprintf( '%s:%d Magic number: %s', @@ -32,9 +41,8 @@ public function printData(OutputInterface $output, FileReportList $fileReportLis $entry['value'] )); - $highlighter = new Highlighter(new ConsoleColor()); $output->writeln( - $highlighter->getCodeSnippet($fileReport->getFile()->getContents(), $entry['line'], 0, 0) + $this->decorator->getLine($contents, $entry['line']) ); if ($hintList->hasHints()) { diff --git a/src/Printer/Decorator.php b/src/Printer/Decorator.php new file mode 100644 index 0000000..3e6ee23 --- /dev/null +++ b/src/Printer/Decorator.php @@ -0,0 +1,8 @@ + %d| %s'; + return sprintf( + $format, + $lineNumber, + explode("\n", $fileContents)[$lineNumber - 1] + ); + } +}