Skip to content

Commit 77601d4

Browse files
committed
Add option to disable colour
1 parent ae7fd90 commit 77601d4

File tree

6 files changed

+102
-2
lines changed

6 files changed

+102
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ $ phpmnd wordpress --ignore-numbers=2,-1 --ignore-funcs=round,sleep --exclude=te
9090

9191
The ``--allow-array-mapping`` option allow keys as strings when using "array" extension.
9292

93+
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
94+
9395
The ``--exclude-file`` option will exclude a file from the code analysis. Multiple values are allowed.
9496

9597
The ``--exclude-path`` option will exclude a path, which must be relative to the source, from the code analysis. Multiple values are allowed.

src/Command/RunCommand.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ protected function configure(): void
143143
'Link to a file containing filenames to search',
144144
''
145145
)
146+
->addOption(
147+
'colour',
148+
null,
149+
InputOption::VALUE_NEGATABLE,
150+
'Show colours in the output',
151+
''
152+
)
146153
;
147154
}
148155

@@ -161,11 +168,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
161168
$progressBar = new ProgressBar($output, $finder->count());
162169
$progressBar->start();
163170
}
171+
$colourRequested = $input->getOption('colour');
172+
$outputDecorator = new Printer\Colour();
173+
174+
if ($colourRequested === false || ($colourRequested === '' && $this->shouldDefaultToPlain())) {
175+
$outputDecorator = new Printer\Plain();
176+
}
164177

165178
$hintList = new HintList();
166179
$detector = new Detector($this->createOption($input), $hintList);
167180

168181
$fileReportList = new FileReportList();
182+
183+
$printer = new Printer\Console($outputDecorator);
184+
169185
$whitelist = $this->getFileOption($input->getOption('whitelist'));
170186

171187
foreach ($finder as $file) {
@@ -304,4 +320,32 @@ private function getResourceUsage()
304320
// php-timer ^2.0||^3.0
305321
return Timer::resourceUsage();
306322
}
323+
324+
/**
325+
* Defaults on plain output when the output is not a tty OR
326+
* running under CI.
327+
*/
328+
private function shouldDefaultToPlain()
329+
{
330+
$ciChecks = [
331+
'CI',
332+
'BUILD_NUMBER',
333+
'RUN_ID',
334+
];
335+
336+
foreach ($ciChecks as $check) {
337+
if (getenv($check)) {
338+
return true;
339+
}
340+
}
341+
342+
if (function_exists('stream_isatty') && !stream_isatty(STDOUT)) {
343+
return true;
344+
}
345+
if (function_exists('posix_isatty') && !posix_isatty(STDOUT)) {
346+
return true;
347+
}
348+
349+
return false;
350+
}
307351
}

src/Printer/Colour.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Povils\PHPMND\Printer;
4+
5+
use JakubOnderka\PhpConsoleColor\ConsoleColor;
6+
use JakubOnderka\PhpConsoleHighlighter\Highlighter;
7+
8+
class Colour implements Decorator
9+
{
10+
private $highlighter;
11+
12+
public function __construct()
13+
{
14+
$this->highlighter = new Highlighter(new ConsoleColor());
15+
}
16+
17+
public function getLine(string $fileContents, int $lineNumber): string
18+
{
19+
return $this->highlighter->getCodeSnippet($fileContents, $lineNumber, 0, 0);
20+
}
21+
}

src/Printer/Console.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
class Console implements Printer
1414
{
1515
private const DEFAULT_LINE_LENGTH = 80;
16+
private $decorator;
17+
18+
public function __construct(Decorator $decorator)
19+
{
20+
$this->decorator = $decorator;
21+
}
1622

1723
public function printData(OutputInterface $output, FileReportList $fileReportList, HintList $hintList): void
1824
{
@@ -21,9 +27,12 @@ public function printData(OutputInterface $output, FileReportList $fileReportLis
2127
$output->writeln(PHP_EOL . $separator . PHP_EOL);
2228

2329
$total = 0;
30+
2431
foreach ($fileReportList->getFileReports() as $fileReport) {
2532
$entries = $fileReport->getEntries();
2633
$total += count($entries);
34+
$contents = $fileReport->getFile()->getContents();
35+
$contents = str_replace(["\r\n", "\r"], "\n", $contents);
2736
foreach ($entries as $entry) {
2837
$output->writeln(sprintf(
2938
'%s:%d Magic number: %s',
@@ -32,9 +41,8 @@ public function printData(OutputInterface $output, FileReportList $fileReportLis
3241
$entry['value']
3342
));
3443

35-
$highlighter = new Highlighter(new ConsoleColor());
3644
$output->writeln(
37-
$highlighter->getCodeSnippet($fileReport->getFile()->getContents(), $entry['line'], 0, 0)
45+
$this->decorator->getLine($contents, $entry['line'])
3846
);
3947

4048
if ($hintList->hasHints()) {

src/Printer/Decorator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace Povils\PHPMND\Printer;
4+
5+
interface Decorator
6+
{
7+
public function getLine(string $fileContents, int $lineNumber): string;
8+
}

src/Printer/Plain.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Povils\PHPMND\Printer;
4+
5+
class Plain implements Decorator
6+
{
7+
8+
public function getLine(string $fileContents, int $lineNumber): string
9+
{
10+
$format = ' > %d| %s';
11+
return sprintf(
12+
$format,
13+
$lineNumber,
14+
explode("\n", $fileContents)[$lineNumber - 1]
15+
);
16+
}
17+
}

0 commit comments

Comments
 (0)