|
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
|
|
|
* Implements 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
|
|
|
$items['field-purge'] = [ |
|
39
|
|
|
'aliases' => ['qafp'], |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
return $items; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Command callback for qa-dependencies. |
|
47
|
|
|
*/ |
|
48
|
|
|
function drush_qa_dependencies() { |
|
49
|
|
|
module_load_include('inc', 'qa', 'qa_dependencies'); |
|
50
|
|
|
$graph = qa_dependencies(); |
|
51
|
|
|
echo $graph->parse(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Command callback for qa-force-removed. |
|
56
|
|
|
*/ |
|
57
|
|
|
function drush_qa_force_removed() { |
|
58
|
|
|
$finder = ForceRemoved::create(); |
|
59
|
|
|
echo $finder->find(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Command callback for qa-project-usage. |
|
64
|
|
|
*/ |
|
65
|
|
|
function drush_qa_project_usage() { |
|
66
|
|
|
$onlyUnused = drush_get_option('only-unused', FALSE); |
|
67
|
|
|
$scanner = new ProjectsScanner(); |
|
68
|
|
|
$result = $scanner->scan($onlyUnused); |
|
69
|
|
|
echo json_encode($result, JSON_PRETTY_PRINT) . "\n"; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Callback for uasort() in drush_qa_variables(). |
|
74
|
|
|
* |
|
75
|
|
|
* @param array $x |
|
76
|
|
|
* The left array to compare. |
|
77
|
|
|
* @param array $y |
|
78
|
|
|
* The right array to compare. |
|
79
|
|
|
* |
|
80
|
|
|
* @return int |
|
81
|
|
|
* As per uasort() |
|
82
|
|
|
* |
|
83
|
|
|
* @see \drush_qa_variables() |
|
84
|
|
|
*/ |
|
85
|
|
|
function _qa_drush_compare_variables(array $x, array $y) { |
|
86
|
|
|
return -($x['len'] <=> $y['len']); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Command callback for qa-variables. |
|
91
|
|
|
*/ |
|
92
|
|
|
function drush_qa_variables() { |
|
93
|
|
|
$result = [ |
|
94
|
|
|
'total' => 0, |
|
95
|
|
|
'detail' => [], |
|
96
|
|
|
]; |
|
97
|
|
|
|
|
98
|
|
|
$cached = cache_get('variables', 'cache_bootstrap'); |
|
99
|
|
|
foreach ($cached->data as $name => $value) { |
|
100
|
|
|
$len = is_scalar($value) ? strlen($value) : strlen(serialize($value)); |
|
101
|
|
|
$result['total'] += $len; |
|
102
|
|
View Code Duplication |
if (is_array($value)) { |
|
|
|
|
|
|
103
|
|
|
$preview = t("Array[@count]", array('@count' => count($value))); |
|
104
|
|
|
} |
|
105
|
|
|
else { |
|
106
|
|
|
$raw = is_string($value) ? $value : serialize($value); |
|
107
|
|
|
$raw = check_plain($raw); |
|
108
|
|
|
$preview = (strlen($raw) > 80) ? substr($raw, 0, 80) . '...' : $raw; |
|
109
|
|
|
} |
|
110
|
|
|
$result['detail'][$name] = ['len' => $len, 'preview' => $preview]; |
|
111
|
|
|
} |
|
112
|
|
|
uasort($result['detail'], _qa_drush_compare_variables::class); |
|
113
|
|
|
|
|
114
|
|
|
echo json_encode($result, JSON_PRETTY_PRINT); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
function drush_qa_field_purge($count = 100) { |
|
118
|
|
|
field_purge_batch($count); |
|
119
|
|
|
} |
|
120
|
|
|
|
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.