php-lsp 0.5.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
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
use super::common::TestServer;
use expect_test::expect;
use serde_json::json;

// ── Helper functions for robust snapshot assertions ────────────────────────────

/// Assert that a symbol exists in the snapshot output, ignoring line numbers.
/// Example: assert_symbol_exists(&out, "UserController", "packages/api/src/Controller/UserController.php")
fn assert_symbol_exists(output: &str, symbol_name: &str, file_path: &str) {
    let pattern = format!("{} @", symbol_name);
    assert!(
        output.contains(&pattern),
        "Expected to find symbol '{}' in output:\n{}",
        symbol_name,
        output
    );
    assert!(
        output.contains(file_path),
        "Expected to find file path '{}' for symbol '{}' in output:\n{}",
        file_path,
        symbol_name,
        output
    );
}

/// Assert that a symbol does NOT exist in the snapshot output.
fn assert_symbol_not_exists(output: &str, symbol_name: &str) {
    let pattern = format!("{} @", symbol_name);
    assert!(
        !output.contains(&pattern),
        "Expected NOT to find symbol '{}' in output:\n{}",
        symbol_name,
        output
    );
}

/// Assert that multiple symbols exist in the output.
fn assert_all_symbols_exist(output: &str, symbols: &[(&str, &str)]) {
    for (symbol, file_path) in symbols {
        assert_symbol_exists(output, symbol, file_path);
    }
}

/// Assert that specific symbols are present and others are absent.
fn assert_workspace_symbols(output: &str, present: &[(&str, &str)], absent: &[&str]) {
    assert_all_symbols_exist(output, present);
    for symbol in absent {
        assert_symbol_not_exists(output, symbol);
    }
}

// ── Monorepo workspace scan ────────────────────────────────────────────────────

#[tokio::test]
async fn monorepo_workspace_scan_indexes_all_packages() {
    let mut s = TestServer::with_fixture("monorepo").await;
    s.wait_for_index_ready().await;

    let out = s.snapshot_workspace_symbols("").await;
    // Verify that symbols from all packages are indexed
    assert!(out.contains("User @"), "Expected User class");
    assert!(
        out.contains("UserRepository @"),
        "Expected UserRepository class"
    );
    assert!(
        out.contains("UserController @"),
        "Expected UserController class"
    );
    assert!(
        out.contains("ListUsersCommand @"),
        "Expected ListUsersCommand class"
    );
    assert!(out.contains("UserTest @"), "Expected UserTest class");
}

#[tokio::test]
async fn monorepo_workspace_symbols_scoped_by_package() {
    let mut s = TestServer::with_fixture("monorepo").await;
    s.wait_for_index_ready().await;

    let out = s.snapshot_workspace_symbols("UserController").await;
    assert_symbol_exists(
        &out,
        "UserController",
        "packages/api/src/Controller/UserController.php",
    );
}

// ── Monorepo cross-package navigation ──────────────────────────────────────────

#[tokio::test]
async fn monorepo_cross_package_definition() {
    let mut s = TestServer::with_fixture("monorepo").await;
    s.wait_for_index_ready().await;

    let out = s
        .check_definition(
            r#"//- /packages/api/src/Controller/UserController.php
<?php
namespace Acme\Api\Controller;

use Acme\Core\Entity\User;
use Acme\Core\Repository\UserRepository;

class UserController {
    public function __construct(
        private UserRepository $repository,
    ) {}

    public function show(int $id): ?User {
        return $this->repository->findById($id);
    }

    public function index(): array {
        return $this->repository->find$0All();
    }
}
"#,
        )
        .await;

    expect!["packages/core/src/Repository/UserRepository.php:18:20-18:27"].assert_eq(&out);
}

