You are here

public function UpgradeStatusCommands::analyze in Upgrade Status 8.2

Same name and namespace in other branches
  1. 8.3 src/Commands/UpgradeStatusCommands.php \Drupal\upgrade_status\Commands\UpgradeStatusCommands::analyze()

Analyze projects output as ASCII.

@command upgrade_status:analyze @option all Analyze all projects. @option skip-existing Return results from a previous scan of a project if available, otherwise start a new one. @option ignore-uninstalled Ignore uninstalled projects. @option ignore-contrib Ignore contributed projects. @option ignore-custom Ignore custom projects. @aliases us-a

Parameters

array $projects: List of projects to analyze.

array $options: Additional options for the command.

Throws

\InvalidArgumentException Thrown when one of the passed arguments is invalid or no arguments were provided.

1 call to UpgradeStatusCommands::analyze()
UpgradeStatusCommands::checkstyle in src/Commands/UpgradeStatusCommands.php
Analyze projects output as XML.

File

src/Commands/UpgradeStatusCommands.php, line 119

Class

UpgradeStatusCommands
Upgrade Status Drush command

Namespace

Drupal\upgrade_status\Commands

Code

public function analyze(array $projects, array $options = [
  'all' => FALSE,
  'skip-existing' => FALSE,
  'ignore-uninstalled' => FALSE,
  'ignore-contrib' => FALSE,
  'ignore-custom' => FALSE,
]) {

  // Group by type here so we can tell loader what is type of each one of
  // these.
  $extensions = [];
  $invalid_names = [];
  if (empty($projects) && !$options['all']) {
    $message = dt('You need to provide at least one installed project\'s machine_name.');
    throw new \InvalidArgumentException($message);
  }

  // Gather project list grouped by custom and contrib projects.
  $available_projects = $this->projectCollector
    ->collectProjects();
  if ($options['all']) {
    foreach ($available_projects as $type => $projects) {
      if (!$options['ignore-' . $type]) {
        foreach ($projects as $name => $project) {
          if (!$options['ignore-uninstalled'] || $project->status !== 0) {
            $extensions[$project
              ->getType()][$name] = $project;
          }
        }
      }
    }
  }
  else {
    foreach ($projects as $name) {
      if (!$options['ignore-custom'] && array_key_exists($name, $available_projects['custom'])) {
        $type = $available_projects['custom'][$name]
          ->getType();
        if (!$options['ignore-uninstalled'] || $available_projects['custom'][$name]->status !== 0) {
          $extensions[$type][$name] = $available_projects['custom'][$name];
        }
        else {
          $invalid_names[] = $name;
        }
      }
      elseif (!$options['ignore-contrib'] && array_key_exists($name, $available_projects['contrib'])) {
        $type = $available_projects['contrib'][$name]
          ->getType();
        if (!$options['ignore-uninstalled'] || $available_projects['contrib'][$name]->status !== 0) {
          $extensions[$type][$name] = $available_projects['contrib'][$name];
        }
        else {
          $invalid_names[] = $name;
        }
      }
      else {
        $invalid_names[] = $name;
      }
    }
  }
  if (!empty($invalid_names)) {
    if (count($invalid_names) == 1) {
      $message = dt('The project machine name @invalid_name is invalid. Is this a project on this site? (For community projects, use the machine name of the drupal.org project itself).', [
        '@invalid_name' => $invalid_names[0],
      ]);
    }
    else {
      $message = dt('The project machine names @invalid_names are invalid. Are these projects on this site? (For community projects, use the machine name of the drupal.org project itself).', [
        '@invalid_names' => implode(', ', $invalid_names),
      ]);
    }
    throw new \InvalidArgumentException($message);
  }
  else {
    $this
      ->logger()
      ->info(dt('Starting the analysis. This may take a while.'));
  }
  foreach ($extensions as $type => $list) {
    foreach ($list as $name => $extension) {
      if ($options['skip-existing']) {
        $scan_result = \Drupal::service('keyvalue')
          ->get('upgrade_status_scan_results')
          ->get($name);
        if (!empty($scan_result)) {
          $this
            ->logger()
            ->info(dt('Using previous results for @name.', [
            '@name' => $name,
          ]));
          continue;
        }
      }
      $this
        ->logger()
        ->info(dt('Processing @name.', [
        '@name' => $name,
      ]));
      $this->deprecationAnalyzer
        ->analyze($extension);
    }
  }
  if ($this->mode !== 'ascii') {
    $xml = new \SimpleXMLElement("<?xml version='1.0'?><checkstyle/>");
  }
  foreach ($extensions as $type => $list) {
    if ($this->mode === 'ascii') {
      $this
        ->output()
        ->writeln('');
      $this
        ->output()
        ->writeln(str_pad('', 80, '='));
    }
    $track = 0;
    foreach ($list as $name => $extension) {
      $result = $this->resultFormatter
        ->getRawResult($extension);
      if (is_null($result)) {
        $this
          ->logger()
          ->error('Project scan @name failed.', [
          '@name' => $name,
        ]);
        continue;
      }
      if ($this->mode === 'ascii') {
        $output = $this
          ->formatDrushStdoutResult($extension);
        foreach ($output as $line) {
          $this
            ->output()
            ->writeln($line);
        }
        if (++$track < count($list)) {
          $this
            ->output()
            ->writeln(str_pad('', 80, '='));
        }
      }
      else {
        foreach ($result['data']['files'] as $filepath => $errors) {
          $short_path = str_replace(DRUPAL_ROOT . '/', '', $filepath);
          $file_xml = $xml
            ->addChild('file');
          $file_xml
            ->addAttribute('name', $short_path);
          foreach ($errors['messages'] as $error) {
            $severity = 'error';
            if ($error['upgrade_status_category'] == 'ignore') {
              $severity = 'info';
            }
            elseif ($error['upgrade_status_category'] == 'later') {
              $severity = 'warning';
            }
            $error_xml = $file_xml
              ->addChild('error');
            $error_xml
              ->addAttribute('line', $error['line']);
            $error_xml
              ->addAttribute('message', $error['message']);
            $error_xml
              ->addAttribute('severity', $severity);
          }
        }
        $this
          ->output()
          ->writeln($xml
          ->asXML());
      }
    }
  }
}