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'] = [ |
|
|
|
|
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)) { |
|
|
|
|
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
|
|
|
} |
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:
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 thebar
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.