phpantom_lsp 0.7.0

Fast PHP language server with deep type intelligence. Generics, Laravel, PHPStan annotations. Ready in an instant.
Documentation
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
use super::*;
use crate::completion::source::throws_analysis;

#[test]
fn test_find_method_throws_tags_with_private() {
    let content = concat!(
        "<?php\n",
        "class Foo {\n",
        "    /** @throws ValidationException */\n",
        "    private function riskyOperation(): void {}\n",
        "}\n",
    );
    let result = throws_analysis::find_method_throws_tags(content, "riskyOperation");
    assert_eq!(
        result,
        vec!["ValidationException"],
        "Should find @throws through 'private' modifier"
    );
}

#[test]
fn test_find_method_throws_tags_with_protected_static() {
    let content = concat!(
        "<?php\n",
        "class Foo {\n",
        "    /** @throws RuntimeException */\n",
        "    protected static function dangerousCall(): void {}\n",
        "}\n",
    );
    let result = throws_analysis::find_method_throws_tags(content, "dangerousCall");
    assert_eq!(
        result,
        vec!["RuntimeException"],
        "Should find @throws through 'protected static' modifiers"
    );
}

#[test]
fn test_find_method_throws_tags_without_modifier() {
    let content = concat!(
        "<?php\n",
        "/** @throws LogicException */\n",
        "function standalone(): void {}\n",
    );
    let result = throws_analysis::find_method_throws_tags(content, "standalone");
    assert_eq!(
        result,
        vec!["LogicException"],
        "Should find @throws on a standalone function (no modifier)"
    );
}

#[test]
fn test_propagated_throws_with_visibility_in_catch() {
    // Full file content — cursor will be inside catch()
    //                                                    v cursor (line 5, char 17)
    // Line 0: <?php
    // Line 1: class Foo {
    // Line 2:     public function doStuff(): void {
    // Line 3:         try {
    // Line 4:             $this->riskyOperation();
    // Line 5:         } catch () {}
    // Line 6:     }
    // Line 7:
    // Line 8:     /** @throws ValidationException */
    // Line 9:     private function riskyOperation(): void {}
    // Line 10: }
    let full_content = concat!(
        "<?php\n",
        "class Foo {\n",
        "    public function doStuff(): void {\n",
        "        try {\n",
        "            $this->riskyOperation();\n",
        "        } catch () {}\n",
        "    }\n",
        "\n",
        "    /** @throws ValidationException */\n",
        "    private function riskyOperation(): void {}\n",
        "}\n",
    );

    // Character 17 is between `(` (char 16) and `)` (char 17) on line 5
    let pos = Position {
        line: 5,
        character: 17,
    };
    let ctx = detect_catch_context(full_content, pos);
    assert!(ctx.is_some(), "Should detect catch context");
    let ctx = ctx.unwrap();
    assert!(
        ctx.suggested_types
            .contains(&"ValidationException".to_string()),
        "Should suggest ValidationException from propagated @throws on private method, got: {:?}",
        ctx.suggested_types
    );
}

#[test]
fn test_propagated_throws_with_protected_static_in_catch() {
    let full_content = concat!(
        "<?php\n",
        "class Bar {\n",
        "    public function handle(): void {\n",
        "        try {\n",
        "            $this->dangerousCall();\n",
        "        } catch () {}\n",
        "    }\n",
        "\n",
        "    /** @throws RuntimeException */\n",
        "    protected static function dangerousCall(): void {}\n",
        "}\n",
    );

    let pos = Position {
        line: 5,
        character: 17,
    };
    let ctx = detect_catch_context(full_content, pos);
    assert!(ctx.is_some(), "Should detect catch context");
    let ctx = ctx.unwrap();
    assert!(
        ctx.suggested_types
            .contains(&"RuntimeException".to_string()),
        "Should suggest RuntimeException through protected static modifier, got: {:?}",
        ctx.suggested_types
    );
}

#[test]
fn test_find_inline_throws_annotations_in_catch() {
    let body = r#"
        /** @throws ModelNotFoundException */
        $model = SomeService::find($id);
        /** @throws \App\Exceptions\AuthException */
        $auth = doSomething();
    "#;
    let result = throws_analysis::find_inline_throws_annotations(body);
    // Raw names are returned; short-name extraction happens in detect_catch_context
    let names: Vec<&str> = result.iter().map(|t| t.type_name.as_str()).collect();
    assert_eq!(
        names,
        vec!["ModelNotFoundException", "App\\Exceptions\\AuthException"]
    );
}

