Completed
Push — 7.x-1.x ( 1070be...027296 )
by Frédéric G.
01:31
created

qa.drush.inc::drush_qa_variables()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 24
Code Lines 17

Duplication

Lines 8
Ratio 33.33 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 11
nop 0
dl 8
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @file
5
 * Drush plugin.
6
 */
7
8
use Drupal\qa\Plugin\Qa\Control\ForceRemoved;
9
use Drupal\qa\Plugin\Qa\Control\System\ProjectsScanner;
10
11
/**
12
 * Implementation of hook_drush_command().
13
 */
14
function qa_drush_command() {
15
  $items['qa-dependencies'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$items was never initialized. Although not strictly required by PHP, it is generally a good practice to add $items = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
16
    'description' => 'Build a Graphviz DOT file showing the module and theme dependencies on the site',
17
    'aliases' => ['qadep'],
18
  ];
19
20
  $items['qa-force-removed'] = [
21
    'description' => 'List extensions removed without a clean uninstall.',
22
    'aliases' => ['qafrm'],
23
  ];
24
25
  $items['qa-project-usage'] = [
26
    'description' => 'List usage and components for projects',
27
    'options' => [
28
      'only-unused' => 'Only include totally unused packages',
29
    ],
30
    'aliases' => ['qapus'],
31
  ];
32
33
  $items['qa-variables'] = [
34
    'description' => 'List size of variables',
35
    'aliases' => ['qavar'],
36
  ];
37
38
  return $items;
39
}
40
41
/**
42
 * Command callback for qa-dependencies.
43
 */
44
function drush_qa_dependencies() {
45
  module_load_include('inc', 'qa', 'qa_dependencies');
46
  $G = qa_dependencies();
47
  echo $G->parse();
48
}
49
50
/**
51
 * Command callback for qa-force-removed.
52
 */
53
function drush_qa_force_removed() {
54
  $finder = ForceRemoved::create();
55
  echo $finder->find();
56
}
57
58
/**
59
 * Command callback for qa-project-usage.
60
 */
61
function drush_qa_project_usage() {
62
  $onlyUnused = drush_get_option('only-unused', FALSE);
63
  $scanner = new ProjectsScanner();
64
  $result = $scanner->scan($onlyUnused);
65
  echo json_encode($result, JSON_PRETTY_PRINT) . "\n";
66
}
67
68
/**
69
 * Callback for uasort() in drush_qa_variables().
70
 *
71
 * @param $x
72
 * @param $y
73
 * @return int
74
 *
75
 * @see \drush_qa_variables()
76
 */
77
function _qa_drush_compare_variables($x, $y) {
78
  return - ($x['len'] <=> $y['len']);
79
}
80
81
/**
82
 * Command callback for qa-variables.
83
 */
84
function drush_qa_variables() {
85
  $result = [
86
    'total' => 0,
87
    'detail' => [],
88
  ];
89
90
  $cached = cache_get('variables', 'cache_bootstrap');
91
  foreach ($cached->data as $name => $value) {
92
    $len = is_scalar($value) ? strlen($value) : strlen(serialize($value));
93
    $result['total'] += $len;
94 View Code Duplication
    if (is_array($value)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
      $preview = t("Array[@count]", array('@count' => count($value)));
96
    }
97
    else {
98
      $raw = is_string($value) ? $value : serialize($value);
99
      $raw = check_plain($raw);
100
      $preview = (strlen($raw) > 80) ? substr($raw, 0, 80) . '...' : $raw;
101
    }
102
    $result['detail'][$name] = ['len' => $len, 'preview' => $preview];
103
  }
104
  uasort($result['detail'], _qa_drush_compare_variables::class);
105
106
  echo json_encode($result, JSON_PRETTY_PRINT);
107
}