You are here

public function RectorProcessor::formatResults in Upgrade Rector 8

Formats processed rector results as a render array.

Parameters

string $raw_rector_result: Raw rector output string.

\Drupal\Core\Extension\Extension $extension: Extension that was parsed.

string $category: One of 'custom' or 'contrib'. Presenting messages may be different for each.

Return value

string Render array with a textarea of the reformatted output as a diff if the rector output was a patch. The verbatim output if there were errors or a note about no patchability otherwise.

File

src/RectorProcessor.php, line 153

Class

RectorProcessor
Runs rector and processes rector results.

Namespace

Drupal\upgrade_rector

Code

public function formatResults(string $raw_rector_result, Extension $extension, string $category) {
  $info = $extension->info;
  $label = $info['name'] . (!empty($info['version']) ? ' ' . $info['version'] : '');

  // The result was empty. Nothing to patch.
  if (empty($raw_rector_result)) {
    return [
      '#title' => $this
        ->t('No results for @extension', [
        '@extension' => $label,
      ]),
      'results' => [
        '#type' => 'markup',
        '#markup' => $this
          ->t('Rector not run yet on the project.'),
      ],
    ];
  }

  // We have results, process it for display.
  $processed_result = $this
    ->processResults($raw_rector_result, $extension);
  $export_button = [
    '#type' => 'link',
    '#title' => $processed_result['state'] === 'fail' ? $this
      ->t('Export errors') : $this
      ->t('Export patch'),
    '#name' => 'export',
    '#url' => Url::fromRoute('upgrade_rector.export', [
      'type' => $extension
        ->getType(),
      'project_machine_name' => $extension
        ->getName(),
    ]),
    '#attributes' => [
      'class' => [
        'button',
      ],
    ],
  ];

  // The result was successful without a patch. Nothing to patch.
  if ($processed_result['state'] === 'success' && empty($processed_result['patch'])) {
    return [
      '#title' => $this
        ->t('Nothing to patch in @extension', [
        '@extension' => $label,
      ]),
      'results' => [
        '#type' => 'markup',
        '#markup' => $this
          ->t('Nothing found to patch. This does not mean the project is entirely Drupal 9 compatible due to the limited number of transformations available. The maintainers of <a href=":url-drupal-rector">Drupal-rector welcome more contributed transformations</a>. Use <a href=":url-upgrade-status">Upgrade Status</a> or <a href=":url-drupal-check">drupal-check</a> to identify deprecated API use that rector may not have coverage for yet.', [
          ':url-upgrade-status' => 'https://drupal.org/project/upgrade_status',
          ':url-drupal-check' => 'https://github.com/mglaman/drupal-check',
          ':url-drupal-rector' => 'https://github.com/palantirnet/drupal-rector-sandbox/blob/master/README.md#developing-with-drupal-rector',
        ]),
      ],
    ];
  }
  elseif ($processed_result['state'] === 'fail') {
    $count = count(explode("\n", $processed_result['log']));
    return [
      '#title' => $this
        ->t('Fail while processing @extension', [
        '@extension' => $label,
      ]),
      'description' => [
        '#type' => 'markup',
        '#markup' => $this
          ->t('Raw rector output shown below for debugging purposes. If you believe the errors are due to the tool used not the code processed, <a href=":url">look for an existing issue or submit a new one for Drupal-rector</a>.', [
          ':url' => 'https://www.drupal.org/project/issues/rector',
        ]),
      ],
      'results' => [
        '#type' => 'textarea',
        '#rows' => min($count, 16),
        '#value' => $processed_result['log'],
      ],
      'export' => $export_button,
    ];
  }
  else {
    $patch_line_count = count(explode("\n", $processed_result['patch']));
    $log_line_count = count(explode("\n", $processed_result['log']));
    $description = $this
      ->t('Review the suggested changes as they may need some further updates manually.');
    if ($category == 'contrib') {
      $description .= ' ' . $this
        ->t('Work with the maintainers of <a href=":project-url">@extension</a> following their Drupal 9 plan (if specified on the project page). Make sure to update to the latest (development) version locally. Remember that there may very well be <a href=":url-issues">issues opened</a> for some or all of these incompatibilities found.', [
        ':project-url' => 'https://drupal.org/project/' . $extension
          ->getName(),
        '@extension' => $label,
        ':url-issues' => 'https://drupal.org/project/issues/' . $extension
          ->getName(),
      ]);
    }
    return [
      '#title' => $this
        ->t('Patch generated for @extension', [
        '@extension' => $label,
      ]),
      'description' => [
        '#type' => 'markup',
        '#markup' => $description,
      ],
      'results' => [
        '#type' => 'textarea',
        '#rows' => min($patch_line_count, 16),
        '#value' => $processed_result['patch'],
      ],
      'export' => $export_button,
      'rectors' => [
        '#theme' => 'item_list',
        '#title' => $this
          ->t('List of applied rectors'),
        '#list_type' => 'ul',
        '#items' => $processed_result['rectors'],
      ],
      'log' => [
        '#type' => 'textarea',
        '#title' => $this
          ->t('Log of errors encountered while running rector'),
        '#rows' => min($log_line_count, 16),
        '#value' => $processed_result['log'],
        '#access' => !empty($processed_result['log']),
      ],
    ];
  }
}