[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/differential/customfield/ -> DifferentialLintField.php (source)

   1  <?php
   2  
   3  final class DifferentialLintField
   4    extends DifferentialCustomField {
   5  
   6    public function getFieldKey() {
   7      return 'differential:lint';
   8    }
   9  
  10    public function getFieldName() {
  11      return pht('Lint');
  12    }
  13  
  14    public function getFieldDescription() {
  15      return pht('Shows lint results.');
  16    }
  17  
  18    public function shouldAppearInPropertyView() {
  19      return true;
  20    }
  21  
  22    public function renderPropertyViewLabel() {
  23      return $this->getFieldName();
  24    }
  25  
  26    public function getRequiredDiffPropertiesForRevisionView() {
  27      return array(
  28        'arc:lint',
  29        'arc:lint-excuse',
  30        'arc:lint-postponed',
  31      );
  32    }
  33  
  34    public function renderPropertyViewValue(array $handles) {
  35      $diff = $this->getObject()->getActiveDiff();
  36  
  37      $path_changesets = mpull($diff->loadChangesets(), 'getID', 'getFilename');
  38  
  39      $lstar = DifferentialRevisionUpdateHistoryView::renderDiffLintStar($diff);
  40      $lmsg = DifferentialRevisionUpdateHistoryView::getDiffLintMessage($diff);
  41      $ldata = $diff->getProperty('arc:lint');
  42      $ltail = null;
  43  
  44      $rows = array();
  45  
  46      $rows[] = array(
  47        'style'     => 'star',
  48        'name'      => $lstar,
  49        'value'     => $lmsg,
  50        'show'      => true,
  51      );
  52  
  53      $excuse = $diff->getProperty('arc:lint-excuse');
  54      if ($excuse) {
  55        $rows[] = array(
  56          'style'   => 'excuse',
  57          'name'    => 'Excuse',
  58          'value'   => phutil_escape_html_newlines($excuse),
  59          'show'    => true,
  60        );
  61      }
  62  
  63      $show_limit = 10;
  64      $hidden = array();
  65  
  66      if ($ldata) {
  67        $ldata = igroup($ldata, 'path');
  68        foreach ($ldata as $path => $messages) {
  69  
  70          $rows[] = array(
  71            'style' => 'section',
  72            'name'  => $path,
  73            'show'  => $show_limit,
  74          );
  75  
  76          foreach ($messages as $message) {
  77            $path = idx($message, 'path');
  78            $line = idx($message, 'line');
  79  
  80            $code = idx($message, 'code');
  81            $severity = idx($message, 'severity');
  82  
  83            $name = idx($message, 'name');
  84            $description = idx($message, 'description');
  85  
  86            $line_link = 'line '.intval($line);
  87            if (isset($path_changesets[$path])) {
  88              $href = '#C'.$path_changesets[$path].'NL'.max(1, $line);
  89  
  90              // TODO: We are always showing the active diff
  91              // if ($diff->getID() != $this->getDiff()->getID()) {
  92              //  $href = '/D'.$diff->getRevisionID().'?id='.$diff->getID().$href;
  93              // }
  94  
  95              $line_link = phutil_tag(
  96                'a',
  97                array(
  98                  'href' => $href,
  99                ),
 100                $line_link);
 101            }
 102  
 103            if ($show_limit) {
 104              --$show_limit;
 105              $show = true;
 106            } else {
 107              $show = false;
 108              if (empty($hidden[$severity])) {
 109                $hidden[$severity] = 0;
 110              }
 111              $hidden[$severity]++;
 112            }
 113  
 114            $rows[] = array(
 115              'style' => $this->getSeverityStyle($severity),
 116              'name'  => ucwords($severity),
 117              'value' => hsprintf(
 118                '(%s) %s at %s',
 119                $code,
 120                $name,
 121                $line_link),
 122              'show'  => $show,
 123            );
 124  
 125            if (!empty($message['locations'])) {
 126              $locations = array();
 127              foreach ($message['locations'] as $location) {
 128                $other_line = idx($location, 'line');
 129                $locations[] =
 130                  idx($location, 'path', $path).
 131                  ($other_line ? ":{$other_line}" : '');
 132              }
 133              $description .= "\nOther locations: ".implode(', ', $locations);
 134            }
 135  
 136            if (strlen($description)) {
 137              $rows[] = array(
 138                'style' => 'details',
 139                'value' => phutil_escape_html_newlines($description),
 140                'show'  => false,
 141              );
 142              if (empty($hidden['details'])) {
 143                $hidden['details'] = 0;
 144              }
 145              $hidden['details']++;
 146            }
 147          }
 148        }
 149      }
 150  
 151      $postponed = $diff->getProperty('arc:lint-postponed');
 152      if ($postponed) {
 153        foreach ($postponed as $linter) {
 154          $rows[] = array(
 155            'style' => $this->getPostponedStyle(),
 156            'name' => 'Postponed',
 157            'value' => $linter,
 158            'show'  => false,
 159            );
 160          if (empty($hidden['postponed'])) {
 161            $hidden['postponed'] = 0;
 162          }
 163          $hidden['postponed']++;
 164        }
 165      }
 166  
 167      $show_string = $this->renderShowString($hidden);
 168  
 169      $view = new DifferentialResultsTableView();
 170      $view->setRows($rows);
 171      $view->setShowMoreString($show_string);
 172  
 173      return $view->render();
 174    }
 175  
 176    private function getSeverityStyle($severity) {
 177      $map = array(
 178        ArcanistLintSeverity::SEVERITY_ERROR      => 'red',
 179        ArcanistLintSeverity::SEVERITY_WARNING    => 'yellow',
 180        ArcanistLintSeverity::SEVERITY_AUTOFIX    => 'yellow',
 181        ArcanistLintSeverity::SEVERITY_ADVICE     => 'yellow',
 182      );
 183      return idx($map, $severity);
 184    }
 185  
 186    private function getPostponedStyle() {
 187      return 'blue';
 188    }
 189  
 190    private function renderShowString(array $hidden) {
 191      if (!$hidden) {
 192        return null;
 193      }
 194  
 195      // Reorder hidden things by severity.
 196      $hidden = array_select_keys(
 197        $hidden,
 198        array(
 199          ArcanistLintSeverity::SEVERITY_ERROR,
 200          ArcanistLintSeverity::SEVERITY_WARNING,
 201          ArcanistLintSeverity::SEVERITY_AUTOFIX,
 202          ArcanistLintSeverity::SEVERITY_ADVICE,
 203          'details',
 204          'postponed',
 205        )) + $hidden;
 206  
 207      $show = array();
 208      foreach ($hidden as $key => $value) {
 209        switch ($key) {
 210          case ArcanistLintSeverity::SEVERITY_ERROR:
 211            $show[] = pht('%d Error(s)', $value);
 212            break;
 213          case ArcanistLintSeverity::SEVERITY_WARNING:
 214            $show[] = pht('%d Warning(s)', $value);
 215            break;
 216          case ArcanistLintSeverity::SEVERITY_AUTOFIX:
 217            $show[] = pht('%d Auto-Fix(es)', $value);
 218            break;
 219          case ArcanistLintSeverity::SEVERITY_ADVICE:
 220            $show[] = pht('%d Advice(s)', $value);
 221            break;
 222          case 'details':
 223            $show[] = pht('%d Detail(s)', $value);
 224            break;
 225          case 'postponed':
 226            $show[] = pht('%d Postponed', $value);
 227            break;
 228          default:
 229            $show[] = $value;
 230            break;
 231        }
 232      }
 233  
 234      return 'Show Full Lint Results ('.implode(', ', $show).')';
 235    }
 236  
 237    public function getWarningsForDetailView() {
 238      $status = $this->getObject()->getActiveDiff()->getLintStatus();
 239      if ($status < DifferentialLintStatus::LINT_WARN) {
 240        return array();
 241      }
 242      if ($status == DifferentialLintStatus::LINT_AUTO_SKIP) {
 243        return array();
 244      }
 245  
 246      $warnings = array();
 247      if ($status == DifferentialLintStatus::LINT_SKIP) {
 248        $warnings[] = pht(
 249          'Lint was skipped when generating these changes.');
 250      } else if ($status == DifferentialLintStatus::LINT_POSTPONED) {
 251        $warnings[] = pht(
 252          'Background linting has not finished executing on these changes.');
 253      } else {
 254        $warnings[] = pht('These changes have lint problems.');
 255      }
 256  
 257      return $warnings;
 258    }
 259  
 260  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1