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
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! Implementation error diagnostic.
//!
//! Flags concrete classes that fail to implement all required methods
//! from their interfaces or abstract parents.  Reuses the same
//! missing-method detection logic as the "Implement missing methods"
//! code action (`code_actions::implement_methods::collect_missing_methods`).

use std::collections::HashSet;
use std::sync::Arc;

use tower_lsp::lsp_types::*;

use crate::Backend;
use crate::code_actions::implement_methods::collect_missing_methods;
use crate::symbol_map::SymbolKind;
use crate::types::ClassLikeKind;
use crate::util::short_name;

impl Backend {
    /// Collect implementation-error diagnostics for a single file.
    ///
    /// For each concrete (non-abstract) class in the file, checks whether
    /// all required methods from interfaces and abstract parents are
    /// implemented.  Emits an Error-severity diagnostic on the class name
    /// span for each class that has missing methods.
    ///
    /// Appends diagnostics to `out`.  The caller is responsible for
    /// publishing or returning them.
    pub fn collect_implementation_error_diagnostics(
        &self,
        uri: &str,
        content: &str,
        out: &mut Vec<Diagnostic>,
    ) {
        let symbol_map = {
            let maps = self.symbol_maps.read();
            match maps.get(uri) {
                Some(sm) => sm.clone(),
                None => return,
            }
        };

        let ctx = self.file_context(uri);
        let class_loader = self.class_loader(&ctx);

        // Iterate all ClassDeclaration spans in the symbol map.
        for span in &symbol_map.spans {
            let class_name = match &span.kind {
                SymbolKind::ClassDeclaration { name } => name,
                _ => continue,
            };

            // Find the matching ClassInfo in the ast_map.
            let class_info = match ctx
                .classes
                .iter()
                .find(|c| c.name == *class_name || self.class_fqn_matches(c, class_name, &ctx))
            {
                Some(c) => Arc::clone(c),
                None => continue,
            };

            // Only concrete classes and enums can have implementation errors.
            // Abstract classes, interfaces, and traits are skipped.
            let is_concrete_class =
                class_info.kind == ClassLikeKind::Class && !class_info.is_abstract;
            let is_enum = class_info.kind == ClassLikeKind::Enum;
            if !is_concrete_class && !is_enum {
                continue;
            }

            // Skip classes with no interfaces and no parent class — they
            // cannot have missing method implementations.
            if class_info.interfaces.is_empty() && class_info.parent_class.is_none() {
                continue;
            }

            let missing = collect_missing_methods(&class_info, &class_loader);

            if missing.is_empty() {
                continue;
            }

            // Build the diagnostic range from the class name span.
            let range = match super::offset_range_to_lsp_range(
                content,
                span.start as usize,
                span.end as usize,
            ) {
                Some(r) => r,
                None => continue,
            };

            // Build a single diagnostic listing all missing methods.
            let kind_label = if class_info.kind == ClassLikeKind::Enum {
                "Enum"
            } else {
                "Class"
            };

            let message = if missing.len() == 1 {
                let m = &missing[0];
                let source = method_source_description(&class_info, &m.name, &class_loader);
                format!(
                    "{} '{}' must implement method '{}()' from {}",
                    kind_label, class_info.name, m.name, source
                )
            } else {
                let method_list: Vec<String> = missing
                    .iter()
                    .map(|m| {
                        let source = method_source_description(&class_info, &m.name, &class_loader);
                        format!("'{}()' from {}", m.name, source)
                    })
                    .collect();
                format!(
                    "{} '{}' must implement {} methods: {}",
                    kind_label,
                    class_info.name,
                    missing.len(),
                    method_list.join(", ")
                )
            };

            out.push(Diagnostic {
                range,
                severity: Some(DiagnosticSeverity::ERROR),
                code: Some(NumberOrString::String("implementation_error".to_string())),
                code_description: None,
                source: Some("phpantom".to_string()),
                message,
                related_information: None,
                tags: None,
                data: None,
            });
        }
    }

    /// Check if a ClassInfo's fully-qualified name matches the given name.
    ///
    /// The symbol map stores the short class name, but classes in the
    /// ast_map may have their FQN stored differently.  This handles the
    /// common case where the class name is unqualified.
    fn class_fqn_matches(
        &self,
        class: &crate::types::ClassInfo,
        name: &str,
        ctx: &crate::types::FileContext,
    ) -> bool {
        // Build FQN from namespace + class name and compare.
        if let Some(ref ns) = ctx.namespace {
            let fqn = format!("{}\\{}", ns, class.name);
            fqn == name || class.name == name
        } else {
            class.name == name
        }
    }
}

