Passed
Pull Request — 8.x-1.x (#5)
by Frédéric G.
54s
created

Size::run()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 51
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 34
nc 4
nop 0
dl 0
loc 51
rs 9.0648
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Drupal\qa\Cache;
4
5
use Drupal\qa\BaseControl;
6
7
class Size extends BaseControl {
8
  const DATA_SIZE_LIMIT = 524288; // Memcache default entry limit: 1024*1024 * 0.5 for safety
9
  const DATA_SUMMARY_LENGTH = 1024;
10
11
  /**
12
   * @param string $bin_name
13
   *
14
   * @return array
15
   *   - name: the name of the checked bin
16
   *   - status: 0 for KO, 1 for OK
17
   *   - result: information in case of failed check.
18
   */
19
  function checkBin($bin_name) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
20
    $ret = array(
21
      'name' => $bin_name,
22
    );
23
    $ret['status'] = FALSE;
24
    $arg = array('@name' => $bin_name);
25
26
    if (!db_table_exists($bin_name)) {
0 ignored issues
show
Bug introduced by
The function db_table_exists was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
    if (!/** @scrutinizer ignore-call */ db_table_exists($bin_name)) {
Loading history...
27
      $ret['result'] = t('Bin @name is missing in the database.', $arg);
0 ignored issues
show
Bug introduced by
The function t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
      $ret['result'] = /** @scrutinizer ignore-call */ t('Bin @name is missing in the database.', $arg);
Loading history...
28
      return $ret;
29
    }
30
31
    $sql = "SELECT cid, data, expire, created, serialized FROM {$bin_name} ORDER BY cid";
32
    $q = db_query($sql);
0 ignored issues
show
Bug introduced by
The function db_query was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
    $q = /** @scrutinizer ignore-call */ db_query($sql);
Loading history...
33
    if (!$q) {
34
      $ret['result'] = t('Failed fetching database data for bin @name.', $arg);
35
      return $ret;
36
    }
37
38
    list($status, $result) = $this->checkBinContents($q);
39
40
    $ret['status'] = $status;
41
    $ret['result'] = $result;
42
43
    return $ret;
44
  }
45
46
  /**
47
   * Check the contents of an existing and accessible bin.
48
   *
49
   * @param $q
50
   *   The DBTNG query object for the bin contents, already queried.
51
   *
52
   * @return array
53
   *   - 0 : status bool
54
   *   - 1 : result array
55
   */
56
  protected function checkBinContents($q) {
57
    $status = TRUE;
58
    $result = array();
59
    foreach ($q->fetchAll() as $row) {
60
      // Cache drivers will need to serialize anyway.
61
      $data = $row->serialized ? $row->data : serialize($row->data);
62
      $len = strlen($data);
63
      if ($len == 0 || $len >= static::DATA_SIZE_LIMIT) {
64
        $status = FALSE;
65
        $result[] = array(
66
          $row->cid,
67
          number_format($len, 0, ',', ''),
68
          check_plain(drupal_substr($data, 0, static::DATA_SUMMARY_LENGTH)) . '&hellip;',
0 ignored issues
show
Bug introduced by
The function drupal_substr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
          check_plain(/** @scrutinizer ignore-call */ drupal_substr($data, 0, static::DATA_SUMMARY_LENGTH)) . '&hellip;',
Loading history...
Bug introduced by
The function check_plain was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
          /** @scrutinizer ignore-call */ 
69
          check_plain(drupal_substr($data, 0, static::DATA_SUMMARY_LENGTH)) . '&hellip;',
Loading history...
69
        );
70
      }
71
    }
72
73
    return array($status, $result);
74
  }
75
76
  /**
77
   * {@inheritdoc]
78
   */
79
  public function init() {
80
    $this->package_name = __NAMESPACE__;
81
    $this->title = t('Suspicious cache content');
0 ignored issues
show
Bug introduced by
The function t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
    $this->title = /** @scrutinizer ignore-call */ t('Suspicious cache content');
Loading history...
82
    $this->description = t('Look for empty or extra-long (>= 1 MB) cache content.');
83
  }
84
85
  /**
86
   * Does the passed schema match the expected cache schema structure ?
87
   *
88
   * @param array $schema
89
   *
90
   * @return bool
91
   */
92
  public static function isSchemaCache(array $schema) {
93
    $reference_schema_keys = array(
94
      'cid',
95
      'created',
96
      'data',
97
      'expire',
98
      'serialized'
99
    );
100
    $keys = array_keys($schema['fields']);
101
    sort($keys);
102
    $ret = $keys == $reference_schema_keys;
103
104
    return $ret;
105
  }
106
107
  public static function getAllBins($rebuild = FALSE) {
108
    $schema = drupal_get_complete_schema($rebuild);
0 ignored issues
show
Bug introduced by
The function drupal_get_complete_schema was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

108
    $schema = /** @scrutinizer ignore-call */ drupal_get_complete_schema($rebuild);
Loading history...
109
    $ret = array();
110
    foreach ($schema as $name => $info) {
111
      if (static::isSchemaCache($info)) {
112
        $ret[] = $name;
113
      }
114
    }
115
    sort($ret);
116
117
    return $ret;
118
  }
119
120
  function run() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
121
    $pass = parent::run();
122
    $bins = self::getAllBins(TRUE);
123
    foreach ($bins as $bin_name) {
124
      $pass->record($this->checkBin($bin_name));
125
    }
126
    $pass->life->end();
127
128
    if ($pass->status) {
129
      $pass->result = format_plural(count($bins), '1 bin checked, not containing suspicious values',
0 ignored issues
show
Bug introduced by
The function format_plural was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
      $pass->result = /** @scrutinizer ignore-call */ format_plural(count($bins), '1 bin checked, not containing suspicious values',
Loading history...
130
        '@count bins checked, none containing suspicious values', array());
131
    }
132
    else {
133
      $info = format_plural(count($bins), '1 view checked and containing suspicious values',
134
        '@count bins checked, @bins containing suspicious values', array(
135
          '@bins' => count($pass->result),
136
        ));
137
138
      // Prepare for theming
139
      $result = array();
140
      // @XXX May be inconsistent with non-BMP strings ?
141
      uksort($pass->result, 'strcasecmp');
142
      foreach ($pass->result as $bin_name => $bin_report) {
143
        foreach ($bin_report as $entry) {
144
          array_unshift($entry, $bin_name);
145
          $result[] = $entry;
146
        }
147
      }
148
      $header = array(
149
        t('Bin'),
0 ignored issues
show
Bug introduced by
The function t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

149
        /** @scrutinizer ignore-call */ 
150
        t('Bin'),
Loading history...
150
        t('CID'),
151
        t('Length'),
152
        t('Beginning of data'),
153
      );
154
155
      $build = array(
156
        'info' => array(
157
          '#markup' => $info,
158
        ),
159
        'list' => array(
160
          '#markup' => '<p>' . t('Checked: @checked', array('@checked' => implode(', ', $bins))) . "</p>\n",
161
        ),
162
        'table' => array(
163
          '#theme' => 'table',
164
          '#header' => $header,
165
          '#rows' => $result,
166
        )
167
      );
168
      $pass->result = drupal_render($build);
0 ignored issues
show
Bug introduced by
The function drupal_render was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

168
      $pass->result = /** @scrutinizer ignore-call */ drupal_render($build);
Loading history...
169
    }
170
    return $pass;
171
  }
172
173
}
174