Passed
Branch 8.x-1.x (e6d5ba)
by Frédéric G.
03:03
created

qa_variables.inc::qa_report_variables()   C

Complexity

Conditions 9
Paths 22

Size

Total Lines 48
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 35
nc 22
nop 0
dl 0
loc 48
rs 5.5102
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @file
5
 * qa.variables.inc
6
 *
7
 * @author: Frédéric G. MARAND <[email protected]>
8
 *
9
 * @copyright (c) 2014 Ouest Systèmes Informatiques (OSInet).
10
 *
11
 * @license General Public License version 2 or later
12
 */
13
14
use Drupal\qa\Variable;
15
16
function qa_report_variable(Variable $variable = NULL) {
17
  $c = cache_get('views_data:fr', 'cache_views');
18
  $ret = '<pre>' . json_encode($c, JSON_PRETTY_PRINT) . '</pre>';
19
  return $ret;
20
  $bc = drupal_get_breadcrumb();
21
  $bc[] = l(t('Administration'), 'admin');
22
  $bc[] = l(t('Reports'), 'admin/reports');
23
  $bc[] = l(t('Quality Assurance'), 'admin/reports/qa');
24
  $bc[] = l(t('Variables'), 'admin/reports/qa/variable');
25
  drupal_set_breadcrumb($bc);
26
27
  drupal_set_title(t('Variable: %name', array('%name' => $variable->name)), PASS_THROUGH);
28
  return $variable->dump();
29
}
30
31
/**
32
 * Page callback for variables list.
33
 *
34
 * @return string
35
 */
36
function qa_report_variables() {
37
  $cached = cache_get('variables', 'cache_bootstrap');
38
  if ($cached === FALSE) {
39
    $cached = (object) array(
40
      'data' => array(),
41
      'serialized' => FALSE,
42
    );
43
  }
44
45
  $header = array(
46
    t('Name'),
47
    t('Length'),
48
    t('Value, or size for arrays'),
49
  );
50
51
  $rows = array();
52
  foreach ($cached->data as $name => $value) {
53
    $row = array();
54
    $variable = new Variable($name);
55
    $row[] = $variable->link();
56
    $row[] = is_scalar($value) ? strlen($value) : strlen(serialize($value));
57
58
    if (is_array($value)) {
59
      $cell = t("Array[@count]", array('@count' => count($value)));
60
    } else {
61
      $raw = is_string($value) ? $value : serialize($value);
62
      $raw = check_plain($raw);
63
      $cell = (strlen($raw) > 200) ? substr($raw, 0, 200) . '&hellip;' : $raw;
64
    }
65
    $row[] = $cell;
66
    $rows[] = $row;
67
  }
68
69
  usort($rows, function ($x, $y) {
70
    if ($x[1] < $y[1]) {
71
      $ret = -1;
72
    } elseif ($x[1] > $y[1]) {
73
      $ret = 1;
74
    } else {
75
      $ret = strcmp($x[0], $y[0]);
76
    }
77
    return - $ret;
78
  });
79
  return theme('table', array(
80
    'header' => $header,
81
    'rows' => $rows,
82
  ));
83
}
84
85