forked from vimeo/psalm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Creator.php
296 lines (244 loc) · 8.3 KB
/
Creator.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
<?php
namespace Psalm\Config;
use Psalm\Config;
use Psalm\Exception\ConfigCreationException;
use Psalm\Internal\Analyzer\IssueData;
use Psalm\Internal\Composer;
use function array_filter;
use function array_keys;
use function array_merge;
use function array_shift;
use function array_sum;
use function array_unique;
use function array_values;
use function count;
use function explode;
use function file_exists;
use function file_get_contents;
use function glob;
use function implode;
use function is_array;
use function is_dir;
use function json_decode;
use function ksort;
use function max;
use function preg_replace;
use function sort;
use function str_replace;
use function strpos;
use const DIRECTORY_SEPARATOR;
use const GLOB_NOSORT;
class Creator
{
private const TEMPLATE = '<?xml version="1.0"?>
<psalm
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
';
/**
* @return non-empty-string
*/
public static function getContents(
string $current_dir,
?string $suggested_dir,
int $level,
string $vendor_dir
): string {
$paths = self::getPaths($current_dir, $suggested_dir);
$template = str_replace(
'<directory name="src" />',
implode("\n ", $paths),
self::TEMPLATE
);
if (is_dir($current_dir . DIRECTORY_SEPARATOR . $vendor_dir)) {
$template = str_replace(
'<directory name="vendor" />',
'<directory name="' . $vendor_dir . '" />',
$template
);
} else {
$template = str_replace(
'<directory name="vendor" />',
'',
$template
);
}
/** @var non-empty-string */
return str_replace(
'errorLevel="1"',
'errorLevel="' . $level . '"',
$template
);
}
public static function createBareConfig(
string $current_dir,
?string $suggested_dir,
string $vendor_dir
): Config {
$config_contents = self::getContents($current_dir, $suggested_dir, 1, $vendor_dir);
return Config::loadFromXML($current_dir, $config_contents);
}
/**
* @param array<IssueData> $issues
*/
public static function getLevel(array $issues, int $counted_types): int
{
if ($counted_types === 0) {
$counted_types = 1;
}
$issues_at_level = [];
foreach ($issues as $issue) {
$issue_type = $issue->type;
$issue_level = $issue->error_level;
if ($issue_level < 1) {
continue;
}
// exclude some directories that are probably ignorable
if (strpos($issue->file_path, 'vendor') || strpos($issue->file_path, 'stub')) {
continue;
}
if (!isset($issues_at_level[$issue_level][$issue_type])) {
$issues_at_level[$issue_level][$issue_type] = 0;
}
$issues_at_level[$issue_level][$issue_type] += 100/$counted_types;
}
foreach ($issues_at_level as $level => $issues) {
ksort($issues);
// remove any issues where < 0.1% of expressions are affected
$filtered_issues = array_filter(
$issues,
function ($amount): bool {
return $amount > 0.1;
}
);
if (array_sum($filtered_issues) > 0.5) {
$issues_at_level[$level] = $filtered_issues;
} else {
unset($issues_at_level[$level]);
}
}
if (!$issues_at_level) {
return 1;
}
if (count($issues_at_level) === 1) {
return array_keys($issues_at_level)[0] + 1;
}
return max(...array_keys($issues_at_level)) + 1;
}
/**
* @return non-empty-list<string>
*/
public static function getPaths(string $current_dir, ?string $suggested_dir): array
{
$replacements = [];
if ($suggested_dir) {
if (is_dir($current_dir . DIRECTORY_SEPARATOR . $suggested_dir)) {
$replacements[] = '<directory name="' . $suggested_dir . '" />';
} else {
$bad_dir_path = $current_dir . DIRECTORY_SEPARATOR . $suggested_dir;
throw new ConfigCreationException(
'The given path "' . $bad_dir_path . '" does not appear to be a directory'
);
}
} elseif (is_dir($current_dir . DIRECTORY_SEPARATOR . 'src')) {
$replacements[] = '<directory name="src" />';
} else {
$composer_json_location = Composer::getJsonFilePath($current_dir);
if (!file_exists($composer_json_location)) {
throw new ConfigCreationException(
'Problem during config autodiscovery - could not find composer.json during initialization.'
);
}
if (!$composer_json = json_decode(file_get_contents($composer_json_location), true)) {
throw new ConfigCreationException('Invalid composer.json at ' . $composer_json_location);
}
if (!is_array($composer_json)) {
throw new ConfigCreationException('Invalid composer.json at ' . $composer_json_location);
}
$replacements = self::getPsr4Or0Paths($current_dir, $composer_json);
if (!$replacements) {
throw new ConfigCreationException(
'Could not located any PSR-0 or PSR-4-compatible paths in ' . $composer_json_location
);
}
}
return $replacements;
}
/**
* @return list<string>
* @psalm-suppress MixedAssignment
* @psalm-suppress MixedArgument
*/
private static function getPsr4Or0Paths(string $current_dir, array $composer_json): array
{
$psr_paths = array_merge(
$composer_json['autoload']['psr-4'] ?? [],
$composer_json['autoload']['psr-0'] ?? []
);
if (!$psr_paths) {
return self::guessPhpFileDirs($current_dir);
}
$nodes = [];
foreach ($psr_paths as $paths) {
if (!is_array($paths)) {
$paths = [$paths];
}
foreach ($paths as $path) {
if ($path === '') {
$nodes = array_merge(
$nodes,
self::guessPhpFileDirs($current_dir)
);
continue;
}
$path = preg_replace('@[/\\\]$@', '', $path, 1);
if ($path !== 'tests') {
$nodes[] = '<directory name="' . $path . '" />';
}
}
}
$nodes = array_unique($nodes);
sort($nodes);
return $nodes;
}
/**
* @return list<string>
*/
private static function guessPhpFileDirs(string $current_dir): array
{
$nodes = [];
/** @var string[] */
$php_files = array_merge(
glob($current_dir . DIRECTORY_SEPARATOR . '*.php', GLOB_NOSORT),
glob($current_dir . DIRECTORY_SEPARATOR . '**/*.php', GLOB_NOSORT),
glob($current_dir . DIRECTORY_SEPARATOR . '**/**/*.php', GLOB_NOSORT)
);
foreach ($php_files as $php_file) {
$php_file = str_replace($current_dir . DIRECTORY_SEPARATOR, '', $php_file);
$parts = explode(DIRECTORY_SEPARATOR, $php_file);
if (!$parts[0]) {
array_shift($parts);
}
if ($parts[0] === 'vendor' || $parts[0] === 'tests') {
continue;
}
if (count($parts) === 1) {
$nodes[] = '<file name="' . $php_file . '" />';
} else {
$nodes[] = '<directory name="' . $parts[0] . '" />';
}
}
return array_values(array_unique($nodes));
}
}