You are here

public function ScanResultFormatter::formatAsciiResult in Upgrade Status 8

Same name and namespace in other branches
  1. 8.3 src/ScanResultFormatter.php \Drupal\upgrade_status\ScanResultFormatter::formatAsciiResult()
  2. 8.2 src/ScanResultFormatter.php \Drupal\upgrade_status\ScanResultFormatter::formatAsciiResult()

Format results output for an extension as ASCII.

Return value

array Build array.

File

src/ScanResultFormatter.php, line 331

Class

ScanResultFormatter
Format scan results for display or export.

Namespace

Drupal\upgrade_status

Code

public function formatAsciiResult(Extension $extension) {
  $result = $this
    ->getRawResult($extension);
  $info = $extension->info;
  $label = $info['name'] . (!empty($info['version']) ? ' ' . $info['version'] : '');

  // This project was not yet scanned or the scan results were removed.
  if (empty($result)) {
    return [
      '#title' => $label,
      'data' => [
        '#type' => 'markup',
        '#markup' => $this
          ->t('No deprecation scanning data available.'),
      ],
    ];
  }
  if (isset($result['data']['totals'])) {
    $project_error_count = $result['data']['totals']['file_errors'];
  }
  else {
    $project_error_count = 0;
  }
  $build = [
    '#title' => $label,
    'date' => [
      '#type' => 'markup',
      '#markup' => wordwrap($this
        ->t('Scanned on @date.', [
        '@date' => $this->dateFormatter
          ->format($result['date']),
      ]), 80, "\n", true),
      '#weight' => -10,
    ],
  ];
  if (!empty($result['plans'])) {
    $build['plans'] = [
      '#type' => 'markup',
      '#markup' => wordwrap(strip_tags($result['plans']), 80, "\n", true),
      '#weight' => 50,
    ];
  }

  // If this project had no known issues found, report that.
  if ($project_error_count === 0) {
    $build['data'] = [
      '#type' => 'markup',
      '#markup' => $this
        ->t('No known issues found.'),
      '#weight' => 5,
    ];
    return $build;
  }

  // Otherwise prepare list of errors in tables.
  $tables = '';
  $hasFixNow = FALSE;
  foreach ($result['data']['files'] as $filepath => $errors) {
    $short_path = wordwrap(str_replace(DRUPAL_ROOT . '/', '', $filepath), 80, "\n", true);
    $tables .= $short_path . ":\n";
    $table = [];
    foreach ($errors['messages'] as $error) {
      $level_label = $this
        ->t('Check manually');
      if (!empty($error['upgrade_status_category'])) {
        if ($error['upgrade_status_category'] == 'ignore') {
          $level_label = $this
            ->t('Ignore');
        }
        elseif ($error['upgrade_status_category'] == 'later') {
          $level_label = $this
            ->t('Fix later');
        }
        elseif (in_array($error['upgrade_status_category'], [
          'safe',
          'old',
        ])) {
          $level_label = $this
            ->t('Fix now');
          $hasFixNow = TRUE;
        }
      }
      $message = str_replace("\n", ' ', $error['message']);
      $table[] = [
        'status' => wordwrap($level_label, 8, "\n", true),
        'line' => wordwrap($error['line'], 7, "\n", true),
        'message' => wordwrap($message . "\n", 60, "\n", true),
      ];
    }
    $asciiRenderer = new ArrayToTextTable($table);
    $tables .= $asciiRenderer
      ->getTable() . "\n";
  }
  $build['data'] = $tables;
  $summary = [];
  if (!empty($result['data']['totals']['upgrade_status_split']['error'])) {
    $summary[] = $this
      ->formatPlural($result['data']['totals']['upgrade_status_split']['error'], '@count error found.', '@count errors found.');
  }
  if (!empty($result['data']['totals']['upgrade_status_split']['warning'])) {
    $summary[] = $this
      ->formatPlural($result['data']['totals']['upgrade_status_split']['warning'], '@count warning found.', '@count warnings found.');
  }
  if ($hasFixNow) {
    if (!empty($extension->info['project'])) {
      $summary[] = $this
        ->t('Items categorized "Fix now" are uses of deprecated APIs from community unsupported core versions.');
    }
    else {
      $summary[] = $this
        ->t('Items categorized "Fix now" are uses of deprecated APIs in custom code from current or older Drupal core version.');
    }
  }
  $build['summary'] = [
    '#type' => '#markup',
    '#markup' => wordwrap(join(' ', $summary), 80, "\n", true),
    '#weight' => 5,
  ];
  return $build;
}