php-lsp 0.11.0

A PHP Language Server Protocol implementation
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
use std::collections::HashMap;
use std::sync::Arc;

use mir_analyzer::AnalysisSession;
use tower_lsp::lsp_types::{CompletionItem, CompletionItemKind, InsertTextFormat, Position};

use crate::document::ast::ParsedDoc;
use crate::text::{fqn_short_name, utf16_offset_to_byte};
use crate::types::stub_members::stub_class_members;
use crate::types::type_map::{
    ClassMembers, enclosing_class_at, is_backed_enum, is_enum, members_of_class, mixin_classes_of,
    parent_class_name,
};

use super::callable_item;

pub(super) fn all_instance_members(
    class_name: &str,
    doc: &ParsedDoc,
    other_docs: &[Arc<ParsedDoc>],
    find_class_doc: Option<super::ClassDocLookup<'_>>,
    session: Option<&AnalysisSession>,
) -> Vec<CompletionItem> {
    all_members(class_name, doc, other_docs, find_class_doc, session, false)
}

pub(super) fn all_static_members(
    class_name: &str,
    doc: &ParsedDoc,
    other_docs: &[Arc<ParsedDoc>],
    find_class_doc: Option<super::ClassDocLookup<'_>>,
    session: Option<&AnalysisSession>,
) -> Vec<CompletionItem> {
    all_members(class_name, doc, other_docs, find_class_doc, session, true)
}

