]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Minor improvements to `ArcanistPhpLinter`
authorJoshua Spence <josh@joshuaspence.com>
Thu, 8 Jan 2015 08:07:06 +0000 (19:07 +1100)
committerJoshua Spence <josh@joshuaspence.com>
Thu, 8 Jan 2015 08:40:37 +0000 (19:40 +1100)
Summary: Various minor improvements to `ArcanistPhpLinter`.

Test Plan: `arc unit`

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin, epriestley

Differential Revision: https://secure.phabricator.com/D11265

src/lint/linter/ArcanistPhpLinter.php

index 789e2e8db92cacbe2417256c0de676df0c672ba2..438cfe6e852b37c5c508d34911b272d73cab4aac 100644 (file)
@@ -5,6 +5,9 @@
  */
 final class ArcanistPhpLinter extends ArcanistExternalLinter {
 
+  const LINT_PARSE_ERROR  = 1;
+  const LINT_FATAL_ERROR  = 2;
+
   public function getInfoName() {
     return 'php -l';
   }
@@ -14,8 +17,7 @@ final class ArcanistPhpLinter extends ArcanistExternalLinter {
   }
 
   public function getInfoDescription() {
-    return pht(
-      'Checks for syntax errors in php files.');
+    return pht('Checks for syntax errors in PHP files.');
   }
 
   public function getLinterName() {
@@ -26,6 +28,13 @@ final class ArcanistPhpLinter extends ArcanistExternalLinter {
     return 'php';
   }
 
+  public function getLintNameMap() {
+    return array(
+      self::LINT_PARSE_ERROR  => pht('Parse Error'),
+      self::LINT_FATAL_ERROR  => pht('Fatal Error'),
+    );
+  }
+
   protected function getMandatoryFlags() {
     return array('-l');
   }
@@ -54,28 +63,47 @@ final class ArcanistPhpLinter extends ArcanistExternalLinter {
     return false;
   }
 
+  protected function canCustomizeLintSeverities() {
+    return false;
+  }
+
   protected function parseLinterOutput($path, $err, $stdout, $stderr) {
-    // Older versions of php had both on $stdout, newer ones split it
-    // Combine $stdout and $stderr for consistency
+    // Older versions of PHP had both on stdout, newer ones split it.
+    // Combine stdout and stderr for consistency.
     $stdout = $stderr."\n".$stdout;
     $matches = array();
-    $regex = '/^(?<type>.+?) error:\s+(?<error>.*?)\s+in\s+(?<file>.*?)'.
-      '\s+on line\s+(?<line>\d*)$/m';
+
+    $regex = '/^(PHP )?(?<type>.+) error: +(?<error>.+) in (?<file>.+) '.
+      'on line (?<line>\d+)$/m';
     if (preg_match($regex, $stdout, $matches)) {
-      $type = strtolower($matches['type']);
-      $message = new ArcanistLintMessage();
-      $message->setPath($matches['file']);
-      $message->setLine($matches['line']);
-      $message->setCode('php.'.$type);
-      $message->setDescription('This file contains a '.$type.' error: '.
-        $matches['error'].' on line '.$matches['line']);
-      $message->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR);
-
-      // php -l only returns the first error
+      $code = $this->getLintCodeFromLinterConfigurationKey($matches['type']);
+
+      $message = id(new ArcanistLintMessage())
+        ->setPath($path)
+        ->setLine($matches['line'])
+        ->setCode($this->getLinterName().$code)
+        ->setName($this->getLintMessageName($code))
+        ->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR)
+        ->setDescription($matches['error']);
+
+      // `php -l` only returns the first error.
       return array($message);
     }
 
     return array();
   }
 
+  protected function getLintCodeFromLinterConfigurationKey($code) {
+    switch (phutil_utf8_strtolower($code)) {
+      case 'parse':
+        return self::LINT_PARSE_ERROR;
+
+      case 'fatal':
+        return self::LINT_FATAL_ERROR;
+
+      default:
+        throw new Exception(pht('Unrecognized lint message code "%s"', $code));
+    }
+  }
+
 }