php-lsp 0.1.52

A PHP Language Server Protocol implementation
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/// Code action: "Implement missing methods"
///
/// When a class `implements` an interface or `extends` an abstract class,
/// this action generates stub methods for any abstract/interface methods
/// that are not yet implemented in the class body.
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use php_ast::{ClassMemberKind, NamespaceBody, Stmt, StmtKind, Visibility};
use tower_lsp::lsp_types::{
    CodeAction, CodeActionKind, CodeActionOrCommand, Position, Range, TextEdit, Url, WorkspaceEdit,
};

use crate::ast::{ParsedDoc, format_type_hint, offset_to_position};
use crate::hover::format_params_str;

struct MethodStub {
    name: String,
    visibility: &'static str,
    is_static: bool,
    params: String,
    return_type: Option<String>,
}

pub fn implement_missing_actions(
    source: &str,
    doc: &ParsedDoc,
    all_docs: &[(Url, Arc<ParsedDoc>)],
    range: Range,
    uri: &Url,
    file_imports: &HashMap<String, String>,
) -> Vec<CodeActionOrCommand> {
    let mut actions = Vec::new();
    collect_actions(
        &doc.program().stmts,
        source,
        all_docs,
        file_imports,
        range,
        uri,
        &mut actions,
    );
    actions
}

fn collect_actions(
    stmts: &[Stmt<'_, '_>],
    source: &str,
    all_docs: &[(Url, Arc<ParsedDoc>)],
    file_imports: &HashMap<String, String>,
    range: Range,
    uri: &Url,
    out: &mut Vec<CodeActionOrCommand>,
) {
    for stmt in stmts {
        match &stmt.kind {
            StmtKind::Class(c) => {
                // Only consider classes whose declaration overlaps the requested range.
                let class_start = offset_to_position(source, stmt.span.start).line;
                let class_end = offset_to_position(source, stmt.span.end).line;
                if class_start > range.end.line || class_end < range.start.line {
                    continue;
                }

                // Gather method names already in this class.
                let existing: HashSet<String> = c
                    .members
                    .iter()
                    .filter_map(|m| {
                        if let ClassMemberKind::Method(method) = &m.kind {
                            Some(method.name.to_string())
                        } else {
                            None
                        }
                    })
                    .collect();

                let mut missing: Vec<MethodStub> = Vec::new();

                // Interfaces this class implements.
                for iface in c.implements.iter() {
                    let iface_name = iface.to_string_repr().into_owned();
                    let short = last_segment(&iface_name).to_string();
                    // Try to resolve through `use` imports first; fall back to short-name scan.
                    let fqn = file_imports.get(&short).cloned();
                    for stub in abstract_methods_of(&short, fqn.as_deref(), all_docs) {
                        if !existing.contains(&stub.name) {
                            missing.push(stub);
                        }
                    }
                }

                // Abstract parent class (if any).
                if let Some(parent) = &c.extends {
                    let parent_name = parent.to_string_repr().into_owned();
                    let short = last_segment(&parent_name).to_string();
                    let fqn = file_imports.get(&short).cloned();
                    for stub in abstract_methods_of(&short, fqn.as_deref(), all_docs) {
                        if !existing.contains(&stub.name) {
                            missing.push(stub);
                        }
                    }
                }

                // Deduplicate by method name (multiple interfaces may declare the same method).
                {
                    let mut seen = HashSet::new();
                    missing.retain(|s| seen.insert(s.name.clone()));
                }

                if missing.is_empty() {
                    continue;
                }

                let stub_text = generate_stub_text(&missing);
                // Insert just before the closing `}` of the class.
                let closing_line = offset_to_position(source, stmt.span.end.saturating_sub(1)).line;
                let insert_pos = Position {
                    line: closing_line,
                    character: 0,
                };
                let edit = TextEdit {
                    range: Range {
                        start: insert_pos,
                        end: insert_pos,
                    },
                    new_text: stub_text,
                };
                let mut changes = HashMap::new();
                changes.insert(uri.clone(), vec![edit]);

                let n = missing.len();
                let title = if n == 1 {
                    "Implement missing method".to_string()
                } else {
                    format!("Implement {n} missing methods")
                };
                out.push(CodeActionOrCommand::CodeAction(CodeAction {
                    title,
                    kind: Some(CodeActionKind::QUICKFIX),
                    edit: Some(WorkspaceEdit {
                        changes: Some(changes),
                        ..Default::default()
                    }),
                    ..Default::default()
                }));
            }
            StmtKind::Namespace(ns) => {
                if let NamespaceBody::Braced(inner) = &ns.body {
                    collect_actions(inner, source, all_docs, file_imports, range, uri, out);
                }
            }
            _ => {}
        }
    }
}