/// Describe where a missing method was required from (interface or
/// abstract parent class).
fn method_source_description(
    class: &crate::types::ClassInfo,
    method_name: &str,
    class_loader: &dyn Fn(&str) -> Option<Arc<crate::types::ClassInfo>>,
) -> String {
    // Check interfaces first.
    for iface_name in &class.interfaces {
        if let Some(iface) = class_loader(iface_name)
            && has_method_in_chain(&iface, method_name, class_loader, &mut HashSet::new())
        {
            let short = short_name(iface_name);
            return format!("interface '{}'", short);
        }
    }

    // Check parent chain for abstract methods.
    if let Some(ref parent_name) = class.parent_class
        && let Some(parent) = class_loader(parent_name)
        && has_abstract_method_in_chain(&parent, method_name, class_loader, &mut HashSet::new())
    {
        let short = short_name(parent_name);
        return format!("class '{}'", short);
    }

    // Fallback — shouldn't happen if collect_missing_methods found it.
    "its hierarchy".to_string()
}

/// Check if a class or its parent chain declares a method (abstract or not).
fn has_method_in_chain(
    class: &crate::types::ClassInfo,
    method_name: &str,
    class_loader: &dyn Fn(&str) -> Option<Arc<crate::types::ClassInfo>>,
    visited: &mut HashSet<String>,
) -> bool {
    if !visited.insert(class.name.clone()) {
        return false;
    }

    let lower = method_name.to_lowercase();
    if class.methods.iter().any(|m| m.name.to_lowercase() == lower) {
        return true;
    }

    // Check parent interfaces.
    for iface_name in &class.interfaces {
        if let Some(iface) = class_loader(iface_name)
            && has_method_in_chain(&iface, method_name, class_loader, visited)
        {
            return true;
        }
    }

    // Check parent class.
    if let Some(ref parent_name) = class.parent_class
        && let Some(parent) = class_loader(parent_name)
        && has_method_in_chain(&parent, method_name, class_loader, visited)
    {
        return true;
    }

    false
}

