~ubuntu-branches/ubuntu/wily/phabricator/wily

« back to all changes in this revision

Viewing changes to src/lint/linter/ArcanistHLintLinter.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2014-11-01 23:20:06 UTC
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: package-import@ubuntu.com-20141101232006-mvlnp0cil67tsboe
Tags: upstream-0~git20141101/arcanist
Import upstream version 0~git20141101, component arcanist

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * Calls `hlint` and parses its results.
 
5
 */
 
6
final class ArcanistHLintLinter extends ArcanistExternalLinter {
 
7
 
 
8
  public function getInfoName() {
 
9
    return 'HLint';
 
10
  }
 
11
 
 
12
  public function getInfoURI() {
 
13
    return 'https://github.com/ndmitchell/hlint';
 
14
  }
 
15
 
 
16
  public function getInfoDescription() {
 
17
    return pht('HLint is a linter for Haskell code.');
 
18
  }
 
19
 
 
20
  public function getLinterName() {
 
21
    return 'HLINT';
 
22
  }
 
23
 
 
24
  public function getLinterConfigurationName() {
 
25
    return 'hlint';
 
26
  }
 
27
 
 
28
  public function getDefaultBinary() {
 
29
    return 'hlint';
 
30
  }
 
31
 
 
32
  public function getInstallInstructions() {
 
33
    return pht('Install hlint with `cabal install hlint`.');
 
34
  }
 
35
 
 
36
  public function supportsReadDataFromStdin() {
 
37
    return true;
 
38
  }
 
39
 
 
40
  public function getReadDataFromStdinFilename() {
 
41
    return '-';
 
42
  }
 
43
 
 
44
  public function shouldExpectCommandErrors() {
 
45
    return true;
 
46
  }
 
47
 
 
48
  public function getMandatoryFlags() {
 
49
    return array('--json');
 
50
  }
 
51
 
 
52
  public function getVersion() {
 
53
    list($stdout, $stderr) = execx(
 
54
      '%C --version', $this->getExecutableCommand());
 
55
 
 
56
    $matches = null;
 
57
    if (preg_match('@HLint v(.*),@', $stdout, $matches)) {
 
58
      return $matches[1];
 
59
    }
 
60
 
 
61
    return null;
 
62
  }
 
63
 
 
64
  protected function parseLinterOutput($path, $err, $stdout, $stderr) {
 
65
 
 
66
    $json = phutil_json_decode($stdout);
 
67
    $messages = array();
 
68
    foreach ($json as $fix) {
 
69
      if ($fix === null) {
 
70
        return;
 
71
      }
 
72
 
 
73
      $message = new ArcanistLintMessage();
 
74
      $message->setCode($this->getLinterName());
 
75
      $message->setPath($path);
 
76
      $message->setLine($fix['startLine']);
 
77
      $message->setChar($fix['startColumn']);
 
78
      $message->setName($fix['hint']);
 
79
      $message->setOriginalText($fix['from']);
 
80
      $message->setReplacementText($fix['to']);
 
81
 
 
82
      /* Some improvements may slightly change semantics, so attach
 
83
         all necessary notes too. */
 
84
      $notes = '';
 
85
      foreach ($fix['note'] as $note) {
 
86
        $notes .= ' **NOTE**: '.trim($note, '"').'.';
 
87
      }
 
88
 
 
89
      $message->setDescription(
 
90
        pht(
 
91
          'In module `%s`, declaration `%s`.%s',
 
92
          $fix['module'], $fix['decl'], $notes));
 
93
 
 
94
      switch ($fix['severity']) {
 
95
        case 'Error':
 
96
          $message->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR);
 
97
          break;
 
98
        case 'Warning':
 
99
          $message->setSeverity(ArcanistLintSeverity::SEVERITY_WARNING);
 
100
          break;
 
101
        default:
 
102
          $message->setSeverity(ArcanistLintSeverity::SEVERITY_ADVICE);
 
103
          break;
 
104
      }
 
105
 
 
106
      $messages[] = $message;
 
107
    }
 
108
 
 
109
    return $messages;
 
110
  }
 
111
}