-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathparser.rs
More file actions
2313 lines (2082 loc) · 70.9 KB
/
Copy pathparser.rs
File metadata and controls
2313 lines (2082 loc) · 70.9 KB
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
mod common;
use common::create_test_backend;
use phpantom_lsp::Visibility;
// ─── PHP Parsing / AST Extraction Tests ─────────────────────────────────────
#[tokio::test]
async fn test_parse_php_extracts_class_and_methods() {
let backend = create_test_backend();
let php = "<?php\nclass User {\n function login() {}\n function logout() {}\n}\n";
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
assert_eq!(classes[0].name, "User");
assert_eq!(classes[0].methods.len(), 2);
assert_eq!(classes[0].methods[0].name, "login");
assert_eq!(classes[0].methods[1].name, "logout");
}
#[tokio::test]
async fn test_parse_php_ignores_standalone_functions() {
let backend = create_test_backend();
let php = "<?php\nfunction standalone() {}\nclass Service {\n function handle() {}\n}\n";
let classes = backend.parse_php(php);
assert_eq!(
classes.len(),
1,
"Only class declarations should be extracted"
);
assert_eq!(classes[0].name, "Service");
assert_eq!(classes[0].methods.len(), 1);
assert_eq!(classes[0].methods[0].name, "handle");
}
#[tokio::test]
async fn test_parse_php_no_classes_returns_empty() {
let backend = create_test_backend();
let php = "<?php\nfunction foo() {}\n$x = 1;\n";
let classes = backend.parse_php(php);
assert!(classes.is_empty(), "No classes should be found");
}
#[tokio::test]
async fn test_parse_php_extracts_properties() {
let backend = create_test_backend();
let php = concat!(
"<?php\n",
"class User {\n",
" public string $name;\n",
" public int $age;\n",
" private $secret;\n",
" function login() {}\n",
"}\n",
);
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
assert_eq!(
classes[0].properties.len(),
3,
"Should extract 3 properties"
);
let prop_names: Vec<&str> = classes[0]
.properties
.iter()
.map(|p| p.name.as_str())
.collect();
assert!(prop_names.contains(&"name"), "Should contain 'name'");
assert!(prop_names.contains(&"age"), "Should contain 'age'");
assert!(prop_names.contains(&"secret"), "Should contain 'secret'");
// Verify type hints
let name_prop = classes[0]
.properties
.iter()
.find(|p| p.name == "name")
.unwrap();
assert_eq!(
name_prop.type_hint.as_deref(),
Some("string"),
"name property should have string type hint"
);
let age_prop = classes[0]
.properties
.iter()
.find(|p| p.name == "age")
.unwrap();
assert_eq!(
age_prop.type_hint.as_deref(),
Some("int"),
"age property should have int type hint"
);
let secret_prop = classes[0]
.properties
.iter()
.find(|p| p.name == "secret")
.unwrap();
assert_eq!(
secret_prop.type_hint, None,
"secret property should have no type hint"
);
}
#[tokio::test]
async fn test_parse_php_extracts_static_properties() {
let backend = create_test_backend();
let php = concat!(
"<?php\n",
"class Counter {\n",
" public static int $count = 0;\n",
" public string $label;\n",
"}\n",
);
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
assert_eq!(classes[0].properties.len(), 2);
let count_prop = classes[0]
.properties
.iter()
.find(|p| p.name == "count")
.expect("Should have count property");
assert!(count_prop.is_static, "count should be static");
let label_prop = classes[0]
.properties
.iter()
.find(|p| p.name == "label")
.expect("Should have label property");
assert!(!label_prop.is_static, "label should not be static");
}
#[tokio::test]
async fn test_parse_php_extracts_method_return_type() {
let backend = create_test_backend();
let php = concat!(
"<?php\n",
"class Greeter {\n",
" function greet(string $name): string {}\n",
" function doStuff() {}\n",
"}\n",
);
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
assert_eq!(classes[0].methods.len(), 2);
let greet = &classes[0].methods[0];
assert_eq!(greet.name, "greet");
assert_eq!(
greet.return_type.as_deref(),
Some("string"),
"greet should have return type 'string'"
);
assert_eq!(greet.parameters.len(), 1);
assert_eq!(greet.parameters[0].name, "$name");
assert!(greet.parameters[0].is_required);
assert_eq!(greet.parameters[0].type_hint.as_deref(), Some("string"));
let do_stuff = &classes[0].methods[1];
assert_eq!(do_stuff.name, "doStuff");
assert_eq!(
do_stuff.return_type, None,
"doStuff should have no return type"
);
}
#[tokio::test]
async fn test_parse_php_method_parameter_info() {
let backend = create_test_backend();
let php = concat!(
"<?php\n",
"class Service {\n",
" function process(string $input, int $count, ?string $label = null, ...$extras): bool {}\n",
"}\n",
);
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
let method = &classes[0].methods[0];
assert_eq!(method.name, "process");
assert_eq!(method.parameters.len(), 4);
let input = &method.parameters[0];
assert_eq!(input.name, "$input");
assert!(input.is_required);
assert_eq!(input.type_hint.as_deref(), Some("string"));
assert!(!input.is_variadic);
let count = &method.parameters[1];
assert_eq!(count.name, "$count");
assert!(count.is_required);
assert_eq!(count.type_hint.as_deref(), Some("int"));
let label = &method.parameters[2];
assert_eq!(label.name, "$label");
assert!(
!label.is_required,
"$label has a default value, should not be required"
);
assert_eq!(label.type_hint.as_deref(), Some("?string"));
let extras = &method.parameters[3];
assert_eq!(extras.name, "$extras");
assert!(
!extras.is_required,
"variadic params should not be required"
);
assert!(extras.is_variadic);
}
#[tokio::test]
async fn test_parse_php_property_with_default_value() {
let backend = create_test_backend();
let php = concat!(
"<?php\n",
"class Settings {\n",
" public bool $debug = false;\n",
" public string $title = 'default';\n",
"}\n",
);
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
assert_eq!(classes[0].properties.len(), 2);
let prop_names: Vec<&str> = classes[0]
.properties
.iter()
.map(|p| p.name.as_str())
.collect();
assert!(prop_names.contains(&"debug"));
assert!(prop_names.contains(&"title"));
}
#[tokio::test]
async fn test_parse_php_class_inside_implicit_namespace() {
let backend = create_test_backend();
let php = "<?php\nnamespace Demo;\n\nclass User {\n function login() {}\n function logout() {}\n}\n";
let classes = backend.parse_php(php);
assert_eq!(
classes.len(),
1,
"Should find class inside implicit namespace"
);
assert_eq!(classes[0].name, "User");
assert_eq!(classes[0].methods.len(), 2);
assert_eq!(classes[0].methods[0].name, "login");
assert_eq!(classes[0].methods[1].name, "logout");
}
#[tokio::test]
async fn test_parse_php_class_inside_brace_delimited_namespace() {
let backend = create_test_backend();
let php =
"<?php\nnamespace Demo {\n class Service {\n function handle() {}\n }\n}\n";
let classes = backend.parse_php(php);
assert_eq!(
classes.len(),
1,
"Should find class inside brace-delimited namespace"
);
assert_eq!(classes[0].name, "Service");
assert_eq!(classes[0].methods.len(), 1);
assert_eq!(classes[0].methods[0].name, "handle");
}
#[tokio::test]
async fn test_parse_php_multiple_classes_in_brace_delimited_namespaces() {
let backend = create_test_backend();
let php = concat!(
"<?php\n",
"namespace Foo {\n",
" class A {\n",
" function doA() {}\n",
" }\n",
"}\n",
"namespace Bar {\n",
" class B {\n",
" function doB() {}\n",
" }\n",
"}\n",
);
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 2, "Should find classes in both namespaces");
assert_eq!(classes[0].name, "A");
assert_eq!(classes[0].methods.len(), 1);
assert_eq!(classes[0].methods[0].name, "doA");
assert_eq!(classes[1].name, "B");
assert_eq!(classes[1].methods.len(), 1);
assert_eq!(classes[1].methods[0].name, "doB");
}
#[tokio::test]
async fn test_parse_php_static_method() {
let backend = create_test_backend();
let php = concat!(
"<?php\n",
"class Factory {\n",
" public static function create(string $type): self {}\n",
" public function build(): void {}\n",
"}\n",
);
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
assert_eq!(classes[0].methods.len(), 2);
let create = &classes[0].methods[0];
assert_eq!(create.name, "create");
assert!(create.is_static, "create should be static");
assert_eq!(create.parameters.len(), 1);
assert_eq!(create.parameters[0].name, "$type");
let build = &classes[0].methods[1];
assert_eq!(build.name, "build");
assert!(!build.is_static, "build should not be static");
}
#[tokio::test]
async fn test_parse_php_extracts_constants() {
let backend = create_test_backend();
let php = concat!(
"<?php\n",
"class Config {\n",
" const VERSION = '1.0';\n",
" const int MAX_RETRIES = 3;\n",
" public string $name;\n",
" public function getName(): string {}\n",
"}\n",
);
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
assert_eq!(classes[0].constants.len(), 2);
let version = &classes[0].constants[0];
assert_eq!(version.name, "VERSION");
assert!(version.type_hint.is_none(), "VERSION has no type hint");
let max_retries = &classes[0].constants[1];
assert_eq!(max_retries.name, "MAX_RETRIES");
assert_eq!(
max_retries.type_hint.as_deref(),
Some("int"),
"MAX_RETRIES should have int type hint"
);
}
#[tokio::test]
async fn test_parse_php_extracts_multiple_constants_in_one_declaration() {
let backend = create_test_backend();
let php = concat!(
"<?php\n",
"class Status {\n",
" const ACTIVE = 1, INACTIVE = 0;\n",
"}\n",
);
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
assert_eq!(classes[0].constants.len(), 2);
assert_eq!(classes[0].constants[0].name, "ACTIVE");
assert_eq!(classes[0].constants[1].name, "INACTIVE");
}
#[tokio::test]
async fn test_parse_php_extracts_parent_class() {
let backend = create_test_backend();
let classes = backend.parse_php(concat!(
"<?php\n",
"class Animal {\n",
" public function breathe(): void {}\n",
"}\n",
"class Dog extends Animal {\n",
" public function bark(): void {}\n",
"}\n",
));
assert_eq!(classes.len(), 2);
assert_eq!(classes[0].name, "Animal");
assert!(classes[0].parent_class.is_none());
assert_eq!(classes[1].name, "Dog");
assert_eq!(classes[1].parent_class.as_deref(), Some("Animal"));
}
#[tokio::test]
async fn test_parse_php_extracts_visibility() {
let backend = create_test_backend();
let classes = backend.parse_php(concat!(
"<?php\n",
"class Foo {\n",
" public function pubMethod(): void {}\n",
" protected function protMethod(): void {}\n",
" private function privMethod(): void {}\n",
" function defaultMethod(): void {}\n",
" public string $pubProp;\n",
" protected string $protProp;\n",
" private string $privProp;\n",
" public const PUB_CONST = 1;\n",
" protected const PROT_CONST = 2;\n",
" private const PRIV_CONST = 3;\n",
" const DEFAULT_CONST = 4;\n",
"}\n",
));
assert_eq!(classes.len(), 1);
let cls = &classes[0];
// Methods
let pub_m = cls.methods.iter().find(|m| m.name == "pubMethod").unwrap();
assert_eq!(pub_m.visibility, Visibility::Public);
let prot_m = cls.methods.iter().find(|m| m.name == "protMethod").unwrap();
assert_eq!(prot_m.visibility, Visibility::Protected);
let priv_m = cls.methods.iter().find(|m| m.name == "privMethod").unwrap();
assert_eq!(priv_m.visibility, Visibility::Private);
let def_m = cls
.methods
.iter()
.find(|m| m.name == "defaultMethod")
.unwrap();
assert_eq!(
def_m.visibility,
Visibility::Public,
"No modifier defaults to public"
);
// Properties
let pub_p = cls.properties.iter().find(|p| p.name == "pubProp").unwrap();
assert_eq!(pub_p.visibility, Visibility::Public);
let prot_p = cls
.properties
.iter()
.find(|p| p.name == "protProp")
.unwrap();
assert_eq!(prot_p.visibility, Visibility::Protected);
let priv_p = cls
.properties
.iter()
.find(|p| p.name == "privProp")
.unwrap();
assert_eq!(priv_p.visibility, Visibility::Private);
// Constants
let pub_c = cls
.constants
.iter()
.find(|c| c.name == "PUB_CONST")
.unwrap();
assert_eq!(pub_c.visibility, Visibility::Public);
let prot_c = cls
.constants
.iter()
.find(|c| c.name == "PROT_CONST")
.unwrap();
assert_eq!(prot_c.visibility, Visibility::Protected);
let priv_c = cls
.constants
.iter()
.find(|c| c.name == "PRIV_CONST")
.unwrap();
assert_eq!(priv_c.visibility, Visibility::Private);
let def_c = cls
.constants
.iter()
.find(|c| c.name == "DEFAULT_CONST")
.unwrap();
assert_eq!(
def_c.visibility,
Visibility::Public,
"No modifier defaults to public"
);
}
// ─── Interface Parsing Tests ────────────────────────────────────────────────
#[tokio::test]
async fn test_parse_php_extracts_interface_methods() {
let backend = create_test_backend();
let php = r#"<?php
interface Loggable {
public function log(string $message): void;
public function getLogLevel(): int;
}
"#;
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
assert_eq!(classes[0].name, "Loggable");
assert_eq!(classes[0].methods.len(), 2);
assert_eq!(classes[0].methods[0].name, "log");
assert_eq!(classes[0].methods[0].return_type.as_deref(), Some("void"));
assert_eq!(classes[0].methods[1].name, "getLogLevel");
assert_eq!(classes[0].methods[1].return_type.as_deref(), Some("int"));
}
#[tokio::test]
async fn test_parse_php_extracts_interface_constants() {
let backend = create_test_backend();
let php = r#"<?php
interface HasStatus {
const STATUS_ACTIVE = 1;
const STATUS_INACTIVE = 0;
public function getStatus(): int;
}
"#;
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
assert_eq!(classes[0].name, "HasStatus");
assert_eq!(classes[0].constants.len(), 2);
assert_eq!(classes[0].constants[0].name, "STATUS_ACTIVE");
assert_eq!(classes[0].constants[1].name, "STATUS_INACTIVE");
assert_eq!(classes[0].methods.len(), 1);
assert_eq!(classes[0].methods[0].name, "getStatus");
}
#[tokio::test]
async fn test_parse_php_interface_extends() {
let backend = create_test_backend();
let php = r#"<?php
interface Readable {
public function read(): string;
}
interface Writable extends Readable {
public function write(string $data): void;
}
"#;
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 2);
let readable = classes.iter().find(|c| c.name == "Readable").unwrap();
assert!(readable.parent_class.is_none());
assert_eq!(readable.methods.len(), 1);
let writable = classes.iter().find(|c| c.name == "Writable").unwrap();
assert_eq!(writable.parent_class.as_deref(), Some("Readable"));
assert_eq!(writable.methods.len(), 1);
assert_eq!(writable.methods[0].name, "write");
}
#[tokio::test]
async fn test_parse_php_interface_inside_namespace() {
let backend = create_test_backend();
let php = r#"<?php
namespace App\Contracts;
interface Repository {
public function find(int $id): mixed;
public function save(object $entity): void;
}
"#;
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
assert_eq!(classes[0].name, "Repository");
assert_eq!(classes[0].methods.len(), 2);
assert_eq!(classes[0].methods[0].name, "find");
assert_eq!(classes[0].methods[1].name, "save");
}
#[tokio::test]
async fn test_parse_php_class_and_interface_together() {
let backend = create_test_backend();
let php = r#"<?php
interface Cacheable {
public function getCacheKey(): string;
const TTL = 3600;
}
class UserRepository implements Cacheable {
public function getCacheKey(): string { return 'users'; }
public function findAll(): array { return []; }
}
"#;
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 2);
let iface = classes.iter().find(|c| c.name == "Cacheable").unwrap();
assert_eq!(iface.methods.len(), 1);
assert_eq!(iface.constants.len(), 1);
assert_eq!(iface.constants[0].name, "TTL");
let class = classes.iter().find(|c| c.name == "UserRepository").unwrap();
assert_eq!(class.methods.len(), 2);
}
#[tokio::test]
async fn test_parse_php_interface_static_method() {
let backend = create_test_backend();
let php = r#"<?php
interface Factory {
public static function create(): static;
public function build(): object;
}
"#;
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
assert_eq!(classes[0].name, "Factory");
assert_eq!(classes[0].methods.len(), 2);
let create = classes[0]
.methods
.iter()
.find(|m| m.name == "create")
.unwrap();
assert!(create.is_static);
assert_eq!(create.return_type.as_deref(), Some("static"));
let build = classes[0]
.methods
.iter()
.find(|m| m.name == "build")
.unwrap();
assert!(!build.is_static);
}
// ─── Promoted Property Tests ────────────────────────────────────────────────
#[tokio::test]
async fn test_parse_php_promoted_properties_basic() {
let backend = create_test_backend();
let php = r#"<?php
class Service {
public function __construct(
private IShoppingCart $cart,
protected Logger $logger,
) {}
}
"#;
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
let cls = &classes[0];
assert_eq!(
cls.properties.len(),
2,
"Should extract 2 promoted properties"
);
let cart = cls.properties.iter().find(|p| p.name == "cart").unwrap();
assert_eq!(cart.type_hint.as_deref(), Some("IShoppingCart"));
assert_eq!(cart.visibility, Visibility::Private);
assert!(!cart.is_static);
let logger = cls.properties.iter().find(|p| p.name == "logger").unwrap();
assert_eq!(logger.type_hint.as_deref(), Some("Logger"));
assert_eq!(logger.visibility, Visibility::Protected);
assert!(!logger.is_static);
}
#[tokio::test]
async fn test_parse_php_promoted_properties_mixed_with_regular() {
let backend = create_test_backend();
let php = r#"<?php
class ShoppingCartService {
private IShoppingCart $regular;
public function __construct(
private IShoppingCart $promoted,
) {}
}
"#;
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
let cls = &classes[0];
assert_eq!(
cls.properties.len(),
2,
"Should have regular + promoted property"
);
let regular = cls.properties.iter().find(|p| p.name == "regular").unwrap();
assert_eq!(regular.type_hint.as_deref(), Some("IShoppingCart"));
assert_eq!(regular.visibility, Visibility::Private);
let promoted = cls
.properties
.iter()
.find(|p| p.name == "promoted")
.unwrap();
assert_eq!(promoted.type_hint.as_deref(), Some("IShoppingCart"));
assert_eq!(promoted.visibility, Visibility::Private);
}
#[tokio::test]
async fn test_parse_php_promoted_property_public_visibility() {
let backend = create_test_backend();
let php = r#"<?php
class Config {
public function __construct(
public string $name,
public int $value,
) {}
}
"#;
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
let cls = &classes[0];
assert_eq!(cls.properties.len(), 2);
for prop in &cls.properties {
assert_eq!(prop.visibility, Visibility::Public);
}
let name = cls.properties.iter().find(|p| p.name == "name").unwrap();
assert_eq!(name.type_hint.as_deref(), Some("string"));
let value = cls.properties.iter().find(|p| p.name == "value").unwrap();
assert_eq!(value.type_hint.as_deref(), Some("int"));
}
#[tokio::test]
async fn test_parse_php_non_promoted_constructor_params_ignored() {
let backend = create_test_backend();
let php = r#"<?php
class Service {
public function __construct(
private string $promoted,
string $regularParam,
) {}
}
"#;
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
let cls = &classes[0];
assert_eq!(
cls.properties.len(),
1,
"Only promoted params (with visibility) should become properties"
);
assert_eq!(cls.properties[0].name, "promoted");
}
#[tokio::test]
async fn test_parse_php_promoted_property_readonly() {
let backend = create_test_backend();
let php = r#"<?php
class User {
public function __construct(
public readonly string $name,
private readonly int $id,
) {}
}
"#;
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
let cls = &classes[0];
assert_eq!(
cls.properties.len(),
2,
"readonly promoted params are still promoted"
);
let name = cls.properties.iter().find(|p| p.name == "name").unwrap();
assert_eq!(name.visibility, Visibility::Public);
assert_eq!(name.type_hint.as_deref(), Some("string"));
let id = cls.properties.iter().find(|p| p.name == "id").unwrap();
assert_eq!(id.visibility, Visibility::Private);
assert_eq!(id.type_hint.as_deref(), Some("int"));
}
// ─── Promoted Property @param Override Tests ────────────────────────────────
/// When a constructor docblock has `@param list<User> $users` and the native
/// hint is `array`, the promoted property should get `list<User>` as its type.
#[tokio::test]
async fn test_parse_promoted_property_param_docblock_override() {
let backend = create_test_backend();
let php = r#"<?php
class UserService {
/**
* @param list<User> $users
* @param string $name
*/
public function __construct(
public array $users,
public string $name,
) {}
}
"#;
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
let cls = &classes[0];
let users = cls.properties.iter().find(|p| p.name == "users").unwrap();
assert_eq!(
users.type_hint.as_deref(),
Some("list<User>"),
"@param list<User> should override native `array` for promoted property"
);
// `string` is a scalar — @param string should NOT override to a class name.
// Both native and docblock agree, so the result stays `string`.
let name = cls.properties.iter().find(|p| p.name == "name").unwrap();
assert_eq!(
name.type_hint.as_deref(),
Some("string"),
"Scalar @param string should keep native `string`"
);
}
/// When the docblock provides a class type but the native hint is also a class,
/// the docblock should win (more specific).
#[tokio::test]
async fn test_parse_promoted_property_param_class_override() {
let backend = create_test_backend();
let php = r#"<?php
class Repository {
/**
* @param UserCollection $items
*/
public function __construct(
public object $items,
) {}
}
"#;
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
let cls = &classes[0];
let items = cls.properties.iter().find(|p| p.name == "items").unwrap();
assert_eq!(
items.type_hint.as_deref(),
Some("UserCollection"),
"@param UserCollection should override native `object` for promoted property"
);
}
/// Without a docblock, promoted property should keep its native type as before.
#[tokio::test]
async fn test_parse_promoted_property_no_docblock_unchanged() {
let backend = create_test_backend();
let php = r#"<?php
class Service {
public function __construct(
public array $items,
private string $name,
) {}
}
"#;
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
let cls = &classes[0];
let items = cls.properties.iter().find(|p| p.name == "items").unwrap();
assert_eq!(items.type_hint.as_deref(), Some("array"));
let name = cls.properties.iter().find(|p| p.name == "name").unwrap();
assert_eq!(name.type_hint.as_deref(), Some("string"));
}
/// When the docblock has a `@param` for a non-promoted parameter, it should
/// not affect promoted properties that don't have their own `@param`.
#[tokio::test]
async fn test_parse_promoted_property_param_only_matching() {
let backend = create_test_backend();
let php = r#"<?php
class Service {
/**
* @param LoggerInterface $logger
*/
public function __construct(
public LoggerInterface $logger,
public array $data,
) {}
}
"#;
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
let cls = &classes[0];
// $logger has matching @param — both agree on LoggerInterface
let logger = cls.properties.iter().find(|p| p.name == "logger").unwrap();
assert_eq!(logger.type_hint.as_deref(), Some("LoggerInterface"));
// $data has no @param — should keep native `array`
let data = cls.properties.iter().find(|p| p.name == "data").unwrap();
assert_eq!(data.type_hint.as_deref(), Some("array"));
}
/// When a native hint is `int` (scalar) and @param says `UserId` (class),
/// `resolve_effective_type` should keep the native `int` because scalar
/// should not be overridden by a class name.
#[tokio::test]
async fn test_parse_promoted_property_param_scalar_not_overridden_by_class() {
let backend = create_test_backend();
let php = r#"<?php
class Service {
/**
* @param UserId $id
*/
public function __construct(
public int $id,
) {}
}
"#;
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
let cls = &classes[0];
let id = cls.properties.iter().find(|p| p.name == "id").unwrap();
assert_eq!(
id.type_hint.as_deref(),
Some("int"),
"Native scalar `int` should not be overridden by docblock class `UserId`"
);
}
/// Generic Collection type in @param should override a plain `object` native hint.
#[tokio::test]
async fn test_parse_promoted_property_param_generic_override() {
let backend = create_test_backend();
let php = r#"<?php
class OrderService {
/**
* @param Collection<int, Order> $orders
* @param array<string, mixed> $config
*/
public function __construct(
public object $orders,
public array $config,
) {}
}
"#;
let classes = backend.parse_php(php);
assert_eq!(classes.len(), 1);
let cls = &classes[0];
let orders = cls.properties.iter().find(|p| p.name == "orders").unwrap();
assert_eq!(
orders.type_hint.as_deref(),
Some("Collection<int, Order>"),
"@param Collection<int, Order> should override native `object`"
);
// array<string, mixed> — although the base is `array` (scalar), the
// generic parameters carry useful type info for destructuring and
// foreach, so resolve_effective_type now keeps the docblock type.
let config = cls.properties.iter().find(|p| p.name == "config").unwrap();
assert_eq!(
config.type_hint.as_deref(),
Some("array<string, mixed>"),
"Docblock `array<string, mixed>` should override native `array` (generic params preserved)"
);
}
// ─── Standalone Function Parsing Tests ──────────────────────────────────────
#[tokio::test]
async fn test_parse_functions_standalone() {
let backend = create_test_backend();
let php = concat!(
"<?php\n",
"function hello(): void {}\n",
"function add(int $a, int $b): int { return $a + $b; }\n",
);
let functions = backend.parse_functions(php);
assert_eq!(functions.len(), 2, "Should extract 2 standalone functions");
let hello = functions.iter().find(|f| f.name == "hello").unwrap();
assert!(hello.parameters.is_empty());
assert_eq!(hello.return_type.as_deref(), Some("void"));
assert!(hello.namespace.is_none());
let add = functions.iter().find(|f| f.name == "add").unwrap();
assert_eq!(add.parameters.len(), 2);
assert_eq!(add.parameters[0].name, "$a");
assert_eq!(add.parameters[0].type_hint.as_deref(), Some("int"));