You are here

public function SiteAuditCheckCodebasePhpMessDetection::calculateScore in Site Audit 8.2

Implements \SiteAudit\Check\Abstract\calculateScore().

Overrides SiteAuditCheckAbstract::calculateScore

File

Check/Codebase/PhpMessDetection.php, line 120
Contains \SiteAudit\Check\Codebase\PhpMessDetection.

Class

SiteAuditCheckCodebasePhpMessDetection
Class SiteAuditCheckCodebasePhpMessDetection.

Code

public function calculateScore() {

  // Get the path of phpmd.
  $phpmd_path = $this
    ->getExecPath('phpmd');
  if ($phpmd_path === '') {
    $this->registry['phpmd_path_error'] = TRUE;
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_INFO;
  }

  // Get the custom code paths.
  // Get the custom code paths.
  $custom_code = $this
    ->getCustomCodePaths();
  if ($custom_code === FALSE) {
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL;
  }
  if (empty($custom_code)) {
    $this->registry['custom_code'] = TRUE;
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_INFO;
  }

  // Get options.
  $valid_options = array(
    'minimumpriority' => NULL,
    'suffixes' => '.php,.module,.install,.test,.inc,.profile,.theme',
    'exclude' => '*.features.*,*_default.inc,*.ds.inc,*.strongarm.inc,*.panelizer.inc,*_defaults.inc,*.box.inc,*.context.inc,*displays.inc',
    'strict' => NULL,
    'ruleset' => 'codesize,naming,design,unusedcode',
  );
  $options = $this
    ->getOptions($valid_options, 'phpmd-');
  $option_string = ' ' . $options['ruleset'];
  foreach ($options as $option => $value) {
    if ($option != 'ruleset') {
      $option_string .= " --{$option}";
      if ($value !== TRUE) {
        $option_string .= "={$value}";
      }
    }
  }

  // Supress XML errors which will be handled by try catch instead.
  libxml_use_internal_errors(TRUE);
  foreach ($custom_code as $path) {
    $command = $phpmd_path . ' ' . $path . ' xml' . $option_string;
    $process = new Process($command);
    $process
      ->run();
    if ($process
      ->getExitCode() == 1) {
      continue;
    }
    try {
      $output = new SimpleXMLElement($process
        ->getOutput());
      foreach ($output as $file) {
        foreach ($file as $violation) {
          $filename = $this
            ->getRelativePath((string) $file[0]['name']);
          $this->registry['phpmd_out'][$filename][] = $violation;
        }
      }
    } catch (Exception $e) {
      $this
        ->logXmlError($path, 'phpmd');
      continue;
    }
  }
  if (empty($this->registry['phpmd_out'])) {
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_PASS;
  }
  return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_WARN;
}