Skip to content

Commit 13b5437

Browse files
authored
Support NO_COLOR env var (#181)
1 parent 7fb3340 commit 13b5437

6 files changed

+39
-16
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ Another approach for DIC-only usages is to scan the generated php file, but that
160160
### Scanning codebase located elsewhere:
161161
- This can be done by pointing `--composer-json` to `composer.json` of the other codebase
162162

163+
### Disable colored output:
164+
- Set `NO_COLOR` environment variable to disable colored output:
165+
```
166+
NO_COLOR=1 vendor/bin/composer-dependency-analyser
167+
```
168+
163169
## Limitations:
164170
- Extension dependencies are not analysed (e.g. `ext-json`)
165171

bin/composer-dependency-analyser

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ spl_autoload_register(static function (string $class) use ($psr4Prefix): void {
2727

2828
/** @var non-empty-string $cwd */
2929
$cwd = getcwd();
30+
$noColor = getenv('NO_COLOR') !== false;
3031

31-
$stdOutPrinter = new Printer(STDOUT);
32-
$stdErrPrinter = new Printer(STDERR);
32+
$stdOutPrinter = new Printer(STDOUT, $noColor);
33+
$stdErrPrinter = new Printer(STDERR, $noColor);
3334
$initializer = new Initializer($cwd, $stdOutPrinter, $stdErrPrinter);
3435
$stopwatch = new Stopwatch();
3536

src/Printer.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,18 @@ class Printer
2828
*/
2929
private $resource;
3030

31+
/**
32+
* @var bool
33+
*/
34+
private $noColor;
35+
3136
/**
3237
* @param resource $resource
3338
*/
34-
public function __construct($resource)
39+
public function __construct($resource, bool $noColor)
3540
{
3641
$this->resource = $resource;
42+
$this->noColor = $noColor;
3743
}
3844

3945
public function printLine(string $string): void
@@ -52,7 +58,11 @@ public function print(string $string): void
5258

5359
private function colorize(string $string): string
5460
{
55-
return str_replace(array_keys(self::COLORS), array_values(self::COLORS), $string);
61+
return str_replace(
62+
array_keys(self::COLORS),
63+
$this->noColor ? '' : array_values(self::COLORS),
64+
$string
65+
);
5666
}
5767

5868
}

tests/ConsoleFormatterTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public function testPrintResult(): void
4141

4242
OUT;
4343

44-
self::assertSame($this->normalizeEol($expectedNoIssuesOutput), $this->removeColors($noIssuesOutput));
45-
self::assertSame($this->normalizeEol($expectedNoIssuesButWarningsOutput), $this->removeColors($noIssuesButUnusedIgnores));
44+
self::assertSame($this->normalizeEol($expectedNoIssuesOutput), $noIssuesOutput);
45+
self::assertSame($this->normalizeEol($expectedNoIssuesButWarningsOutput), $noIssuesButUnusedIgnores);
4646

4747
$analysisResult = new AnalysisResult(
4848
10,
@@ -194,8 +194,8 @@ public function testPrintResult(): void
194194

195195
OUT;
196196

197-
self::assertSame($this->normalizeEol($expectedRegularOutput), $this->removeColors($regularOutput));
198-
self::assertSame($this->normalizeEol($expectedVerboseOutput), $this->removeColors($verboseOutput));
197+
self::assertSame($this->normalizeEol($expectedRegularOutput), $regularOutput);
198+
self::assertSame($this->normalizeEol($expectedVerboseOutput), $verboseOutput);
199199
// editorconfig-checker-enable
200200
}
201201

tests/FormatterTestCase.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PHPUnit\Framework\TestCase;
77
use ShipMonk\ComposerDependencyAnalyser\Result\ResultFormatter;
88
use function fopen;
9-
use function preg_replace;
109
use function str_replace;
1110
use function stream_get_contents;
1211

@@ -23,7 +22,7 @@ protected function getFormatterNormalizedOutput(Closure $closure): string
2322
$stream = fopen('php://memory', 'w');
2423
self::assertNotFalse($stream);
2524

26-
$printer = new Printer($stream);
25+
$printer = new Printer($stream, true);
2726
$formatter = $this->createFormatter($printer);
2827

2928
$closure($formatter);
@@ -35,9 +34,4 @@ protected function normalizeEol(string $string): string
3534
return str_replace("\r\n", "\n", $string);
3635
}
3736

38-
protected function removeColors(string $output): string
39-
{
40-
return (string) preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $output);
41-
}
42-
4337
}

tests/PrinterTest.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,24 @@ public function testPrintLine(): void
1515
$stream = fopen('php://memory', 'w');
1616
self::assertNotFalse($stream);
1717

18-
$printer = new Printer($stream);
18+
$printer = new Printer($stream, false);
1919

2020
$printer->printLine('Hello, <red>world</red>!');
2121
$printer->print('New line!');
2222

2323
self::assertSame("Hello, \033[31mworld\033[0m!" . PHP_EOL . 'New line!', stream_get_contents($stream, -1, 0));
2424
}
2525

26+
public function testPrintNoColor(): void
27+
{
28+
$stream = fopen('php://memory', 'w');
29+
self::assertNotFalse($stream);
30+
31+
$printer = new Printer($stream, true);
32+
33+
$printer->print('Hello, <red>world</red>!');
34+
35+
self::assertSame('Hello, world!', stream_get_contents($stream, -1, 0));
36+
}
37+
2638
}

0 commit comments

Comments
 (0)