You are here

public function ScanResultFormatter::formatAsciiResult in Upgrade Status 8.2

Same name and namespace in other branches
  1. 8.3 src/ScanResultFormatter.php \Drupal\upgrade_status\ScanResultFormatter::formatAsciiResult()
  2. 8 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 395

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 = '';
  $hasFixRector = FALSE;
  foreach ($result['data']['files'] as $filepath => $errors) {

    // Remove the Drupal root directory name. If this is a composer setup,
    // then the webroot is in a web/ directory, add that back in for easy
    // path copy-pasting.
    $short_path = str_replace(DRUPAL_ROOT . '/', '', $filepath);
    if (preg_match('!/web$!', DRUPAL_ROOT)) {
      $short_path = 'web/' . $short_path;
    }
    $short_path = wordwrap($short_path, 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');
        }
        elseif ($error['upgrade_status_category'] == 'rector') {
          $level_label = $this
            ->t('Fix with rector');
          $hasFixRector = 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 ($hasFixRector) {
    $summary[] = $this
      ->t('Avoid some manual work by using drupal-rector for fixing issues automatically or Upgrade Rector to generate patches.');
  }
  $build['summary'] = [
    '#type' => '#markup',
    '#markup' => wordwrap(join(' ', $summary), 80, "\n", true),
    '#weight' => 5,
  ];
  return $build;
}