Skip to content

Commit ddd68d6

Browse files
committed
Add option to disable colour
1 parent b4167c5 commit ddd68d6

7 files changed

Lines changed: 115 additions & 22 deletions

File tree

README.md

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

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

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

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

phpunit.xml.dist

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
3-
<phpunit bootstrap="./vendor/autoload.php">
4-
5-
<testsuites>
6-
<testsuite name="PHPMND Test Suite">
7-
<directory>tests/</directory>
8-
</testsuite>
9-
</testsuites>
10-
11-
<filter>
12-
<whitelist>
13-
<directory>src/</directory>
14-
<exclude>
15-
<directory>vendor/</directory>
16-
</exclude>
17-
</whitelist>
18-
</filter>
19-
20-
</phpunit>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory>src/</directory>
6+
</include>
7+
<exclude>
8+
<directory>vendor/</directory>
9+
</exclude>
10+
</coverage>
11+
<testsuites>
12+
<testsuite name="PHPMND Test Suite">
13+
<directory>tests/</directory>
14+
</testsuite>
15+
</testsuites>
16+
</phpunit>

src/Console/Command.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ protected function configure(): void
144144
'Link to a file containing filenames to search',
145145
''
146146
)
147+
->addOption(
148+
'colour',
149+
null,
150+
InputOption::VALUE_NEGATABLE,
151+
'Show colours in the output',
152+
''
153+
)
147154
;
148155
}
149156

@@ -162,12 +169,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
162169
$progressBar = new ProgressBar($output, $finder->count());
163170
$progressBar->start();
164171
}
172+
$colourRequested = $input->getOption('colour');
173+
$outputDecorator = new Printer\Colour();
174+
175+
if ($colourRequested === false || ($colourRequested === '' && $this->shouldDefaultToPlain())) {
176+
$outputDecorator = new Printer\Plain();
177+
}
165178

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

169182
$fileReportList = new FileReportList();
170-
$printer = new Printer\Console();
183+
$printer = new Printer\Console($outputDecorator);
171184
$whitelist = $this->getFileOption($input->getOption('whitelist'));
172185

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

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
@@ -17,16 +17,25 @@ class Console implements Printer
1717
{
1818
const LINE_LENGTH = 80;
1919
const TAB = 4;
20+
private $decorator;
21+
22+
public function __construct(Decorator $decorator)
23+
{
24+
$this->decorator = $decorator;
25+
}
2026

2127
public function printData(OutputInterface $output, FileReportList $fileReportList, HintList $hintList): void
2228
{
2329
$separator = str_repeat('-', self::LINE_LENGTH);
2430
$output->writeln(PHP_EOL . $separator . PHP_EOL);
2531

2632
$total = 0;
33+
2734
foreach ($fileReportList->getFileReports() as $fileReport) {
2835
$entries = $fileReport->getEntries();
2936
$total += count($entries);
37+
$contents = $fileReport->getFile()->getContents();
38+
$contents = str_replace(["\r\n", "\r"], "\n", $contents);
3039
foreach ($entries as $entry) {
3140
$output->writeln(sprintf(
3241
'%s:%d Magic number: %s',
@@ -35,9 +44,8 @@ public function printData(OutputInterface $output, FileReportList $fileReportLis
3544
$entry['value']
3645
));
3746

38-
$highlighter = new Highlighter(new ConsoleColor());
3947
$output->writeln(
40-
$highlighter->getCodeSnippet($fileReport->getFile()->getContents(), $entry['line'], 0, 0)
48+
$this->decorator->getLine($contents, $entry['line'])
4149
);
4250

4351
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)