php-lsp 0.29.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
//! Extract constant code action transformation tests.
//! Tests verify that selected literals are extracted into named constants.

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

#[tokio::test]
async fn extract_constant_string_in_class() {
    let mut s = TestServer::new().await;
    let out = s
        .check_code_action_apply(
            r#"<?php
class Greeter {
    public function greet(): string {
        return $0"Hello, World!"$0;
    }
}
"#,
            "Extract constant",
        )
        .await;
    expect![[r#"
        <?php
        class Greeter {
            private const HELLO_WORLD = "Hello, World!";
            public function greet(): string {
                return self::HELLO_WORLD;
            }
        }
    "#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn extract_constant_integer_in_class() {
    let mut s = TestServer::new().await;
    let out = s
        .check_code_action_apply(
            r#"<?php
class Timer {
    public function delay(): void {
        sleep($042$0);
    }
}
"#,
            "Extract constant",
        )
        .await;
    expect![[r#"
        <?php
        class Timer {
            private const CONSTANT_42 = 42;
            public function delay(): void {
                sleep(self::CONSTANT_42);
            }
        }
    "#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn extract_constant_float_in_class() {
    let mut s = TestServer::new().await;
    let out = s
        .check_code_action_apply(
            r#"<?php
class Calculator {
    public function ratio(): float {
        return $03.14$0;
    }
}
"#,
            "Extract constant",
        )
        .await;
    expect![[r#"
        <?php
        class Calculator {
            private const CONSTANT_3_14 = 3.14;
            public function ratio(): float {
                return self::CONSTANT_3_14;
            }
        }
    "#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn extract_constant_at_file_scope() {
    let mut s = TestServer::new().await;
    let out = s
        .check_code_action_apply(
            r#"<?php
function getName() {
    return $0"app"$0;
}
"#,
            "Extract constant",
        )
        .await;
    expect![[r#"
        <?php
        const APP = "app";
        function getName() {
            return APP;
        }
    "#]]
    .assert_eq(&out);
}

/// Selecting the RHS of an existing `const NAME = value;` must not offer the
/// action — extracting it would insert a second `const ACTIVE = ...;` above,
/// redeclaring the same constant name (a PHP fatal error).
#[tokio::test]
async fn extract_constant_already_in_const_declaration_returns_no_action() {
    let mut s = TestServer::new().await;
    let out = s
        .check_code_action_apply(
            r#"<?php
interface Status {
    const ACTIVE = $0"active"$0;
}
"#,
            "Extract constant",
        )
        .await;
    expect!["<action not found: Extract constant>"].assert_eq(&out);
}

#[tokio::test]
async fn extract_constant_in_trait() {
    let mut s = TestServer::new().await;
    let out = s
        .check_code_action_apply(
            r#"<?php
trait Logging {
    public function log(): void {
        $level = $0"info"$0;
    }
}
"#,
            "Extract constant",
        )
        .await;
    expect![[r#"
        <?php
        trait Logging {
            private const INFO = "info";
            public function log(): void {
                $level = self::INFO;
            }
        }
    "#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn extract_constant_after_interface_at_file_scope() {
    let mut s = TestServer::new().await;
    let out = s
        .check_code_action_apply(
            r#"<?php
interface PaymentGateway {
    public function charge(): void;
}
$fee = $0250$0;
"#,
            "Extract constant",
        )
        .await;
    expect![[r#"
        <?php
        const CONSTANT_250 = 250;
        interface PaymentGateway {
            public function charge(): void;
        }
        $fee = CONSTANT_250;
    "#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn extract_constant_non_literal_returns_no_action() {
    let mut s = TestServer::new().await;
    let out = s
        .check_code_action_apply(
            r#"<?php
$x = $0foo()$0;
"#,
            "Extract constant",
        )
        .await;
    expect!["<action not found: Extract constant>"].assert_eq(&out);
}

/// A class/interface constant initializer must be a compile-time constant
/// expression. A double-quoted string with simple `$var` interpolation is
/// not one — hoisting it verbatim would be a PHP fatal error ("Constant
/// expression contains invalid operations").
#[tokio::test]
async fn extract_constant_interpolated_string_returns_no_action() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_action_apply(
            r#"<?php
$name = "world";
$x = $0"Hello $name"$0;
"#,
            "Extract constant",
        )
        .await;
    expect!["<action not found: Extract constant>"].assert_eq(&out);
}

/// Same as above but for complex `{$expr}` interpolation syntax.
#[tokio::test]
async fn extract_constant_complex_interpolated_string_returns_no_action() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_action_apply(
            r#"<?php
class Foo {
    public $x = 1;
    public function bar() {
        $y = $0"Value: {$this->x}"$0;
    }
}
"#,
            "Extract constant",
        )
        .await;
    expect!["<action not found: Extract constant>"].assert_eq(&out);
}

/// A `$` that can't start a variable name (not followed by a letter/`_`/`{`)
/// does not interpolate in PHP, so it must still be extractable.
#[tokio::test]
async fn extract_constant_dollar_sign_without_interpolation_is_allowed() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_action_apply(
            r#"<?php
$x = $0"price: $5"$0;
"#,
            "Extract constant",
        )
        .await;
    expect![[r#"
        <?php
        const PRICE_5 = "price: $5";
        $x = PRICE_5;
    "#]]
    .assert_eq(&out);
}

/// A single-quoted string never interpolates in PHP regardless of content,
/// so a literal `$name` inside one must still be extractable.
#[tokio::test]
async fn extract_constant_single_quoted_dollar_is_allowed() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_code_action_apply(
            r#"<?php
$x = $0'Hello $name'$0;
"#,
            "Extract constant",
        )
        .await;
    expect![[r#"
        <?php
        const HELLO_NAME = 'Hello $name';
        $x = HELLO_NAME;
    "#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn extract_constant_file_scope_inserts_before_use_statement() {
    let mut s = TestServer::new().await;
    let out = s
        .check_code_action_apply(
            r#"<?php
$x = $0"hello"$0;
use Foo\Bar;
"#,
            "Extract constant",
        )
        .await;
    expect![[r#"
        <?php
        const HELLO = "hello";
        $x = HELLO;
        use Foo\Bar;
    "#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn extract_constant_url_string_derives_screaming_snake_name() {
    let mut s = TestServer::new().await;
    let out = s
        .check_code_action_apply(
            r#"<?php
class ApiClient {
    public function endpoint(): string {
        return $0"https://api.example.com"$0;
    }
}
"#,
            "Extract constant",
        )
        .await;
    expect![[r#"
        <?php
        class ApiClient {
            private const HTTPS_API_EXAMPLE_COM = "https://api.example.com";
            public function endpoint(): string {
                return self::HTTPS_API_EXAMPLE_COM;
            }
        }
    "#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn extract_constant_all_special_chars_falls_back_to_default_name() {
    let mut s = TestServer::new().await;
    let out = s
        .check_code_action_apply(
            r#"<?php
class Symbols {
    public function noise(): string {
        return $0"!!!"$0;
    }
}
"#,
            "Extract constant",
        )
        .await;
    expect![[r#"
        <?php
        class Symbols {
            private const EXTRACTED_CONSTANT = "!!!";
            public function noise(): string {
                return self::EXTRACTED_CONSTANT;
            }
        }
    "#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn extract_constant_class_scope_when_method_body_has_brace_in_string() {
    // Regression: `find_matching_close` used to count `{` / `}` inside string
    // literals as structural braces, so `"hello { world }"` caused it to return
    // the wrong closing line and fall back to file scope instead of class scope.
    let mut s = TestServer::new().await;
    let out = s
        .check_code_action_apply(
            r#"<?php
class Formatter {
    public function wrap(): string {
        $template = "{ content }";
        return $0"output"$0;
    }
}
"#,
            "Extract constant",
        )
        .await;
    expect![[r#"
        <?php
        class Formatter {
            private const OUTPUT = "output";
            public function wrap(): string {
                $template = "{ content }";
                return self::OUTPUT;
            }
        }
    "#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn extract_constant_class_scope_when_method_has_line_comment_with_brace() {
    // Regression: `}` inside a `//` comment must not decrement the brace depth.
    let mut s = TestServer::new().await;
    let out = s
        .check_code_action_apply(
            r#"<?php
class Builder {
    public function build(): string {
        // end of block: }
        return $0"result"$0;
    }
}
"#,
            "Extract constant",
        )
        .await;
    expect![[r#"
        <?php
        class Builder {
            private const RESULT = "result";
            public function build(): string {
                // end of block: }
                return self::RESULT;
            }
        }
    "#]]
    .assert_eq(&out);
}