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 : /* Quick fixes for analyzer (PHPStan/Psalm) diagnostics: suppression comments
17 : * and type guarantees via "@var" docblocks or assert() statements. The fix
18 : * material comes from the diagnostics echoed back by the client inside the
19 : * codeAction request context, so no extra server state is required. */
20 :
21 16 : static inline bool lsp_quick_fix_line_bounds(zend_string *text, zend_long line, size_t *line_start, size_t *line_end, size_t *indent_end)
22 : {
23 16 : const char *value = ZSTR_VAL(text);
24 16 : size_t offset, length = ZSTR_LEN(text);
25 :
26 16 : offset = lsp_offset_at(text, line, 0);
27 16 : if (offset > length) {
28 0 : return false;
29 : }
30 :
31 16 : *line_start = offset;
32 336 : while (offset < length && value[offset] != '\n') {
33 320 : offset++;
34 : }
35 16 : *line_end = offset;
36 :
37 16 : offset = *line_start;
38 16 : while (offset < *line_end && (value[offset] == ' ' || value[offset] == '\t')) {
39 0 : offset++;
40 : }
41 16 : *indent_end = offset;
42 :
43 16 : return true;
44 : }
45 :
46 16 : static inline zend_long lsp_quick_fix_diagnostic_line(zval *diagnostic)
47 : {
48 : zval *range, *start;
49 :
50 16 : range = lsp_array_find(diagnostic, "range");
51 16 : start = range ? lsp_array_find(range, "start") : NULL;
52 :
53 16 : return start ? lsp_array_long(start, "line", 0) : 0;
54 : }
55 :
56 24 : static inline void lsp_quick_fix_add_insert_action(zval *actions, lsp_document *document, zval *diagnostic, zend_string *title, zend_long line, zend_string *indent, zend_string *inserted)
57 : {
58 : zend_string *new_text;
59 : zval action, diagnostics, diagnostic_copy, edit, changes, edits, text_edit, range, start, end;
60 :
61 24 : new_text = strpprintf(0, "%s%s\n", ZSTR_VAL(indent), ZSTR_VAL(inserted));
62 :
63 24 : array_init(&action);
64 24 : add_assoc_str(&action, "title", zend_string_copy(title));
65 24 : add_assoc_string(&action, "kind", "quickfix");
66 :
67 24 : array_init(&diagnostics);
68 24 : ZVAL_COPY(&diagnostic_copy, diagnostic);
69 24 : add_next_index_zval(&diagnostics, &diagnostic_copy);
70 24 : add_assoc_zval(&action, "diagnostics", &diagnostics);
71 :
72 24 : array_init(&start);
73 24 : add_assoc_long(&start, "line", line);
74 24 : add_assoc_long(&start, "character", 0);
75 24 : array_init(&end);
76 24 : add_assoc_long(&end, "line", line);
77 24 : add_assoc_long(&end, "character", 0);
78 24 : array_init(&range);
79 24 : add_assoc_zval(&range, "start", &start);
80 24 : add_assoc_zval(&range, "end", &end);
81 :
82 24 : array_init(&text_edit);
83 24 : add_assoc_zval(&text_edit, "range", &range);
84 24 : add_assoc_str(&text_edit, "newText", new_text);
85 :
86 24 : array_init(&edits);
87 24 : add_next_index_zval(&edits, &text_edit);
88 :
89 24 : array_init(&changes);
90 24 : add_assoc_zval_ex(&changes, ZSTR_VAL(document->uri), ZSTR_LEN(document->uri), &edits);
91 :
92 24 : array_init(&edit);
93 24 : add_assoc_zval(&edit, "changes", &changes);
94 24 : add_assoc_zval(&action, "edit", &edit);
95 :
96 24 : add_next_index_zval(actions, &action);
97 24 : }
98 :
99 24 : static inline bool lsp_quick_fix_type_char_is_plain(char c)
100 : {
101 24 : return lsp_doc_is_identifier_char(c) || c == '\\';
102 : }
103 :
104 : /* Extract an "expected type" out of well-known analyzer message templates:
105 : * "... expects TYPE, ... given" (argument.type)
106 : * "Property ... (TYPE) does not accept ..."
107 : * "... should return TYPE but ..." (return.type)
108 : * "Cannot assign TYPE ..." style messages are intentionally not handled.
109 : */
110 16 : static inline zend_string *lsp_quick_fix_expected_type(zend_string *message)
111 : {
112 16 : const char *value = ZSTR_VAL(message), *marker, *type_start, *type_end, *limit;
113 :
114 16 : limit = value + ZSTR_LEN(message);
115 :
116 16 : marker = strstr(value, " expects ");
117 16 : if (marker) {
118 8 : type_start = marker + sizeof(" expects ") - 1;
119 8 : type_end = type_start;
120 56 : while (type_end < limit && *type_end != ',' && *type_end != '\0') {
121 48 : type_end++;
122 : }
123 8 : if (type_end > type_start) {
124 8 : return zend_string_init(type_start, type_end - type_start, 0);
125 : }
126 : }
127 :
128 8 : marker = strstr(value, ") does not accept ");
129 8 : if (marker) {
130 0 : type_end = marker;
131 0 : type_start = type_end;
132 0 : while (type_start > value && *(type_start - 1) != '(') {
133 0 : type_start--;
134 : }
135 0 : if (type_start > value && type_end > type_start) {
136 0 : return zend_string_init(type_start, type_end - type_start, 0);
137 : }
138 : }
139 :
140 8 : marker = strstr(value, " should return ");
141 8 : if (marker) {
142 0 : type_start = marker + sizeof(" should return ") - 1;
143 0 : type_end = type_start;
144 0 : while (type_end < limit && *type_end != ' ' && *type_end != ',' && *type_end != '\0') {
145 0 : type_end++;
146 : }
147 0 : if (type_end > type_start) {
148 0 : return zend_string_init(type_start, type_end - type_start, 0);
149 : }
150 : }
151 :
152 8 : return NULL;
153 : }
154 :
155 : /* Find the variable a "@var"/assert() guarantee should target: prefer the
156 : * left-hand side of an assignment on the diagnostic line, otherwise a
157 : * variable that the analyzer message names and the line actually contains. */
158 8 : static inline zend_string *lsp_quick_fix_target_variable(zend_string *text, size_t indent_end, size_t line_end, zend_string *message)
159 : {
160 8 : const char *value = ZSTR_VAL(text), *cursor, *name_start, *message_value, *dollar, *message_limit;
161 : zend_string *candidate;
162 : size_t name_length;
163 : bool found;
164 :
165 8 : if (indent_end < line_end && value[indent_end] == '$') {
166 0 : name_start = value + indent_end + 1;
167 0 : cursor = name_start;
168 0 : while ((size_t) (cursor - value) < line_end && lsp_doc_is_identifier_char(*cursor)) {
169 0 : cursor++;
170 : }
171 0 : name_length = cursor - name_start;
172 :
173 0 : while ((size_t) (cursor - value) < line_end && (*cursor == ' ' || *cursor == '\t')) {
174 0 : cursor++;
175 : }
176 :
177 0 : if (name_length > 0 && (size_t) (cursor - value) < line_end && *cursor == '=' &&
178 0 : ((size_t) (cursor + 1 - value) >= line_end || *(cursor + 1) != '=')
179 : ) {
180 0 : return zend_string_init(name_start - 1, name_length + 1, 0);
181 : }
182 : }
183 :
184 8 : message_value = ZSTR_VAL(message);
185 8 : message_limit = message_value + ZSTR_LEN(message);
186 8 : dollar = message_value;
187 12 : while ((dollar = memchr(dollar, '$', message_limit - dollar)) != NULL) {
188 8 : name_start = dollar + 1;
189 8 : cursor = name_start;
190 32 : while (cursor < message_limit && lsp_doc_is_identifier_char(*cursor)) {
191 24 : cursor++;
192 : }
193 8 : name_length = cursor - name_start;
194 8 : if (name_length == 0) {
195 0 : dollar++;
196 0 : continue;
197 : }
198 :
199 8 : candidate = zend_string_init(dollar, name_length + 1, 0);
200 :
201 : /* The variable must occur on the diagnostic line itself. */
202 8 : found = false;
203 132 : for (cursor = value + indent_end; (size_t) (cursor - value) + ZSTR_LEN(candidate) <= line_end; cursor++) {
204 128 : if (memcmp(cursor, ZSTR_VAL(candidate), ZSTR_LEN(candidate)) == 0 &&
205 4 : ((size_t) (cursor - value) + ZSTR_LEN(candidate) >= line_end ||
206 4 : !lsp_doc_is_identifier_char(cursor[ZSTR_LEN(candidate)]))
207 : ) {
208 4 : found = true;
209 4 : break;
210 : }
211 : }
212 :
213 8 : if (found) {
214 4 : return candidate;
215 : }
216 :
217 4 : zend_string_release(candidate);
218 4 : dollar = name_start;
219 : }
220 :
221 4 : return NULL;
222 : }
223 :
224 4 : static inline bool lsp_quick_fix_type_is_plain(zend_string *type)
225 : {
226 : size_t i;
227 :
228 4 : if (ZSTR_LEN(type) == 0) {
229 0 : return false;
230 : }
231 :
232 28 : for (i = 0; i < ZSTR_LEN(type); i++) {
233 24 : if (!lsp_quick_fix_type_char_is_plain(ZSTR_VAL(type)[i])) {
234 0 : return false;
235 : }
236 : }
237 :
238 4 : return true;
239 : }
240 :
241 8 : static inline const char *lsp_quick_fix_scalar_assert_function(zend_string *type)
242 : {
243 8 : if (zend_string_equals_literal(type, "string")) {
244 8 : return "is_string";
245 : }
246 0 : if (zend_string_equals_literal(type, "int")) {
247 0 : return "is_int";
248 : }
249 0 : if (zend_string_equals_literal(type, "float")) {
250 0 : return "is_float";
251 : }
252 0 : if (zend_string_equals_literal(type, "bool")) {
253 0 : return "is_bool";
254 : }
255 0 : if (zend_string_equals_literal(type, "array")) {
256 0 : return "is_array";
257 : }
258 0 : if (zend_string_equals_literal(type, "callable")) {
259 0 : return "is_callable";
260 : }
261 0 : if (zend_string_equals_literal(type, "iterable")) {
262 0 : return "is_iterable";
263 : }
264 0 : if (zend_string_equals_literal(type, "object")) {
265 0 : return "is_object";
266 : }
267 :
268 0 : return NULL;
269 : }
270 :
271 4 : static inline bool lsp_quick_fix_type_is_class_like(zend_string *type)
272 : {
273 : char first;
274 :
275 4 : if (!lsp_quick_fix_type_is_plain(type) || lsp_quick_fix_scalar_assert_function(type) != NULL ||
276 0 : zend_string_equals_literal(type, "null") ||
277 0 : zend_string_equals_literal(type, "mixed") ||
278 0 : zend_string_equals_literal(type, "void") ||
279 0 : zend_string_equals_literal(type, "true") ||
280 0 : zend_string_equals_literal(type, "false")
281 : ) {
282 4 : return false;
283 : }
284 :
285 0 : first = ZSTR_VAL(type)[0];
286 :
287 0 : return first == '\\' || (first >= 'A' && first <= 'Z');
288 : }
289 :
290 16 : static inline void lsp_quick_fix_add_type_guarantee_actions(zval *actions, lsp_document *document, zval *diagnostic, zend_string *message, zend_long line, zend_string *indent, size_t indent_end, size_t line_end)
291 : {
292 : const char *assert_function;
293 : zend_string *expected_type, *variable, *title, *inserted;
294 :
295 16 : expected_type = lsp_quick_fix_expected_type(message);
296 16 : if (!expected_type) {
297 8 : return;
298 : }
299 :
300 8 : variable = lsp_quick_fix_target_variable(document->text, indent_end, line_end, message);
301 8 : if (!variable) {
302 4 : zend_string_release(expected_type);
303 :
304 4 : return;
305 : }
306 :
307 4 : title = strpprintf(0, "Assume type: @var %s %s", ZSTR_VAL(expected_type), ZSTR_VAL(variable));
308 4 : inserted = strpprintf(0, "/** @var %s %s */", ZSTR_VAL(expected_type), ZSTR_VAL(variable));
309 4 : lsp_quick_fix_add_insert_action(actions, document, diagnostic, title, line, indent, inserted);
310 4 : zend_string_release(inserted);
311 4 : zend_string_release(title);
312 :
313 4 : if (lsp_quick_fix_type_is_class_like(expected_type)) {
314 0 : title = strpprintf(0, "Guarantee type: assert(%s instanceof %s)", ZSTR_VAL(variable), ZSTR_VAL(expected_type));
315 0 : inserted = strpprintf(0, "assert(%s instanceof %s);", ZSTR_VAL(variable), ZSTR_VAL(expected_type));
316 0 : lsp_quick_fix_add_insert_action(actions, document, diagnostic, title, line, indent, inserted);
317 0 : zend_string_release(inserted);
318 0 : zend_string_release(title);
319 : } else {
320 4 : assert_function = lsp_quick_fix_scalar_assert_function(expected_type);
321 4 : if (assert_function) {
322 4 : title = strpprintf(0, "Guarantee type: assert(%s(%s))", assert_function, ZSTR_VAL(variable));
323 4 : inserted = strpprintf(0, "assert(%s(%s));", assert_function, ZSTR_VAL(variable));
324 4 : lsp_quick_fix_add_insert_action(actions, document, diagnostic, title, line, indent, inserted);
325 4 : zend_string_release(inserted);
326 4 : zend_string_release(title);
327 : }
328 : }
329 :
330 4 : zend_string_release(variable);
331 4 : zend_string_release(expected_type);
332 : }
333 :
334 16 : static inline void lsp_quick_fix_add_suppress_action(zval *actions, lsp_document *document, zval *diagnostic, zend_string *source, zend_string *code, zend_long line, zend_string *indent)
335 : {
336 : zend_string *title, *inserted;
337 :
338 16 : if (zend_string_equals_literal(source, "phpstan")) {
339 8 : if (code) {
340 4 : title = strpprintf(0, "Suppress PHPStan: @phpstan-ignore %s", ZSTR_VAL(code));
341 4 : inserted = strpprintf(0, "/* @phpstan-ignore %s */", ZSTR_VAL(code));
342 : } else {
343 4 : title = zend_string_init(ZEND_STRL("Suppress PHPStan: @phpstan-ignore-next-line"), 0);
344 4 : inserted = zend_string_init(ZEND_STRL("/* @phpstan-ignore-next-line */"), 0);
345 : }
346 8 : } else if (zend_string_equals_literal(source, "psalm") || zend_string_equals_literal(source, "psalm-ls")) {
347 8 : if (!code) {
348 0 : return;
349 : }
350 :
351 8 : title = strpprintf(0, "Suppress Psalm: @psalm-suppress %s", ZSTR_VAL(code));
352 8 : inserted = strpprintf(0, "/** @psalm-suppress %s */", ZSTR_VAL(code));
353 : } else {
354 0 : return;
355 : }
356 :
357 16 : lsp_quick_fix_add_insert_action(actions, document, diagnostic, title, line, indent, inserted);
358 16 : zend_string_release(inserted);
359 16 : zend_string_release(title);
360 : }
361 :
362 8 : extern void lsp_add_analyzer_quick_fixes(zval *actions, lsp_document *document, zval *params)
363 : {
364 : zend_long line;
365 : zend_string *source, *code, *message, *indent;
366 : zval *context, *diagnostics, *diagnostic;
367 8 : size_t line_start = 0, line_end = 0, indent_end = 0;
368 :
369 8 : context = lsp_array_find(params, "context");
370 8 : diagnostics = context ? lsp_array_find(context, "diagnostics") : NULL;
371 8 : if (!diagnostics || Z_TYPE_P(diagnostics) != IS_ARRAY) {
372 0 : return;
373 : }
374 :
375 24 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(diagnostics), diagnostic) {
376 16 : if (Z_TYPE_P(diagnostic) != IS_ARRAY) {
377 0 : continue;
378 : }
379 :
380 16 : source = lsp_array_string(diagnostic, "source");
381 16 : if (!source || (
382 16 : !zend_string_equals_literal(source, "phpstan") &&
383 8 : !zend_string_equals_literal(source, "psalm") &&
384 4 : !zend_string_equals_literal(source, "psalm-ls")
385 : )) {
386 0 : continue;
387 : }
388 :
389 16 : message = lsp_array_string(diagnostic, "message");
390 16 : code = lsp_array_string(diagnostic, "code");
391 16 : line = lsp_quick_fix_diagnostic_line(diagnostic);
392 16 : if (!message || !lsp_quick_fix_line_bounds(document->text, line, &line_start, &line_end, &indent_end)) {
393 0 : continue;
394 : }
395 :
396 16 : indent = zend_string_init(ZSTR_VAL(document->text) + line_start, indent_end - line_start, 0);
397 :
398 16 : lsp_quick_fix_add_type_guarantee_actions(actions, document, diagnostic, message, line, indent, indent_end, line_end);
399 16 : lsp_quick_fix_add_suppress_action(actions, document, diagnostic, source, code, line, indent);
400 :
401 16 : zend_string_release(indent);
402 : } ZEND_HASH_FOREACH_END();
403 : }
|