/// Check if a class or its parent chain declares an abstract method.
fn has_abstract_method_in_chain(
    class: &crate::types::ClassInfo,
    method_name: &str,
    class_loader: &dyn Fn(&str) -> Option<Arc<crate::types::ClassInfo>>,
    visited: &mut HashSet<String>,
) -> bool {
    if !visited.insert(class.name.clone()) {
        return false;
    }

    let lower = method_name.to_lowercase();
    if class
        .methods
        .iter()
        .any(|m| m.name.to_lowercase() == lower && m.is_abstract)
    {
        return true;
    }

    if let Some(ref parent_name) = class.parent_class
        && let Some(parent) = class_loader(parent_name)
        && has_abstract_method_in_chain(&parent, method_name, class_loader, visited)
    {
        return true;
    }

    false
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use crate::Backend;
    use tower_lsp::lsp_types::*;

    fn collect(php: &str) -> Vec<Diagnostic> {
        let backend = Backend::new_test();
        let uri = "file:///test.php";
        backend.update_ast(uri, &Arc::new(php.to_string()));
        let mut out = Vec::new();
        backend.collect_implementation_error_diagnostics(uri, php, &mut out);
        out
    }

    #[test]
    fn no_diagnostic_for_abstract_class() {
        let php = r#"<?php
interface Foo { public function bar(): void; }
abstract class Baz implements Foo {}
"#;
        let diags = collect(php);
        assert!(
            diags.is_empty(),
            "Abstract classes should not get diagnostics"
        );
    }

    #[test]
    fn no_diagnostic_for_interface() {
        let php = r#"<?php
interface Foo { public function bar(): void; }
interface Baz extends Foo { public function qux(): void; }
"#;
        let diags = collect(php);
        assert!(diags.is_empty(), "Interfaces should not get diagnostics");
    }

    #[test]
    fn no_diagnostic_when_all_methods_implemented() {
        let php = r#"<?php
interface Foo { public function bar(): void; }
class Baz implements Foo {
    public function bar(): void {}
}
"#;
        let diags = collect(php);
        assert!(
            diags.is_empty(),
            "Fully implemented class should have no diagnostics"
        );
    }

    #[test]
    fn diagnostic_for_missing_interface_method() {
        let php = r#"<?php
interface Foo {
    public function bar(): void;
}
class Baz implements Foo {
}
"#;
        let diags = collect(php);
        assert_eq!(diags.len(), 1);
        assert!(diags[0].message.contains("Baz"));
        assert!(diags[0].message.contains("bar()"));
        assert!(diags[0].message.contains("interface"));
        assert_eq!(diags[0].severity, Some(DiagnosticSeverity::ERROR));
    }

    #[test]
    fn diagnostic_for_missing_abstract_method() {
        let php = r#"<?php
abstract class Base {
    abstract public function doSomething(): void;
}
class Child extends Base {
}
"#;
        let diags = collect(php);
        assert_eq!(diags.len(), 1);
        assert!(diags[0].message.contains("Child"));
        assert!(diags[0].message.contains("doSomething()"));
        assert!(diags[0].message.contains("class"));
    }

    #[test]
    fn diagnostic_lists_multiple_missing_methods() {
        let php = r#"<?php
interface Foo {
    public function bar(): void;
    public function baz(): void;
    public function qux(): void;
}
class Impl implements Foo {
}
"#;
        let diags = collect(php);
        assert_eq!(diags.len(), 1);
        assert!(diags[0].message.contains("3 methods"));
        assert!(diags[0].message.contains("bar()"));
        assert!(diags[0].message.contains("baz()"));
        assert!(diags[0].message.contains("qux()"));
    }

    #[test]
    fn no_diagnostic_for_plain_class_without_interfaces() {
        let php = r#"<?php
class Simple {
    public function foo(): void {}
}
"#;
        let diags = collect(php);
        assert!(diags.is_empty());
    }

    #[test]
    fn diagnostic_has_correct_code_and_source() {
        let php = r#"<?php
interface Foo { public function bar(): void; }
class Baz implements Foo {}
"#;
        let diags = collect(php);
        assert_eq!(diags.len(), 1);
        assert_eq!(
            diags[0].code,
            Some(NumberOrString::String("implementation_error".to_string()))
        );
        assert_eq!(diags[0].source, Some("phpantom".to_string()));
    }

    #[test]
    fn no_diagnostic_for_trait() {
        let php = r#"<?php
trait MyTrait {
    abstract public function doIt(): void;
}
"#;
        let diags = collect(php);
        assert!(diags.is_empty(), "Traits should not get diagnostics");
    }

    #[test]
    fn no_diagnostic_for_enum_with_all_methods_implemented() {
        let php = r#"<?php
interface HasLabel { public function label(): string; }
enum Color implements HasLabel {
    case Red;
    case Blue;

    public function label(): string {
        return $this->name;
    }
}
"#;
        let diags = collect(php);
        assert!(
            diags.is_empty(),
            "Enum with implemented methods should have no diagnostics"
        );
    }

    #[test]
    fn diagnostic_for_enum_missing_interface_method() {
        let php = r#"<?php
interface HasLabel { public function label(): string; }
enum Color implements HasLabel {
    case Red;
    case Blue;
}
"#;
        let diags = collect(php);
        assert_eq!(diags.len(), 1);
        assert!(diags[0].message.contains("Enum"));
        assert!(diags[0].message.contains("Color"));
        assert!(diags[0].message.contains("label()"));
        assert!(diags[0].message.contains("interface"));
        assert_eq!(diags[0].severity, Some(DiagnosticSeverity::ERROR));
    }

    #[test]
    fn no_diagnostic_for_enum_without_interfaces() {
        let php = r#"<?php
enum Suit {
    case Hearts;
    case Diamonds;
}
"#;
        let diags = collect(php);
        assert!(
            diags.is_empty(),
            "Enum without interfaces should have no diagnostics"
        );
    }

    #[test]
    fn enum_multiple_missing_methods() {
        let php = r#"<?php
interface HasLabel {
    public function label(): string;
    public function description(): string;
}
enum Color implements HasLabel {
    case Red;
}
"#;
        let diags = collect(php);
        assert_eq!(diags.len(), 1);
        assert!(diags[0].message.contains("Enum"));
        assert!(diags[0].message.contains("2 methods"));
        assert!(diags[0].message.contains("label()"));
        assert!(diags[0].message.contains("description()"));
    }

    #[test]
    fn case_insensitive_method_matching() {
        let php = r#"<?php
interface Foo { public function doSomething(): void; }
class Bar implements Foo {
    public function DOSOMETHING(): void {}
}
"#;
        let diags = collect(php);
        assert!(
            diags.is_empty(),
            "Method matching should be case-insensitive"
        );
    }

    #[test]
    fn parent_implements_interface_method() {
        let php = r#"<?php
interface Foo { public function bar(): void; }
class Base implements Foo {
    public function bar(): void {}
}
class Child extends Base {}
"#;
        let diags = collect(php);
        // Child doesn't declare implements Foo, so no check needed.
        // But even if it did, bar() is inherited from Base.
        assert!(diags.is_empty());
    }

    #[test]
    fn trait_satisfies_interface_method() {
        let php = r#"<?php
interface Wireable {
    public function toLivewire(): array;
    public function fromLivewire($value): static;
}

trait WireableData {
    public function toLivewire(): array { return []; }
    public static function fromLivewire($value): static { return new static(); }
}

class MyData implements Wireable {
    use WireableData;
}
"#;
        let diags = collect(php);
        assert!(
            diags.is_empty(),
            "Trait methods should satisfy interface requirements, got: {:?}",
            diags.iter().map(|d| &d.message).collect::<Vec<_>>()
        );
    }

    #[test]
    fn trait_satisfies_abstract_parent_method() {
        let php = r#"<?php
abstract class Base {
    abstract public function doSomething(): void;
}

trait DoesIt {
    public function doSomething(): void {}
}

class Child extends Base {
    use DoesIt;
}
"#;
        let diags = collect(php);
        assert!(
            diags.is_empty(),
            "Trait methods should satisfy abstract parent requirements, got: {:?}",
            diags.iter().map(|d| &d.message).collect::<Vec<_>>()
        );
    }

    #[test]
    fn nested_trait_satisfies_interface() {
        let php = r#"<?php
interface HasLabel {
    public function label(): string;
}

trait InnerTrait {
    public function label(): string { return 'hi'; }
}

trait OuterTrait {
    use InnerTrait;
}

class Widget implements HasLabel {
    use OuterTrait;
}
"#;
        let diags = collect(php);
        assert!(
            diags.is_empty(),
            "Nested trait methods should satisfy interface requirements, got: {:?}",
            diags.iter().map(|d| &d.message).collect::<Vec<_>>()
        );
    }

    #[test]
    fn parent_trait_satisfies_interface() {
        let php = r#"<?php
interface Serializable {
    public function toArray(): array;
    public function toJson(): string;
}

trait SerializableTrait {
    public function toArray(): array { return []; }
    public function toJson(): string { return '{}'; }
}

class Base {
    use SerializableTrait;
}

class Child extends Base implements Serializable {
}
"#;
        let diags = collect(php);
        assert!(
            diags.is_empty(),
            "Parent class trait methods should satisfy child interface requirements, got: {:?}",
            diags.iter().map(|d| &d.message).collect::<Vec<_>>()
        );
    }

    #[test]
    fn trait_with_abstract_method_does_not_satisfy() {
        let php = r#"<?php
interface Foo {
    public function bar(): void;
}

trait HalfImpl {
    abstract public function bar(): void;
}

class Baz implements Foo {
    use HalfImpl;
}
"#;
        let diags = collect(php);
        assert_eq!(
            diags.len(),
            1,
            "Abstract trait methods should not satisfy interface requirements"
        );
        assert!(diags[0].message.contains("bar()"));
    }

    #[test]
    fn cyclic_interface_hierarchy_does_not_stack_overflow() {
        // interface A extends B, interface B extends A — user error, but
        // should not crash the LSP server.
        let php = r#"<?php
interface A extends B { public function foo(): void; }
interface B extends A { public function bar(): void; }
class C implements A {
    public function foo(): void {}
    public function bar(): void {}
}
"#;
        let diags = collect(php);
        // We only care that it doesn't hang or crash.  Whether a
        // diagnostic is emitted is secondary.
        let _ = diags;
    }

    #[test]
    fn cyclic_parent_class_does_not_stack_overflow() {
        // class A extends B, class B extends A — user error.
        let php = r#"<?php
interface I { public function work(): void; }
class A extends B implements I {}
class B extends A {}
"#;
        let diags = collect(php);
        let _ = diags;
    }

    #[test]
    fn diagnostic_range_covers_class_name() {
        let php = r#"<?php
interface Foo { public function bar(): void; }
class MyClass implements Foo {}
"#;
        let diags = collect(php);
        assert_eq!(diags.len(), 1);
        let range = diags[0].range;
        // The range should cover "MyClass" — verify it is on the correct line.
        let class_line = php[..php.find("MyClass").unwrap()]
            .chars()
            .filter(|&c| c == '\n')
            .count() as u32;
        assert_eq!(range.start.line, class_line);
    }
}