/// Common class-hierarchy traversal for both instance (`->`) and static (`::`)
/// member completion. `is_static` selects which subset of members to surface:
/// - `false` (instance): non-static methods + properties, enum built-ins, mixins
/// - `true`  (static):   static methods + properties, constants
fn all_members(
    class_name: &str,
    doc: &ParsedDoc,
    other_docs: &[Arc<ParsedDoc>],
    find_class_doc: Option<super::ClassDocLookup<'_>>,
    session: Option<&AnalysisSession>,
    is_static: bool,
) -> Vec<CompletionItem> {
    let all: Vec<&ParsedDoc> = std::iter::once(doc)
        .chain(other_docs.iter().map(|d| d.as_ref()))
        .collect();
    let mut items = Vec::new();
    let mut seen_names: std::collections::HashSet<String> = std::collections::HashSet::new();
    let mut visited: std::collections::HashSet<String> = std::collections::HashSet::new();
    let mut queue: Vec<String> = vec![class_name.to_string()];
    while let Some(current) = queue.pop() {
        if !visited.insert(current.clone()) {
            continue;
        }
        let mut parent: Option<String> = None;
        let mut found_in_docs = false;

        // Fast path: workspace-index lookup gives O(1) access to the one doc
        // that defines `current`. Fallback: scan all docs linearly (original
        // behaviour, used when the index is unavailable or the class is not
        // yet indexed, e.g. built-ins or unsaved files).
        let fast_doc: Option<Arc<ParsedDoc>> = find_class_doc.and_then(|f| f(&current));
        let fast_ref: Option<&ParsedDoc> = fast_doc.as_deref();
        let defining: Option<(&ParsedDoc, ClassMembers)> = if let Some(fd) = fast_ref {
            let m = members_of_class(fd, &current);
            m.found.then_some((fd, m))
        } else {
            // PHP defines a class in exactly one file, so stop scanning once
            // the defining doc is hit.
            all.iter().find_map(|d| {
                let m = members_of_class(d, &current);
                m.found.then_some((*d, m))
            })
        };

        if let Some((d, members)) = defining {
            found_in_docs = true;
            parent = members.parent.clone();
            for (name, meth_is_static) in members.methods {
                if (meth_is_static == is_static) && seen_names.insert(name.clone()) {
                    // Method params unknown here; use has_params=true so
                    // snippet cursor lands inside parens.
                    items.push(callable_item(&name, CompletionItemKind::METHOD, true));
                }
            }
            for (name, prop_is_static) in &members.properties {
                if *prop_is_static == is_static {
                    let label = format!("${name}");
                    if seen_names.insert(label.clone()) {
                        let detail = if !is_static && members.readonly_properties.contains(name) {
                            Some("readonly".to_string())
                        } else {
                            None
                        };
                        // Static access (Class::$prop) keeps "$name" as the inserted text because
                        // the $ is syntactically required. Instance access ($obj->prop) must NOT
                        // insert the $, or the client produces `$obj->$prop` (invalid PHP).
                        let insert_text = if is_static { None } else { Some(name.clone()) };
                        items.push(CompletionItem {
                            label,
                            kind: Some(CompletionItemKind::PROPERTY),
                            detail,
                            insert_text,
                            ..Default::default()
                        });
                    }
                }
            }
            if is_static {
                for name in members.constants {
                    if seen_names.insert(name.clone()) {
                        items.push(CompletionItem {
                            label: name,
                            kind: Some(CompletionItemKind::CONSTANT),
                            ..Default::default()
                        });
                    }
                }
            } else {
                // Built-in enum properties: every enum case has `->name: string`
                // and backed enums also have `->value`.
                if is_enum(d, &current) {
                    if seen_names.insert("name".to_string()) {
                        items.push(CompletionItem {
                            label: "name".to_string(),
                            kind: Some(CompletionItemKind::PROPERTY),
                            detail: Some("string".to_string()),
                            ..Default::default()
                        });
                    }
                    if is_backed_enum(d, &current) && seen_names.insert("value".to_string()) {
                        items.push(CompletionItem {
                            label: "value".to_string(),
                            kind: Some(CompletionItemKind::PROPERTY),
                            detail: Some("string|int".to_string()),
                            ..Default::default()
                        });
                    }
                }
                for mixin in mixin_classes_of(d, &current) {
                    queue.push(mixin);
                }
            }
            for trait_name in members.trait_uses {
                queue.push(trait_name);
            }
        }

        // Built-in stubs only apply when the class is not defined in any user
        // document — a user class shadowing a built-in name wins.
        if !found_in_docs && let Some(stub) = session.and_then(|s| stub_class_members(s, &current))
        {
            if parent.is_none() {
                parent = stub.parent.clone();
            }
            for (name, meth_is_static) in &stub.methods {
                if (*meth_is_static == is_static) && seen_names.insert(name.clone()) {
                    items.push(callable_item(name, CompletionItemKind::METHOD, true));
                }
            }
            for (name, prop_is_static) in &stub.properties {
                if *prop_is_static == is_static {
                    let label = format!("${name}");
                    if seen_names.insert(label.clone()) {
                        let insert_text = if is_static { None } else { Some(name.clone()) };
                        items.push(CompletionItem {
                            label,
                            kind: Some(CompletionItemKind::PROPERTY),
                            insert_text,
                            ..Default::default()
                        });
                    }
                }
            }
            if is_static {
                for name in &stub.constants {
                    if seen_names.insert(name.clone()) {
                        items.push(CompletionItem {
                            label: name.clone(),
                            kind: Some(CompletionItemKind::CONSTANT),
                            ..Default::default()
                        });
                    }
                }
            }
        }
        if let Some(p) = parent {
            queue.push(p);
        }
    }
    items
}

