forked from Automattic/vip-go-compatibility-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan-files.php
executable file
·233 lines (199 loc) · 4.85 KB
/
scan-files.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
<?php
/*
* Scan all PHP files in the
* repository using the options
* given, return the issues found
* in one array.
*/
function vipgocs_scan_files(
$options
) {
vipgoci_log(
'Fetching tree of files in the repository',
array(
'repo_owner' => $options['repo-owner'],
'repo_name' => $options['repo-name'],
'local_git_repo' => $options['local-git-repo'],
)
);
/*
* Get tree of files that are
* available in the commit specified.
*/
$tree = vipgoci_gitrepo_fetch_tree(
$options,
$options['commit']
);
$all_results = array(
'files' => array(),
'warnings' => 0,
'errors' => 0,
'fixable' => 0,
);
/*
* Loop through each file, note any issues.
*/
foreach( $tree as $file_path ) {
vipgoci_log(
'Looking at file ' . $file_path,
array(
'repo-owner' => $options['repo-owner'],
'repo-name' => $options['repo-name'],
'file_path' => $file_path,
'commit' => $options['commit']
)
);
/*
* Determine file-extension, and if it is not PHP, skip the file.
*/
$file_extension = vipgoci_file_extension_get(
$file_path
);
if ( 'php' !== $file_extension ) {
vipgoci_log(
'Skipping file, as it is not a PHP file',
array(
'file_path' => $file_path,
)
);
continue;
}
/*
* Get URL to commit of
* the file -- make sure to
* be compatible with submodules.
*/
$file_is_in_submodule =
vipgoci_gitrepo_submodule_file_path_get(
$options['local-git-repo'],
$file_path
);
$file_path_without_submodule = null;
if ( null === $file_is_in_submodule ) {
$github_commit_url =
'https://github.com/' .
$options['repo-owner'] . '/' .
$options['repo-name'] . '/' .
'blob/' .
$options['commit'];
}
else {
$github_commit_url =
vipgoci_gitrepo_submodule_get_url(
$options['local-git-repo'],
$file_is_in_submodule['submodule_path']
) . '/' .
'blob/' .
$file_is_in_submodule['commit_id'];
$file_path_without_submodule = $file_path;
$file_path_without_submodule = substr(
$file_path_without_submodule,
( strlen( $file_is_in_submodule['submodule_path'] ) + 1 )
);
}
/*
* Get path to file relative
* to repository.
*
* Replace '.'/ with '/'.
*/
$file_path_dir = dirname(
$file_path
);
if ( '.' === $file_path_dir ) {
$file_path_dir = '/';
}
/*
* Scan a single PHP file, and process the results
* -- i.e., collect the information and save it.
*/
$file_results = vipgoci_phpcs_scan_single_file(
$options,
$file_path
);
/*
* Check for errors.
*/
if (
( ! isset( $file_results['file_issues_arr_master'] ) ) ||
( null === $file_results['file_issues_arr_master'] )
) {
vipgoci_log(
'Failed parsing output from PHPCS',
array(
'repo_owner' => $options['repo-owner'],
'repo_name' => $options['repo-name'],
'commit_id' => $options['commit'],
'file_results' => $file_results,
),
0
);
continue;
}
$temp_file_name = $file_results['temp_file_name'];
/*
* Loop through each PHPCS message, add 'file_path'
* and other information to each item.
*/
$file_results
['file_issues_arr_master']
['files']
[ $temp_file_name ]
['messages']
= array_map(
function(
$msg
) use (
$file_path,
$github_commit_url,
$file_is_in_submodule,
$file_path_without_submodule
) {
$msg['github_commit_url'] = $github_commit_url;
$msg['file_path'] = $file_path;
$msg['file_is_in_submodule'] = $file_is_in_submodule !== null;
$msg['file_path_without_submodule'] = $file_path_without_submodule;
return $msg;
},
$file_results
['file_issues_arr_master']
['files']
[ $temp_file_name ]
['messages']
);
$file_results_master = $file_results['file_issues_arr_master'];
/*
* Figure out how to group
* issues, either by file or folder.
*/
if ( 'file' === $options['github-issue-group-by'] ) {
$group_issues_by = $file_path;
}
else if ( 'folder' === $options['github-issue-group-by'] ) {
$group_issues_by = $file_path_dir;
}
else {
vipgoci_sysexit(
'Unexpected value for option --github-issue-group-by',
array(
'github-issue-group-by' => $options['github-issue-group-by'],
)
);
}
if ( ! isset( $all_results['files'][ $group_issues_by ]['messages'] ) ) {
$all_results['files'][ $group_issues_by ]['messages'] = array();
}
$all_results['files'][ $group_issues_by ]['messages'] = array_merge(
$all_results['files'][ $group_issues_by ]['messages'],
$file_results_master['files'][ $temp_file_name ]['messages']
);
$all_results['warnings'] += $file_results_master['totals']['warnings'];
$all_results['errors'] += $file_results_master['totals']['errors'];
unset( $file_extension );
unset( $file_results );
unset( $file_results_master );
unset( $temp_file_name );
gc_collect_cycles();
}
return $all_results;
}