/// Collect abstract/interface methods declared by `name` across all documents.
///
/// When `fqn` is provided (resolved from a `use` statement), the search uses
/// FQN-aware matching only — it looks for a document whose namespace + class
/// name matches the FQN exactly.  This avoids picking up a different class that
/// happens to share the same short name in another namespace.
///
/// When `fqn` is `None` (no `use` import found), falls back to a plain
/// short-name scan across all documents, preserving the original behaviour.
fn abstract_methods_of(
    name: &str,
    fqn: Option<&str>,
    all_docs: &[(Url, Arc<ParsedDoc>)],
) -> Vec<MethodStub> {
    if let Some(fqn) = fqn {
        // FQN-aware pass: only return stubs when the exact namespace matches.
        // Do NOT fall back to short-name scan to avoid picking the wrong class.
        for (_, doc) in all_docs {
            if let Some(stubs) = collect_abstract_methods_fqn(&doc.program().stmts, fqn, "") {
                return stubs;
            }
        }
        return vec![];
    }

    // Short-name fallback (no `use` import): scan all docs as before.
    for (_, doc) in all_docs {
        if let Some(stubs) = collect_abstract_methods(&doc.program().stmts, name) {
            return stubs;
        }
    }
    vec![]
}

/// Like `collect_abstract_methods` but matches the fully-qualified name
/// `namespace\ClassName` by tracking the current namespace prefix while
/// recursing into `StmtKind::Namespace` blocks.
fn collect_abstract_methods_fqn(
    stmts: &[Stmt<'_, '_>],
    fqn: &str,
    current_ns: &str,
) -> Option<Vec<MethodStub>> {
    // The expected short name is the last segment of the FQN.
    let short = last_segment(fqn);

    for stmt in stmts {
        match &stmt.kind {
            StmtKind::Interface(i) if i.name == short => {
                // Verify the namespace matches.
                let declared_fqn = if current_ns.is_empty() {
                    i.name.to_string()
                } else {
                    format!("{}\\{}", current_ns, i.name)
                };
                if fqn_eq(fqn, &declared_fqn) {
                    let stubs = i
                        .members
                        .iter()
                        .filter_map(|m| {
                            if let ClassMemberKind::Method(method) = &m.kind {
                                Some(MethodStub {
                                    name: method.name.to_string(),
                                    visibility: "public",
                                    is_static: method.is_static,
                                    params: format_params_str(&method.params),
                                    return_type: method
                                        .return_type
                                        .as_ref()
                                        .map(|t| format_type_hint(t)),
                                })
                            } else {
                                None
                            }
                        })
                        .collect();
                    return Some(stubs);
                }
            }
            StmtKind::Class(c) if c.name == Some(short) && c.modifiers.is_abstract => {
                let declared_fqn = if current_ns.is_empty() {
                    short.to_string()
                } else {
                    format!("{}\\{}", current_ns, short)
                };
                if fqn_eq(fqn, &declared_fqn) {
                    let stubs = c
                        .members
                        .iter()
                        .filter_map(|m| {
                            if let ClassMemberKind::Method(method) = &m.kind {
                                if method.is_abstract {
                                    Some(MethodStub {
                                        name: method.name.to_string(),
                                        visibility: visibility_str(method.visibility.as_ref()),
                                        is_static: method.is_static,
                                        params: format_params_str(&method.params),
                                        return_type: method
                                            .return_type
                                            .as_ref()
                                            .map(|t| format_type_hint(t)),
                                    })
                                } else {
                                    None
                                }
                            } else {
                                None
                            }
                        })
                        .collect();
                    return Some(stubs);
                }
            }
            StmtKind::Namespace(ns) => {
                if let NamespaceBody::Braced(inner) = &ns.body {
                    let ns_name = ns.name.as_ref().map(|n| n.to_string_repr().into_owned());
                    let child_ns = match &ns_name {
                        Some(n) if !current_ns.is_empty() => format!("{}\\{}", current_ns, n),
                        Some(n) => n.clone(),
                        None => current_ns.to_string(),
                    };
                    if let Some(stubs) = collect_abstract_methods_fqn(inner, fqn, &child_ns) {
                        return Some(stubs);
                    }
                }
            }
            _ => {}
        }
    }
    None
}