/// Resolve `ClassName::` or the aliases `self::`, `static::`, `parent::`.
///
/// `imports` is the file's `use`-import map (`alias → FQCN`). When the name
/// before `::` is an import alias (including `as`-renamed aliases such as
/// `use Foo\Bar as Baz`), the short class name extracted from the FQCN is
/// returned so that `all_static_members` can find the class in the workspace
/// index by its canonical short name.
pub(super) fn resolve_static_receiver(
    source: &str,
    doc: &ParsedDoc,
    other_docs: &[Arc<ParsedDoc>],
    position: Position,
    imports: &HashMap<String, String>,
) -> Option<String> {
    let line = source.lines().nth(position.line as usize)?;
    let col = utf16_offset_to_byte(line, position.character as usize);
    let before = &line[..col];
    let before = before.strip_suffix("::").unwrap_or(before);
    let name: String = before
        .chars()
        .rev()
        .take_while(|&c| c.is_alphanumeric() || c == '_' || c == '\\')
        .collect::<String>()
        .chars()
        .rev()
        .collect();
    match name.as_str() {
        "" => None,
        "self" | "static" => enclosing_class_at(source, doc, position),
        "parent" => {
            let enclosing = enclosing_class_at(source, doc, position)?;
            // Look for the parent class in current doc then other docs
            if let Some(p) = parent_class_name(doc, &enclosing) {
                return Some(p);
            }
            for other in other_docs {
                if let Some(p) = parent_class_name(other, &enclosing) {
                    return Some(p);
                }
            }
            None
        }
        _ => {
            // If the name contains a backslash it's already a FQN — extract its
            // short component directly (e.g. `\Illuminate\Support\Str::` → `Str`).
            if name.contains('\\') {
                return Some(fqn_short_name(&name).to_owned());
            }
            // Otherwise try to expand a `use`-import alias. This handles both
            // the unaliased case (`use Foo\Bar\Str` → alias `Str` = short name `Str`)
            // and the `as`-aliased case (`use Foo\Bar\Str as Helper` → alias `Helper`
            // maps to FQN `Foo\Bar\Str`, short name `Str`).
            if let Some(fqcn) = imports.get(&name) {
                return Some(fqn_short_name(fqcn).to_owned());
            }
            Some(name)
        }
    }
}

const PHP_MAGIC_METHODS: &[(&str, &str)] = &[
    (
        "__construct",
        "public function __construct($1)\n{\n    $2\n}",
    ),
    ("__destruct", "public function __destruct()\n{\n    $1\n}"),
    (
        "__get",
        "public function __get(string $name): mixed\n{\n    $1\n}",
    ),
    (
        "__set",
        "public function __set(string $name, mixed $value): void\n{\n    $1\n}",
    ),
    (
        "__isset",
        "public function __isset(string $name): bool\n{\n    $1\n}",
    ),
    (
        "__unset",
        "public function __unset(string $name): void\n{\n    $1\n}",
    ),
    (
        "__call",
        "public function __call(string $name, array $arguments): mixed\n{\n    $1\n}",
    ),
    (
        "__callStatic",
        "public static function __callStatic(string $name, array $arguments): mixed\n{\n    $1\n}",
    ),
    (
        "__toString",
        "public function __toString(): string\n{\n    $1\n}",
    ),
    (
        "__invoke",
        "public function __invoke($1): mixed\n{\n    $2\n}",
    ),
    ("__clone", "public function __clone(): void\n{\n    $1\n}"),
    ("__sleep", "public function __sleep(): array\n{\n    $1\n}"),
    ("__wakeup", "public function __wakeup(): void\n{\n    $1\n}"),
    (
        "__serialize",
        "public function __serialize(): array\n{\n    $1\n}",
    ),
    (
        "__unserialize",
        "public function __unserialize(array $data): void\n{\n    $1\n}",
    ),
    (
        "__debugInfo",
        "public function __debugInfo(): ?array\n{\n    $1\n}",
    ),
];