#[test]
fn test_find_inline_throws_multiline_docblock_in_catch() {
    let body = r#"
        /**
         * @throws RuntimeException
         */
        doStuff();
    "#;
    let result = throws_analysis::find_inline_throws_annotations(body);
    let names: Vec<&str> = result.iter().map(|t| t.type_name.as_str()).collect();
    assert_eq!(names, vec!["RuntimeException"]);
}

#[test]
fn test_parse_catch_paren_content_empty() {
    let (partial, already) = parse_catch_paren_content("");
    assert_eq!(partial, "");
    assert!(already.is_empty());
}

#[test]
fn test_parse_catch_paren_content_partial() {
    let (partial, already) = parse_catch_paren_content("IOEx");
    assert_eq!(partial, "IOEx");
    assert!(already.is_empty());
}

#[test]
fn test_parse_catch_paren_content_multi_catch() {
    let (partial, already) = parse_catch_paren_content("IOException | ");
    assert_eq!(partial, "");
    assert_eq!(already, vec!["IOException"]);
}

#[test]
fn test_parse_catch_paren_content_multi_catch_with_partial() {
    let (partial, already) = parse_catch_paren_content("IOException | Time");
    assert_eq!(partial, "Time");
    assert_eq!(already, vec!["IOException"]);
}

#[test]
fn test_parse_catch_paren_content_three_types() {
    let (partial, already) = parse_catch_paren_content("IOException | TimeoutException | ");
    assert_eq!(partial, "");
    assert_eq!(already, vec!["IOException", "TimeoutException"]);
}

#[test]
fn test_detect_catch_context_always_includes_throwable() {
    let content = concat!(
        "<?php\n",
        "try {\n",
        "    throw new RuntimeException('error');\n",
        "} catch (",
    );
    let pos = Position {
        line: 3,
        character: 10,
    };
    let ctx = detect_catch_context(content, pos).unwrap();
    assert!(
        ctx.suggested_types.contains(&"\\Throwable".to_string()),
        "Should always include \\Throwable, got: {:?}",
        ctx.suggested_types
    );
    assert!(ctx.has_specific_types);
}

#[test]
fn test_detect_catch_context_no_specific_types_sets_flag() {
    let content = concat!("<?php\n", "try {\n", "    doSomething();\n", "} catch (",);
    let pos = Position {
        line: 3,
        character: 10,
    };
    let ctx = detect_catch_context(content, pos).unwrap();
    assert!(
        !ctx.has_specific_types,
        "Should have no specific types when try block has no throws"
    );
    // Throwable is still offered
    assert!(ctx.suggested_types.contains(&"\\Throwable".to_string()));
}

#[test]
fn test_detect_catch_context_simple() {
    let content = concat!(
        "<?php\n",
        "try {\n",
        "    throw new RuntimeException('error');\n",
        "} catch (",
    );
    let pos = Position {
        line: 3,
        character: 10,
    };
    let ctx = detect_catch_context(content, pos);
    assert!(ctx.is_some(), "Should detect catch context");
    let ctx = ctx.unwrap();
    assert!(
        ctx.suggested_types
            .contains(&"RuntimeException".to_string()),
        "Should suggest RuntimeException, got: {:?}",
        ctx.suggested_types
    );
}

#[test]
fn test_detect_catch_context_with_inline_throws() {
    let content = concat!(
        "<?php\n",
        "try {\n",
        "    /** @throws ModelNotFoundException */\n",
        "    $model = SomeService::find($id);\n",
        "} catch (",
    );
    let pos = Position {
        line: 4,
        character: 10,
    };
    let ctx = detect_catch_context(content, pos);
    assert!(ctx.is_some(), "Should detect catch context");
    let ctx = ctx.unwrap();
    assert!(
        ctx.suggested_types
            .contains(&"ModelNotFoundException".to_string()),
        "Should suggest ModelNotFoundException from inline @throws, got: {:?}",
        ctx.suggested_types
    );
}

