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 error severity levels #68

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
17 changes: 15 additions & 2 deletions src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -29,6 +33,14 @@ public function getMessage()
return $this->message;
}

/**
* @return string
*/
public function getSeverity()
{
return $this->severity;
}

/**
* @return string
*/
Expand Down Expand Up @@ -63,7 +75,7 @@ public function getShortFilePath()
public function jsonSerialize()
{
return array(
'type' => 'error',
'type' => $this->getSeverity(),
'file' => $this->getFilePath(),
'message' => $this->getMessage(),
);
Expand Down Expand Up @@ -221,6 +233,7 @@ public function jsonSerialize()
'message' => $this->getMessage(),
'normalizeMessage' => $this->getNormalizedMessage(),
'blame' => $this->blame,
'severity' => $this->getSeverity(),
);
}
}
8 changes: 6 additions & 2 deletions src/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

Expand All @@ -439,8 +442,9 @@ public function writeResult(Result $result, ErrorFormatter $errorFormatter, $ign
foreach ($fileErrors as $fileError) {
$this->writer->write(
sprintf(
' <error line="%d" severity="ERROR" message="%s" source="%s" />',
' <error line="%d" severity="%s" message="%s" source="%s" />',
$fileError['line'],
$fileError['severity'],
$fileError['message'],
$fileError['source']
) .
Expand Down
4 changes: 2 additions & 2 deletions src/ParallelLint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions src/Process/LintProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,20 +67,23 @@ public function getSyntaxError()
// Look for fatal errors first
foreach (explode("\n", $this->getOutput()) as $line) {
if ($this->containsFatalError($line)) {
$this->severity = 'error';
return $line;
}
}

// Look for parser errors second
foreach (explode("\n", $this->getOutput()) as $line) {
if ($this->containsParserError($line)) {
$this->severity = 'error';
return $line;
}
}

// Look for deprecated errors third
foreach (explode("\n", $this->getOutput()) as $line) {
if ($this->containsDeprecatedError($line)) {
$this->severity = 'warning';
return $line;
}
}
Expand All @@ -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;
}
}
Spreeuw marked this conversation as resolved.
Show resolved Hide resolved

/**
* @return bool
* @throws RunTimeException
Expand Down