You are here

public function SiteAuditCheckFrontEndGooglePageSpeed::renderResults in Site Audit 8.2

Returns the rendered result from JSON decoded object.

Parameters

mixed $json_result: Json decodes object.

Return value

string Rendered Output.

1 call to SiteAuditCheckFrontEndGooglePageSpeed::renderResults()
SiteAuditCheckFrontEndGooglePageSpeed::getResultPass in Check/FrontEnd/GooglePageSpeed.php
Implements \SiteAudit\Check\Abstract\getResultPass().

File

Check/FrontEnd/GooglePageSpeed.php, line 56
Contains \SiteAudit\Check\Insights\Analyze.

Class

SiteAuditCheckFrontEndGooglePageSpeed
Class SiteAuditCheckFrontEndGooglePageSpeed.

Code

public function renderResults($json_result) {
  $ret_val = '';
  $stats = array();
  foreach ($json_result->pageStats as $stat_name => $count) {
    $formatted_stat_name = ucfirst(preg_replace('/(?<!^)((?<![[:upper:]])[[:upper:]]|[[:upper:]](?![[:upper:]]))/', ' $1', $stat_name));
    if (stripos($stat_name, 'bytes') !== FALSE) {
      $stats[$formatted_stat_name] = round($count / 1024, 2) . 'kB';
    }
    else {
      $stats[$formatted_stat_name] = $count;
    }
  }

  // Render PageStats.
  if (drush_get_option('html')) {
    $ret_val .= '<h3>' . dt('Page stats') . '</h3>';
    $ret_val .= '<dl class="dl-horizontal">';
    foreach ($stats as $name => $count) {
      $ret_val .= '<dt>' . $name . '</dt>';
      $ret_val .= '<dd>' . $count . '</dd>';
    }
    $ret_val .= '</dl>';
  }
  else {
    $ret_val .= PHP_EOL . str_repeat(' ', 6) . dt('Page stats');
    foreach ($stats as $name => $count) {
      $ret_val .= PHP_EOL;
      if (!drush_get_option('json')) {
        $ret_val .= str_repeat(' ', 8);
      }
      $ret_val .= '* ' . $name . ': ' . $count;
    }
  }
  $impact_filter = drush_get_option('impact');

  // Results.
  if (drush_get_option('html')) {
    $ret_val .= '<h3>' . dt('Detailed results') . '</h3>';
  }
  else {
    $ret_val .= PHP_EOL . str_repeat(' ', 6) . dt('Detailed results:');
  }
  $rendered_result_count = 0;

  // @codingStandardsIgnoreStart
  foreach ($json_result->formattedResults->ruleResults as $resultValues) {
    rtrim($ret_val);

    // Filter out based on impact threshold.
    if ($resultValues->ruleImpact < $impact_filter) {
      continue;
    }
    $rendered_result_count++;

    // Build impact label.
    $impact = '';
    if ($resultValues->ruleImpact >= 3) {
      $impact = dt('(HIGH impact: @ruleImpact)', array(
        '@ruleImpact' => $resultValues->ruleImpact,
      ));
    }
    elseif ($resultValues->ruleImpact > 0) {
      $impact = dt('(low impact: @ruleImpact)', array(
        '@ruleImpact' => $resultValues->ruleImpact,
      ));
    }

    // Render Rule, score and impact.
    $rule_score_impact = dt('@localizedRuleName: @impact', array(
      '@localizedRuleName' => $resultValues->localizedRuleName,
      '@impact' => $impact,
    ));
    if (drush_get_option('html')) {
      $ret_val .= '<div class="alert alert-block ';
      if ($resultValues->ruleImpact == 0) {
        $ret_val .= 'alert-success';
      }
      elseif ($resultValues->ruleImpact >= 10) {
        $ret_val .= 'alert-danger';
      }
      else {
        $ret_val .= 'alert-warning';
      }
      $ret_val .= '">' . $rule_score_impact . '</div>';
    }
    else {
      $ret_val .= PHP_EOL;
      if (!drush_get_option('json')) {
        $ret_val .= str_repeat(' ', 8);
      }
      $ret_val .= $rule_score_impact;
    }

    // Render Summary
    $summary = $resultValues->summary;
    if (!isset($summary->args)) {
      $header = google_json_text_replacement($summary->format);
    }
    else {
      $header = google_json_text_replacement($summary->format, $summary->args);
    }
    if (drush_get_option('html')) {
      $ret_val .= "<p>{$header}</p>";
    }
    else {
      $ret_val .= PHP_EOL;
      if (!drush_get_option('json')) {
        $ret_val .= str_repeat(' ', 10);
      }
      $ret_val .= $header;
    }

    // URL blocks.
    if (isset($resultValues->urlBlocks)) {
      foreach ($resultValues->urlBlocks as $block) {

        // @codingStandardsIgnoreEnd
        // Header.
        if (!isset($block->header->args)) {
          $header = google_json_text_replacement($block->header->format);
        }
        else {
          $header = google_json_text_replacement($block->header->format, $block->header->args);
        }
        $limit = drush_get_option('limit', 0);
        if ($limit > 0 && isset($block->urls) && $limit != count($block->urls) && $limit < count($block->urls)) {
          $header .= ' ' . dt('Showing @limit out of @total total:', array(
            '@limit' => $limit,
            '@total' => count($block->urls),
          ));
        }
        if (drush_get_option('html')) {
          $ret_val .= '<blockquote>' . $header;
        }
        else {
          $ret_val .= PHP_EOL;
          if (!drush_get_option('json')) {
            $ret_val .= str_repeat(' ', 10);
          }
          $ret_val .= $header;
        }
        if (isset($block->urls) && !empty($block->urls)) {
          $urls = array();
          $count = 0;
          foreach ($block->urls as $url) {
            if ($limit > 0) {
              if (++$count > $limit) {
                continue;
              }
            }
            $urls[] = google_json_text_replacement($url->result->format, $url->result->args);
          }
          if (drush_get_option('html')) {
            $ret_val .= '<small>' . dt('URLs:');
            $ret_val .= '<ul><li>' . implode('</li><li>', $urls) . '</li></ul>';
            $ret_val .= '</small>';
          }
          else {
            foreach ($urls as $url) {
              $ret_val .= PHP_EOL;
              if (!drush_get_option('json')) {
                $ret_val .= str_repeat(' ', 12);
              }
              $ret_val .= $url;
            }
          }
        }
        if (drush_get_option('html')) {
          $ret_val .= '</blockquote>';
        }
      }
    }
  }

  // Explain if there are no results so it doesn't look like its broken.
  if ($rendered_result_count == 0) {
    if ($impact_filter) {
      $ret_val .= dt('Nice, no problems to report!');
    }
    else {
      $ret_val .= dt('No results, which is unusual...');
    }
  }
  return $ret_val;
}