You are here

public function SiteAuditCheckCodebasePhpDeadCodeDetection::calculateScore in Site Audit 8.2

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

Overrides SiteAuditCheckAbstract::calculateScore

File

Check/Codebase/PhpDeadCodeDetection.php, line 115
Contains \SiteAudit\Check\Codebase\PhpDeadCodeDetection.

Class

SiteAuditCheckCodebasePhpDeadCodeDetection
Class SiteAuditCheckCodebasePhpDeadCodeDetection.

Code

public function calculateScore() {

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

  // 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(
    'names' => '*.php,*.module,*.install,*.test,*.inc,*.profile,*.theme',
    'names-exclude' => '*.features.*,*_default.inc,*.ds.inc,*.strongarm.inc,*.panelizer.inc,*_defaults.inc,*.box.inc,*.context.inc,*displays.inc',
  );
  $options = $this
    ->getOptions($valid_options, 'phpdcd-');
  $temp_file = tempnam(sys_get_temp_dir(), 'site_audit');
  $option_string = " --log-xml={$temp_file}";
  foreach ($options as $option => $value) {
    $option_string .= " --{$option}";
    if ($value !== TRUE) {
      $option_string .= "={$value}";
    }
  }

  // Suppress XML errors which will be handled by try catch instead.
  libxml_use_internal_errors(TRUE);
  foreach ($custom_code as $path) {
    $command = $phpdcd_path . ' ' . $path . $option_string;
    $process = new Process($command);
    $process
      ->run();
    $error = $process
      ->getErrorOutput();
    if (strpos($error, 'The "--log-xml" option does not exist.') !== FALSE) {
      return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_INFO;
    }
    try {
      $output = simplexml_load_file($temp_file);
      foreach ($output as $data) {
        $filename = $this
          ->getRelativePath((string) $data->file);
        $this->registry['phpdcd_out'][$filename][] = $data;
      }
    } catch (Exception $e) {
      $this
        ->logXmlError($path, 'phpdcd');
      continue;
    }
  }
  if (empty($this->registry['phpdcd_out'])) {
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_PASS;
  }
  return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_WARN;
}