Skip to content

Commit b127277

Browse files
committed
Issue #15: implemented Dependencies.Undeclared and System.External plugins.
1 parent 6bcd2c4 commit b127277

File tree

13 files changed

+693
-283
lines changed

13 files changed

+693
-283
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@
2121
"drupal/core": "^8 || ^9",
2222
"osinet/grafizzi": "^0.0.2"
2323
},
24-
"type": "drupal-module"
24+
"type": "drupal-module",
25+
"require-dev": {
26+
"nikic/php-parser": "^4.8@dev"
27+
}
2528
}

qa.module

Lines changed: 89 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
* @file
55
* OSInet Quality Assurance module for Drupal.
66
*
7-
* @copyright Copyright (C) 2005-2018 Frederic G. MARAND for Ouest Systèmes Informatiques (OSInet)
7+
* @copyright Copyright (C) 2005-2018 Frederic G. MARAND for Ouest Systèmes
8+
* Informatiques (OSInet)
89
*
910
* @since DRUPAL-4-6
1011
*
11-
* @license Licensed under the disjunction of the CeCILL, version 2 and General Public License version 2 and later
12+
* @license Licensed under the disjunction of the CeCILL, version 2 and General
13+
* Public License version 2 and later
1214
*
1315
* License note: QA is distributed by OSInet to its customers under the
1416
* CeCILL 2.0 license. OSInet support services only apply to the module
@@ -21,7 +23,11 @@
2123
* are abided by, the module distributor in that case being the
2224
* drupal.org organization or the downstream distributor, not OSInet.
2325
*/
26+
27+
use Drupal\Component\Utility\Xss;
28+
use Drupal\Core\Url;
2429
use Drupal\qa\Variable\Variable;
30+
use Symfony\Component\HttpFoundation\RedirectResponse;
2531

