php-lsp 0.23.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
//! "Extract interface from class" code action tests.
//!
//! Extracts an interface from a class's public methods and adds an `implements`
//! clause to the class declaration.

use super::*;
use expect_test::expect;

// ── Happy path ────────────────────────────────────────────────────────────────

#[tokio::test]
async fn extract_interface_single_method() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_action_apply(
            r#"<?php
class $0Service$0 {
    public function process(string $input): string { return $input; }
}
"#,
            "Extract interface 'ServiceInterface'",
        )
        .await;
    expect![[r#"
        <?php
        interface ServiceInterface
        {
            public function process(string $input): string;
        }

        class Service implements ServiceInterface {
            public function process(string $input): string { return $input; }
        }
    "#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn extract_interface_multiple_methods() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_action_apply(
            r#"<?php
class $0Repository$0 {
    public function find(int $id): ?object { return null; }
    public function save(object $entity): void {}
    public function delete(int $id): bool { return true; }
}
"#,
            "Extract interface 'RepositoryInterface'",
        )
        .await;
    expect![[r#"
        <?php
        interface RepositoryInterface
        {
            public function find(int $id): ?object;
            public function save(object $entity): void;
            public function delete(int $id): bool;
        }

        class Repository implements RepositoryInterface {
            public function find(int $id): ?object { return null; }
            public function save(object $entity): void {}
            public function delete(int $id): bool { return true; }
        }
    "#]]
    .assert_eq(&out);
}

/// A public method brought in via `use SomeTrait;` is genuinely part of the
/// class's public API and must appear in the extracted interface, not just
/// methods declared directly in the class body.
#[tokio::test]
async fn extract_interface_includes_trait_provided_public_method() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_action_apply(
            r#"<?php
trait Greets {
    public function greet(): string { return 'hi'; }
}
class $0Person$0 {
    use Greets;
    public function name(): string { return 'Bob'; }
}
"#,
            "Extract interface 'PersonInterface'",
        )
        .await;
    expect![[r#"
        <?php
        trait Greets {
            public function greet(): string { return 'hi'; }
        }
        interface PersonInterface
        {
            public function name(): string;
            public function greet(): string;
        }

        class Person implements PersonInterface {
            use Greets;
            public function name(): string { return 'Bob'; }
        }
    "#]]
    .assert_eq(&out);
}

/// A class's own override of a trait method wins — the trait's signature
/// must not also appear, which would produce a duplicate interface member.
#[tokio::test]
async fn extract_interface_class_override_of_trait_method_not_duplicated() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_action_apply(
            r#"<?php
trait Greets {
    public function greet(): string { return 'hi'; }
}
class $0Person$0 {
    use Greets;
    public function greet(): string { return 'hello'; }
}
"#,
            "Extract interface 'PersonInterface'",
        )
        .await;
    expect![[r#"
        <?php
        trait Greets {
            public function greet(): string { return 'hi'; }
        }
        interface PersonInterface
        {
            public function greet(): string;
        }

        class Person implements PersonInterface {
            use Greets;
            public function greet(): string { return 'hello'; }
        }
    "#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn extract_interface_excludes_non_public_methods() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_action_apply(
            r#"<?php
class $0Handler$0 {
    public function handle(string $cmd): void {}
    protected function prepare(string $cmd): string { return $cmd; }
    private function log(string $msg): void {}
}
"#,
            "Extract interface 'HandlerInterface'",
        )
        .await;
    expect![[r#"
        <?php
        interface HandlerInterface
        {
            public function handle(string $cmd): void;
        }

        class Handler implements HandlerInterface {
            public function handle(string $cmd): void {}
            protected function prepare(string $cmd): string { return $cmd; }
            private function log(string $msg): void {}
        }
    "#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn extract_interface_excludes_constructor_and_destructor() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_action_apply(
            r#"<?php
class $0Worker$0 {
    public function __construct(private string $name) {}
    public function __destruct() {}
    public function run(): void {}
}
"#,
            "Extract interface 'WorkerInterface'",
        )
        .await;
    expect![[r#"
        <?php
        interface WorkerInterface
        {
            public function run(): void;
        }

        class Worker implements WorkerInterface {
            public function __construct(private string $name) {}
            public function __destruct() {}
            public function run(): void {}
        }
    "#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn extract_interface_with_no_return_type() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_action_apply(
            r#"<?php
class $0Notifier$0 {
    public function send($message) {}
}
"#,
            "Extract interface 'NotifierInterface'",
        )
        .await;
    expect![[r#"
        <?php
        interface NotifierInterface
        {
            public function send($message);
        }

        class Notifier implements NotifierInterface {
            public function send($message) {}
        }
    "#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn extract_interface_with_static_method() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_action_apply(
            r#"<?php
class $0Factory$0 {
    public static function create(string $type): self { return new self(); }
}
"#,
            "Extract interface 'FactoryInterface'",
        )
        .await;
    expect![[r#"
        <?php
        interface FactoryInterface
        {
            public static function create(string $type): self;
        }

        class Factory implements FactoryInterface {
            public static function create(string $type): self { return new self(); }
        }
    "#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn extract_interface_appends_to_existing_implements() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_action_apply(
            r#"<?php
interface Stringable { public function __toString(): string; }
class $0Model$0 implements Stringable {
    public function __toString(): string { return ''; }
    public function getId(): int { return 0; }
}
"#,
            "Extract interface 'ModelInterface'",
        )
        .await;
    expect![[r#"
        <?php
        interface Stringable { public function __toString(): string; }
        interface ModelInterface
        {
            public function __toString(): string;
            public function getId(): int;
        }

        class Model implements Stringable, ModelInterface {
            public function __toString(): string { return ''; }
            public function getId(): int { return 0; }
        }
    "#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn extract_interface_with_union_return_type() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_action_apply(
            r#"<?php
class $0Converter$0 {
    public function convert(mixed $value): int|string|null { return null; }
}
"#,
            "Extract interface 'ConverterInterface'",
        )
        .await;
    expect![[r#"
        <?php
        interface ConverterInterface
        {
            public function convert(mixed $value): int|string|null;
        }

        class Converter implements ConverterInterface {
            public function convert(mixed $value): int|string|null { return null; }
        }
    "#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn extract_interface_with_unbraced_namespace() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_action_apply(
            r#"<?php
namespace App\Services;

class $0Logger$0 {
    public function log(string $message): void {}
}
"#,
            "Extract interface 'LoggerInterface'",
        )
        .await;
    expect![[r#"
        <?php
        namespace App\Services;

        interface LoggerInterface
        {
            public function log(string $message): void;
        }

        class Logger implements LoggerInterface {
            public function log(string $message): void {}
        }
    "#]]
    .assert_eq(&out);
}

// ── Cursor position ───────────────────────────────────────────────────────────

#[tokio::test]
async fn extract_interface_offered_anywhere_on_class_declaration() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    // Cursor on the `class` keyword itself.
    let out = s
        .check_code_actions(
            r#"<?php
$0class$0 Foo {
    public function bar(): void {}
}
"#,
        )
        .await;
    assert!(
        out.contains("Extract interface 'FooInterface'"),
        "expected action in: {out}"
    );
}

#[tokio::test]
async fn extract_interface_not_offered_when_cursor_inside_body() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_actions(
            r#"<?php
class Foo {
    public function $0bar$0(): void {}
}
"#,
        )
        .await;
    assert!(
        !out.contains("Extract interface"),
        "action must not appear inside method body: {out}"
    );
}

// ── Not offered ───────────────────────────────────────────────────────────────

#[tokio::test]
async fn extract_interface_not_offered_when_no_eligible_methods() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_actions(
            r#"<?php
class $0Bag$0 {
    private string $data = '';
    protected function init(): void {}
}
"#,
        )
        .await;
    assert!(
        !out.contains("Extract interface"),
        "action must not appear when no public methods: {out}"
    );
}

#[tokio::test]
async fn extract_interface_not_offered_when_already_implements_interface() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_actions(
            r#"<?php
class $0Foo$0 implements FooInterface {
    public function bar(): void {}
}
"#,
        )
        .await;
    assert!(
        !out.contains("Extract interface 'FooInterface'"),
        "action must not be offered when class already implements FooInterface: {out}"
    );
}

// ── Action title ──────────────────────────────────────────────────────────────

#[tokio::test]
async fn extract_interface_action_title_uses_class_name() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_actions(
            r#"<?php
class $0EmailSender$0 {
    public function send(string $to, string $body): bool { return true; }
}
"#,
        )
        .await;
    assert!(
        out.contains("Extract interface 'EmailSenderInterface'"),
        "expected action in: {out}"
    );
}