diff --git a/README.md b/README.md
index 3b6b78a..586dc68 100644
--- a/README.md
+++ b/README.md
@@ -89,6 +89,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/phpunit.xml.dist b/phpunit.xml.dist
index 84f09bc..86f98d9 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -1,20 +1,16 @@
-
-
-
-
-
- tests/
-
-
-
-
-
- src/
-
- vendor/
-
-
-
-
-
\ No newline at end of file
+
+
+
+ src/
+
+
+ vendor/
+
+
+
+
+ tests/
+
+
+
diff --git a/src/Console/Command.php b/src/Console/Command.php
index cc88557..955525a 100644
--- a/src/Console/Command.php
+++ b/src/Console/Command.php
@@ -144,6 +144,13 @@ protected function configure(): void
'Link to a file containing filenames to search',
''
)
+ ->addOption(
+ 'colour',
+ null,
+ InputOption::VALUE_NEGATABLE,
+ 'Show colours in the output',
+ ''
+ )
;
}
@@ -162,12 +169,18 @@ 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();
+ $printer = new Printer\Console($outputDecorator);
$whitelist = $this->getFileOption($input->getOption('whitelist'));
foreach ($finder as $file) {
@@ -304,4 +317,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 2e2c95b..2f0076f 100644
--- a/src/Printer/Console.php
+++ b/src/Printer/Console.php
@@ -17,6 +17,12 @@ class Console implements Printer
{
const LINE_LENGTH = 80;
const TAB = 4;
+ private $decorator;
+
+ public function __construct(Decorator $decorator)
+ {
+ $this->decorator = $decorator;
+ }
public function printData(OutputInterface $output, FileReportList $fileReportList, HintList $hintList): void
{
@@ -24,9 +30,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',
@@ -35,9 +44,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]
+ );
+ }
+}