pub(super) fn magic_method_completions() -> Vec<CompletionItem> {
    PHP_MAGIC_METHODS
        .iter()
        .map(|(name, snippet)| CompletionItem {
            label: name.to_string(),
            kind: Some(CompletionItemKind::METHOD),
            insert_text: Some(snippet.to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            detail: Some("magic method".to_string()),
            ..Default::default()
        })
        .collect()
}

pub(super) fn resolve_receiver_class(
    source: &str,
    doc: &ParsedDoc,
    position: Position,
    analysis: Option<&mir_analyzer::FileAnalysis>,
    type_map: &crate::types::type_map::TypeMap,
) -> Option<String> {
    let line = source.lines().nth(position.line as usize)?;
    let col = utf16_offset_to_byte(line, position.character as usize);
    let before = &line[..col];
    // Try ?-> first (longer pattern) so `$s?->` doesn't get stripped to `$s?` by the `->` rule.
    let before = before
        .strip_suffix("?->")
        .or_else(|| before.strip_suffix("->"))
        .unwrap_or(before);

    // Handle (new ClassName()) before ->
    if let Some(class_name) = extract_new_class_before_arrow(before) {
        return Some(class_name);
    }

    let var_name: String = before
        .chars()
        .rev()
        .take_while(|&c| c.is_alphanumeric() || c == '_' || c == '$')
        .collect::<String>()
        .chars()
        .rev()
        .collect();
    if var_name.is_empty() {
        // Method-chain receiver like `$obj->getUser()->`: `before` ends with `)`,
        // so the take_while extractor collects nothing.  Ask mir for the type of
        // the call-expression at that position; mir records the return type there.
        let chain_offset = line_byte_offset(doc, position.line, before.len());
        return analysis.and_then(|a| receiver_class_at(a, chain_offset));
    }
    let var_name = if var_name.starts_with('$') {
        var_name
    } else {
        format!("${var_name}")
    };
    // `before` now ends with the receiver variable; its last byte lands strictly
    // inside the (end-exclusive, identifier-only) mir symbol span.
    let var_offset = line_byte_offset(doc, position.line, before.len());
    if var_name == "$this" {
        return enclosing_class_at(source, doc, position)
            .or_else(|| analysis.and_then(|a| receiver_class_at(a, var_offset)));
    }
    // mir-primary; TypeMap covers gaps where mir records no class-typed symbol.
    // For foreach value vars over array_map results, add a targeted fallback.
    analysis
        .and_then(|a| receiver_class_at(a, var_offset))
        .or_else(|| type_map.get(&var_name).map(str::to_owned))
        .or_else(|| {
            let val_name = var_name.trim_start_matches('$');
            let arr_name = crate::types::array_inference::find_foreach_array_var(
                &doc.program().stmts,
                val_name,
            )?;
            crate::types::array_inference::scan_array_map_return(&doc.program().stmts, arr_name)
        })
}

/// Resolve the class(es) of a receiver variable from mir's recorded symbol at
/// `var_offset` (a byte offset inside the variable token), returning short
/// names for member lookup. A union receiver yields `Foo|Bar`. `None` if mir
/// recorded no class-typed symbol there.
pub(super) fn receiver_class_at(
    analysis: &mir_analyzer::FileAnalysis,
    var_offset: u32,
) -> Option<String> {
    let ty = crate::types::type_query::type_at_offset(analysis, var_offset)?;
    let names: Vec<String> = crate::types::type_query::class_names(ty)
        .iter()
        .map(|fqcn| fqn_short_name(fqcn).to_string())
        .collect();
    (!names.is_empty()).then(|| names.join("|"))
}

/// Doc byte offset of `byte_in_line` bytes into line `line`, minus one — i.e.
/// the last source byte of a token that ends at `byte_in_line`. Used to land an
/// offset strictly inside a receiver variable's end-exclusive mir span.
pub(super) fn line_byte_offset(doc: &ParsedDoc, line: u32, byte_in_line: usize) -> u32 {
    let line_start = doc.view().byte_of_position(Position { line, character: 0 });
    line_start + byte_in_line.saturating_sub(1) as u32
}

/// Extract the class name from `(new ClassName(...))` or `new ClassName(...)` text
/// appearing immediately before `->`.
fn extract_new_class_before_arrow(text: &str) -> Option<String> {
    let text = text.trim_end();
    // Strip optional closing paren wrapping: `(new Foo())`
    let inner = if let Some(without_last) = text.strip_suffix(')') {
        // Find matching open paren — look for `(new` pattern
        if let Some(pos) = without_last.rfind("(new ") {
            &without_last[pos + 1..]
        } else if let Some(pos) = without_last.rfind("(new\t") {
            &without_last[pos + 1..]
        } else {
            text
        }
    } else {
        text
    };
    // Now inner should start with `new ClassName(...)`
    let inner = inner.trim();
    if !inner.starts_with("new ") && !inner.starts_with("new\t") {
        return None;
    }
    let after_new = inner[3..].trim_start();
    // Extract class name (alphanumeric + _ + \)
    let class: String = after_new
        .chars()
        .take_while(|&c| c.is_alphanumeric() || c == '_' || c == '\\')
        .collect();
    if class.is_empty() {
        return None;
    }
    // Return short name
    Some(fqn_short_name(&class).to_string())
}