Skip to content

Commit

Permalink
Add option to disable colour
Browse files Browse the repository at this point in the history
  • Loading branch information
exussum12 committed Oct 30, 2021
1 parent b4167c5 commit ddd68d6
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 22 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 15 additions & 19 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="./vendor/autoload.php">

<testsuites>
<testsuite name="PHPMND Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>src/</directory>
<exclude>
<directory>vendor/</directory>
</exclude>
</whitelist>
</filter>

</phpunit>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>src/</directory>
</include>
<exclude>
<directory>vendor/</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="PHPMND Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>
</phpunit>
43 changes: 42 additions & 1 deletion src/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
''
)
;
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
}
21 changes: 21 additions & 0 deletions src/Printer/Colour.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Povils\PHPMND\Printer;

use JakubOnderka\PhpConsoleColor\ConsoleColor;
use JakubOnderka\PhpConsoleHighlighter\Highlighter;

class Colour implements Decorator
{
private $highlighter;

public function __construct()
{
$this->highlighter = new Highlighter(new ConsoleColor());
}

public function getLine(string $fileContents, int $lineNumber): string
{
return $this->highlighter->getCodeSnippet($fileContents, $lineNumber, 0, 0);
}
}
12 changes: 10 additions & 2 deletions src/Printer/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,25 @@ 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
{
$separator = str_repeat('-', self::LINE_LENGTH);
$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',
Expand All @@ -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()) {
Expand Down
8 changes: 8 additions & 0 deletions src/Printer/Decorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
namespace Povils\PHPMND\Printer;

interface Decorator
{
public function getLine(string $fileContents, int $lineNumber): string;
}
17 changes: 17 additions & 0 deletions src/Printer/Plain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Povils\PHPMND\Printer;

class Plain implements Decorator
{

public function getLine(string $fileContents, int $lineNumber): string
{
$format = ' > %d| %s';
return sprintf(
$format,
$lineNumber,
explode("\n", $fileContents)[$lineNumber - 1]
);
}
}

0 comments on commit ddd68d6

Please sign in to comment.