Skip to content

Commit

Permalink
Added support for Github output
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm committed Apr 26, 2021
1 parent 13d640c commit 0282b66
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
15 changes: 15 additions & 0 deletions lib/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
class Configuration
{
public const THEME_DEFAULT = 'default';
public const OUTPUT_FORMAT_CONSOLE = 'console';
public const OUTPUT_FORMAT_GITHUB = 'github';

/** @var string */
private $cacheDir;
Expand All @@ -52,6 +54,9 @@ class Configuration

/** @var bool */
private $warningsAsError = false;

/** @var string */
private $outputFormat = self::OUTPUT_FORMAT_CONSOLE;

/** @var bool */
private $ignoreInvalidReferences = false;
Expand Down Expand Up @@ -227,6 +232,16 @@ public function isWarningsAsError(): bool
return $this->warningsAsError;
}

public function getOutputFormat(): string
{
return $this->outputFormat;
}

public function setOutputFormat(string $outputFormat): void
{
$this->outputFormat = $outputFormat;
}

public function getIgnoreInvalidReferences(): bool
{
return $this->ignoreInvalidReferences;
Expand Down
6 changes: 5 additions & 1 deletion lib/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(string $message, ?string $file = null, ?int $line =
public function asString(): string
{
$output = $this->message;
if ($this->file !== null && $this->file !== '') {
if ($this->getFile() !== null) {
$output .= sprintf(' in "%s"', $this->file);

if ($this->line !== null) {
Expand All @@ -51,6 +51,10 @@ public function getMessage(): string

public function getFile(): ?string
{
if ('' === $this->file) {
return null;
}

return $this->file;
}

Expand Down
25 changes: 23 additions & 2 deletions lib/ErrorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ public function addError(string $message, ?string $file = null, ?int $line = nul
$this->errors[] = $error = new Error($message, $file, $line, $throwable);

if (! $this->configuration->isSilentOnError()) {
echo '⚠️ ' . $error->__toString() . "\n";
if ($this->configuration->getOutputFormat() === Configuration::OUTPUT_FORMAT_GITHUB) {
$file = $error->getFile();
echo sprintf(
'::error %s%s::%s',
$file !== null ? 'file='.$file : '',
$file !== null && $error->getLine() !== null ? ',linefile='.$error->getLine() : '',
$error->getMessage()
);
} else {
echo '⚠️ ' . $error->__toString() . "\n";
}
}

if ($this->configuration->isAbortOnError()) {
Expand All @@ -45,7 +55,18 @@ public function addWarning(string $message, ?string $file = null, ?int $line = n
return;
}

echo (new Error($message, $file, $line, $throwable))->asString() . "\n";
$error = new Error($message, $file, $line, $throwable);
if ($this->configuration->getOutputFormat() === Configuration::OUTPUT_FORMAT_GITHUB) {
$file = $error->getFile();
echo sprintf(
'::warning %s%s::%s',
$file !== null ? 'file='.$file : '',
$file !== null && $error->getLine() !== null ? ',linefile='.$error->getLine() : '',
$error->getMessage()
);
} else {
echo ($error)->asString() . "\n";
}
}

/**
Expand Down

0 comments on commit 0282b66

Please sign in to comment.