From f177dadbbba20b90bdba7237e5968954e8b2f6e7 Mon Sep 17 00:00:00 2001 From: Ewout Fernhout Date: Mon, 23 Aug 2021 21:50:56 +0200 Subject: [PATCH 1/3] add error severity levels --- src/Error.php | 17 +++++++++++++++-- src/Output.php | 8 ++++++-- src/ParallelLint.php | 4 ++-- src/Process/LintProcess.php | 21 +++++++++++++++++++++ 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/Error.php b/src/Error.php index 0c9f65f..296c13c 100644 --- a/src/Error.php +++ b/src/Error.php @@ -11,14 +11,18 @@ class Error implements \JsonSerializable /** @var string */ protected $message; + /** @var string */ + protected $severity; + /** * @param string $filePath * @param string $message */ - public function __construct($filePath, $message) + public function __construct($filePath, $message, $severity = 'error') { $this->filePath = $filePath; $this->message = rtrim($message); + $this->severity = $severity; } /** @@ -29,6 +33,14 @@ public function getMessage() return $this->message; } + /** + * @return string + */ + public function getSeverity() + { + return $this->severity; + } + /** * @return string */ @@ -63,7 +75,7 @@ public function getShortFilePath() public function jsonSerialize() { return array( - 'type' => 'error', + 'type' => $this->getSeverity(), 'file' => $this->getFilePath(), 'message' => $this->getMessage(), ); @@ -221,6 +233,7 @@ public function jsonSerialize() 'message' => $this->getMessage(), 'normalizeMessage' => $this->getNormalizedMessage(), 'blame' => $this->blame, + 'severity' => $this->getSeverity(), ); } } \ No newline at end of file diff --git a/src/Output.php b/src/Output.php index 974dde8..0539437 100644 --- a/src/Output.php +++ b/src/Output.php @@ -422,15 +422,18 @@ public function writeResult(Result $result, ErrorFormatter $errorFormatter, $ign if ($error instanceof SyntaxError) { $line = $error->getLine(); $source = "Syntax Error"; + $severity = $error->getSeverity(); } else { $line = 1; $source = "Linter Error"; + $severity = 'error'; } $errors[$error->getShortFilePath()][] = array( 'message' => $message, 'line' => $line, - 'source' => $source + 'source' => $source, + 'severity' => $severity, ); } @@ -439,8 +442,9 @@ public function writeResult(Result $result, ErrorFormatter $errorFormatter, $ign foreach ($fileErrors as $fileError) { $this->writer->write( sprintf( - ' ', + ' ', $fileError['line'], + $fileError['severity'], $fileError['message'], $fileError['source'] ) . diff --git a/src/ParallelLint.php b/src/ParallelLint.php index b1825f3..7b7ea89 100644 --- a/src/ParallelLint.php +++ b/src/ParallelLint.php @@ -96,7 +96,7 @@ public function lint(array $files) } else if ($process->containsError()) { $checkedFiles[] = $file; - $errors[] = $this->triggerSyntaxErrorCallback(new SyntaxError($file, $process->getSyntaxError())); + $errors[] = $this->triggerSyntaxErrorCallback(new SyntaxError($file, $process->getSyntaxError(), $process->getSeverity())); $processCallback(self::STATUS_ERROR, $file); } else if ($process->isSuccess()) { @@ -135,7 +135,7 @@ public function lint(array $files) } else if ($process->containsError()) { $checkedFiles[] = $file; - $errors[] = $this->triggerSyntaxErrorCallback(new SyntaxError($file, $process->getSyntaxError())); + $errors[] = $this->triggerSyntaxErrorCallback(new SyntaxError($file, $process->getSyntaxError(), $process->getSeverity())); $processCallback(self::STATUS_ERROR, $file); } else { diff --git a/src/Process/LintProcess.php b/src/Process/LintProcess.php index e2e6b2d..e06b0bd 100644 --- a/src/Process/LintProcess.php +++ b/src/Process/LintProcess.php @@ -14,6 +14,11 @@ class LintProcess extends PhpProcess */ private $showDeprecatedErrors; + /** + * @var string + */ + protected $severity; + /** * @param PhpExecutable $phpExecutable * @param string $fileToCheck Path to file to check @@ -62,6 +67,7 @@ public function getSyntaxError() // Look for fatal errors first foreach (explode("\n", $this->getOutput()) as $line) { if ($this->containsFatalError($line)) { + $this->severity = 'error'; return $line; } } @@ -69,6 +75,7 @@ public function getSyntaxError() // Look for parser errors second foreach (explode("\n", $this->getOutput()) as $line) { if ($this->containsParserError($line)) { + $this->severity = 'error'; return $line; } } @@ -76,6 +83,7 @@ public function getSyntaxError() // Look for deprecated errors third foreach (explode("\n", $this->getOutput()) as $line) { if ($this->containsDeprecatedError($line)) { + $this->severity = 'warning'; return $line; } } @@ -86,6 +94,19 @@ public function getSyntaxError() return false; } + /** + * @return string + */ + public function getSeverity() + { + if (!empty($this->severity)) { + return $this->severity; + } else { + $error = $this->getSyntaxError(); + return '$this->severity'; + } + } + /** * @return bool * @throws RunTimeException From 4f2d28a7b72b3e1c15a8632ea0fe9e9f77d7833c Mon Sep 17 00:00:00 2001 From: Ewout Fernhout Date: Mon, 1 Nov 2021 23:09:53 +0100 Subject: [PATCH 2/3] fix: unintended variable quotation --- src/Process/LintProcess.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Process/LintProcess.php b/src/Process/LintProcess.php index e06b0bd..1281101 100644 --- a/src/Process/LintProcess.php +++ b/src/Process/LintProcess.php @@ -103,7 +103,7 @@ public function getSeverity() return $this->severity; } else { $error = $this->getSyntaxError(); - return '$this->severity'; + return $this->severity; } } From a516e6702e5bfea8dc743fc57c5cb841429d4d59 Mon Sep 17 00:00:00 2001 From: Ewout Fernhout Date: Tue, 2 Nov 2021 21:16:35 +0100 Subject: [PATCH 3/3] Update src/Process/LintProcess.php Co-authored-by: Fabien Villepinte --- src/Process/LintProcess.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Process/LintProcess.php b/src/Process/LintProcess.php index 1281101..55c439f 100644 --- a/src/Process/LintProcess.php +++ b/src/Process/LintProcess.php @@ -96,15 +96,15 @@ public function getSyntaxError() /** * @return string + * @throws RunTimeException */ public function getSeverity() { - if (!empty($this->severity)) { - return $this->severity; - } else { - $error = $this->getSyntaxError(); - return $this->severity; + if (empty($this->severity)) { + $this->getSyntaxError(); } + + return $this->severity; } /**