#[tokio::test]
async fn monorepo_cross_package_references() {
    let mut s = TestServer::with_fixture("monorepo").await;
    s.wait_for_index_ready().await;

    let out = s
        .check_references(
            r#"//- /packages/core/src/Entity/User.php
<?php
namespace Acme\Core\Entity;

class User$0 {
    public function __construct(
        public readonly int $id,
        public readonly string $name,
        public readonly string $email,
    ) {}

    public function getDisplayName(): string {
        return $this->name;
    }
}
"#,
        )
        .await;

    expect![[r#"
        packages/api/src/Controller/UserController.php:11:36-11:40
        packages/core/src/Entity/User.php:3:6-3:10
        packages/core/src/Repository/UserRepository.php:22:25-22:29
        packages/core/src/Repository/UserRepository.php:9:40-9:44
        packages/tests/src/Integration/UserTest.php:9:20-9:24"#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn monorepo_cross_package_hover() {
    let mut s = TestServer::with_fixture("monorepo").await;
    s.wait_for_index_ready().await;

    let out = s
        .check_hover(
            r#"//- /packages/api/src/Controller/UserController.php
<?php
namespace Acme\Api\Controller;

use Acme\Core\Entity\User;
use Acme\Core\Repository\UserRepository;

class UserController {
    public function __construct(
        private UserRepository$0 $repository,
    ) {}
}
"#,
        )
        .await;

    expect![[r#"
        ```php
        class UserRepository
        ```"#]]
    .assert_eq(&out);
}

// ── Monorepo diagnostics ───────────────────────────────────────────────────────

#[tokio::test]
async fn monorepo_cross_package_clean_file() {
    let mut s = TestServer::with_fixture("monorepo").await;
    s.wait_for_index_ready().await;

    s.check_diagnostics(
        r#"//- /packages/api/src/Controller/UserController.php
<?php
namespace Acme\Api\Controller;

use Acme\Core\Entity\User;
use Acme\Core\Repository\UserRepository;

class UserController {
    public function __construct(
        private UserRepository $repository,
    ) {}

    public function show(int $id): ?User {
        return $this->repository->findById($id);
    }
}
"#,
    )
    .await;
}

#[tokio::test]
async fn monorepo_undefined_cross_package_class_diagnostic() {
    let mut s = TestServer::with_fixture("monorepo").await;
    s.wait_for_index_ready().await;

    s.check_diagnostics(
        r#"<?php
namespace Acme\Api\Controller;

use Acme\Core\Entity\NonexistentUser;

class UserController {
    public function __construct(
        private NonexistentUser $user,
        //      ^^^^^^^^^^^^^^^ error: Class Acme\Core\Entity\NonexistentUser does not exist
    ) {}
}
"#,
    )
    .await;
}

// ── Multi-PSR4 workspace scan ──────────────────────────────────────────────────

#[tokio::test]
async fn multi_psr4_all_mappings_indexed() {
    let mut s = TestServer::with_fixture("multi-psr4").await;
    s.wait_for_index_ready().await;

    let out = s.snapshot_workspace_symbols("").await;
    // Verify that symbols from all PSR-4 mappings are indexed
    assert_all_symbols_exist(
        &out,
        &[
            ("Mailer", "src/Service/Mailer.php"),
            ("MailerTest", "tests/Unit/MailerTest.php"),
            ("SmtpClient", "lib/Transport/SmtpClient.php"),
        ],
    );
}

#[tokio::test]
async fn multi_psr4_cross_mapping_definition() {
    let mut s = TestServer::with_fixture("multi-psr4").await;
    s.wait_for_index_ready().await;

    let out = s
        .check_definition(
            r#"//- /src/Service/Mailer.php
<?php
namespace App\Service;

use Lib\Transport\SmtpClient;

class Mailer {
    public function __construct(
        private SmtpClient $client,
    ) {}

    public function send(string $to, string $subject, string $body): bool {
        return $this->client->deliver$0($to, $subject, $body);
    }
}
"#,
        )
        .await;

    expect!["lib/Transport/SmtpClient.php:4:20-4:27"].assert_eq(&out);
}

#[tokio::test]
async fn multi_psr4_cross_mapping_clean_diagnostics() {
    let mut s = TestServer::with_fixture("multi-psr4").await;
    s.wait_for_index_ready().await;

    s.check_diagnostics(
        r#"//- /src/Service/Mailer.php
<?php
namespace App\Service;

use Lib\Transport\SmtpClient;

class Mailer {
    public function __construct(
        private SmtpClient $client,
    ) {}

    public function send(string $to, string $subject, string $body): bool {
        return $this->client->deliver($to, $subject, $body);
    }
}
"#,
    )
    .await;
}

#[tokio::test]
async fn multi_psr4_autoload_dev_scanned() {
    let mut s = TestServer::with_fixture("multi-psr4").await;
    s.wait_for_index_ready().await;

    let out = s.snapshot_workspace_symbols("MailerTest").await;
    expect!["Class       MailerTest @ tests/Unit/MailerTest.php:6"].assert_eq(&out);
}

// ── PHP version with project structure ─────────────────────────────────────────

#[tokio::test]
async fn monorepo_php80_str_contains_no_error() {
    let mut s = TestServer::with_fixture_and_options(
        "monorepo",
        json!({
            "phpVersion": "8.0",
            "diagnostics": { "enabled": true }
        }),
    )
    .await;
    s.wait_for_index_ready().await;

    s.check_diagnostics(
        r#"<?php
namespace Acme\Core\Util;

class StringHelper {
    public function hasSubstring(string $haystack, string $needle): bool {
        return str_contains($haystack, $needle);
    }
}
"#,
    )
    .await;
}

#[tokio::test]
async fn monorepo_php74_str_contains_error() {
    let mut s = TestServer::with_fixture_and_options(
        "monorepo",
        json!({
            "phpVersion": "7.4",
            "diagnostics": { "enabled": true }
        }),
    )
    .await;
    s.wait_for_index_ready().await;

    let notif = s
        .open(
            "test.php",
            "<?php\nnamespace Acme\\Core\\Util;\nclass StringHelper {\n    public function hasSubstring(string $haystack, string $needle): bool {\n        return str_contains($haystack, $needle);\n    }\n}\n",
        )
        .await;
    let empty = vec![];
    let diags = notif["params"]["diagnostics"].as_array().unwrap_or(&empty);
    assert!(
        diags
            .iter()
            .any(|d| d["message"].as_str().unwrap_or("").contains("str_contains")),
        "Expected str_contains undefined error with PHP 7.4"
    );
}

#[tokio::test]
async fn multi_psr4_php81_array_is_list_available() {
    let mut s = TestServer::with_fixture_and_options(
        "multi-psr4",
        json!({
            "phpVersion": "8.1",
            "diagnostics": { "enabled": true }
        }),
    )
    .await;
    s.wait_for_index_ready().await;

    s.check_diagnostics(
        r#"<?php
namespace Lib\Util;

class ArrayHelper {
    public function isList(mixed $value): bool {
        if (array_is_list($value)) {
            return true;
        }
        return false;
    }
}
"#,
    )
    .await;
}

// ── Exclude paths interaction ──────────────────────────────────────────────────

#[tokio::test]
async fn monorepo_exclude_one_package() {
    let mut s = TestServer::with_fixture_and_options(
        "monorepo",
        json!({
            "excludePaths": ["packages/cli/"]
        }),
    )
    .await;
    s.wait_for_index_ready().await;

    let out = s.snapshot_workspace_symbols("").await;
    // Verify that Core, Api, and Tests are indexed but Cli is excluded
    assert_workspace_symbols(
        &out,
        &[
            ("User", "packages/core/src/Entity/User.php"),
            (
                "UserController",
                "packages/api/src/Controller/UserController.php",
            ),
            (
                "UserRepository",
                "packages/core/src/Repository/UserRepository.php",
            ),
            ("UserTest", "packages/tests/src/Integration/UserTest.php"),
        ],
        &["ListUsersCommand"],
    );
}

#[tokio::test]
async fn multi_psr4_exclude_tests_dir() {
    let mut s = TestServer::with_fixture_and_options(
        "multi-psr4",
        json!({
            "excludePaths": ["tests/"]
        }),
    )
    .await;
    s.wait_for_index_ready().await;

    let out = s.snapshot_workspace_symbols("").await;
    // Verify that App and Lib are indexed but Tests is excluded
    assert_workspace_symbols(
        &out,
        &[
            ("Mailer", "src/Service/Mailer.php"),
            ("SmtpClient", "lib/Transport/SmtpClient.php"),
        ],
        &["MailerTest"],
    );
}

// ── Workspace structure edge cases ────────────────────────────────────────────

#[tokio::test]
async fn monorepo_multiple_files_same_namespace_different_packages() {
    let mut s = TestServer::with_fixture("monorepo").await;
    s.wait_for_index_ready().await;

    let out = s.snapshot_workspace_symbols("User").await;
    // Verify that all User-related symbols are found across packages
    assert_all_symbols_exist(
        &out,
        &[
            ("User", "packages/core/src/Entity/User.php"),
            (
                "UserController",
                "packages/api/src/Controller/UserController.php",
            ),
            (
                "UserRepository",
                "packages/core/src/Repository/UserRepository.php",
            ),
            ("UserTest", "packages/tests/src/Integration/UserTest.php"),
        ],
    );
}

#[tokio::test]
async fn monorepo_with_fixture_and_options_and_version() {
    let mut s = TestServer::with_fixture_and_options(
        "monorepo",
        json!({
            "phpVersion": "8.4",
            "diagnostics": { "enabled": true }
        }),
    )
    .await;
    s.wait_for_index_ready().await;

    s.check_diagnostics(
        r#"<?php
namespace Acme\Core\Util;

class ArrayFunctions {
    public function findElement(array $arr, callable $fn): mixed {
        return array_find($arr, $fn);
    }
}
"#,
    )
    .await;
}

#[tokio::test]
async fn multi_psr4_cross_package_in_same_namespace_call() {
    let mut s = TestServer::with_fixture("multi-psr4").await;
    s.wait_for_index_ready().await;

    s.check_diagnostics(
        r#"<?php
namespace Tests\Unit;

use App\Service\Mailer;
use Lib\Transport\SmtpClient;

class MailerTest {
    public function testSend(): void {
        $client = new SmtpClient();
        $mailer = new Mailer($client);
        $result = $mailer->send('test@example.com', 'Subject', 'Body');
    }
}
"#,
    )
    .await;
}