forked from backdrop-contrib/coder_review
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoder_review.module
1012 lines (958 loc) · 34.1 KB
/
coder_review.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Developer module to assist with coder reviews and API upgrade suggestions.
*
* Developer Module that assists with code review and version upgrade that
* supports a plug-in extensible hook system so contributed modules can define
* additional review standards.
*
* Built-in support for:
* - Backdrop Coding Standards - http://backdrop.org/node/318
* - Handle text in a secure fashion - http://backdrop.org/node/28984
* - Converting 4.6.x modules to 4.7.x - http://backdrop.org/node/22218
* - Converting 4.7.x modules to 5.x - http://backdrop.org/node/64279
* - Converting 5.x modules to 6.x - http://backdrop.org/node/114774
* - Comment Standards - http://backdrop.org/node/1354
* - SQL coding conventions - http://backdrop.org/node/2497
*
* Credit also to dries:
* - http://cvs.backdrop.org/viewcvs/backdrop/backdrop/scripts/code-style.pl
*/
/**
* Implements hook_flush_caches().
*/
function coder_review_flush_caches() {
return array('cache_coder');
}
/**
* Implements hook_help().
*/
function coder_review_help($page, $arg) {
switch ($page) {
case 'coder_review#disclaimer':
return _t('Coder provides helpful hints trying to minimize false positives, but offers no guarantees. You are the final arbitrar. If in doubt, please read the Backdrop documentation (see review links below and <a href="@api">api.backdrop.org</a>).', array('@api' => 'http://api.backdrop.org'));
}
}
/**
* Implements hook_permission().
*/
function coder_review_permission() {
$permission['view code review'] = array(
'title' => t('View code review'),
'description' => t('Perform code reviews on modules and themes.'),
);
$permission['view code review all'] = array(
'title' => t('View all code reviews'),
'description' => t('Perform code reviews on modules and themes, using the "all" modules feature.'),
);
return $permission;
}
/**
* Implements hook_menu().
*/
function coder_review_menu() {
$items['admin/config/development/coder-review'] = array(
'title' => 'Review',
'page callback' => 'backdrop_get_form',
'page arguments' => array('coder_review_page_form'),
'access arguments' => array('view code review'),
'weight' => -2,
);
// @ignore upgrade6x_1
foreach (array('select', 'default', 'active', 'core', 'all', 'files', 'patches') as $delta => $option) {
$items["admin/config/development/coder-review/$option"] = array(
'title' => backdrop_ucfirst($option),
'page callback' => 'backdrop_get_form',
'page arguments' => array('coder_review_page_form', $option),
'access arguments' => array('view code review'),
'type' => $delta ? MENU_LOCAL_TASK : MENU_DEFAULT_LOCAL_TASK,
'weight' => $delta,
);
}
$items['admin/config/development/coder-review/settings'] = array(
'title' => 'Settings',
'description' => 'Select code review plugins and modules',
'page callback' => 'backdrop_get_form',
'page arguments' => array('coder_review_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_LOCAL_TASK,
'file' => 'coder_review.admin.inc',
'weight' => 9,
);
return $items;
}
/**
* Implements hook_menu_alter().
*/
function coder_review_menu_alter(&$items) {
$items['admin/config/development/coder']['page callback'] = 'backdrop_get_form';
$items['admin/config/development/coder']['page arguments'] = array('coder_review_page_form');
}
/**
* Implements hook_form_FORMID_alter().
*
* Modify the module display view by adding a Coder Review link to every module
* description.
*/
function coder_review_form_system_modules_alter(&$form, &$form_state, $form_id) {
if (!user_access('view code review') || isset($form['confirm'])) {
return;
}
$path = backdrop_get_path('module', 'coder_review');
foreach ($form['modules'] as $package_name => &$package) {
if ($package_name[0] != '#') {
foreach ($package as $module_name => &$module) {
if ($module_name[0] != '#') {
$module['links']['#type'] = 'dropbutton';
$module['links']['#links']['coder_review'] = array(
'title' => t('Code review'),
'href' => "admin/config/development/coder-review/$module_name",
'attributes' => array('class' => array('module-link', 'module-link-coder-review'), 'title' => t('Coder review')),
);
}
}
}
}
}
/**
* Form constructor: Builds settings form API array for coder_review.
*
* Generates a form with the default reviews and default modules/themes to
* run Coder on.
*
* Actual forms may have additional sections added to them, this is simply a
* base.
*
* @param array $settings
* Settings array for coder_review in the format of
* _coder_review_get_default_settings().
* @param array $system
* Array of module and theme information, in form string theme/module
* name => boolean TRUE if checked by coder_review already, passed by
* reference.
* @param array $files
* Associative array of files, in form string theme/module name => string
* filename to check, passed by reference.
*
* @return array
* Array for form API for the settings box.
*
* @see coder_review_page_form()
*/
function _coder_review_settings_form(array $settings, &$system, &$files) {
// Bootstrap enough to run.
if (!function_exists('_coder_review_reviews')) {
require_once realpath(__DIR__) . '/coder_review.common.inc';
}
// Add the javascript.
if (function_exists('backdrop_get_path')) {
$form['#attached']['js'][] = _coder_review_path() . '/coder_review.js';
}
// Create the list of review options from the coder review plug-ins.
// Maintain a secondary list based on #title only, to make sorting possible.
$reviews = _coder_review_reviews();
foreach ($reviews as $name => $review) {
$review_options[$name] = isset($review['#link']) ? _l($review['#title'], $review['#link']) : $review['#title'];
if (isset($review['#description'])) {
$review_options[$name] .= ' (' . _t($review['#description']) . ')';
}
$review_sort[$name] = _t($review['#title']);
}
// Sort the reviews by #title.
asort($review_sort);
foreach ($review_sort as $name => $review) {
$review_sort[$name] = $review_options[$name];
}
// What reviews should be used?
$form['coder_reviews_group'] = array(
'#type' => 'fieldset',
'#title' => _t('Reviews'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['coder_reviews_group']['coder_reviews'] = array(
'#type' => 'checkboxes',
'#options' => $review_sort,
'#description' => _t('apply the checked coding reviews'),
'#default_value' => $settings['coder_reviews'],
);
// What severities should be used?
$form['coder_reviews_group']['coder_severity'] = array(
'#type' => 'radios',
'#options' => array(
SEVERITY_MINOR => 'minor (most)',
SEVERITY_NORMAL => 'normal',
SEVERITY_CRITICAL => 'critical (fewest)',
),
'#description' => _t('show warnings at or above the severity warning level'),
'#default_value' => $settings['coder_severity'],
);
$form['coder_reviews_group']['coder_ignore'] = array(
'#type' => 'checkbox',
'#default_value' => $settings['coder_ignore'],
'#title' => _t('handle code ignores'),
'#description' => _t('Coder is a helpful assistant, not a Drill Sergant. Since the developer should always know better than Coder, the <a href="!help">Coder @ignore system</a> allows developers to tell coder to ignore certain warnings. Uncheck this field to see all warnings, even those that are ignored.', array('!help' => _backdropnode(1772676))),
);
if ($settings['coder_files']) {
$form['coder_what'] = array(
'#type' => 'fieldset',
'#title' => _t('What to review'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['coder_what']['coder_file_list'] = array(
'#type' => 'textarea',
'#title' => _t('List of files'),
'#description' => _t('List each relative file path on a separate line without a / at the start. For example: %example_path', array('%example_path' => 'sites/all/modules/contrib/coder/coder.module')),
'#default_value' => $settings['coder_file_list'],
);
$form['coder_files'] = array(
'#type' => 'value',
'#value' => 1,
);
foreach (explode("\n", $settings['coder_file_list']) as $line) {
$line = trim($line);
$system[$line] = $line;
$files[$line] = $line;
}
}
elseif ($settings['coder_patches']) {
// Display what to review options.
$form['coder_what'] = array(
'#type' => 'fieldset',
'#title' => _t('What to review'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['coder_what']['coder_patch_help'] = array(
'#markup' => '<p>' . _t('All patches must be in unified format. It\'s also preferable for the patch to be created with the "-p" option, which shows the function closest to the code change. Without this, some function dependent tests may not be triggered. Coder offers no guarantee that the patch will apply cleanly or will function correctly.') . '</p>',
'#weight' => -4,
);
$form['coder_what']['coder_patch_link'] = array(
'#type' => 'textfield',
'#title' => _t('Link to patch'),
'#size' => 150,
'#default_value' => $settings['coder_patch_link'],
'#description' => _t('The path should be relative to the backdrop home directory, without a / at the start, for example: %example_path', array('%example_path' => backdrop_get_path('module', 'coder') . '/name-of-file.patch')),
'#weight' => -3,
);
$form['coder_what']['coder_patch_or'] = array(
'#markup' => '<p>' . _t('Or') . '</p>',
'#weight' => -2,
);
$form['coder_what']['coder_patch_text'] = array(
'#type' => 'textarea',
'#title' => _t('Patch text'),
'#rows' => 20,
'#weight' => -1,
'#default_value' => $settings['coder_patch_text'],
'#description' => _t('Paste the contents of the patch file here.'),
'#attributes' => array('wrap' => 'OFF'),
);
$form['coder_patches'] = array(
'#type' => 'value',
'#value' => 1,
);
$in_patch = 0;
$patch = $link_contents = $textarea_contents = '';
$patches = array();
if ($settings['coder_patch_link']) {
$link_contents = file_get_contents($settings['coder_patch_link']);
}
if ($settings['coder_patch_text']) {
$textarea_contents = $settings['coder_patch_text'];
}
$patch_contents = $link_contents . "\n" . $textarea_contents;
$lines = preg_split("/(\r\n|\n)/", $patch_contents);
foreach ($lines as $line) {
if ($line == '') {
continue;
}
if (preg_match("/^\+\+\+\s+([\w\.\-\/]+)\s*.*?$/", $line, $matches)) {
if (!empty($patch)) {
$patches[$filename . ': ' . $patch_line_numbers] = $patch;
$system[$filename . ': ' . $patch_line_numbers] = $filename;
$patch = '';
}
$filename = $matches[1];
}
elseif (preg_match("/^(@@\s+\-\d+,\d+\s+\+\d+,\d+\s+@@)\s*((function|(protected|private|public)*\s*class)\s([\w]+).*?)*$/", $line, $matches)) {
if (!empty($patch)) {
$patches["$filename: $patch_line_numbers"] = $patch;
$system["$filename: $patch_line_numbers"] = $filename;
$patch = '';
}
if (isset($matches[3]) && isset($matches[4])) {
if ($matches[3] == 'function') {
$function_current = $matches[4];
$patch = 'function ' . $function_current . "() {\n";
}
else {
$class_current = $matches[4];
$patch = $matches[2] . "\n";
}
}
$patch_line_numbers = $matches[1];
}
elseif (preg_match("/^\+(?!\+)/", $line)) {
$patch .= ltrim($line, '+') . "\n";
$in_patch = 1;
}
elseif (preg_match("/^\s/", $line)) {
$patch .= backdrop_substr($line, 1) . "\n";
$in_patch = 1;
}
else {
$in_patch = 0;
}
}
if (!empty($patch)) {
$patches[$filename . ': ' . $patch_line_numbers] = $patch;
$system[$filename . ': ' . $patch_line_numbers] = $filename;
$patch = '';
}
$files = $patches;
}
elseif (function_exists('db_query')) {
// Get the modules and theme.
$sql = "SELECT name, filename, type, status FROM {system} WHERE type='module' OR type='theme' ORDER BY weight ASC, filename ASC";
$result = db_query($sql);
$system_modules = $system_themes = $system_core = array();
foreach ($result as $system) {
$display_name = $system->name;
if ($system->status) {
$display_name .= ' (' . _t('active') . ')';
$system_active[$system->name] = $system->name;
}
if (_coder_review_is_backdrop_core($system)) {
$display_name .= ' (' . _t('core') . ')';
$system_core[$system->name] = $system->name;
}
if ($system->type == 'module') {
$system_modules[$system->name] = $system->name;
}
else {
$system_themes[$system->name] = $system->name;
}
$system_links[$system->name] = _l($display_name, "admin/config/development/coder-review/$system->name");
$files[$system->name] = $system->filename;
}
asort($system_links);
// Display what to review options.
$form['coder_what'] = array(
'#type' => 'fieldset',
'#title' => _t('What to review'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
// @note: Should rename var.
$form['coder_what']['coder_active_modules'] = array(
'#type' => 'checkbox',
'#default_value' => $settings['coder_active_modules'],
'#title' => _t('active modules and themes'),
);
$form['coder_what']['coder_core'] = array(
'#type' => 'checkbox',
'#default_value' => $settings['coder_core'],
'#title' => _t('core files (php, modules, and includes)'),
);
$ext = _coder_review_php_ext();
$form['coder_what']['coder_includes'] = array(
'#type' => 'checkbox',
'#default_value' => $settings['coder_includes'],
// @ignore security_fapi_title
'#title' => _t('include files (@ext)', array('@ext' => implode(' | ', $ext))),
);
$form['coder_what']['coder_includes_exclusion_fieldset'] = array(
'#type' => 'fieldset',
'#title' => _t('Include file exclusions'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['coder_what']['coder_includes_exclusion_fieldset']['coder_includes_exclusions'] = array(
'#rows' => 3,
'#type' => 'textarea',
'#default_value' => $settings['coder_includes_exclusions'],
'#description' => _t('List file names or paths, one per line, which should be excluded (only valid if "include files" is checked above). For example, modules/system/*.php will exclude all php files in the modules/system directory.'),
);
// Display the modules in a fieldset.
$form['coder_what']['coder_modules'] = array(
'#type' => 'fieldset',
'#title' => _t('Select specific modules'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'checkboxes' => array(
'#theme' => 'coder_review_table_cols',
'#cols' => 3,
),
);
if ($settings['coder_all']) {
$modules = $system_modules;
}
elseif ($settings['coder_active_modules']) {
if ($settings['coder_core']) {
$modules = array_intersect($system_active, $system_core);
$modules = array_intersect($modules, $system_modules);
}
else {
$modules = array_intersect($system_active, $system_modules);
}
}
elseif ($settings['coder_core']) {
$modules = array_intersect($system_core, $system_modules);
}
elseif ($settings['coder_contrib']) {
$modules = array_diff($system_modules, array_intersect($system_core, $system_modules));
}
else {
$modules = isset($settings['coder_modules']) && is_array($settings['coder_modules']) ? $settings['coder_modules'] : array();
}
// Display the themes in a fieldset.
$form['coder_what']['coder_themes'] = array(
'#type' => 'fieldset',
'#title' => _t('Select specific themes'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'checkboxes' => array(
'#theme' => 'coder_review_table_cols',
),
);
if ($settings['coder_all']) {
$themes = $system_themes;
}
elseif ($settings['coder_active_modules']) {
if ($settings['coder_core']) {
$themes = array_intersect($system_active, $system_core);
$themes = array_intersect($themes, $system_themes);
}
else {
$themes = array_intersect($system_active, $system_themes);
}
}
elseif (isset($settings['coder_core']) && $settings['coder_core']) {
$themes = array_intersect($system_core, $system_themes);
}
elseif ($settings['coder_active_modules']) {
$themes = array_intersect($system_active, $system_themes);
}
elseif ($settings['coder_contrib']) {
$themes = array_diff($system_themes, array_intersect($system_core, $system_themes));
}
else {
$themes = isset($settings['coder_themes']) && is_array($settings['coder_themes']) ? $settings['coder_themes'] : array();
}
foreach ($system_links as $name => $link) {
$classes = array();
if (in_array($name, $system_active)) {
$classes[] = 'coder-active';
}
if (in_array($name, $system_core)) {
$classes[] = 'coder-core';
}
if (in_array($name, $system_themes)) {
$type = 'theme';
$default_value = isset($themes[$name]);
}
else {
$type = 'module';
$default_value = isset($modules[$name]);
}
$form['coder_what']["coder_${type}s"]['checkboxes']["coder_${type}s-$name"] = array(
'#type' => 'checkbox',
// @ignore security_fapi_title
'#title' => $link,
'#default_value' => $default_value,
'#attributes' => array('class' => $classes),
);
}
$system = array_merge($modules, $themes);
}
return $form;
}
/**
* Generates a settings array for either modules or themes names.
*
* @param array $form_state
* The form state array that is passed to a submit function, passed by
* reference. Note: As entries are processed, they are removed for the sake
* of efficiency.
* @param string $type
* String type to generate settings for, either 'module' or 'theme'.
*
* @return array
* An associative array with keys of module/theme name and a value of 1.
*
* @see coder_review_page_form()
*/
function _coder_review_settings_array(array &$form_state, $type) {
$typekey = "coder_{$type}s-";
$typelen = _strlen($typekey);
$systems = array();
foreach ($form_state['storage'] as $key => $value) {
if (_substr($key, 0, $typelen) == $typekey) {
if ($value == 1) {
$system = _substr($key, $typelen);
$systems[$system] = 1;
}
unset($form_state['storage'][$key]);
unset($form_state['values'][$key]);
}
}
return $systems;
}
/**
* Implements hook_submit().
*/
function coder_review_page_form_submit($form, &$form_state) {
// In D7, setting values in this variable does not trigger a form rebuild.
$form_state['storage'] = $form_state['values'];
// Rebuild form with user selections.
$form_state['rebuild'] = TRUE;
}
/**
* Implements hook_form().
*
* Implements coder_review's main form, in which a user can select reviews and
* modules/themes to run them on.
*
* @see _coder_review_settings_array()
* @see _coder_review_settings_form()
*/
function coder_review_page_form($form, &$form_state, $arg = '') {
// Bootstrap enough to run.
if (!function_exists('_coder_review_reviews')) {
require_once 'coder_review.common.inc';
}
if (isset($form_state['storage'])) {
$settings = $form_state['storage'];
$settings['coder_modules'] = _coder_review_settings_array($form_state, 'module');
$settings['coder_themes'] = _coder_review_settings_array($form_state, 'theme');
if (function_exists('backdrop_set_title')) {
backdrop_set_title(t('Code review (submitted options)'), PASS_THROUGH);
}
}
else {
$settings = _coder_review_get_default_settings($arg);
if ($arg && function_exists('backdrop_set_title')) {
backdrop_set_title(t('Code review (@options)', array('@options' => isset($arg) ? $arg : 'default options')), PASS_THROUGH);
}
}
$settings += array(
'coder_patches' => '',
'coder_patch_link' => '',
'coder_patch_text' => '',
'coder_all' => 0,
'coder_core' => 0,
'coder_contrib' => 0,
'coder_includes' => 0,
'coder_includes_exclusions' => '',
'coder_files' => 0,
'coder_files_list' => '',
'coder_active_modules' => 0,
'coder_ignore' => 1,
);
$coder_form = _coder_review_settings_form($settings, $system, $files);
if ($coder_form) {
// Get this list of the reviews to perform.
$avail_reviews = _coder_review_reviews();
$reviews = array();
foreach ($settings['coder_reviews'] as $name => $checked) {
if ($checked) {
$reviews[$name] = $avail_reviews[$name];
}
}
// Code review non-module core files.
$module_weight = 0;
if ($settings['coder_core'] && function_exists('backdrop_alter')) {
$php_extensions = config_get('coder_review.settings', 'php_ext');
$coder_args = array(
'#reviews' => $reviews,
'#severity' => $settings['coder_severity'],
'#php_extensions' => $php_extensions,
'#include_extensions' => array(),
'#cache' => TRUE,
'#settings_ignore' => $settings['coder_ignore'],
);
backdrop_alter('coder_review_args', $coder_args);
// Review core PHP files.
$form['core_php'] = array(
'#type' => 'fieldset',
'#title' => 'core (php)',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => ++$module_weight,
);
$phpfiles = _file_list('./core', '/\.php$/', FALSE);
sort($phpfiles);
_coder_review_page_form_includes(
$form,
$coder_args,
'core_php',
$phpfiles,
2,
0,
$settings['coder_includes_exclusions']);
// Review core include files.
$form['core_includes'] = array(
'#type' => 'fieldset',
'#title' => 'core (includes)',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => ++$module_weight,
);
$includefiles = _file_list('core/includes', '/\.inc$/');
sort($includefiles);
_coder_review_page_form_includes(
$form,
$coder_args,
'core_includes',
$includefiles,
0,
$settings['coder_includes_exclusions']);
}
// Loop through the selected modules and themes.
if (isset($system)) {
// Determine which file extensions to include, based on the rules.
$php_extensions = _coder_review_php_ext();
$include_extensions = _coder_review_get_reviews_extensions($php_extensions, $reviews);
$includes = array_merge($php_extensions, $include_extensions);
// Avoid duplicate includes.
$dups = $stats = array();
$patch = '';
foreach ($system as $name => $checked) {
if ($checked) {
$filename = isset($files[$name]) ? $files[$name] : FALSE;
if (!$filename) {
_message(t('Code review file for %module not found', array('%module' => $name)), 'error');
continue;
}
if ($settings['coder_patches']) {
$patch = $filename;
$filename = $name;
}
$pathinfo = pathinfo($filename);
$coder_args = array(
'#reviews' => $reviews,
'#severity' => $settings['coder_severity'],
'#filename' => $filename,
'#patch' => $patch,
'#php_extensions' => $php_extensions,
'#include_extensions' => $include_extensions,
'#cache' => TRUE,
'#settings_ignore' => $settings['coder_ignore'],
);
if (function_exists('backdrop_alter')) {
backdrop_alter('coder_review_args', $coder_args);
}
// Process this one file.
$results = do_coder_reviews($coder_args);
$stats[$filename] = $results['#stats'];
unset($results['#stats']);
if (function_exists('module_invoke_all')) {
module_invoke_all('coder_review_results_view', $results);
}
// Output the results in a collapsible fieldset.
$form[$name] = array(
'#type' => 'fieldset',
// @ignore security_fapi_title
'#title' => $filename,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => ++$module_weight,
);
if ($results) {
$form[$name]['#collapsed'] = FALSE;
}
else {
$results[] = _t('No Problems Found');
}
$form[$name]['output'] = _coder_review_form_output($name, $filename, $results);
$form[$name]['output']['#weight'] = -1;
// Process the same directory include files.
if ($settings['coder_includes']) {
$path = str_replace('\\', '/', dirname(realpath($filename)));
if ($path && !isset($dups[$path])) {
if ($pathinfo['extension'] == 'module') {
$coder_args['#php_minor'] = 1;
}
$dups[$path] = 1;
$regex = '/\.(' . implode('|', $includes) . ')$/';
$includefiles = _file_list($pathinfo['dirname'], $regex);
$includefiles = array_diff($includefiles, $system);
if ($includefiles) {
sort($includefiles);
$offset = strpos($filename, dirname($filename));
$stats[$filename]['#includes'] = _coder_review_page_form_includes(
$form,
$coder_args,
$name,
$includefiles,
$offset,
$settings['coder_includes_exclusions']);
}
}
}
}
}
if ($stats) {
$summary = array('files' => 0, 'sums' => array('minor' => 0, 'normal' => 0, 'critical' => 0, 'ignored' => 0));
foreach ($stats as $stat) {
++$summary['files'];
foreach (array_keys($summary['sums']) as $key) {
$summary['sums'][$key] += $stat[$key];
}
if (isset($stat['#includes'])) {
foreach ($stat['#includes'] as $includestat) {
++$summary['files'];
foreach (array_keys($summary['sums']) as $key) {
$summary['sums'][$key] += $includestat[$key];
}
}
}
}
if ($settings['coder_patches']) {
$display[] = _t('Coder found @count patch hunks', array('@count' => count($stats)));
}
else {
$display[] = _t('Coder found @count projects', array('@count' => count($stats)));
$display[] = _t('@count files', array('@count' => $summary['files']));
}
foreach (array('critical', 'normal', 'minor') as $severity_name) {
if ($summary['sums'][$severity_name] > 0) {
$display[] = _t('@count %severity_name warnings', array('@count' => $summary['sums'][$severity_name], '%severity_name' => $severity_name));
}
}
$display[] = _t('@count warnings were flagged to be ignored', array('@count' => $summary['sums']['ignored']));
_message(implode(', ', $display));
if (_drush()) {
coder_review_print_drush_messages($summary);
}
}
}
if (!_drush()) {
// Prepend the settings form.
$form['settings'] = array(
'#type' => 'fieldset',
'#title' => _t('Selection form'),
'#collapsible' => TRUE,
'#collapsed' => !empty($form),
'#weight' => -2,
);
$form['disclaimer'] = array(
'#prefix' => '<div class="coder-disclaimer">',
'#markup' => coder_review_help('coder_review#disclaimer', array()),
'#suffix' => '</div>',
'#weight' => -3,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => _t('Run reviews'),
'#weight' => -1,
);
if ($form['settings']['#collapsed']) {
$form['settings']['#prefix'] = _t('<div class="coder-settings">Use the Selection form to select options for this code review, or change the <a href="@settings">Default Settings</a> and use the <a href="@default">Default</a> tab above.</div>', array('@settings' => url('admin/config/development/coder-review/settings'), '@default' => url('admin/config/development/coder-review/default')));
}
$form['settings'][] = $coder_form;
if (function_exists('backdrop_get_path')) {
$form['#attached']['css'][] = _coder_review_path() . '/coder_review.css';
}
}
}
return $form;
}
/**
* Adds results to form definition for display on the coder review page.
*
* @param array $form
* Form array definition to be modified, passed by reference.
* @param array $coder_args
* An array of Coder settings. See do_coder_reviews() for details.
* @param string $name
* Name of the form element.
* @param array $files
* An array of file objects to check and display the results for. See
* file_scan_directory() for details.
* @param int $offset
* An integer offset to munge filenames with.
* @param array $exclusions
* An array of exclusions that should be ignored in the $files array.
*
* @return array
* Statistics array in form: string filename => array value of '#stats' from
* do_coder_reviews().
*
* @see do_coder_review()
* @see file_scan_directory()
*/
function _coder_review_page_form_includes(array &$form, array $coder_args, $name, array $files, $offset, $exclusions) {
$stats = array();
$coder_args['#name'] = $name;
$weight = 0;
$exclusions = str_replace(array("\r\n", "\r", "\n", '/', '*', '.'), array('|', '|', '|', '\\/', '.*', '\\.'), $exclusions);
foreach ($files as $file) {
if (!empty($exclusions) && preg_match("/$exclusions/", $file)) {
// Don't review this file.
continue;
}
$filename = _substr($file, $offset);
$php_extensions = _coder_review_php_ext();
$coder_args['#filename'] = $filename;
$coder_args['#php_extensions'] = $php_extensions;
$results = do_coder_reviews($coder_args);
$stats[$filename] = $results['#stats'];
unset($results['#stats']);
if (function_exists('module_invoke_all')) {
module_invoke_all('coder_review_results_view', $results);
}
// Output the results in a collapsible fieldset.
$form[$name][$filename] = array(
'#type' => 'fieldset',
'#title' => $filename,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => ++$weight,
);
if ($results) {
$form[$name][$filename]['#collapsed'] = FALSE;
$form[$name]['#collapsed'] = FALSE;
}
else {
$results[] = _t('No Problems Found');
}
if (_drush()) {
theme_drush_coder_review(array('name' => $name, 'filename' => $filename, 'results' => $results));
}
else {
$form[$name][$filename]['output'] = array(
'#theme' => 'coder_review',
'#name' => $name,
'#filename' => $filename,
'#results' => $results,
);
}
}
return $stats;
}
/**
* ???
*
* @param ??? $name
* ???
* @param ??? $filename
* ???
* @param ??? $results
* ???
*
* @return array
* ???
*/
function _coder_review_form_output($name, $filename, $results) {
$output = array(
'#theme' => 'coder_review',
'#name' => $name,
'#filename' => $filename,
'#results' => $results,
);
if (_drush()) {
theme_drush_coder_review(array('name' => $name, 'filename' => $filename, 'results' => $results));
}
return $output;
}
/**
* Implements hook_simpletest().
*/
function coder_review_simpletest() {
return array_keys(file_scan_directory(_coder_review_file_path() . '/tests', '/\.test/'));
}
/**
* @{ Theme functions.
*/
/**
* Implements hook_theme().
*/
function coder_review_theme() {
return array(
'coder_review' => array(
'variables' => array(
'name' => NULL,
'filename' => NULL,
'results' => NULL,
),
),
'coder_review_table_cols' => array(
'render element' => 'form',
),
);
}
/**
* Returns HTML for line entry in coder_review_form() and the results produced.
*
* @param array $variables
* An associative array containing the following keys:
* - filename: The filename that was checked as a string.
* - results: An array that lists of the results (in HTML format) to display.
* See do_coder_reviews() for format.
*
* @ingroup themeable
*/
function theme_coder_review($variables) {
$output = '<h2>' . basename($variables['filename']) . '</h2>';
if (!empty($variables['results'])) {
$output .= theme('item_list', array('items' => $variables['results']));
}
return '<div class="coder">' . $output . '</div>';
}
/**
* Returns HTML for table column elements of coder_review_form().
*
* This function themes the radio buttons and check boxes form elements that
* might exist in a table column.
*
* @param array $variables
* An associative array containing the follwoing keys:
* - form: An array with the form definition elements.
*
* @ingroup themeable
*/
function theme_coder_review_table_cols($variables) {
if (!isset($variables['form']) || !is_array($variables['form'])) {
return '';
}
$form = $variables['form'];
$total = 0;
$cols = isset($form['#cols']) ? $form['#cols'] : 3;
foreach ($form as $element_id => $element) {
if ($element_id[0] != '#') {
++$total;
}
}
$total = (int) (($total % $cols) ? (($total + $cols - 1) / $cols) : ($total / $cols));
$pos = 0;
$rows = array();
foreach ($form as $element_id => $element) {
if ($element_id[0] != '#') {
++$pos;
$row = $pos % $total;
$col = $pos / $total;
if (!isset($rows[$row])) {
$rows[$row] = array();
}
$rows[$row][$col] = backdrop_render($element);
}
}
return theme('table', array('rows' => $rows, 'attributes' => array('id' => 'filter-order')));
}
/**
* @} End of Theme functions.
*/