You are here

public function RectorProcessor::processResults in Upgrade Rector 8

Processes the rector output string for display.

Parameters

string $raw_rector_result: Raw rector output string.

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

Return value

bool|array FALSE if the rector run did not succeed. TRUE if succeeded and found nothing to patch. Otherwise an array with two keys: 'patch' holding a string with a processed patch and 'rectors' with an array of rector names that were executed on the files processed.

1 call to RectorProcessor::processResults()
RectorProcessor::formatResults in src/RectorProcessor.php
Formats processed rector results as a render array.

File

src/RectorProcessor.php, line 269

Class

RectorProcessor
Runs rector and processes rector results.

Namespace

Drupal\upgrade_rector

Code

public function processResults(string $raw_rector_result, Extension $extension) {
  $lines = explode("\n", $raw_rector_result);
  $processed_result = [
    'state' => strpos($raw_rector_result, '[OK] Rector is done!') ? 'success' : 'mixed',
    'patch' => '',
    'log' => '',
    'rectors' => [],
  ];
  if (!preg_match('!^\\d+ files? with changes$!m', $raw_rector_result) && !strpos($raw_rector_result, '[OK] Rector is done!')) {
    $processed_result['state'] = 'fail';
    $processed_result['log'] = $raw_rector_result;
    return $processed_result;
  }

  // If this was at least a partially successful run, reformat as patch. This rector
  // version does not have an output format option yet.
  $state = 'log';
  $file = '';
  $rectors = [];
  foreach ($lines as $num => &$line) {
    switch ($state) {
      case 'log':

        // Found a file that was patched.
        if (preg_match('!^\\d+\\) (.+)!', $line, $found)) {
          $file = str_replace(DRUPAL_ROOT . '/' . $extension
            ->getPath() . '/', '', $found[1]);
          unset($lines[$num]);
          $state = 'seeking diff';
        }
        elseif (preg_match('!^ \\* (.+)$!', $line, $found)) {
          $rectors[$found[1]] = TRUE;
          unset($lines[$num]);
        }
        elseif ($line == '===================' || $line == 'Applied rules:' || preg_match('!^\\d+ files? with changes$!', $line)) {
          unset($lines[$num]);
        }
        else {

          // Keep saving to the log until we find a patch portion.
          if ($processed_result['state'] == 'mixed') {

            // Avoid repeating empty lines, they make reading the log harder.
            if (empty($lines[$num])) {
              $processed_result['log'] = trim($processed_result['log']) . "\n\n";
            }
            else {
              $processed_result['log'] .= $lines[$num] . "\n";
            }
          }
          unset($lines[$num]);
        }
        break;

      // File is known, seeking until diff is found.
      case 'seeking diff':
        if ($line != "    ---------- begin diff ----------") {
          unset($lines[$num]);
        }
        else {
          $line = 'Index: ' . $file;
          $state = 'diff';
        }
        break;

      // Reformatting the diff part.
      case 'diff':
        if ($line == '--- Original') {
          $line = '--- a/' . $file;
        }
        elseif ($line == '+++ New') {
          $line = '+++ b/' . $file;
        }
        if ($line == '    ----------- end diff -----------') {
          $state = 'log';
          unset($lines[$num]);
        }
        break;
    }
  }
  if (count($lines)) {
    $processed_result['patch'] = join("\n", $lines);
    $processed_result['rectors'] = array_keys($rectors);
  }
  return $processed_result;
}