/// Compare two FQNs ignoring a leading backslash.
fn fqn_eq(a: &str, b: &str) -> bool {
    a.trim_start_matches('\\') == b.trim_start_matches('\\')
}

fn collect_abstract_methods(stmts: &[Stmt<'_, '_>], name: &str) -> Option<Vec<MethodStub>> {
    for stmt in stmts {
        match &stmt.kind {
            StmtKind::Interface(i) if i.name == name => {
                let stubs = i
                    .members
                    .iter()
                    .filter_map(|m| {
                        if let ClassMemberKind::Method(method) = &m.kind {
                            Some(MethodStub {
                                name: method.name.to_string(),
                                visibility: "public",
                                is_static: method.is_static,
                                params: format_params_str(&method.params),
                                return_type: method
                                    .return_type
                                    .as_ref()
                                    .map(|t| format_type_hint(t)),
                            })
                        } else {
                            None
                        }
                    })
                    .collect();
                return Some(stubs);
            }
            StmtKind::Class(c) if c.name == Some(name) && c.modifiers.is_abstract => {
                let stubs = c
                    .members
                    .iter()
                    .filter_map(|m| {
                        if let ClassMemberKind::Method(method) = &m.kind {
                            if method.is_abstract {
                                Some(MethodStub {
                                    name: method.name.to_string(),
                                    visibility: visibility_str(method.visibility.as_ref()),
                                    is_static: method.is_static,
                                    params: format_params_str(&method.params),
                                    return_type: method
                                        .return_type
                                        .as_ref()
                                        .map(|t| format_type_hint(t)),
                                })
                            } else {
                                None
                            }
                        } else {
                            None
                        }
                    })
                    .collect();
                return Some(stubs);
            }
            StmtKind::Namespace(ns) => {
                if let NamespaceBody::Braced(inner) = &ns.body
                    && let Some(stubs) = collect_abstract_methods(inner, name)
                {
                    return Some(stubs);
                }
            }
            _ => {}
        }
    }
    None
}

fn visibility_str(v: Option<&Visibility>) -> &'static str {
    match v {
        Some(Visibility::Protected) => "protected",
        Some(Visibility::Private) => "private",
        _ => "public",
    }
}

fn generate_stub_text(stubs: &[MethodStub]) -> String {
    let mut text = String::new();
    for stub in stubs {
        let static_kw = if stub.is_static { "static " } else { "" };
        let ret = match &stub.return_type {
            Some(t) => format!(": {t}"),
            None => String::new(),
        };
        text.push_str(&format!(
            "    {} {}function {}({}){ret}\n    {{\n        throw new \\RuntimeException('Not implemented');\n    }}\n\n",
            stub.visibility, static_kw, stub.name, stub.params
        ));
    }
    text
}