2632
/**
2733
* Page callback for qa/dependencies
@@ -33,7 +39,7 @@ use Drupal\qa\Variable\Variable;
3339
*/
3440
function qa_page_dependencies() {
3541
/** @var \Drupal\qa\Dependencies $qaDep */
36-
$qaDep = \Drupal::service('qa.dependencies');
42+
$qaDep = Drupal::service('qa.dependencies');
3743
$G = $qaDep->build();
3844
// passed by reference: cannot pass a function return
3945
return graphviz_filter_render($G);
@@ -49,14 +55,16 @@ function qa_page_dependencies() {
4955
function qa_report_finished($success, $results, $operations) {
5056
unset($results['#message']);
5157
if ($success) {
52-
$message = format_plural(count($results), 'One control pass ran.', '@count control passes ran.');
58+
$message = Drupal::translation()
59+
->formatPlural(count($results), 'One control pass ran.',
60+
'@count control passes ran.');
5361
}
5462
else {
5563
$message = t('Finished with an error.');
5664
}
5765
drupal_set_message($message);
5866
$_SESSION['qa_results'] = $results;
59-
drupal_goto('admin/reports/qa/results');
67+
return new RedirectResponse(Url::fromRoute('qa.reports.results'));
6068
}
6169

6270
/**
@@ -66,37 +74,41 @@ function qa_report_finished($success, $results, $operations) {
6674
*/
6775
function qa_report_results() {
6876
if (empty($_SESSION['qa_results'])) {
69-
drupal_goto('admin/reports/qa');
77+
return new RedirectResponse(Url::fromRoute('qa.reports'));
7078
}
7179
// Work around incomplete classes
7280
$results = unserialize(serialize($_SESSION['qa_results']));
7381

74-
$header = array(
82+
$header = [
7583
t('Control'),
7684
t('Status'),
7785
t('Results'),
78-
);
79-
$data = array();
86+
];
87+
$data = [];
88+
$r = Drupal::service('renderer');
8089
foreach ($results as $pass) {
8190
$control = $pass->control;
82-
$data[] = array(
91+
$data[] = [
8392
$control->title,
8493
$pass->status
85-
? theme('image', array(
86-
'path' => 'misc/watchdog-ok.png',
87-
'alt' => t('OK'),
88-
))
89-
: theme('image', array(
90-
'path' => 'misc/watchdog-error.png',
91-
'alt' => t('Error'),
92-
)),
94+
? $r->render([
95+
'#theme' => 'image',
96+
'#path' => 'misc/watchdog-ok.png',
97+
'#alt' => t('OK'),
98+
])
99+
: $r->render([
100+
'#theme' => 'image',
101+
'#path' => 'misc/watchdog-error.png',
102+
'#alt' => t('Error'),
103+
]),
93104
$pass->result,
94-
);
105+
];
95106
}
96-
$ret = theme('table', [
97-
'header' => $header,
98-
'rows' => $data,
99-
'attributes' => [
107+
$ret = $r->render([
108+
'#theme' => 'table',
109+
'#header' => $header,
110+
'#rows' => $data,
111+
'#attributes' => [
100112
'id' => 'qa-results',
101113
],
102114
'#attached' => ['library' => ['qa/results']],
@@ -111,19 +123,20 @@ function qa_report_results() {
111123
* @return array
112124
*/
113125
function qa_report_form($form, $form_state) {
114-
$form = array();
126+
$form = [];
115127
$base = drupal_get_path('module', 'qa');
116128
$packages = Exportable::getClasses($base, 'Drupal\qa\BasePackage');
117129
ksort($packages);
118130
foreach ($packages as $package_name => $package) {
119131
$collapsed = TRUE;
120-
$form[$package_name] = array(
132+
$form[$package_name] = [
121133
'#type' => 'fieldset',
122-
'#title' => filter_xss_admin($package->title),
123-
'#description' => filter_xss_admin($package->description),
134+
'#title' => Xss::filterAdmin($package->title),
135+
'#description' => Xss::filterAdmin($package->description),
124136
'#collapsible' => TRUE,
125-
);
126-
$controls = $package->getClasses($package->dir, 'Drupal\qa\Plugin\Qa\Control\BaseControl');
137+
];
138+
$controls = $package->getClasses($package->dir,
139+
'Drupal\qa\Plugin\Qa\Control\BaseControl');
127140

128141
foreach ($controls as $control_name => $control) {
129142
$default_value = isset($_SESSION[$control_name])
@@ -133,37 +146,39 @@ function qa_report_form($form, $form_state) {
133146
$collapsed = FALSE;
134147
}
135148

136-
$deps = array();
149+
$deps = [];
137150
$met = TRUE;
138151
foreach ($control->getDependencies() as $dep_name) {
139-
if (module_exists($dep_name)) {
140-
$deps[] = t('@module (<span class="admin-enabled">available</span>)', array('@module' => $dep_name));
152+
if (Drupal::moduleHandler()->moduleExists($dep_name)) {
153+
$deps[] = t('@module (<span class="admin-enabled">available</span>)',
154+
['@module' => $dep_name]);
141155
}
142156
else {
143-
$deps[] = t('@module (<span class="admin-disabled">unavailable</span>)', array('@module' => $dep_name));
157+
$deps[] = t('@module (<span class="admin-disabled">unavailable</span>)',
158+
['@module' => $dep_name]);
144159
$met = FALSE;
145160
}
146161
}
147-
$form[$package_name][$control_name] = array(
148-
'#type' => 'checkbox',
162+
$form[$package_name][$control_name] = [
163+
'#type' => 'checkbox',
149164
'#default_value' => $met ? $default_value : 0,
150-
'#title' => filter_xss_admin($control->title),
151-
'#description' => filter_xss_admin($control->description),
152-
'#disabled' => !$met,
153-
);
154-
$form[$package_name][$control_name .'-dependencies'] = array(
155-
'#value' => t('Depends on: !dependencies', array(
165+
'#title' => Xss::filterAdmin($control->title),
166+
'#description' => Xss::filterAdmin($control->description),
167+
'#disabled' => !$met,
168+
];
169+
$form[$package_name][$control_name . '-dependencies'] = [
170+
'#value' => t('Depends on: !dependencies', [
156171
'!dependencies' => implode(', ', $deps),
157-
)),
172+
]),
158173
'#prefix' => '<div class="admin-dependencies">',
159174
'#suffix' => '</div>',
160-
);
175+
];
161176
}
162177
$form[$package_name]['#collapsed'] = $collapsed;
163178
}
164179

165180
$form['submit'] = [
166-
'#type' => 'submit',
181+
'#type' => 'submit',
167182
'#value' => t('Run controls'),
168183
];
169184

@@ -177,37 +192,40 @@ function qa_report_form($form, $form_state) {
177192
* @param array $form_state
178193
*/
179194
function qa_report_form_submit($form, &$form_state) {
180-
$controls = array();
195+
$controls = [];
181196
foreach ($form_state['values'] as $item => $value) {
182-
if (class_exists($item) && is_subclass_of($item, '\Drupal\qa\Plugin\Qa\Control\BaseControl')) {
197+
if (class_exists($item) && is_subclass_of($item,
198+
'\Drupal\qa\Plugin\Qa\Control\BaseControl')) {
183199
if ($value) {
184200
$controls[$item] = $value;
185201
}
186202
$_SESSION[$item] = $value;
187203
}
188204
elseif ($value == 1) {
189-
$args = array(
205+
$args = [
190206
'%control' => $item,
191-
);
192-
drupal_set_message(t('Requested invalid control %control', $args), 'error');
193-
watchdog('qa', 'Requested invalid control %control', $args, WATCHDOG_ERROR);
207+
];
208+
drupal_set_message(t('Requested invalid control %control', $args),
209+
'error');
210+
\Drupal::logger('qa')->error('Requested invalid control %control', $args);
194211
}
195212
}
196213

197-
drupal_set_message(t('Prepare to run these controls: @controls', array(
198-
'@controls' => implode(', ', array_keys($controls)))), 'status');
199-
$batch = array(
200-
'operations' => array(),
201-
'title' => t('QA Controls running'),
202-
'init_message' => t('QA Controls initializing'),
214+
drupal_set_message(t('Prepare to run these controls: @controls', [
215+
'@controls' => implode(', ', array_keys($controls)),
216+
]), 'status');
217+
$batch = [
218+
'operations' => [],
219+
'title' => t('QA Controls running'),
220+
'init_message' => t('QA Controls initializing'),
203221
// 'progress_message' => t('current: @current, Remaining: @remaining, Total: @total'),
204-
'error_message' => t('Error in QA Control'),
205-
'finished' => 'qa_report_finished',
222+
'error_message' => t('Error in QA Control'),
223+
'finished' => 'qa_report_finished',
206224
// 'file' => '', // only if outside module file
207-
);
225+
];
208226

209227
foreach ($controls as $item => $value) {
210-
$batch['operations'][] = array('qa_report_run_pass', array($item));
228+
$batch['operations'][] = ['qa_report_run_pass', [$item]];
211229
}
212230
batch_set($batch);
213231
}
@@ -218,23 +236,29 @@ function qa_report_form_submit($form, &$form_state) {
218236
* @return void
219237
*/
220238
function qa_report_run_pass($class_name, &$context) {
221-
$name_arg = array('@class' => $class_name);
239+
$name_arg = ['@class' => $class_name];
222240

223241
$control = new $class_name();
224242
if (!is_object($control)) {
225-
drupal_set_message(t('Cannot obtain an instance for @class', $name_arg), 'error');
226-
$context['results']['#message'] = t('Control @class failed to run.', $name_arg);
243+
drupal_set_message(t('Cannot obtain an instance for @class', $name_arg),
244+
'error');
245+
$context['results']['#message'] = t('Control @class failed to run.',
246+
$name_arg);
227247
$context['message'] = t('Control @class failed to run.', $name_arg);
228248
$context['results'][$class_name] = 'wow';
229249
}
230250
else {
231-
drupal_set_message(t('Running a control instance for @class', $name_arg), 'status');
251+
drupal_set_message(t('Running a control instance for @class', $name_arg),
252+
'status');
232253
$pass = $control->run();
233254
if (!$pass->status) {
234255
$context['success'] = FALSE;
235256
}
236257
$context['results']['#message'][] = t('Control @class ran', $name_arg);
237-
$context['message'] = theme('item_list', $context['results']['#message']);
258+
$context['message'] = \Drupal::service('renderer')->render([
259+
'#theme' => 'item_list',
260+
'#items' => $context['results']['#message'],
261+
]);
238262
$context['results'][$class_name] = $pass;
239263
}
240264
}

qa.services.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
services:
22
plugin.manager.qa_check:
33
class: Drupal\qa\Plugin\QaCheckManager
4+
arguments:
5+
- '@kernel'
46
parent: default_plugin_manager
57

68
logger.channel.qa:

src/Commands/QaCommands.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
namespace Drupal\qa\Commands;
66

77
use Drupal\qa\Controller\WorkflowsReportController;
8+
use Drupal\qa\Plugin\QaCheck\Dependencies\Undeclared;
89
use Drupal\qa\Plugin\QaCheck\References\Integrity;
910
use Drupal\qa\Plugin\QaCheck\References\TaxonomyIndex;
11+
use Drupal\qa\Plugin\QaCheck\System\ExternalCode;
1012
use Drupal\qa\Plugin\QaCheck\System\UnusedExtensions;
1113
use Drupal\qa\Plugin\QaCheckManager;
1214
use Drupal\qa\Workflows\ContentModerationGraph;
@@ -123,6 +125,29 @@ public function runPlugin(string $name): void {
123125
$this->output->writeln(Yaml::dump($res, 5, 2));
124126
}
125127

128+
129+
/**
130+
* Show undeclared function-based dependencies.
131+
*
132+
* @command qa:dependencies:undeclared
133+
*
134+
* @throws \Drupal\Component\Plugin\Exception\PluginException
135+
*/
136+
public function dependenciesUndeclared() {
137+
$this->runPlugin(Undeclared::NAME);
138+
}
139+
140+
/**
141+
* Show code which is either external or without an identifiable source.
142+
*
143+
* @command qa:system:external
144+
*
145+
* @throws \Drupal\Component\Plugin\Exception\PluginException
146+
*/
147+
public function externalCode() {
148+
$this->runPlugin(ExternalCode::NAME);
149+
}
150+
126151
/**
127152
* Show broken entity_reference fields.
128153
*

src/Dependencies.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Dependencies {
3434
protected $logger;
3535

3636
/**
37-
* The extension_list.module service.
37+
* The extension.list.module service.
3838
*
3939
* @var \Drupal\Core\Extension\ModuleExtensionList
4040
*/
@@ -48,7 +48,7 @@ class Dependencies {
4848
protected $pimple;
4949

5050
/**
51-
* The extension_list.theme service.
51+
* The extension.list.theme service.
5252
*
5353
* @var \Drupal\Core\Extension\ThemeExtensionList
5454
*/
@@ -58,9 +58,9 @@ class Dependencies {
5858
* Dependencies constructor.
5959
*
6060
* @param \Drupal\Core\Extension\ModuleExtensionList $moduleExtensionList
61-
* The extension_list.module service.
61+
* The extension.list.module service.
6262
* @param \Drupal\Core\Extension\ThemeExtensionList $themeExtensionList
63-
* The extension_list.theme service.
63+
* The extension.list.theme service.
6464
* @param \Psr\Log\LoggerInterface $logger
6565
* A logger service.
6666
*/

0 commit comments

Comments
 (0)