-
Notifications
You must be signed in to change notification settings - Fork 22
/
phpcs-scan.php
1579 lines (1374 loc) · 35.4 KB
/
phpcs-scan.php
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
/**
* PHPCS scanning logic for vip-go-ci.
*
* @package Automattic/vip-go-ci
*/
declare(strict_types=1);
/**
* Get PHPCS version number.
*
* @param string $phpcs_path Path to PHPCS scanner.
* @param string $phpcs_php_path Path to PHP to use to execute PHPCS scanner.
*
* @return string|null PHPCS version number, null on failure.
*/
function vipgoci_phpcs_get_version(
string $phpcs_path,
string $phpcs_php_path
) :string|null {
$cache_id = array(
__FUNCTION__,
$phpcs_path,
$phpcs_php_path,
);
$cached_data = vipgoci_cache( $cache_id );
if ( false !== $cached_data ) {
return $cached_data;
}
$cmd = sprintf(
'%s %s %s',
escapeshellcmd( $phpcs_php_path ),
escapeshellcmd( $phpcs_path ),
escapeshellarg( '--version' )
);
$phpcs_output_2 = '';
$phpcs_result_code = -255;
$phpcs_output = vipgoci_runtime_measure_exec_with_retry(
$cmd,
array( 0 ),
$phpcs_output_2,
$phpcs_result_code,
'phpcs_cli',
false
);
if ( null === $phpcs_output ) {
vipgoci_sysexit(
'Unable to run PHPCS due to error',
array(
'cmd' => $cmd,
'output' => $phpcs_output,
),
VIPGOCI_EXIT_SYSTEM_PROBLEM
);
}
$phpcs_output = str_replace(
array( 'PHP_CodeSniffer ', 'version ' ),
array( '', '' ),
$phpcs_output
);
$phpcs_output_arr = explode(
' ',
$phpcs_output
);
// If something went wrong, return null.
if (
( ! isset( $phpcs_output_arr[0] ) ) ||
( empty( $phpcs_output_arr[0] ) )
) {
return null;
}
$phpcs_version_str = $phpcs_output_arr[0];
vipgoci_log(
'PHPCS version retrieved',
array(
'phpcs-path' => $phpcs_path,
'phpcs-version' => $phpcs_version_str,
),
2
);
vipgoci_cache( $cache_id, $phpcs_version_str );
return $phpcs_version_str;
}
/**
* Run PHPCS for the file specified, using the
* appropriate standards. Return the results.
*
* @param string $filename_tmp Path to file to scan.
* @param string $phpcs_path Path to PHPCS scanner.
* @param string $phpcs_php_path Path to PHP to use to execute PHPCS scanner.
* @param string|array $phpcs_standard PHPCS standard to use.
* @param string|array $phpcs_sniffs_exclude PHPCS sniffs to exclude.
* @param int $phpcs_severity PHPCS severity level to report.
* @param array $phpcs_runtime_set PHPCS runtime settings.
*
* @return string|null Results of PHPCS scanning as string on success, null on failure.
*/
function vipgoci_phpcs_do_scan(
string $filename_tmp,
string $phpcs_path,
string $phpcs_php_path,
array|string $phpcs_standard,
array|string $phpcs_sniffs_exclude,
int $phpcs_severity,
array $phpcs_runtime_set
) :string|null {
/*
* Run PHPCS from the shell, making sure we escape everything.
*
* Feed PHPCS the temporary file specified by our caller.
*/
$cmd = sprintf(
'%s -d memory_limit=500M -d max_execution_time=300 %s --severity=%s --report=%s',
escapeshellcmd( $phpcs_php_path ),
escapeshellcmd( $phpcs_path ),
escapeshellarg( (string) $phpcs_severity ),
escapeshellarg( 'json' )
);
/*
* Add standard to the command-line string.
*/
if ( is_array(
$phpcs_standard
) ) {
$phpcs_standard = join(
',',
$phpcs_standard
);
}
if ( ! empty( $phpcs_standard ) ) {
$cmd .= sprintf(
' --standard=%s',
escapeshellarg( $phpcs_standard )
);
}
/*
* If we have sniffs to exclude, add them
* to the command-line string.
*/
if ( is_array(
$phpcs_sniffs_exclude
) ) {
$phpcs_sniffs_exclude = join(
',',
$phpcs_sniffs_exclude
);
}
if ( ! empty( $phpcs_sniffs_exclude ) ) {
$cmd .= sprintf(
' --exclude=%s',
escapeshellarg( $phpcs_sniffs_exclude )
);
}
/*
* If we have specific runtime-set values,
* put them in them now.
*/
if ( ! empty( $phpcs_runtime_set ) ) {
foreach (
$phpcs_runtime_set as
$phpcs_runtime_set_value
) {
$cmd .= sprintf(
' --runtime-set %s %s',
escapeshellarg( $phpcs_runtime_set_value[0] ),
escapeshellarg( $phpcs_runtime_set_value[1] )
);
}
}
/*
* Lastly, append the target filename
* to the command-line string.
*/
$cmd .= sprintf(
' %s',
escapeshellarg( $filename_tmp )
);
vipgoci_log(
'Running PHPCS now',
array(
'cmd' => $cmd,
),
0
);
/*
* Actually execute
*/
$result_output = '';
$result_code = -255;
$result = vipgoci_runtime_measure_exec_with_retry(
$cmd,
array( 0, 1, 2 ),
$result_output,
$result_code,
'phpcs_cli',
true
);
return $result;
}
/**
* Write a custom PHPCS XML coding standard
* file to a file specified. Will write out
* all the PHPCS standards specified along
* with PHPCS sniffs specified.
*
* @param string $file_name File to write to.
* @param array $phpcs_standard PHPCS standards to write to file.
* @param array $phpcs_sniffs_include PHPCS sniffs to include.
*/
function vipgoci_phpcs_write_xml_standard_file(
string $file_name,
array $phpcs_standard,
array $phpcs_sniffs_include
) {
$xml_doc = xmlwriter_open_memory();
xmlwriter_set_indent( $xml_doc, true );
xmlwriter_set_indent_string( $xml_doc, "\t" );
xmlwriter_start_document( $xml_doc, '1.0', 'UTF-8' );
xmlwriter_start_element( $xml_doc, 'ruleset' );
xmlwriter_start_attribute( $xml_doc, 'xmlns:xsi' );
xmlwriter_text( $xml_doc, 'http://www.w3.org/2001/XMLSchema-instance' );
xmlwriter_end_attribute( $xml_doc );
xmlwriter_start_attribute( $xml_doc, 'name' );
xmlwriter_text( $xml_doc, 'PHP_CodeSniffer' );
xmlwriter_end_attribute( $xml_doc );
xmlwriter_start_attribute( $xml_doc, 'xsi:noNamespaceSchemaLocation' );
xmlwriter_text( $xml_doc, 'phpcs.xsd' );
xmlwriter_end_attribute( $xml_doc );
xmlwriter_start_element( $xml_doc, 'description' );
xmlwriter_text( $xml_doc, 'Custom coding standard' );
xmlwriter_end_element( $xml_doc );
/*
* Merge an array of rulesets and
* write them into the XML document.
*/
$rulesets_arr = array_merge(
$phpcs_standard,
$phpcs_sniffs_include
);
foreach ( $rulesets_arr as $ruleset_item ) {
xmlwriter_start_element( $xml_doc, 'rule' );
xmlwriter_start_attribute( $xml_doc, 'ref' );
xmlwriter_text( $xml_doc, $ruleset_item );
xmlwriter_end_attribute( $xml_doc );
xmlwriter_end_element( $xml_doc );
}
xmlwriter_end_element( $xml_doc );
xmlwriter_end_document( $xml_doc );
file_put_contents(
$file_name,
xmlwriter_output_memory( $xml_doc )
);
unset( $xml_doc );
return $file_name;
}
/**
* Scan a single file using the PHPCS scanner,
* return JSON decoded results. Avoid scanning
* a file if it is too large to scan.
*
* @param array $options Options needed.
* @param string $file_name File path to scan.
*
* @return array Results of scanning and validation.
*/
function vipgoci_phpcs_scan_single_file(
array $options,
string $file_name
) {
$file_contents = vipgoci_gitrepo_fetch_committed_file(
$options['repo-owner'],
$options['repo-name'],
$options['token'],
$options['commit'],
$file_name,
$options['local-git-repo']
);
if ( false === $file_contents ) {
vipgoci_sysexit(
'Unable to fetch file from repository',
array(
'file_name' => $file_name,
),
VIPGOCI_EXIT_SYSTEM_PROBLEM
);
}
$file_extension = vipgoci_file_extension_get(
$file_name
);
if ( empty( $file_extension ) ) {
$file_extension = null;
}
$temp_file_name = vipgoci_save_temp_file(
'vipgoci-phpcs-scan-',
$file_extension,
$file_contents
);
/*
* Skips the phpcs scan when the validation contains any issue
*/
if ( true === $options['skip-large-files'] ) {
$validation = vipgoci_validate(
$temp_file_name,
$file_name,
$options['commit'],
$options['skip-large-files-limit']
);
if ( 0 !== $validation['total'] ) {
$skipped = array(
'file_issues_arr_master' => array(),
'file_issues_str' => array(),
'temp_file_name' => $temp_file_name,
'validation' => $validation,
);
unlink( $temp_file_name );
return $skipped;
}
}
vipgoci_log(
'About to PHPCS-scan file',
array(
'repo_owner' => $options['repo-owner'],
'repo_name' => $options['repo-name'],
'commit_id' => $options['commit'],
'filename' => $file_name,
'file_extension' => $file_extension,
'temp_file_name' => $temp_file_name,
)
);
$retry_cnt = 0;
/*
* Try to PHPCS scan.
*/
$file_issues_str = vipgoci_phpcs_do_scan(
$temp_file_name,
$options['phpcs-path'],
$options['phpcs-php-path'],
$options['phpcs-standard'],
$options['phpcs-sniffs-exclude'],
$options['phpcs-severity'],
$options['phpcs-runtime-set']
);
if ( null !== $file_issues_str ) {
/* Remove linebreak PHPCS possibly adds */
$file_issues_str = rtrim(
$file_issues_str,
"\n"
);
$file_issues_arr_master = json_decode(
$file_issues_str,
true
);
} else {
$file_issues_arr_master = null;
}
/*
* Log results.
*/
vipgoci_log(
( null !== $file_issues_arr_master ) ?
'PHPCS returned results' :
'Error when running PHPCS',
array(
'filename' => $file_name,
'file_issues_str' => $file_issues_str,
'issues_stats' => isset( $file_issues_arr_master['totals'] ) ?
$file_issues_arr_master['totals'] : null,
)
);
/* Get rid of temporary file */
unlink( $temp_file_name );
return array(
'file_issues_arr_master' => $file_issues_arr_master,
'file_issues_str' => $file_issues_str,
'temp_file_name' => $temp_file_name,
'validation' => $validation ?? array(),
);
}
/**
* Scan a particular commit which should live within
* a particular repository on GitHub, and use the specified
* access-token to gain access.
*
* @param array $options Options array for the program.
* @param array $commit_issues_submit Results for PHP linting (reference).
* @param array $commit_issues_stats Result statistics, focussed on PHP linting (reference).
* @param array $commit_skipped_files Information about skipped files (reference).
*
* @return void
*/
function vipgoci_phpcs_scan_commit(
array $options,
array &$commit_issues_submit,
array &$commit_issues_stats,
array &$commit_skipped_files
) :void {
$repo_owner = $options['repo-owner'];
$repo_name = $options['repo-name'];
$commit_id = $options['commit'];
$github_token = $options['token'];
if ( false === $options['phpcs'] ) {
vipgoci_log(
'Will not PHPCS scan files, not configured to do so',
array(
'repo_owner' => $repo_owner,
'repo_name' => $repo_name,
'commit_id' => $commit_id,
)
);
}
vipgoci_runtime_measure( VIPGOCI_RUNTIME_START, 'phpcs_scan_commit' );
vipgoci_log(
'About to PHPCS-scan repository',
array(
'repo_owner' => $repo_owner,
'repo_name' => $repo_name,
'commit_id' => $commit_id,
)
);
// Fetch list of all pull requests which the commit is a part of.
$prs_implicated = vipgoci_github_prs_implicated(
$repo_owner,
$repo_name,
$commit_id,
$github_token,
$options['branches-ignore'],
$options['skip-draft-prs']
);
/*
* Get list of all files affected by
* each pull request implicated by the commit.
*/
vipgoci_log(
'Fetching list of all files affected by each pull request ' .
'implicated by the commit',
array(
'repo_owner' => $repo_owner,
'repo_name' => $repo_name,
'commit_id' => $commit_id,
)
);
$commit_skipped_files_empty = array();
$pr_item_files_changed = vipgoci_github_files_affected_by_commit(
$options,
$commit_id,
$commit_skipped_files_empty,
false, // Exclude renamed files.
false, // Exclude removed files.
false, // Exclude permission changes.
array(
// If SVG-checks are enabled, include it in the file-extensions.
'file_extensions' => array_merge(
$options['phpcs-file-extensions'],
( $options['svg-checks'] ?
$options['svg-file-extensions'] :
array()
)
),
'skip_folders' => $options['phpcs-skip-folders'],
),
true
);
$files_issues_arr = array();
/*
* Loop through each altered file in all the pull requests,
* use PHPCS to scan for issues, save the issues; they will
* be processed in the next step.
*/
vipgoci_log(
'About to PHPCS-scan all files affected by any of the ' .
'pull requests',
array(
'repo_owner' => $repo_owner,
'repo_name' => $repo_name,
'commit_id' => $commit_id,
'all_files_changed_by_prs' => $pr_item_files_changed['all'],
)
);
vipgoci_runtime_measure( VIPGOCI_RUNTIME_START, 'phpcs_scan_single_file' );
/*
* To keep account of files that could not be PHPCS scanned
* or SVG scanned.
*/
$files_failed_phpcs_scanning = array();
/*
* Get first PR array item found. Needed
* for skipped files logic below.
*/
foreach ( $prs_implicated as $pr_item ) {
break;
}
foreach ( $pr_item_files_changed['all'] as $file_name ) {
if (
isset( $commit_skipped_files[ $pr_item->number ]['issues'][ VIPGOCI_VALIDATION_MAXIMUM_LINES ] )
&& true === in_array(
$file_name,
$commit_skipped_files[ $pr_item->number ]['issues'][ VIPGOCI_VALIDATION_MAXIMUM_LINES ],
true
)
) {
$files_issues_arr[ $file_name ] = array();
continue;
}
/*
* Loop through each file affected by
* the commit.
*/
$file_extension = vipgoci_file_extension_get(
$file_name
);
/*
* If a SVG file, scan using a
* custom internal function, otherwise
* use PHPCS.
*
* However, only do this if SVG-checks
* is enabled.
*/
$scanning_func =
(
( true === in_array(
$file_extension,
$options['svg-file-extensions'],
true
) )
&&
( $options['svg-checks'] )
) ?
'vipgoci_svg_scan_single_file' :
'vipgoci_phpcs_scan_single_file';
$tmp_scanning_results = $scanning_func(
$options,
$file_name
);
if (
( true === $options['skip-large-files'] ) &&
( 0 !== $tmp_scanning_results['validation']['total'] )
) {
vipgoci_log(
VIPGOCI_SKIPPED_FILES,
array(
'repo_owner' => $repo_owner,
'repo_name' => $repo_name,
'commit_id' => $commit_id,
'file' => $file_name,
),
0,
true
);
/*
* No further processing in case of invalid file
* Set the skipped file
* Set an empty array just in case to avoid warnings.
*/
vipgoci_set_skipped_file(
$commit_skipped_files,
$tmp_scanning_results['validation'],
$pr_item->number
);
$files_issues_arr[ $file_name ] = array();
continue;
}
$file_issues_arr_master =
$tmp_scanning_results['file_issues_arr_master'];
$file_issues_str =
$tmp_scanning_results['file_issues_str'];
$temp_file_name =
$tmp_scanning_results['temp_file_name'];
/*
* Keep statistics on number of lines
* and files we scan.
*/
vipgoci_stats_per_file(
$options,
$file_name,
'phpcs_scanned'
);
/*
* Do sanity-checking
*/
if (
( null === $file_issues_arr_master ) ||
( ! isset( $file_issues_arr_master['totals'] ) ) ||
( ! isset( $file_issues_arr_master['files'] ) )
) {
vipgoci_log(
'Failed parsing output from PHPCS',
array(
'repo_owner' => $repo_owner,
'repo_name' => $repo_name,
'commit_id' => $commit_id,
'file_name' => $file_name,
'file_issues_arr_master' => $file_issues_arr_master,
'file_issues_str' => $file_issues_str,
),
0,
true // log to IRC.
);
/*
* No further processing in case of an error.
*
* Set an empty array just in case to avoid warnings.
*/
$files_issues_arr[ $file_name ] = array();
/*
* Keep track of files that we could not
* PHPCS or SVG scan. Avoid duplicate entries.
*/
vipgoci_array_push_uniquely(
$files_failed_phpcs_scanning,
$file_name,
);
continue;
}
unset( $file_issues_str );
/*
* In some cases filename in PHPCS output
* is without leading "/", but usually it is
* with leading "/". Make sure we cover both
* cases here.
*/
/* First attempt the default, with leading "/". */
if ( isset(
$file_issues_arr_master
['files']
[ $temp_file_name ]
) ) {
$file_issues_arr_index =
$temp_file_name;
} elseif ( isset(
// If this fails, try without leading "/".
$file_issues_arr_master
['files']
[ ltrim( $temp_file_name, '/' ) ]
) ) {
$file_issues_arr_index =
ltrim( $temp_file_name, '/' );
} else {
// Everything failed, print error and continue.
/* Empty placeholder, to avoid warnings. */
$files_issues_arr[ $file_name ] = array();
vipgoci_log(
'Unable to read results of PHPCS scanning, missing index',
array(
'temp_file_name' => $temp_file_name,
)
);
continue;
}
/*
* Make sure items in $file_issues_arr_master have
* 'level' key and value.
*/
$file_issues_arr_master = array_map(
function( $item ) {
if ( isset( $item['type'] ) ) {
$item['level'] = $item['type'];
unset( $item['type'] );
}
return $item;
},
$file_issues_arr_master
['files']
[ $file_issues_arr_index ]
['messages']
);
/*
* Remove any duplicate issues.
*/
$file_issues_arr_master = vipgoci_results_filter_duplicate(
$file_issues_arr_master
);
$files_issues_arr[ $file_name ] = $file_issues_arr_master;
/*
* Get rid of data, and
* attempt to garbage-collect.
*/
vipgoci_log(
'Cleaning up after scanning of file...',
array()
);
unset( $file_contents );
unset( $file_extension );
unset( $temp_file_name );
unset( $file_issues_arr_master );
unset( $file_issues_str );
gc_collect_cycles();
}
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'phpcs_scan_single_file' );
/*
* Send generic message to each pull request
* on GitHub when there were problems PHPCS/SVG scanning
* notifying users about the problems and which
* files were not PHPCS/SVG scanned.
*/
if ( ! empty( $files_failed_phpcs_scanning ) ) {
vipgoci_report_submit_scanning_files_failed(
$options,
$prs_implicated,
$files_failed_phpcs_scanning,
VIPGOCI_PHPCS_SCAN_FAILED_MSG_START,
VIPGOCI_PHPCS_SCAN_FAILED_MSG_END
);
}
/*
* Loop through each pull request implicated,
* get comments made on GitHub already,
* then filter out any PHPCS-issues irrelevant
* as they are not due to any commit that is part
* of the pull request, and skip any PHPCS-issue
* already reported. Report the rest, if any.
*/
vipgoci_log(
'Figuring out which comment(s) to submit to GitHub, if any',
array(
'repo_owner' => $repo_owner,
'repo_name' => $repo_name,
'commit_id' => $commit_id,
)
);
foreach ( $prs_implicated as $pr_item ) {
vipgoci_log(
'Preparing to process PHPCS scanned files in ' .
'pull request, to construct results ' .
'to be submitted',
array(
'repo_owner' => $repo_owner,
'repo_name' => $repo_name,
'commit_id' => $commit_id,
'pr_number' => $pr_item->number,
'files_changed' => $pr_item_files_changed[ $pr_item->number ],
)
);
/*
* Check if user requested to turn off PHPCS
* scanning for the pull request by adding a label
* to the pull request, and if so, skip scanning.
* Make sure to indicate so in the statistics.
*
* This is only done if allowed via option.
*/
if (
true ===
$options['phpcs-skip-scanning-via-labels-allowed']
) {
$pr_label_skip_phpcs = vipgoci_github_pr_labels_get(
$repo_owner,
$repo_name,
$github_token,
$pr_item->number,
'skip-phpcs-scan'
);
if ( ! empty( $pr_label_skip_phpcs ) ) {
vipgoci_log(
'Label on pull request indicated to ' .
'skip PHPCS-scanning; ' .
'scanning will be skipped',
array(
'repo_owner'
=> $repo_owner,
'repo_name'
=> $repo_name,
'commit_id'
=> $commit_id,
'pr_number'
=> $pr_item->number,
'pr_label_skip_phpcs'
=> $pr_label_skip_phpcs,
)
);
unset(
$commit_issues_stats
[ $pr_item->number ]
);
continue;
}
}
/*
* Get all commits related to the current
* pull request.
*/
$pr_item_commits = vipgoci_github_prs_commits_list(
$repo_owner,
$repo_name,
$pr_item->number,
$github_token
);
/*
* Loop through each file, get a
* 'git blame' log for the file, then
* filter out issues stemming
* from commits that are not a
* part of the current pull request.
*/
foreach (
$pr_item_files_changed[ $pr_item->number ] as
$_tmp => $file_name
) {
if (
isset( $commit_skipped_files[ $pr_item->number ]['issues'][ VIPGOCI_VALIDATION_MAXIMUM_LINES ] )
&& true === in_array(
$file_name,
$commit_skipped_files[ $pr_item->number ]['issues'][ VIPGOCI_VALIDATION_MAXIMUM_LINES ],
true
)
) {
continue;
}
/*
* Get blame log for file
*/
$file_blame_log = vipgoci_gitrepo_blame_for_file(
$commit_id,
$file_name,
$options['local-git-repo']
);
/*
* Get patch for the file
*/
$file_changed_lines = vipgoci_patch_changed_lines(
$options['local-git-repo'],
$options['repo-owner'],
$options['repo-name'],
$options['token'],
$pr_item->base->sha,
$commit_id,
$file_name
);
/*
* If no changed lines were available, log
* and continue.
*/
if ( null === $file_changed_lines ) {
vipgoci_log(
'Unable to fetch changed lines for file, ' .
'skipping scanning',
array(
'local-git-repo' => $options['local-git-repo'],
'repo-owner' => $options['repo-owner'],
'repo-name' => $options['repo-name'],
'base_sha' => $pr_item->base->sha,
'commit_id' => $commit_id,
'file_name' => $file_name,
)
);
continue;
}
$file_relative_lines = @array_flip( // phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
$file_changed_lines
);
/*
* Filter the issues we found
* previously in this file; remove
* the ones that the are not found
* in the blame-log (meaning that
* they are due to commits outside of
* the pull request).
*/
$file_issues_arr_filtered = vipgoci_results_filter_irrellevant(
$file_name,
$files_issues_arr,
$file_blame_log,
$pr_item_commits,
$file_relative_lines
);
/*
* Collect all the issues that