Line data Source code
1 : /*
2 : +----------------------------------------------------------------------+
3 : | LSParrot PHP LSP Extension |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) LSParrot GitHub Organization |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to the 0BSD license that is |
8 : | bundled with this package in the file LICENSE. |
9 : +----------------------------------------------------------------------+
10 : | Author: Go Kudo <zeriyoshi@gmail.com> |
11 : +----------------------------------------------------------------------+
12 : */
13 :
14 : #include "lsp_internal.h"
15 :
16 156 : static inline zend_string *lsp_analyzer_config_file_named(zend_string *root, const char **names, uint32_t count)
17 : {
18 : zend_string *path;
19 : uint32_t i;
20 :
21 584 : for (i = 0; i < count; i++) {
22 484 : path = lsp_join_path2(root, names[i]);
23 484 : if (lsp_is_regular_file(path)) {
24 56 : return path;
25 : }
26 :
27 428 : zend_string_release(path);
28 : }
29 :
30 100 : return NULL;
31 : }
32 :
33 176 : static inline void lsp_append_neon_single_quoted(smart_str *target, zend_string *value)
34 : {
35 : const char *cursor, *end, *quote;
36 :
37 176 : cursor = ZSTR_VAL(value);
38 176 : end = cursor + ZSTR_LEN(value);
39 176 : smart_str_appendc(target, '\'');
40 176 : while (cursor < end) {
41 176 : quote = memchr(cursor, '\'', end - cursor);
42 176 : if (!quote) {
43 176 : smart_str_appendl(target, cursor, end - cursor);
44 :
45 176 : break;
46 : }
47 :
48 0 : smart_str_appendl(target, cursor, quote - cursor);
49 0 : smart_str_appendl(target, "''", sizeof("''") - 1);
50 0 : cursor = quote + 1;
51 : }
52 :
53 176 : smart_str_appendc(target, '\'');
54 176 : }
55 :
56 156 : static inline zend_long lsp_project_vscode_config_long(zend_string *project_root, const char *key, zend_long fallback)
57 : {
58 : zend_string *path, *contents;
59 : zval decoded, *value;
60 : zend_long result;
61 :
62 156 : result = fallback;
63 156 : path = strpprintf(0, "%s/.lsparrot/vscode_config.json", ZSTR_VAL(project_root));
64 156 : if (!lsp_is_regular_file(path)) {
65 140 : zend_string_release(path);
66 :
67 140 : return fallback;
68 : }
69 :
70 16 : contents = lsp_read_file(path);
71 16 : zend_string_release(path);
72 16 : if (contents == zend_empty_string) {
73 0 : return fallback;
74 : }
75 :
76 16 : ZVAL_UNDEF(&decoded);
77 16 : php_json_decode_ex(&decoded, ZSTR_VAL(contents), ZSTR_LEN(contents), PHP_JSON_OBJECT_AS_ARRAY, 512);
78 16 : zend_string_release(contents);
79 16 : if (Z_TYPE(decoded) != IS_ARRAY) {
80 0 : if (!Z_ISUNDEF(decoded)) {
81 0 : zval_ptr_dtor(&decoded);
82 : }
83 :
84 0 : return fallback;
85 : }
86 :
87 16 : value = zend_hash_str_find(Z_ARRVAL(decoded), key, strlen(key));
88 16 : if (value && Z_TYPE_P(value) == IS_LONG && Z_LVAL_P(value) >= 0) {
89 16 : result = Z_LVAL_P(value);
90 0 : } else if (value && Z_TYPE_P(value) == IS_DOUBLE && Z_DVAL_P(value) >= 0.0) {
91 0 : result = (zend_long) Z_DVAL_P(value);
92 : }
93 :
94 16 : zval_ptr_dtor(&decoded);
95 :
96 16 : return result;
97 : }
98 :
99 592 : static inline const char *lsp_bounded_memstr(const char *haystack, const char *needle, size_t needle_length, const char *end)
100 : {
101 : const char *cursor, *limit;
102 :
103 592 : if (needle_length == 0) {
104 0 : return haystack;
105 : }
106 592 : if (end < haystack || (size_t) (end - haystack) < needle_length) {
107 36 : return NULL;
108 : }
109 :
110 556 : limit = end - needle_length + 1;
111 31404 : for (cursor = haystack; cursor < limit; cursor++) {
112 31152 : if (*cursor == *needle && memcmp(cursor, needle, needle_length) == 0) {
113 304 : return cursor;
114 : }
115 : }
116 :
117 252 : return NULL;
118 : }
119 :
120 284 : static inline const char *lsp_xml_start_tag_end(const char *start, const char *end)
121 : {
122 : const char *cursor;
123 : char quote;
124 :
125 284 : quote = '\0';
126 25516 : for (cursor = start; cursor < end; cursor++) {
127 25516 : if (quote != '\0') {
128 13960 : if (*cursor == quote) {
129 536 : quote = '\0';
130 : }
131 13960 : continue;
132 : }
133 :
134 11556 : if (*cursor == '\'' || *cursor == '"') {
135 536 : quote = *cursor;
136 536 : continue;
137 : }
138 :
139 11020 : if (*cursor == '>') {
140 284 : return cursor;
141 : }
142 : }
143 :
144 0 : return NULL;
145 : }
146 :
147 364 : static inline void lsp_append_xml_escaped(smart_str *target, const char *value, size_t value_length)
148 : {
149 : size_t i;
150 : char c;
151 :
152 8420 : for (i = 0; i < value_length; i++) {
153 8056 : c = value[i];
154 8056 : if (c == '&') {
155 0 : smart_str_appendl(target, "&", sizeof("&") - 1);
156 8056 : } else if (c == '"') {
157 0 : smart_str_appendl(target, """, sizeof(""") - 1);
158 8056 : } else if (c == '\'') {
159 0 : smart_str_appendl(target, "'", sizeof("'") - 1);
160 8056 : } else if (c == '<') {
161 0 : smart_str_appendl(target, "<", sizeof("<") - 1);
162 8056 : } else if (c == '>') {
163 0 : smart_str_appendl(target, ">", sizeof(">") - 1);
164 : } else {
165 8056 : smart_str_appendc(target, c);
166 : }
167 : }
168 364 : }
169 :
170 364 : static inline void lsp_append_xml_attribute(smart_str *target, const char *name, size_t name_length, const char *value, size_t value_length)
171 : {
172 364 : smart_str_appendc(target, ' ');
173 364 : smart_str_appendl(target, name, name_length);
174 364 : smart_str_appendl(target, "=\"", sizeof("=\"") - 1);
175 364 : lsp_append_xml_escaped(target, value, value_length);
176 364 : smart_str_appendc(target, '"');
177 364 : }
178 :
179 16 : static inline bool lsp_xml_attribute_name_matches(const char *candidate, const char *tag_start, const char *tag_end, const char *name, size_t name_length)
180 : {
181 : const char *after;
182 :
183 16 : if (candidate <= tag_start || !isspace((unsigned char) candidate[-1])) {
184 0 : return false;
185 : }
186 :
187 16 : after = candidate + name_length;
188 16 : while (after < tag_end && isspace((unsigned char) *after)) {
189 0 : after++;
190 : }
191 :
192 16 : return after < tag_end && *after == '=' && memcmp(candidate, name, name_length) == 0;
193 : }
194 :
195 272 : static inline zend_string *lsp_psalm_config_upsert_attribute(zend_string *contents, const char *name, const char *value, size_t value_length)
196 : {
197 : const char *contents_start, *contents_end, *tag_start, *tag_end, *cursor, *attr, *attr_start,
198 : *attr_value_start, *attr_value_end, *eq, *insert_at
199 : ;
200 272 : smart_str edited = {0};
201 : size_t name_length;
202 : bool found;
203 : char quote;
204 :
205 272 : found = false;
206 272 : contents_start = ZSTR_VAL(contents);
207 272 : contents_end = contents_start + ZSTR_LEN(contents);
208 272 : name_length = strlen(name);
209 272 : tag_start = lsp_bounded_memstr(contents_start, "<psalm", sizeof("<psalm") - 1, contents_end);
210 272 : if (!tag_start) {
211 0 : return NULL;
212 : }
213 272 : tag_end = lsp_xml_start_tag_end(tag_start, contents_end);
214 272 : if (!tag_end) {
215 0 : return NULL;
216 : }
217 :
218 272 : cursor = tag_start + sizeof("<psalm") - 1;
219 272 : attr = NULL;
220 272 : while ((attr = lsp_bounded_memstr(cursor, name, name_length, tag_end)) != NULL) {
221 4 : if (lsp_xml_attribute_name_matches(attr, tag_start, tag_end, name, name_length)) {
222 4 : found = true;
223 4 : break;
224 : }
225 0 : cursor = attr + 1;
226 : }
227 :
228 272 : if (found) {
229 4 : eq = memchr(attr + name_length, '=', tag_end - (attr + name_length));
230 4 : if (!eq) {
231 0 : return NULL;
232 : }
233 4 : attr_value_start = eq + 1;
234 4 : while (attr_value_start < tag_end && isspace((unsigned char) *attr_value_start)) {
235 0 : attr_value_start++;
236 : }
237 4 : if (attr_value_start >= tag_end || (*attr_value_start != '\'' && *attr_value_start != '"')) {
238 0 : return NULL;
239 : }
240 4 : quote = *attr_value_start;
241 4 : attr_value_start++;
242 4 : attr_value_end = memchr(attr_value_start, quote, tag_end - attr_value_start);
243 4 : if (!attr_value_end) {
244 0 : return NULL;
245 : }
246 4 : attr_start = attr;
247 8 : while (attr_start > tag_start && isspace((unsigned char) attr_start[-1])) {
248 4 : attr_start--;
249 : }
250 4 : smart_str_appendl(&edited, contents_start, attr_start - contents_start);
251 4 : lsp_append_xml_attribute(&edited, name, name_length, value, value_length);
252 4 : smart_str_appendl(&edited, attr_value_end + 1, contents_end - (attr_value_end + 1));
253 : } else {
254 268 : cursor = tag_end;
255 268 : while (cursor > tag_start && isspace((unsigned char) cursor[-1])) {
256 0 : cursor--;
257 : }
258 268 : insert_at = cursor > tag_start && cursor[-1] == '/' ? cursor - 1 : tag_end;
259 268 : smart_str_appendl(&edited, contents_start, insert_at - contents_start);
260 268 : lsp_append_xml_attribute(&edited, name, name_length, value, value_length);
261 268 : smart_str_appendl(&edited, insert_at, contents_end - insert_at);
262 : }
263 :
264 272 : smart_str_0(&edited);
265 :
266 272 : return edited.s;
267 : }
268 :
269 2044 : static inline bool lsp_directory_exists(zend_string *path)
270 : {
271 : zend_stat_t st;
272 :
273 2044 : return VCWD_STAT(ZSTR_VAL(path), &st) == 0 && S_ISDIR(st.st_mode);
274 : }
275 :
276 12 : static inline bool lsp_xml_tag_name_is(const char *tag_start, const char *tag_end, const char *name, size_t name_length)
277 : {
278 : const char *cursor;
279 :
280 12 : cursor = tag_start;
281 12 : if (cursor >= tag_end || *cursor != '<') {
282 0 : return false;
283 : }
284 :
285 12 : cursor++;
286 24 : return (size_t) (tag_end - cursor) >= name_length &&
287 24 : memcmp(cursor, name, name_length) == 0 &&
288 12 : (cursor + name_length == tag_end || isspace((unsigned char) cursor[name_length]) || cursor[name_length] == '/' || cursor[name_length] == '>')
289 : ;
290 : }
291 :
292 16 : static inline const char *lsp_xml_next_file_filter_tag(const char *cursor, const char *end)
293 : {
294 : const char *directory_tag, *file_tag;
295 :
296 16 : directory_tag = lsp_bounded_memstr(cursor, "<directory", sizeof("<directory") - 1, end);
297 16 : file_tag = lsp_bounded_memstr(cursor, "<file", sizeof("<file") - 1, end);
298 16 : if (!directory_tag) {
299 4 : return file_tag;
300 : }
301 12 : if (!file_tag) {
302 12 : return directory_tag;
303 : }
304 :
305 0 : return directory_tag < file_tag ? directory_tag : file_tag;
306 : }
307 :
308 12 : static inline bool lsp_xml_name_attribute_bounds(const char *tag_start, const char *tag_end, const char **value_start, const char **value_end)
309 : {
310 : const char *attr, *eq, *cursor;
311 : char quote;
312 :
313 12 : attr = lsp_bounded_memstr(tag_start, "name", sizeof("name") - 1, tag_end);
314 12 : while (attr) {
315 12 : if (lsp_xml_attribute_name_matches(attr, tag_start, tag_end, "name", sizeof("name") - 1)) {
316 12 : break;
317 : }
318 0 : attr = lsp_bounded_memstr(attr + 1, "name", sizeof("name") - 1, tag_end);
319 : }
320 12 : if (!attr) {
321 0 : return false;
322 : }
323 :
324 12 : eq = memchr(attr + sizeof("name") - 1, '=', tag_end - (attr + sizeof("name") - 1));
325 12 : if (!eq) {
326 0 : return false;
327 : }
328 :
329 12 : cursor = eq + 1;
330 12 : while (cursor < tag_end && isspace((unsigned char) *cursor)) {
331 0 : cursor++;
332 : }
333 12 : if (cursor >= tag_end || (*cursor != '\'' && *cursor != '"')) {
334 0 : return false;
335 : }
336 :
337 12 : quote = *cursor;
338 12 : *value_start = cursor + 1;
339 12 : *value_end = memchr(*value_start, quote, tag_end - *value_start);
340 :
341 12 : return *value_end != NULL;
342 : }
343 :
344 12 : static inline bool lsp_psalm_path_value_is_lsparrot_root(zend_string *project_root, const char *value_start, const char *value_end)
345 : {
346 : const char *start, *end;
347 : size_t root_length, value_length;
348 : bool root_has_slash;
349 :
350 12 : start = value_start;
351 12 : end = value_end;
352 12 : while (start < end && isspace((unsigned char) *start)) {
353 0 : start++;
354 : }
355 16 : while (end > start && (isspace((unsigned char) end[-1]) || lsp_is_path_separator(end[-1]))) {
356 4 : end--;
357 : }
358 :
359 12 : value_length = end > start ? (size_t) (end - start) : 0;
360 12 : if ((value_length == sizeof(".lsparrot") - 1 && memcmp(start, ".lsparrot", sizeof(".lsparrot") - 1) == 0) ||
361 8 : (value_length == sizeof("./.lsparrot") - 1 && memcmp(start, "./.lsparrot", sizeof("./.lsparrot") - 1) == 0) ||
362 0 : (value_length == sizeof(".\\.lsparrot") - 1 && memcmp(start, ".\\.lsparrot", sizeof(".\\.lsparrot") - 1) == 0)
363 : ) {
364 4 : return true;
365 : }
366 :
367 8 : root_length = ZSTR_LEN(project_root);
368 8 : root_has_slash = root_length > 0 && lsp_is_path_separator(ZSTR_VAL(project_root)[root_length - 1]);
369 8 : if (value_length == root_length + (root_has_slash ? 0 : 1) + sizeof(".lsparrot") - 1 &&
370 0 : lsp_path_compare(start, ZSTR_VAL(project_root), root_length) == 0
371 : ) {
372 0 : if (root_has_slash) {
373 0 : return memcmp(start + root_length, ".lsparrot", sizeof(".lsparrot") - 1) == 0;
374 : }
375 :
376 0 : return lsp_is_path_separator(start[root_length]) && memcmp(start + root_length + 1, ".lsparrot", sizeof(".lsparrot") - 1) == 0;
377 : }
378 :
379 8 : return false;
380 : }
381 :
382 4 : static inline zend_string *lsp_psalm_config_without_lsparrot_root_ignore(zend_string *contents, zend_string *project_root)
383 : {
384 : const char *contents_start, *contents_end, *copy_start, *tag_start, *tag_end, *value_start, *value_end, *remove_start, *remove_end;
385 4 : smart_str edited = {0};
386 :
387 4 : contents_start = ZSTR_VAL(contents);
388 4 : contents_end = contents_start + ZSTR_LEN(contents);
389 4 : copy_start = contents_start;
390 4 : tag_start = contents_start;
391 16 : while ((tag_start = lsp_xml_next_file_filter_tag(tag_start, contents_end)) != NULL) {
392 12 : tag_end = lsp_xml_start_tag_end(tag_start, contents_end);
393 12 : if (!tag_end) {
394 0 : break;
395 : }
396 :
397 24 : if ((lsp_xml_tag_name_is(tag_start, tag_end, "directory", sizeof("directory") - 1) || lsp_xml_tag_name_is(tag_start, tag_end, "file", sizeof("file") - 1)) &&
398 24 : lsp_xml_name_attribute_bounds(tag_start, tag_end, &value_start, &value_end) &&
399 12 : lsp_psalm_path_value_is_lsparrot_root(project_root, value_start, value_end)
400 : ) {
401 4 : remove_start = tag_start;
402 52 : while (remove_start > contents_start && (remove_start[-1] == ' ' || remove_start[-1] == '\t')) {
403 48 : remove_start--;
404 : }
405 4 : remove_end = tag_end + 1;
406 4 : if (remove_end < contents_end && *remove_end == '\r') {
407 0 : remove_end++;
408 : }
409 4 : if (remove_end < contents_end && *remove_end == '\n') {
410 4 : remove_end++;
411 : }
412 4 : smart_str_appendl(&edited, copy_start, remove_start - copy_start);
413 4 : copy_start = remove_end;
414 4 : tag_start = remove_end;
415 4 : continue;
416 : }
417 :
418 8 : tag_start = tag_end + 1;
419 : }
420 :
421 4 : smart_str_appendl(&edited, copy_start, contents_end - copy_start);
422 4 : smart_str_0(&edited);
423 :
424 4 : return edited.s;
425 : }
426 :
427 2070 : static inline zend_string *lsp_analyzer_config_resolve_path(zend_string *root, zend_string *path)
428 : {
429 : bool slash;
430 :
431 2070 : if (ZSTR_LEN(path) == 0) {
432 0 : return zend_string_copy(root);
433 : }
434 :
435 2070 : if (lsp_path_is_absolute(path)) {
436 0 : return zend_string_copy(path);
437 : }
438 :
439 2070 : slash = lsp_path_has_trailing_separator(root);
440 :
441 2070 : return strpprintf(0, "%s%s%s", ZSTR_VAL(root), slash ? "" : "/", ZSTR_VAL(path));
442 : }
443 :
444 2090 : static inline bool lsp_analysis_path_exists(zend_string *path)
445 : {
446 2090 : return lsp_is_regular_file(path) || lsp_directory_exists(path);
447 : }
448 :
449 2090 : static inline bool lsp_analysis_paths_contains(zval *paths, zend_string *candidate)
450 : {
451 : zval *path_zv;
452 :
453 2458 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(paths), path_zv) {
454 396 : if (Z_TYPE_P(path_zv) == IS_STRING && zend_string_equals(Z_STR_P(path_zv), candidate)) {
455 28 : return true;
456 : }
457 : } ZEND_HASH_FOREACH_END();
458 :
459 2062 : return false;
460 : }
461 :
462 2070 : static inline void lsp_analysis_paths_add(zval *paths, zend_string *path)
463 : {
464 2070 : if (!lsp_analysis_path_exists(path) || lsp_analysis_paths_contains(paths, path)) {
465 28 : return;
466 : }
467 :
468 2042 : add_next_index_str(paths, zend_string_copy(path));
469 : }
470 :
471 5686 : static inline void lsp_composer_collect_path_value(zend_string *project_root, zval *paths, zval *value)
472 : {
473 : zend_string *resolved;
474 : zval *entry;
475 :
476 5686 : if (!value) {
477 3230 : return;
478 : }
479 :
480 2456 : if (Z_TYPE_P(value) == IS_STRING) {
481 2070 : resolved = lsp_analyzer_config_resolve_path(project_root, Z_STR_P(value));
482 2070 : lsp_analysis_paths_add(paths, resolved);
483 2070 : zend_string_release(resolved);
484 :
485 2070 : return;
486 : }
487 :
488 386 : if (Z_TYPE_P(value) != IS_ARRAY) {
489 0 : return;
490 : }
491 :
492 772 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(value), entry) {
493 386 : lsp_composer_collect_path_value(project_root, paths, entry);
494 : } ZEND_HASH_FOREACH_END();
495 : }
496 :
497 3616 : static inline void lsp_composer_collect_psr_map(zend_string *project_root, zval *paths, zval *map)
498 : {
499 : zval *value;
500 :
501 3616 : if (!map || Z_TYPE_P(map) != IS_ARRAY) {
502 1948 : return;
503 : }
504 :
505 3352 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(map), value) {
506 1684 : lsp_composer_collect_path_value(project_root, paths, value);
507 : } ZEND_HASH_FOREACH_END();
508 : }
509 :
510 3872 : static inline void lsp_composer_collect_autoload_section(zend_string *project_root, zval *paths, zval *section)
511 : {
512 : zval *psr4, *psr0, *classmap, *files;
513 :
514 3872 : if (!section || Z_TYPE_P(section) != IS_ARRAY) {
515 2064 : return;
516 : }
517 :
518 1808 : psr4 = zend_hash_str_find(Z_ARRVAL_P(section), "psr-4", sizeof("psr-4") - 1);
519 1808 : psr0 = zend_hash_str_find(Z_ARRVAL_P(section), "psr-0", sizeof("psr-0") - 1);
520 1808 : classmap = zend_hash_str_find(Z_ARRVAL_P(section), "classmap", sizeof("classmap") - 1);
521 1808 : files = zend_hash_str_find(Z_ARRVAL_P(section), "files", sizeof("files") - 1);
522 :
523 1808 : lsp_composer_collect_psr_map(project_root, paths, psr4);
524 1808 : lsp_composer_collect_psr_map(project_root, paths, psr0);
525 1808 : lsp_composer_collect_path_value(project_root, paths, classmap);
526 1808 : lsp_composer_collect_path_value(project_root, paths, files);
527 : }
528 :
529 76 : static inline void lsp_append_phpstan_analysis_paths(smart_str *contents, zval *paths)
530 : {
531 : zval *path_zv;
532 :
533 76 : if (zend_hash_num_elements(Z_ARRVAL_P(paths)) == 0) {
534 0 : return;
535 : }
536 :
537 76 : smart_str_appendl(contents, " paths:\n", sizeof(" paths:\n") - 1);
538 152 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(paths), path_zv) {
539 76 : if (Z_TYPE_P(path_zv) != IS_STRING) {
540 0 : continue;
541 : }
542 :
543 76 : smart_str_appendl(contents, " - ", sizeof(" - ") - 1);
544 76 : lsp_append_neon_single_quoted(contents, Z_STR_P(path_zv));
545 76 : smart_str_appendc(contents, '\n');
546 : } ZEND_HASH_FOREACH_END();
547 : }
548 :
549 24 : static inline void lsp_append_psalm_analysis_paths(smart_str *contents, zval *paths)
550 : {
551 : zend_string *tag;
552 : zval *path_zv;
553 :
554 48 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(paths), path_zv) {
555 24 : if (Z_TYPE_P(path_zv) != IS_STRING) {
556 0 : continue;
557 : }
558 :
559 24 : tag = lsp_directory_exists(Z_STR_P(path_zv)) ?
560 24 : zend_string_init("directory", sizeof("directory") - 1, 0) :
561 0 : zend_string_init("file", sizeof("file") - 1, 0)
562 : ;
563 24 : smart_str_appendl(contents, " <", sizeof(" <") - 1);
564 24 : smart_str_append(contents, tag);
565 24 : lsp_append_xml_attribute(contents, "name", sizeof("name") - 1, ZSTR_VAL(Z_STR_P(path_zv)), ZSTR_LEN(Z_STR_P(path_zv)));
566 24 : smart_str_appendl(contents, " />\n", sizeof(" />\n") - 1);
567 24 : zend_string_release(tag);
568 : } ZEND_HASH_FOREACH_END();
569 24 : }
570 :
571 20 : static inline void lsp_psalm_ignore_paths_add_existing(zval *paths, zend_string *path)
572 : {
573 20 : if (lsp_analysis_path_exists(path) && !lsp_analysis_paths_contains(paths, path)) {
574 20 : add_next_index_str(paths, zend_string_copy(path));
575 : }
576 20 : }
577 :
578 4 : static inline void lsp_psalm_collect_shadow_ignore_children(zval *paths, zend_string *shadow_dir)
579 : {
580 : const char *entry_name;
581 : lsp_dir *handle;
582 : zend_string *child;
583 :
584 4 : handle = lsp_dir_open(shadow_dir);
585 4 : if (!handle) {
586 0 : return;
587 : }
588 :
589 16 : while ((entry_name = lsp_dir_read(handle)) != NULL) {
590 12 : if (strcmp(entry_name, ".") == 0 || strcmp(entry_name, "..") == 0 || strcmp(entry_name, "psalm-type") == 0) {
591 8 : continue;
592 : }
593 :
594 4 : child = lsp_join_path2(shadow_dir, entry_name);
595 4 : lsp_psalm_ignore_paths_add_existing(paths, child);
596 4 : zend_string_release(child);
597 : }
598 :
599 4 : lsp_dir_close(handle);
600 : }
601 :
602 4 : static inline void lsp_psalm_collect_type_shadow_ignore_paths(zend_string *project_root, zval *paths)
603 : {
604 : const char *entry_name;
605 : lsp_dir *handle;
606 : zend_string *lsparrot_dir, *child;
607 :
608 4 : array_init(paths);
609 4 : lsparrot_dir = lsp_join_path2(project_root, ".lsparrot");
610 4 : handle = lsp_dir_open(lsparrot_dir);
611 4 : if (!handle) {
612 0 : zend_string_release(lsparrot_dir);
613 :
614 0 : return;
615 : }
616 :
617 32 : while ((entry_name = lsp_dir_read(handle)) != NULL) {
618 28 : if (strcmp(entry_name, ".") == 0 || strcmp(entry_name, "..") == 0) {
619 8 : continue;
620 : }
621 :
622 20 : child = lsp_join_path2(lsparrot_dir, entry_name);
623 20 : if (strcmp(entry_name, "shadow") == 0) {
624 4 : lsp_psalm_collect_shadow_ignore_children(paths, child);
625 : } else {
626 16 : lsp_psalm_ignore_paths_add_existing(paths, child);
627 : }
628 20 : zend_string_release(child);
629 : }
630 :
631 4 : lsp_dir_close(handle);
632 4 : zend_string_release(lsparrot_dir);
633 : }
634 :
635 20 : static inline void lsp_psalm_append_ignore_path_entry(smart_str *contents, zend_string *path)
636 : {
637 : const char *tag_name;
638 : size_t tag_name_length;
639 :
640 20 : tag_name = lsp_directory_exists(path) ? "directory" : "file";
641 20 : tag_name_length = lsp_directory_exists(path) ? sizeof("directory") - 1 : sizeof("file") - 1;
642 20 : smart_str_appendl(contents, " <", sizeof(" <") - 1);
643 20 : smart_str_appendl(contents, tag_name, tag_name_length);
644 20 : lsp_append_xml_attribute(contents, "name", sizeof("name") - 1, ZSTR_VAL(path), ZSTR_LEN(path));
645 20 : smart_str_appendl(contents, " />\n", sizeof(" />\n") - 1);
646 20 : }
647 :
648 4 : static inline void lsp_psalm_append_ignore_path_entries(smart_str *contents, zval *paths)
649 : {
650 : zval *path_zv;
651 :
652 24 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(paths), path_zv) {
653 20 : if (Z_TYPE_P(path_zv) != IS_STRING) {
654 0 : continue;
655 : }
656 :
657 20 : lsp_psalm_append_ignore_path_entry(contents, Z_STR_P(path_zv));
658 : } ZEND_HASH_FOREACH_END();
659 4 : }
660 :
661 4 : static inline zend_string *lsp_psalm_config_append_type_shadow_ignores(zend_string *contents, zend_string *project_root)
662 : {
663 : const char *contents_start, *contents_end, *ignore_close, *project_close;
664 4 : smart_str edited = {0};
665 : zval paths;
666 :
667 4 : lsp_psalm_collect_type_shadow_ignore_paths(project_root, &paths);
668 4 : if (zend_hash_num_elements(Z_ARRVAL(paths)) == 0) {
669 0 : zval_ptr_dtor(&paths);
670 :
671 0 : return zend_string_copy(contents);
672 : }
673 :
674 4 : contents_start = ZSTR_VAL(contents);
675 4 : contents_end = contents_start + ZSTR_LEN(contents);
676 4 : ignore_close = lsp_bounded_memstr(contents_start, "</ignoreFiles>", sizeof("</ignoreFiles>") - 1, contents_end);
677 4 : if (ignore_close) {
678 4 : smart_str_appendl(&edited, contents_start, ignore_close - contents_start);
679 4 : lsp_psalm_append_ignore_path_entries(&edited, &paths);
680 4 : smart_str_appendl(&edited, ignore_close, contents_end - ignore_close);
681 4 : smart_str_0(&edited);
682 4 : zval_ptr_dtor(&paths);
683 :
684 4 : return edited.s;
685 : }
686 :
687 0 : project_close = lsp_bounded_memstr(contents_start, "</projectFiles>", sizeof("</projectFiles>") - 1, contents_end);
688 0 : if (!project_close) {
689 0 : zval_ptr_dtor(&paths);
690 :
691 0 : return zend_string_copy(contents);
692 : }
693 :
694 0 : smart_str_appendl(&edited, contents_start, project_close - contents_start);
695 0 : smart_str_appendl(&edited, " <ignoreFiles>\n", sizeof(" <ignoreFiles>\n") - 1);
696 0 : lsp_psalm_append_ignore_path_entries(&edited, &paths);
697 0 : smart_str_appendl(&edited, " </ignoreFiles>\n", sizeof(" </ignoreFiles>\n") - 1);
698 0 : smart_str_appendl(&edited, project_close, contents_end - project_close);
699 0 : smart_str_0(&edited);
700 0 : zval_ptr_dtor(&paths);
701 :
702 0 : return edited.s;
703 : }
704 :
705 2872 : extern void lsp_composer_analysis_paths(zend_string *project_root, zval *paths)
706 : {
707 : zval *decoded, *autoload, *autoload_dev;
708 :
709 2872 : array_init(paths);
710 2872 : decoded = lsp_composer_json_decoded(project_root);
711 2872 : if (!decoded) {
712 936 : return;
713 : }
714 :
715 1936 : autoload = zend_hash_str_find(Z_ARRVAL_P(decoded), "autoload", sizeof("autoload") - 1);
716 1936 : autoload_dev = zend_hash_str_find(Z_ARRVAL_P(decoded), "autoload-dev", sizeof("autoload-dev") - 1);
717 1936 : lsp_composer_collect_autoload_section(project_root, paths, autoload);
718 1936 : lsp_composer_collect_autoload_section(project_root, paths, autoload_dev);
719 : }
720 :
721 640 : extern bool lsp_composer_project_has_analysis_paths(zend_string *project_root)
722 : {
723 : zval paths;
724 : bool result;
725 :
726 640 : lsp_composer_analysis_paths(project_root, &paths);
727 640 : result = zend_hash_num_elements(Z_ARRVAL(paths)) > 0;
728 640 : zval_ptr_dtor(&paths);
729 :
730 640 : return result;
731 : }
732 :
733 1060 : extern bool lsp_path_is_in_composer_analysis_paths(zend_string *path, zend_string *project_root)
734 : {
735 : zval paths, *path_zv;
736 : bool result;
737 :
738 1060 : result = false;
739 1060 : lsp_composer_analysis_paths(project_root, &paths);
740 1080 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL(paths), path_zv) {
741 628 : if (Z_TYPE_P(path_zv) == IS_STRING && lsp_path_is_same_or_under(path, Z_STR_P(path_zv))) {
742 608 : result = true;
743 608 : break;
744 : }
745 : } ZEND_HASH_FOREACH_END();
746 1060 : zval_ptr_dtor(&paths);
747 :
748 1060 : return result;
749 : }
750 :
751 24 : static inline zend_string *lsp_psalm_default_config_contents(zend_string *project_root, zend_long level)
752 : {
753 24 : smart_str contents = {0};
754 : zend_string *vendor_dir;
755 : zval paths;
756 : char level_buffer[32];
757 : int level_length;
758 :
759 24 : if (level < 1) {
760 0 : level = 6;
761 : }
762 :
763 24 : level_length = snprintf(level_buffer, sizeof(level_buffer), ZEND_LONG_FMT, level);
764 24 : vendor_dir = lsp_composer_vendor_dir(project_root);
765 24 : lsp_composer_analysis_paths(project_root, &paths);
766 :
767 24 : smart_str_appendl(&contents, "<?xml version=\"1.0\"?>\n<psalm", sizeof("<?xml version=\"1.0\"?>\n<psalm") - 1);
768 24 : if (level_length > 0) {
769 24 : lsp_append_xml_attribute(&contents, "errorLevel", sizeof("errorLevel") - 1, level_buffer, (size_t) level_length);
770 : }
771 :
772 24 : smart_str_appendl(&contents, ">\n <projectFiles>\n", sizeof(">\n <projectFiles>\n") - 1);
773 24 : lsp_append_psalm_analysis_paths(&contents, &paths);
774 24 : smart_str_appendl(&contents, " <ignoreFiles>\n <directory name=\".lsparrot\" />\n", sizeof(" <ignoreFiles>\n <directory name=\".lsparrot\" />\n") - 1);
775 24 : if (lsp_directory_exists(vendor_dir)) {
776 24 : smart_str_appendl(&contents, " <directory", sizeof(" <directory") - 1);
777 24 : lsp_append_xml_attribute(&contents, "name", sizeof("name") - 1, ZSTR_VAL(vendor_dir), ZSTR_LEN(vendor_dir));
778 24 : smart_str_appendl(&contents, " />\n", sizeof(" />\n") - 1);
779 : }
780 :
781 24 : smart_str_appendl(&contents, " </ignoreFiles>\n </projectFiles>\n</psalm>\n", sizeof(" </ignoreFiles>\n </projectFiles>\n</psalm>\n") - 1);
782 24 : smart_str_0(&contents);
783 :
784 24 : zend_string_release(vendor_dir);
785 24 : zval_ptr_dtor(&paths);
786 :
787 24 : return contents.s;
788 : }
789 :
790 88 : extern zend_string *lsp_phpstan_config_file(zend_string *root)
791 : {
792 88 : const char *names[] = {"phpstan.neon", "phpstan.neon.dist", "phpstan.dist.neon", ".phpstan.neon", ".phpstan.neon.dist"};
793 :
794 88 : return lsp_analyzer_config_file_named(root, names, sizeof(names) / sizeof(names[0]));
795 : }
796 :
797 88 : extern zend_string *lsp_phpstan_lsp_config_file(zend_string *project_root, zend_string *project_config, zend_long parallel_workers)
798 : {
799 88 : smart_str contents = {0};
800 : zend_string *dir, *cache_dir, *path;
801 : zval paths;
802 : char parallel_buffer[32];
803 : int parallel_length;
804 : bool ok;
805 :
806 88 : ok = false;
807 88 : dir = strpprintf(0, "%s/.lsparrot/phpstan", ZSTR_VAL(project_root));
808 88 : cache_dir = strpprintf(0, "%s/cache", ZSTR_VAL(dir));
809 88 : path = strpprintf(0, "%s/lsp.neon", ZSTR_VAL(dir));
810 88 : lsp_mkdir_p(cache_dir);
811 :
812 88 : if (project_config) {
813 12 : smart_str_appendl(&contents, "includes:\n - ", sizeof("includes:\n - ") - 1);
814 12 : lsp_append_neon_single_quoted(&contents, project_config);
815 12 : smart_str_appendl(&contents, "\n\n", sizeof("\n\n") - 1);
816 : }
817 :
818 88 : smart_str_appendl(&contents, "parameters:\n tmpDir: ", sizeof("parameters:\n tmpDir: ") - 1);
819 88 : lsp_append_neon_single_quoted(&contents, cache_dir);
820 88 : smart_str_appendc(&contents, '\n');
821 :
822 88 : if (!project_config) {
823 76 : lsp_composer_analysis_paths(project_root, &paths);
824 76 : lsp_append_phpstan_analysis_paths(&contents, &paths);
825 76 : zval_ptr_dtor(&paths);
826 : }
827 :
828 88 : if (!project_config && parallel_workers > 1) {
829 76 : parallel_length = snprintf(parallel_buffer, sizeof(parallel_buffer), ZEND_LONG_FMT, parallel_workers);
830 76 : if (parallel_length > 0) {
831 76 : smart_str_appendl(&contents, " parallel:\n maximumNumberOfProcesses: ", sizeof(" parallel:\n maximumNumberOfProcesses: ") - 1);
832 76 : smart_str_appendl(&contents, parallel_buffer, (size_t) parallel_length);
833 76 : smart_str_appendl(&contents, "\n processTimeout: 600.0\n jobSize: 20\n", sizeof("\n processTimeout: 600.0\n jobSize: 20\n") - 1);
834 : }
835 : }
836 88 : smart_str_0(&contents);
837 :
838 88 : if (contents.s) {
839 88 : ok = lsp_write_string_file(path, contents.s);
840 88 : zend_string_release(contents.s);
841 : }
842 :
843 88 : zend_string_release(cache_dir);
844 88 : zend_string_release(dir);
845 :
846 88 : if (!ok) {
847 0 : zend_string_release(path);
848 :
849 0 : return NULL;
850 : }
851 :
852 88 : return path;
853 : }
854 :
855 88 : extern zend_long lsp_project_phpstan_level(lsp_server *server, zend_string *project_root)
856 : {
857 88 : return lsp_project_vscode_config_long(project_root, "phpstanLevel", server->options.phpstan_level);
858 : }
859 :
860 68 : extern zend_string *lsp_psalm_config_file(zend_string *root)
861 : {
862 68 : const char *names[] = {"psalm.xml", "psalm.xml.dist"};
863 :
864 68 : return lsp_analyzer_config_file_named(root, names, sizeof(names) / sizeof(names[0]));
865 : }
866 :
867 68 : extern zend_long lsp_project_psalm_level(lsp_server *server, zend_string *project_root)
868 : {
869 68 : return lsp_project_vscode_config_long(project_root, "psalmLevel", server->options.psalm_level);
870 : }
871 :
872 68 : extern zend_string *lsp_psalm_ls_config_file(zend_string *project_root, zend_string *project_config, zend_long level, bool live_dead_code_diagnostics)
873 : {
874 : zend_string *dir, *cache_dir, *path, *contents, *cache_updated, *resolve_updated, *unused_updated, *baseline_updated;
875 : bool ok;
876 :
877 68 : ok = false;
878 68 : dir = strpprintf(0, "%s/.lsparrot/psalm", ZSTR_VAL(project_root));
879 68 : cache_dir = strpprintf(0, "%s/cache", ZSTR_VAL(dir));
880 68 : path = strpprintf(0, "%s/lsp.xml", ZSTR_VAL(dir));
881 68 : lsp_mkdir_p(cache_dir);
882 :
883 68 : contents = project_config ? lsp_read_file(project_config) : lsp_psalm_default_config_contents(project_root, level);
884 68 : if (contents == zend_empty_string) {
885 0 : zend_string_release(cache_dir);
886 0 : zend_string_release(dir);
887 0 : zend_string_release(path);
888 :
889 0 : return NULL;
890 : }
891 :
892 68 : cache_updated = lsp_psalm_config_upsert_attribute(contents, "cacheDirectory", ZSTR_VAL(cache_dir), ZSTR_LEN(cache_dir));
893 68 : zend_string_release(contents);
894 68 : if (!cache_updated) {
895 0 : zend_string_release(cache_dir);
896 0 : zend_string_release(dir);
897 0 : zend_string_release(path);
898 :
899 0 : return NULL;
900 : }
901 :
902 68 : resolve_updated = lsp_psalm_config_upsert_attribute(cache_updated, "resolveFromConfigFile", "false", sizeof("false") - 1);
903 68 : zend_string_release(cache_updated);
904 68 : if (!resolve_updated) {
905 0 : zend_string_release(cache_dir);
906 0 : zend_string_release(dir);
907 0 : zend_string_release(path);
908 :
909 0 : return NULL;
910 : }
911 :
912 68 : if (!live_dead_code_diagnostics) {
913 68 : unused_updated = lsp_psalm_config_upsert_attribute(resolve_updated, "findUnusedCode", "false", sizeof("false") - 1);
914 68 : zend_string_release(resolve_updated);
915 68 : if (!unused_updated) {
916 0 : zend_string_release(cache_dir);
917 0 : zend_string_release(dir);
918 0 : zend_string_release(path);
919 :
920 0 : return NULL;
921 : }
922 :
923 68 : baseline_updated = lsp_psalm_config_upsert_attribute(unused_updated, "findUnusedBaselineEntry", "false", sizeof("false") - 1);
924 68 : zend_string_release(unused_updated);
925 68 : if (!baseline_updated) {
926 0 : zend_string_release(cache_dir);
927 0 : zend_string_release(dir);
928 0 : zend_string_release(path);
929 :
930 0 : return NULL;
931 : }
932 :
933 68 : resolve_updated = baseline_updated;
934 : }
935 :
936 68 : if (resolve_updated) {
937 68 : ok = lsp_write_string_file(path, resolve_updated);
938 :
939 68 : zend_string_release(resolve_updated);
940 : }
941 :
942 68 : zend_string_release(cache_dir);
943 68 : zend_string_release(dir);
944 :
945 68 : if (!ok) {
946 0 : zend_string_release(path);
947 :
948 0 : return NULL;
949 : }
950 :
951 68 : return path;
952 : }
953 :
954 4 : extern zend_string *lsp_psalm_type_config_file(zend_string *project_root, zend_string *project_config, zend_long level, bool live_dead_code_diagnostics)
955 : {
956 : zend_string *base_path, *base_contents, *without_lsparrot_ignore, *type_contents, *dir, *path;
957 : bool ok;
958 :
959 4 : ok = false;
960 4 : base_path = lsp_psalm_ls_config_file(project_root, project_config, level, live_dead_code_diagnostics);
961 4 : if (!base_path) {
962 0 : return NULL;
963 : }
964 :
965 4 : base_contents = lsp_read_file(base_path);
966 4 : zend_string_release(base_path);
967 4 : if (base_contents == zend_empty_string) {
968 0 : return NULL;
969 : }
970 :
971 4 : without_lsparrot_ignore = lsp_psalm_config_without_lsparrot_root_ignore(base_contents, project_root);
972 4 : zend_string_release(base_contents);
973 4 : type_contents = lsp_psalm_config_append_type_shadow_ignores(without_lsparrot_ignore, project_root);
974 4 : zend_string_release(without_lsparrot_ignore);
975 :
976 4 : dir = strpprintf(0, "%s/.lsparrot/psalm", ZSTR_VAL(project_root));
977 4 : path = strpprintf(0, "%s/type.xml", ZSTR_VAL(dir));
978 4 : lsp_mkdir_p(dir);
979 4 : ok = lsp_write_string_file(path, type_contents);
980 4 : zend_string_release(type_contents);
981 4 : zend_string_release(dir);
982 :
983 4 : if (!ok) {
984 0 : zend_string_release(path);
985 :
986 0 : return NULL;
987 : }
988 :
989 4 : return path;
990 : }
|