#[test]
fn test_detect_catch_context_multi_throw() {
    let content = concat!(
        "<?php\n",
        "try {\n",
        "    throw new IOException('io');\n",
        "    throw new TimeoutException('timeout');\n",
        "} catch (",
    );
    let pos = Position {
        line: 4,
        character: 10,
    };
    let ctx = detect_catch_context(content, pos);
    assert!(ctx.is_some());
    let ctx = ctx.unwrap();
    assert!(ctx.suggested_types.contains(&"IOException".to_string()));
    assert!(
        ctx.suggested_types
            .contains(&"TimeoutException".to_string())
    );
}

#[test]
fn test_detect_catch_context_second_catch() {
    let content = concat!(
        "<?php\n",
        "try {\n",
        "    throw new IOException('io');\n",
        "    throw new TimeoutException('timeout');\n",
        "} catch (IOException $e) {\n",
        "    // handled\n",
        "} catch (",
    );
    let pos = Position {
        line: 6,
        character: 10,
    };
    let ctx = detect_catch_context(content, pos);
    assert!(ctx.is_some(), "Should detect second catch context");
    let ctx = ctx.unwrap();
    // Both types are in the try block
    assert!(ctx.suggested_types.contains(&"IOException".to_string()));
    assert!(
        ctx.suggested_types
            .contains(&"TimeoutException".to_string())
    );
}

#[test]
fn test_detect_catch_context_partial_typed() {
    let content = concat!(
        "<?php\n",
        "try {\n",
        "    throw new RuntimeException('error');\n",
        "    throw new InvalidArgumentException('bad');\n",
        "} catch (Run",
    );
    let pos = Position {
        line: 4,
        character: 13,
    };
    let ctx = detect_catch_context(content, pos);
    assert!(ctx.is_some());
    let ctx = ctx.unwrap();
    assert_eq!(ctx.partial, "Run");
    // Both are suggested (filtering happens in build_catch_completions)
    assert!(
        ctx.suggested_types
            .contains(&"RuntimeException".to_string())
    );
    assert!(
        ctx.suggested_types
            .contains(&"InvalidArgumentException".to_string())
    );
}

#[test]
fn test_detect_catch_context_not_catch() {
    let content = concat!("<?php\n", "function foo(",);
    let pos = Position {
        line: 1,
        character: 14,
    };
    let ctx = detect_catch_context(content, pos);
    assert!(ctx.is_none(), "Should not detect catch context in function");
}

#[test]
fn test_build_catch_completions_filters_by_partial() {
    let ctx = CatchContext {
        partial: "Run".to_string(),
        suggested_types: vec![
            "RuntimeException".to_string(),
            "InvalidArgumentException".to_string(),
        ],
        has_specific_types: true,
    };
    let empty_use_map = std::collections::HashMap::new();
    let no_namespace = None;
    let items = build_catch_completions(&ctx, &empty_use_map, &no_namespace);
    assert_eq!(items.len(), 1);
    assert_eq!(items[0].label, "RuntimeException");
}

#[test]
fn test_build_catch_completions_empty_partial_shows_all() {
    let ctx = CatchContext {
        partial: String::new(),
        suggested_types: vec![
            "RuntimeException".to_string(),
            "InvalidArgumentException".to_string(),
        ],
        has_specific_types: true,
    };
    let empty_use_map = std::collections::HashMap::new();
    let no_namespace = None;
    let items = build_catch_completions(&ctx, &empty_use_map, &no_namespace);
    assert_eq!(items.len(), 2);
}

#[test]
fn test_detect_catch_context_multi_catch_pipe() {
    let content = concat!(
        "<?php\n",
        "try {\n",
        "    throw new IOException('io');\n",
        "    throw new TimeoutException('timeout');\n",
        "    throw new RuntimeException('rt');\n",
        "} catch (IOException | ",
    );
    let pos = Position {
        line: 5,
        character: 23,
    };
    let ctx = detect_catch_context(content, pos);
    assert!(ctx.is_some());
    let ctx = ctx.unwrap();
    // IOException should be filtered out since it's already listed
    assert!(
        !ctx.suggested_types.contains(&"IOException".to_string()),
        "IOException should be filtered out"
    );
    assert!(
        ctx.suggested_types
            .contains(&"TimeoutException".to_string())
    );
    assert!(
        ctx.suggested_types
            .contains(&"RuntimeException".to_string())
    );
}