These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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'] = [ |
||
0 ignored issues
–
show
|
|||
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 | $graph = qa_dependencies(); |
||
47 | echo $graph->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 array $x |
||
72 | * The left array to compare. |
||
73 | * @param array $y |
||
74 | * The right array to compare. |
||
75 | * |
||
76 | * @return int |
||
77 | * As per uasort() |
||
78 | * |
||
79 | * @see \drush_qa_variables() |
||
80 | */ |
||
81 | function _qa_drush_compare_variables(array $x, array $y) { |
||
82 | return -($x['len'] <=> $y['len']); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Command callback for qa-variables. |
||
87 | */ |
||
88 | function drush_qa_variables() { |
||
89 | $result = [ |
||
90 | 'total' => 0, |
||
91 | 'detail' => [], |
||
92 | ]; |
||
93 | |||
94 | $cached = cache_get('variables', 'cache_bootstrap'); |
||
95 | foreach ($cached->data as $name => $value) { |
||
96 | $len = is_scalar($value) ? strlen($value) : strlen(serialize($value)); |
||
97 | $result['total'] += $len; |
||
98 | View Code Duplication | if (is_array($value)) { |
|
0 ignored issues
–
show
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. ![]() |
|||
99 | $preview = t("Array[@count]", array('@count' => count($value))); |
||
100 | } |
||
101 | else { |
||
102 | $raw = is_string($value) ? $value : serialize($value); |
||
103 | $raw = check_plain($raw); |
||
104 | $preview = (strlen($raw) > 80) ? substr($raw, 0, 80) . '...' : $raw; |
||
105 | } |
||
106 | $result['detail'][$name] = ['len' => $len, 'preview' => $preview]; |
||
107 | } |
||
108 | uasort($result['detail'], _qa_drush_compare_variables::class); |
||
109 | |||
110 | echo json_encode($result, JSON_PRETTY_PRINT); |
||
111 | } |
||
112 |
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.