Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to disable colour #146

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,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
44 changes: 44 additions & 0 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,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 @@ -161,11 +168,20 @@ 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($outputDecorator);

$whitelist = $this->getFileOption($input->getOption('whitelist'));

foreach ($finder as $file) {
Expand Down Expand Up @@ -304,4 +320,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 @@ -13,6 +13,12 @@
class Console implements Printer
{
private const DEFAULT_LINE_LENGTH = 80;
private $decorator;

public function __construct(Decorator $decorator)
{
$this->decorator = $decorator;
}

public function printData(OutputInterface $output, FileReportList $fileReportList, HintList $hintList): void
{
Expand All @@ -21,9 +27,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',
Expand All @@ -32,9 +41,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]
);
}
}