fn last_segment(name: &str) -> &str {
    name.rsplit('\\').next().unwrap_or(name)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn uri(path: &str) -> Url {
        Url::parse(&format!("file://{path}")).unwrap()
    }

    fn doc(src: &str) -> (Url, Arc<ParsedDoc>) {
        (uri("/a.php"), Arc::new(ParsedDoc::parse(src.to_string())))
    }

    fn full_range() -> Range {
        Range {
            start: Position {
                line: 0,
                character: 0,
            },
            end: Position {
                line: u32::MAX,
                character: u32::MAX,
            },
        }
    }

    #[test]
    fn generates_action_for_unimplemented_interface_method() {
        let iface_src = "<?php\ninterface Countable {\n    public function count(): int;\n}";
        let class_src = "<?php\nclass MyList implements Countable {\n}";
        let all_docs = vec![doc(iface_src), doc(class_src)];
        let class_doc = ParsedDoc::parse(class_src.to_string());
        let actions = implement_missing_actions(
            class_src,
            &class_doc,
            &all_docs,
            full_range(),
            &uri("/b.php"),
            &HashMap::new(),
        );
        assert!(!actions.is_empty(), "expected at least one action");
        if let CodeActionOrCommand::CodeAction(action) = &actions[0] {
            assert!(
                action.title.contains("missing method"),
                "title should mention 'missing method'"
            );
            let changes = action.edit.as_ref().unwrap().changes.as_ref().unwrap();
            let edits = changes.values().next().unwrap();
            assert!(
                edits[0].new_text.contains("function count()"),
                "stub should contain 'function count()'"
            );
            assert!(
                edits[0].new_text.contains(": int"),
                "stub should contain ': int' return type"
            );
        } else {
            panic!("expected CodeAction");
        }
    }

    #[test]
    fn no_action_when_all_methods_implemented() {
        let iface_src = "<?php\ninterface Countable {\n    public function count(): int;\n}";
        let class_src = "<?php\nclass MyList implements Countable {\n    public function count(): int { return 0; }\n}";
        let all_docs = vec![doc(iface_src), doc(class_src)];
        let class_doc = ParsedDoc::parse(class_src.to_string());
        let actions = implement_missing_actions(
            class_src,
            &class_doc,
            &all_docs,
            full_range(),
            &uri("/b.php"),
            &HashMap::new(),
        );
        assert!(
            actions.is_empty(),
            "no action needed when all methods are implemented"
        );
    }

    #[test]
    fn generates_action_for_abstract_class_method() {
        let abstract_src =
            "<?php\nabstract class Shape {\n    abstract public function area(): float;\n}";
        let class_src = "<?php\nclass Circle extends Shape {\n}";
        let all_docs = vec![doc(abstract_src), doc(class_src)];
        let class_doc = ParsedDoc::parse(class_src.to_string());
        let actions = implement_missing_actions(
            class_src,
            &class_doc,
            &all_docs,
            full_range(),
            &uri("/b.php"),
            &HashMap::new(),
        );
        assert!(!actions.is_empty(), "expected action for abstract method");
        if let CodeActionOrCommand::CodeAction(action) = &actions[0] {
            let changes = action.edit.as_ref().unwrap().changes.as_ref().unwrap();
            let edits = changes.values().next().unwrap();
            assert!(
                edits[0].new_text.contains("function area()"),
                "stub should contain 'function area()'"
            );
        }
    }

    #[test]
    fn stub_body_throws_runtime_exception() {
        let iface_src = "<?php\ninterface Runnable {\n    public function run(): void;\n}";
        let class_src = "<?php\nclass Task implements Runnable {\n}";
        let all_docs = vec![doc(iface_src), doc(class_src)];
        let class_doc = ParsedDoc::parse(class_src.to_string());
        let actions = implement_missing_actions(
            class_src,
            &class_doc,
            &all_docs,
            full_range(),
            &uri("/b.php"),
            &HashMap::new(),
        );
        assert!(!actions.is_empty());
        if let CodeActionOrCommand::CodeAction(action) = &actions[0] {
            let changes = action.edit.as_ref().unwrap().changes.as_ref().unwrap();
            let edits = changes.values().next().unwrap();
            assert!(
                edits[0]
                    .new_text
                    .contains("throw new \\RuntimeException('Not implemented')"),
                "stub body should throw RuntimeException, got: {}",
                edits[0].new_text
            );
        } else {
            panic!("expected CodeAction");
        }
    }

    #[test]
    fn resolves_interface_through_use_import() {
        // The interface is declared in a braced namespace; the class file imports it via `use`.
        let iface_src = "<?php\nnamespace App\\Contracts {\ninterface Renderable {\n    public function render(): string;\n}\n}";
        let class_src =
            "<?php\nuse App\\Contracts\\Renderable;\nclass View implements Renderable {\n}";
        let all_docs = vec![
            (
                uri("/contracts/Renderable.php"),
                Arc::new(ParsedDoc::parse(iface_src.to_string())),
            ),
            (
                uri("/View.php"),
                Arc::new(ParsedDoc::parse(class_src.to_string())),
            ),
        ];
        let class_doc = ParsedDoc::parse(class_src.to_string());
        let actions = implement_missing_actions(
            class_src,
            &class_doc,
            &all_docs,
            full_range(),
            &uri("/View.php"),
            &HashMap::new(),
        );
        assert!(
            !actions.is_empty(),
            "expected action when interface is resolved through use import"
        );
        if let CodeActionOrCommand::CodeAction(action) = &actions[0] {
            let changes = action.edit.as_ref().unwrap().changes.as_ref().unwrap();
            let edits = changes.values().next().unwrap();
            assert!(
                edits[0].new_text.contains("function render()"),
                "stub should contain 'function render()', got: {}",
                edits[0].new_text
            );
        } else {
            panic!("expected CodeAction");
        }
    }

    #[test]
    fn use_import_resolution_picks_correct_interface_over_same_short_name() {
        // Two interfaces share the short name `Logger`; only the imported one's
        // methods should be stubbed.  Both use braced-namespace syntax so the
        // AST traversal can track the namespace prefix.
        let wrong_iface = "<?php\nnamespace Other {\ninterface Logger {\n    public function wrong(): void;\n}\n}";
        let right_iface = "<?php\nnamespace App\\Logging {\ninterface Logger {\n    public function log(string $msg): void;\n}\n}";
        let class_src = "<?php\nuse App\\Logging\\Logger;\nclass FileLogger implements Logger {\n}";
        let all_docs = vec![
            (
                uri("/other/Logger.php"),
                Arc::new(ParsedDoc::parse(wrong_iface.to_string())),
            ),
            (
                uri("/logging/Logger.php"),
                Arc::new(ParsedDoc::parse(right_iface.to_string())),
            ),
            (
                uri("/FileLogger.php"),
                Arc::new(ParsedDoc::parse(class_src.to_string())),
            ),
        ];
        let class_doc = ParsedDoc::parse(class_src.to_string());
        let imports = HashMap::from([("Logger".to_string(), "App\\Logging\\Logger".to_string())]);
        let actions = implement_missing_actions(
            class_src,
            &class_doc,
            &all_docs,
            full_range(),
            &uri("/FileLogger.php"),
            &imports,
        );
        assert!(!actions.is_empty(), "expected action");
        if let CodeActionOrCommand::CodeAction(action) = &actions[0] {
            let changes = action.edit.as_ref().unwrap().changes.as_ref().unwrap();
            let edits = changes.values().next().unwrap();
            assert!(
                edits[0].new_text.contains("function log("),
                "should stub the correct Logger's 'log' method, got: {}",
                edits[0].new_text
            );
            assert!(
                !edits[0].new_text.contains("function wrong("),
                "should NOT stub the wrong Logger's 'wrong' method"
            );
        } else {
            panic!("expected CodeAction");
        }
    }
}