mago-semantics 1.20.1

PHP Semantics Checker.
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
use mago_php_version::feature::Feature;
use mago_reporting::Annotation;
use mago_reporting::Issue;
use mago_span::HasSpan;
use mago_span::Span;
use mago_syntax::ast::Modifier;
use mago_syntax::ast::Property;
use mago_syntax::ast::PropertyHookBody;
use mago_syntax::ast::PropertyItem;

use crate::internal::checker::function_like::check_for_promoted_properties_outside_constructor;
use crate::internal::context::Context;

#[inline]
#[allow(clippy::too_many_arguments)]
pub fn check_property(
    property: &Property,
    class_like_span: Span,
    class_like_kind: &str,
    class_like_name: &str,
    class_like_fqcn: &str,
    class_like_is_interface: bool,
    class_like_is_abstract: bool,
    class_like_is_readonly: bool,
    context: &mut Context<'_, '_, '_>,
) {
    let first_variable = property.first_variable();
    let first_variable_name = first_variable.name;

    let modifiers = property.modifiers();
    let mut last_final: Option<Span> = None;
    let mut last_static: Option<Span> = None;
    let mut last_readonly: Option<Span> = None;
    let mut last_abstract: Option<Span> = None;
    let mut last_private: Option<Span> = None;
    let mut last_read_visibility: Option<Span> = None;
    let mut last_write_visibility: Option<Span> = None;

    for modifier in modifiers {
        match modifier {
            Modifier::Abstract(_) => {
                let is_hooked_property = matches!(property, Property::Hooked(_));
                let class_like_is_trait = class_like_kind == "trait";
                let class_allows_abstract = class_like_is_abstract || class_like_is_interface || class_like_is_trait;

                if !is_hooked_property || !class_allows_abstract {
                    context.report(
                        Issue::error(format!(
                            "Property `{class_like_name}::{first_variable_name}` cannot be declared abstract"
                        ))
                        .with_annotation(
                            Annotation::primary(modifier.span())
                                .with_message("`abstract` modifier cannot be used on properties"),
                        )
                        .with_annotation(
                            Annotation::secondary(first_variable.span())
                                .with_message(format!("Property `{first_variable_name}` declared here.")),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                        ),
                    );
                }

                last_abstract = Some(modifier.span());
            }
            Modifier::Static(_) => {
                if let Some(last_readonly) = last_readonly {
                    context.report(
                        Issue::error(format!(
                            "Readonly property `{class_like_name}::{first_variable_name}` cannot be static."
                        ))
                        .with_annotation(
                            Annotation::primary(modifier.span())
                                .with_message("`static` modifier cannot be used on readonly properties."),
                        )
                        .with_annotation(
                            Annotation::primary(last_readonly).with_message("Property is marked as readonly here."),
                        )
                        .with_annotation(
                            Annotation::secondary(first_variable.span())
                                .with_message(format!("Property `{first_variable_name}` declared here.")),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                        ),
                    );
                }

                if let Some(last_static) = last_static {
                    context.report(
                        Issue::error(format!(
                            "Property `{class_like_name}::{first_variable_name}` has multiple `static` modifiers."
                        ))
                        .with_annotation(
                            Annotation::primary(modifier.span()).with_message("Duplicate `static` modifier."),
                        )
                        .with_annotation(Annotation::secondary(last_static).with_message("Previous `static` modifier."))
                        .with_annotation(
                            Annotation::secondary(first_variable.span())
                                .with_message(format!("Property `{first_variable_name}` declared here.")),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                        ),
                    );
                }

                if let Some(last_visibility) = last_write_visibility
                    && !context.version.is_supported(Feature::AsymmetricVisibilityForStaticProperties)
                {
                    context.report(
                            Issue::error(format!(
                                "Asymmetric visibility for static property `{class_like_name}::{first_variable_name}` is not available in your current PHP version, this feature was introduced in PHP 8.5.",
                            ))
                            .with_annotation(
                                Annotation::primary(last_visibility).with_message("This write visibility modifier is used here"),
                            )
                            .with_annotation(
                                Annotation::secondary(modifier.span()).with_message("On this static property"),
                            )
                            .with_annotation(
                                Annotation::secondary(first_variable.span()).with_message(format!("Static property `{first_variable_name}`")),
                            )
                            .with_note(
                                "PHP 8.4 introduced asymmetric visibility for properties, and PHP 8.5 extended this support to static properties."
                            )
                            .with_help(
                                "To use this feature, please configure your project for PHP 8.5+. Alternatively, remove the specific write visibility from this static property or make the property non-static."
                            ),
                        );
                }

                last_static = Some(modifier.span());
            }
            Modifier::Readonly(modifier) => {
                if !context.version.is_supported(Feature::ReadonlyProperties) {
                    context.report(
                        Issue::error("Readonly properties are only available in PHP 8.1 and above.").with_annotation(
                            Annotation::primary(modifier.span()).with_message("Readonly modifier used here."),
                        ),
                    );

                    continue;
                }

                if let Some(last_static) = last_static {
                    context.report(
                        Issue::error(format!(
                            "Static property `{class_like_name}::{first_variable_name}` cannot be readonly."
                        ))
                        .with_annotation(
                            Annotation::primary(modifier.span())
                                .with_message("`readonly` modifier cannot be used on static properties."),
                        )
                        .with_annotation(
                            Annotation::primary(last_static).with_message("Property is marked as static here."),
                        )
                        .with_annotation(
                            Annotation::secondary(first_variable.span())
                                .with_message(format!("Property `{first_variable_name}` declared here.")),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                        ),
                    );
                }

                if let Some(last_readonly) = last_readonly {
                    context.report(
                        Issue::error(format!(
                            "Property `{class_like_name}::{first_variable_name}` has multiple `readonly` modifiers."
                        ))
                        .with_annotation(
                            Annotation::primary(modifier.span()).with_message("Duplicate `readonly` modifier."),
                        )
                        .with_annotation(
                            Annotation::secondary(last_readonly).with_message("Previous `readonly` modifier."),
                        )
                        .with_annotation(
                            Annotation::secondary(first_variable.span())
                                .with_message(format!("Property `{first_variable_name}` declared here.")),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                        ),
                    );
                }

                last_readonly = Some(modifier.span());
            }
            Modifier::Final(_) => {
                if let Some(last_final) = last_final {
                    context.report(
                        Issue::error("Property has multiple `final` modifiers.")
                            .with_annotation(
                                Annotation::primary(modifier.span()).with_message("Duplicate `final` modifier."),
                            )
                            .with_annotation(Annotation::primary(last_final).with_message("Previous `final` modifier."))
                            .with_annotation(
                                Annotation::secondary(first_variable.span())
                                    .with_message(format!("Property `{first_variable_name}` declared here.")),
                            )
                            .with_annotation(
                                Annotation::secondary(class_like_span)
                                    .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                            ),
                    );
                }

                last_final = Some(modifier.span());
            }
            Modifier::Private(_) | Modifier::Protected(_) | Modifier::Public(_) => {
                if let Some(last_visibility) = last_read_visibility {
                    context.report(
                        Issue::error(format!(
                            "Property `{class_like_name}::{first_variable_name}` has multiple visibility modifiers."
                        ))
                        .with_annotation(
                            Annotation::primary(modifier.span()).with_message("Duplicate visibility modifier."),
                        )
                        .with_annotation(
                            Annotation::primary(last_visibility).with_message("Previous visibility modifier."),
                        )
                        .with_annotation(
                            Annotation::secondary(first_variable.span())
                                .with_message(format!("Property `{first_variable_name}` declared here.")),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                        ),
                    );
                }

                if matches!(modifier, Modifier::Private(_)) {
                    last_private = Some(modifier.span());
                }

                last_read_visibility = Some(modifier.span());
            }
            Modifier::PrivateSet(_) | Modifier::ProtectedSet(_) | Modifier::PublicSet(_) => {
                if !context.version.is_supported(Feature::AsymmetricVisibility) {
                    context.report(
                        Issue::error("Asymmetric visibility is only available in PHP 8.4 and above.").with_annotation(
                            Annotation::primary(modifier.span())
                                .with_message("Asymmetric visibility modifier used here."),
                        ),
                    );

                    continue;
                }

                if let Some(last_visibility) = last_write_visibility {
                    context.report(
                        Issue::error(format!(
                            "Property `{class_like_name}::{first_variable_name}` has multiple write visibility modifiers."
                        ))
                        .with_annotation(
                            Annotation::primary(modifier.span()).with_message("Duplicate write visibility modifier."),
                        )
                        .with_annotation(
                            Annotation::primary(last_visibility).with_message("Previous write visibility modifier."),
                        )
                        .with_annotation(
                            Annotation::secondary(first_variable.span())
                                .with_message(format!("Property `{first_variable_name}` declared here.")),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                        ),
                    );
                }

                if let Some(last_static) = last_static
                    && !context.version.is_supported(Feature::AsymmetricVisibilityForStaticProperties)
                {
                    context.report(
                            Issue::error(format!(
                                "Asymmetric visibility for static property `{class_like_name}::{first_variable_name}` is not available in your current PHP version, this feature was introduced in PHP 8.5.",
                            ))
                            .with_annotation(
                                Annotation::primary(modifier.span()).with_message("This write visibility modifier is used here"),
                            )
                            .with_annotation(
                                Annotation::secondary(last_static).with_message("On this static property"),
                            )
                            .with_annotation(
                                Annotation::secondary(first_variable.span()).with_message(format!("Static property `{first_variable_name}`")),
                            )
                            .with_note(
                                "PHP 8.4 introduced asymmetric visibility for properties, and PHP 8.5 extended this support to static properties."
                            )
                            .with_help(
                                "To use this feature, please configure your project for PHP 8.5+. Alternatively, remove the specific write visibility from this static property or make the property non-static."
                            ),
                        );
                }

                last_write_visibility = Some(modifier.span());
            }
        }
    }

    if let (Some(abstract_span), Some(private_span)) = (last_abstract, last_private) {
        context.report(
            Issue::error(format!(
                "Property `{class_like_name}::{first_variable_name}` cannot be both abstract and private."
            ))
            .with_annotation(Annotation::primary(abstract_span).with_message("`abstract` modifier used here."))
            .with_annotation(Annotation::primary(private_span).with_message("`private` modifier used here."))
            .with_annotation(
                Annotation::secondary(first_variable.span())
                    .with_message(format!("Property `{first_variable_name}` declared here.")),
            )
            .with_annotation(
                Annotation::secondary(class_like_span)
                    .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
            )
            .with_help("Abstract properties must be visible to subclasses. Use `protected` or `public` instead."),
        );
    }

    if let (Some(abstract_span), Some(final_span)) = (last_abstract, last_final) {
        context.report(
            Issue::error(format!(
                "Property `{class_like_name}::{first_variable_name}` cannot be both abstract and final."
            ))
            .with_annotation(Annotation::primary(abstract_span).with_message("`abstract` modifier used here."))
            .with_annotation(Annotation::primary(final_span).with_message("`final` modifier used here."))
            .with_annotation(
                Annotation::secondary(first_variable.span())
                    .with_message(format!("Property `{first_variable_name}` declared here.")),
            )
            .with_annotation(
                Annotation::secondary(class_like_span)
                    .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
            ),
        );
    }

    if let (Some(final_span), Some(private_span)) = (last_final, last_private) {
        context.report(
            Issue::error(format!(
                "Property `{class_like_name}::{first_variable_name}` cannot be both final and private."
            ))
            .with_annotation(Annotation::primary(final_span).with_message("`final` modifier used here."))
            .with_annotation(Annotation::primary(private_span).with_message("`private` modifier used here."))
            .with_annotation(
                Annotation::secondary(first_variable.span())
                    .with_message(format!("Property `{first_variable_name}` declared here.")),
            )
            .with_annotation(
                Annotation::secondary(class_like_span)
                    .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
            )
            .with_help("Private properties cannot be overridden, so `final` is meaningless."),
        );
    }

    if class_like_is_interface && let Some(final_span) = last_final {
        context.report(
            Issue::error(format!("Property `{class_like_name}::{first_variable_name}` in interface cannot be final."))
                .with_annotation(Annotation::primary(final_span).with_message("`final` modifier used here."))
                .with_annotation(
                    Annotation::secondary(first_variable.span())
                        .with_message(format!("Property `{first_variable_name}` declared here.")),
                )
                .with_annotation(
                    Annotation::secondary(class_like_span)
                        .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                ),
        );
    }

    if let Some(var) = property.var()
        && !modifiers.is_empty()
    {
        let first = modifiers.first().unwrap();
        let last = modifiers.last().unwrap();

        context.report(
            Issue::error(format!("Var property `{class_like_name}::{first_variable_name}` cannot have modifiers."))
                .with_annotation(
                    Annotation::primary(first.span().join(last.span())).with_message("Modifiers used here."),
                )
                .with_annotation(Annotation::primary(var.span()).with_message("Property is marked as `var` here."))
                .with_annotation(
                    Annotation::secondary(first_variable.span())
                        .with_message(format!("Property `{first_variable_name}` declared here.")),
                )
                .with_annotation(
                    Annotation::secondary(class_like_span)
                        .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                )
                .with_help("Remove either the `var` keyword, or the modifiers.".to_string()),
        );
    }

    if let Some(hint) = property.hint() {
        if !context.version.is_supported(Feature::TypedProperties) {
            context.report(
                Issue::error("Typed properties are only available in PHP 7.4 and above.")
                    .with_annotation(Annotation::primary(hint.span()).with_message("Type hint used here."))
                    .with_help("Remove the type hint to make the code compatible with PHP 7.3 and earlier versions, or upgrade to PHP 7.4 or later."),
            );
        }

        if !context.version.is_supported(Feature::NativeUnionTypes) && hint.is_union() {
            context.report(
                Issue::error(
                    "Union type hints (e.g. `int|float`) are only available in PHP 8.0 and above.",
                )
                .with_annotation(Annotation::primary(hint.span()).with_message("Union type hint used here."))
                .with_note(
                    "Union type hints are only available in PHP 8.0 and above. Consider using a different approach.",
                ),
            );
        }

        if hint.is_bottom() {
            let hint_name = context.get_code_snippet(hint);
            context.report(
                Issue::error(format!(
                    "Property `{class_like_name}::{first_variable_name}` cannot have type `{hint_name}`."
                ))
                .with_annotation(
                    Annotation::primary(hint.span())
                        .with_message(format!("Type `{hint_name}` is not allowed on properties.")),
                )
                .with_annotation(
                    Annotation::secondary(first_variable.span())
                        .with_message(format!("Property `{first_variable_name}` declared here.")),
                )
                .with_annotation(
                    Annotation::secondary(class_like_span)
                        .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                ),
            );
        }
    } else if let Some(readonly) = last_readonly {
        context.report(
            Issue::error(format!(
                "Readonly property `{class_like_name}::{first_variable_name}` must have a type hint."
            ))
            .with_annotation(Annotation::primary(readonly).with_message("Property is marked as readonly here."))
            .with_annotation(
                Annotation::secondary(first_variable.span())
                    .with_message(format!("Property `{first_variable_name}` declared here.")),
            )
            .with_annotation(
                Annotation::secondary(class_like_span)
                    .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
            ),
        );
    }

    match &property {
        Property::Plain(plain_property) => {
            if !context.version.is_supported(Feature::AsymmetricVisibility)
                && let Some(write_visibility) = plain_property.modifiers.get_first_write_visibility()
            {
                context.report(
                    Issue::error("Asymmetric visibility is only available in PHP 8.4 and above.").with_annotation(
                        Annotation::primary(write_visibility.span()).with_message("Asymmetric visibility used here."),
                    ),
                );
            }

            for item in &plain_property.items {
                if let PropertyItem::Concrete(property_concrete_item) = &item {
                    let item_name = property_concrete_item.variable.name;

                    if !property_concrete_item.value.is_constant(&context.version, false) {
                        context.report(
                            Issue::error(format!(
                                "Property `{class_like_name}::{item_name}` value contains a non-constant expression."
                            ))
                            .with_annotation(
                                Annotation::primary(property_concrete_item.value.span())
                                    .with_message("This is a non-constant expression."),
                            )
                            .with_annotation(
                                Annotation::secondary(property_concrete_item.variable.span())
                                    .with_message(format!("Property `{item_name}` declared here.")),
                            )
                            .with_annotation(
                                Annotation::secondary(class_like_span)
                                    .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                            ),
                        );
                    }

                    if let Some(readonly) = last_readonly {
                        context.report(
                            Issue::error(format!(
                                "Readonly property `{class_like_name}::{item_name}` cannot have a default value."
                            ))
                            .with_annotation(
                                Annotation::primary(property_concrete_item.value.span())
                                    .with_message("This is a default value."),
                            )
                            .with_annotation(Annotation::primary(readonly).with_message(format!(
                                "Property `{class_like_name}::{item_name}` is marked as readonly here."
                            )))
                            .with_annotation(
                                Annotation::secondary(property_concrete_item.variable.span())
                                    .with_message(format!("Property `{item_name}` is declared here.")),
                            )
                            .with_annotation(
                                Annotation::secondary(class_like_span)
                                    .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                            ),
                        );
                    }
                }
            }
        }
        Property::Hooked(hooked_property) => {
            if !context.version.is_supported(Feature::PropertyHooks) {
                let issue = Issue::error("Hooked properties are only available in PHP 8.4 and above.").with_annotation(
                    Annotation::primary(hooked_property.span()).with_message("Hooked property declaration used here."),
                );

                context.report(issue);
            }

            let item_name = hooked_property.item.variable().name;

            if let Some(readonly) = last_readonly {
                context.report(
                    Issue::error(format!("Hooked property `{class_like_name}::{item_name}` cannot be readonly."))
                        .with_annotation(Annotation::primary(readonly).with_message(format!(
                            "Property `{class_like_name}::{item_name}` is marked as readonly here."
                        )))
                        .with_annotation(
                            Annotation::secondary(hooked_property.hook_list.span())
                                .with_message("Property hooks are defined here."),
                        )
                        .with_annotation(
                            Annotation::secondary(hooked_property.item.variable().span())
                                .with_message(format!("Property `{item_name}` is declared here.")),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                        ),
                );
            } else if class_like_is_readonly {
                context.report(
                    Issue::error(format!("Hooked property `{class_like_name}::{item_name}` cannot be readonly."))
                        .with_annotation(
                            Annotation::primary(hooked_property.hook_list.span())
                                .with_message("Property hooks are defined here."),
                        )
                        .with_annotation(
                            Annotation::secondary(hooked_property.item.variable().span())
                                .with_message(format!("Property `{item_name}` is declared here.")),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` is readonly, making all properties implicitly readonly.")),
                        )
                        .with_note("Hooked properties cannot be readonly, but properties in readonly classes are implicitly readonly."),
                );
            }

            if let Some(r#static) = last_static {
                context.report(
                    Issue::error(format!("Hooked property `{class_like_name}::{item_name}` cannot be static."))
                        .with_annotation(Annotation::primary(r#static).with_message(format!(
                            "Property `{class_like_name}::{item_name}` is marked as static here."
                        )))
                        .with_annotation(
                            Annotation::secondary(hooked_property.hook_list.span())
                                .with_message("Property hooks are defined here."),
                        )
                        .with_annotation(
                            Annotation::secondary(hooked_property.item.variable().span())
                                .with_message(format!("Property `{item_name}` is declared here.")),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                        ),
                );
            }

            if let Some(abstract_span) = last_abstract {
                let has_abstract_hook = hooked_property
                    .hook_list
                    .hooks
                    .iter()
                    .any(|hook| matches!(hook.body, PropertyHookBody::Abstract(_)));

                if !has_abstract_hook {
                    context.report(
                        Issue::error(format!(
                            "Abstract property `{class_like_name}::{item_name}` must specify at least one abstract hook."
                        ))
                        .with_annotation(
                            Annotation::primary(abstract_span).with_message("`abstract` modifier used here."),
                        )
                        .with_annotation(
                            Annotation::secondary(hooked_property.hook_list.span())
                                .with_message("All hooks have concrete bodies."),
                        )
                        .with_annotation(
                            Annotation::secondary(hooked_property.item.variable().span())
                                .with_message(format!("Property `{item_name}` is declared here.")),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                        )
                        .with_help("Remove the `abstract` modifier or replace hook bodies with semicolons."),
                    );
                }
            }

            // Check: empty hook list is not allowed
            if hooked_property.hook_list.hooks.is_empty() {
                context.report(
                    Issue::error(format!("Property `{class_like_name}::{item_name}` hook list must not be empty."))
                        .with_annotation(
                            Annotation::primary(hooked_property.hook_list.span()).with_message("Empty hook list here."),
                        )
                        .with_annotation(
                            Annotation::secondary(hooked_property.item.variable().span())
                                .with_message(format!("Property `{item_name}` is declared here.")),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                        )
                        .with_help(
                            "Add at least one hook (`get` or `set`) to the property, or remove the hook braces.",
                        ),
                );
            }

            if hooked_property.hint.is_none()
                && let PropertyItem::Concrete(concrete_item) = &hooked_property.item
            {
                let has_concrete_get = hooked_property.hook_list.hooks.iter().any(|hook| {
                    hook.name.value.eq_ignore_ascii_case("get") && !matches!(hook.body, PropertyHookBody::Abstract(_))
                });
                let has_concrete_set = hooked_property.hook_list.hooks.iter().any(|hook| {
                    hook.name.value.eq_ignore_ascii_case("set") && !matches!(hook.body, PropertyHookBody::Abstract(_))
                });

                if has_concrete_get && has_concrete_set {
                    context.report(
                        Issue::error(format!(
                            "Cannot specify default value for virtual hooked property `{class_like_name}::{item_name}`."
                        ))
                        .with_annotation(
                            Annotation::primary(concrete_item.value.span())
                                .with_message("Default value specified here."),
                        )
                        .with_annotation(
                            Annotation::secondary(hooked_property.hook_list.span())
                                .with_message("Property has both get and set hooks with concrete implementations."),
                        )
                        .with_annotation(
                            Annotation::secondary(concrete_item.variable.span())
                                .with_message(format!("Property `{item_name}` has no type hint, making it virtual.")),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                        )
                        .with_note("A virtual property has no backing store, so a default value cannot be stored.")
                        .with_help("Remove the default value, or add a type hint to make the property backed."),
                    );
                }
            }

            let mut hook_names: Vec<(std::string::String, Span)> = vec![];
            for hook in &hooked_property.hook_list.hooks {
                let name = hook.name.value;
                let lowered_name = name.to_ascii_lowercase();

                let invalid_modifiers: Vec<_> =
                    hook.modifiers.iter().filter(|m| !matches!(m, Modifier::Final(_))).collect();

                if !invalid_modifiers.is_empty() {
                    let first = invalid_modifiers.first().unwrap();
                    let last = invalid_modifiers.last().unwrap();

                    context.report(
                        Issue::error(format!(
                            "Hook `{name}` for property `{class_like_name}::{item_name}` can only have `final` modifier."
                        ))
                        .with_annotation(
                            Annotation::primary(first.span().join(last.span())).with_message("Invalid hook modifier here."),
                        )
                        .with_annotation(
                            Annotation::secondary(hooked_property.item.variable().span())
                                .with_message(format!("Property `{item_name}` is declared here.")),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                        ),
                    );
                }

                if class_like_is_interface
                    && let Some(final_modifier) = hook.modifiers.iter().find(|m| matches!(m, Modifier::Final(_)))
                {
                    context.report(
                            Issue::error(format!(
                                "Hook `{name}` for property `{class_like_name}::{item_name}` cannot be both abstract and final."
                            ))
                            .with_annotation(
                                Annotation::primary(final_modifier.span()).with_message("`final` modifier used here."),
                            )
                            .with_annotation(
                                Annotation::secondary(hook.name.span())
                                    .with_message(format!("Hook `{name}` is declared here.")),
                            )
                            .with_annotation(
                                Annotation::secondary(hooked_property.item.variable().span())
                                    .with_message(format!("Property `{item_name}` is declared here.")),
                            )
                            .with_annotation(
                                Annotation::secondary(class_like_span)
                                    .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                            )
                            .with_note("Interface property hooks are implicitly abstract."),
                        );
                }

                let property_is_abstract = modifiers.contains_abstract();
                if !class_like_is_interface
                    && !property_is_abstract
                    && let PropertyHookBody::Abstract(property_hook_abstract_body) = &hook.body
                {
                    context.report(
                        Issue::error(format!("Non-abstract property hook `{name}` must have a body."))
                            .with_annotation(
                                Annotation::primary(property_hook_abstract_body.span())
                                    .with_message("Abstract hook body here."),
                            )
                            .with_annotation(
                                Annotation::secondary(hook.name.span())
                                    .with_message(format!("Hook `{name}` is declared here.")),
                            )
                            .with_annotation(
                                Annotation::secondary(hooked_property.item.variable().span())
                                    .with_message(format!("Property `{item_name}` is declared here.")),
                            )
                            .with_annotation(
                                Annotation::secondary(class_like_span)
                                    .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                            ),
                    );
                }

                if let Some(parameter_list) = &hook.parameter_list {
                    check_for_promoted_properties_outside_constructor(parameter_list, context);

                    match lowered_name.as_str() {
                        "set" => {
                            if parameter_list.parameters.len() == 1 {
                                let first_parameter = parameter_list.parameters.first().unwrap();
                                let first_parameter_name = first_parameter.variable.name;

                                let property_has_type = hooked_property.hint.is_some();
                                let param_has_type = first_parameter.hint.is_some();

                                if !property_has_type && param_has_type {
                                    let param_hint = first_parameter.hint.as_ref().unwrap();
                                    context.report(
                                        Issue::error(format!(
                                            "Type of parameter `{first_parameter_name}` of hook `{class_like_name}::{item_name}::{name}` must be compatible with property type."
                                        ))
                                        .with_annotation(
                                            Annotation::primary(param_hint.span()).with_message(
                                                "Parameter has a type hint, but property has no type.",
                                            ),
                                        )
                                        .with_annotation(
                                            Annotation::secondary(hooked_property.item.variable().span())
                                                .with_message(format!("Property `{item_name}` has no type hint.")),
                                        )
                                        .with_annotation(
                                            Annotation::secondary(hook.name.span())
                                                .with_message(format!("Hook `{name}` is declared here.")),
                                        )
                                        .with_annotation(
                                            Annotation::secondary(class_like_span).with_message(format!(
                                                "{class_like_kind} `{class_like_fqcn}` defined here."
                                            )),
                                        )
                                        .with_help("Remove the type hint from the set hook parameter, or add a type to the property."),
                                    );
                                } else if property_has_type && !param_has_type {
                                    let property_hint = hooked_property.hint.as_ref().unwrap();
                                    context.report(
                                        Issue::error(format!(
                                            "Type of parameter `{first_parameter_name}` of hook `{class_like_name}::{item_name}::{name}` must be compatible with property type."
                                        ))
                                        .with_annotation(
                                            Annotation::primary(first_parameter.variable.span()).with_message(
                                                "Parameter has no type hint.",
                                            ),
                                        )
                                        .with_annotation(
                                            Annotation::secondary(property_hint.span())
                                                .with_message(format!("Property `{item_name}` has type.")),
                                        )
                                        .with_annotation(
                                            Annotation::secondary(hook.name.span())
                                                .with_message(format!("Hook `{name}` is declared here.")),
                                        )
                                        .with_annotation(
                                            Annotation::secondary(class_like_span).with_message(format!(
                                                "{class_like_kind} `{class_like_fqcn}` defined here."
                                            )),
                                        )
                                        .with_help("Add a type hint to the set hook parameter that is compatible with the property type."),
                                    );
                                }

                                if let Some(ellipsis) = first_parameter.ellipsis {
                                    context.report(
                                        Issue::error(format!(
                                            "Parameter `{first_parameter_name}` of hook `{class_like_name}::{item_name}::{name}` must not be variadic."
                                        ))
                                        .with_annotation(Annotation::primary(ellipsis.span()).with_message(format!(
                                            "Parameter `{first_parameter_name}` is marked as variadic here."
                                        )))
                                        .with_annotation(
                                            Annotation::secondary(first_parameter.variable.span()).with_message(
                                                format!("Parameter `{first_parameter_name}` declared here."),
                                            ),
                                        )
                                        .with_annotation(
                                            Annotation::secondary(hook.name.span())
                                                .with_message(format!("Hook `{name}` is declared here.")),
                                        )
                                        .with_annotation(
                                            Annotation::secondary(hooked_property.item.variable().span())
                                                .with_message(format!("Property `{item_name}` is declared here.")),
                                        )
                                        .with_annotation(
                                            Annotation::secondary(class_like_span).with_message(format!(
                                                "{class_like_kind} `{class_like_fqcn}` defined here."
                                            )),
                                        ),
                                    );
                                }

                                if let Some(ampersand) = first_parameter.ampersand {
                                    context.report(
                                        Issue::error(format!(
                                            "Parameter `{first_parameter_name}` of hook `{class_like_name}::{item_name}::{name}` must not be pass-by-reference."
                                        ))
                                        .with_annotation(Annotation::primary(ampersand.span()).with_message(format!(
                                            "Parameter `{first_parameter_name}` is marked as pass-by-reference here."
                                        )))
                                        .with_annotation(
                                            Annotation::secondary(first_parameter.variable.span()).with_message(
                                                format!("Parameter `{first_parameter_name}` declared here."),
                                            ),
                                        )
                                        .with_annotation(
                                            Annotation::secondary(hook.name.span())
                                                .with_message(format!("Hook `{name}` is declared here.")),
                                        )
                                        .with_annotation(
                                            Annotation::secondary(hooked_property.item.variable().span())
                                                .with_message(format!("Property `{item_name}` is declared here.")),
                                        )
                                        .with_annotation(
                                            Annotation::secondary(class_like_span).with_message(format!(
                                                "{class_like_kind} `{class_like_fqcn}` defined here."
                                            )),
                                        ),
                                    );
                                }

                                if let Some(default_value) = &first_parameter.default_value {
                                    context.report(
                                        Issue::error(format!(
                                            "Parameter `{first_parameter_name}` of hook `{class_like_name}::{item_name}::{name}` must not have a default value."
                                        ))
                                        .with_annotation(Annotation::primary(default_value.span()))
                                        .with_annotation(
                                            Annotation::secondary(first_parameter.variable.span()).with_message(
                                                format!("Parameter `{first_parameter_name}` declared here."),
                                            ),
                                        )
                                        .with_annotation(
                                            Annotation::secondary(hook.name.span())
                                                .with_message(format!("Hook `{name}` is declared here.")),
                                        )
                                        .with_annotation(
                                            Annotation::secondary(hooked_property.item.variable().span())
                                                .with_message(format!("Property `{item_name}` is declared here.")),
                                        )
                                        .with_annotation(
                                            Annotation::secondary(class_like_span).with_message(format!(
                                                "{class_like_kind} `{class_like_fqcn}` defined here."
                                            )),
                                        ),
                                    );
                                }
                            } else {
                                context.report(
                                    Issue::error(format!(
                                        "Hook `{}` of property `{}::{}` must accept exactly one parameter, found {}.",
                                        name,
                                        class_like_name,
                                        item_name,
                                        parameter_list.parameters.len()
                                    ))
                                    .with_annotation(
                                        Annotation::primary(parameter_list.span())
                                            .with_message("Parameters are defined here."),
                                    )
                                    .with_annotation(
                                        Annotation::secondary(hook.name.span())
                                            .with_message(format!("Hook `{name}` is declared here.")),
                                    )
                                    .with_annotation(
                                        Annotation::secondary(hooked_property.item.variable().span())
                                            .with_message(format!("Property `{item_name}` is declared here.")),
                                    )
                                    .with_annotation(
                                        Annotation::secondary(class_like_span).with_message(format!(
                                            "{class_like_kind} `{class_like_fqcn}` defined here."
                                        )),
                                    ),
                                );
                            }
                        }
                        "get" => {
                            context.report(
                                Issue::error(format!(
                                    "Hook `{name}` of property `{class_like_name}::{item_name}` must not have a parameters list."
                                ))
                                .with_annotation(
                                    Annotation::primary(parameter_list.span())
                                        .with_message("Parameters are defined here."),
                                )
                                .with_annotation(
                                    Annotation::secondary(hook.name.span())
                                        .with_message(format!("Hook `{name}` is declared here.")),
                                )
                                .with_annotation(
                                    Annotation::secondary(hooked_property.item.variable().span())
                                        .with_message(format!("Property `{item_name}` is declared here.")),
                                )
                                .with_annotation(
                                    Annotation::secondary(class_like_span).with_message(format!(
                                        "{class_like_kind} `{class_like_fqcn}` defined here."
                                    )),
                                ),
                            );
                        }
                        _ => {}
                    }
                }

                if !lowered_name.as_str().eq("set") && !lowered_name.as_str().eq("get") {
                    context.report(
                        Issue::error(format!(
                            "Hooked property `{class_like_name}::{item_name}` contains an unknown hook `{name}`, expected `set` or `get`."
                        ))
                        .with_annotation(
                            Annotation::primary(hook.name.span())
                                .with_message(format!("Hook `{name}` declared here.")),
                        )
                        .with_annotation(
                            Annotation::secondary(hooked_property.item.variable().span())
                                .with_message(format!("Property `{item_name}` is declared here.")),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                        ),
                    );
                }

                if let Some((_, previous_span)) = hook_names.iter().find(|(previous, _)| previous.eq(&lowered_name)) {
                    context.report(
                        Issue::error(format!(
                            "Hook `{name}` has already been defined for property `{class_like_name}::{item_name}`."
                        ))
                        .with_annotation(
                            Annotation::primary(hook.name.span()).with_message(format!("Duplicate hook `{name}`.")),
                        )
                        .with_annotation(
                            Annotation::secondary(*previous_span)
                                .with_message(format!("Previous declaration of hook `{previous_span}`")),
                        )
                        .with_annotation(
                            Annotation::secondary(hooked_property.item.variable().span())
                                .with_message(format!("Property `{item_name}` is declared here.")),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` defined here.")),
                        ),
                    );
                } else {
                    hook_names.push((lowered_name, hook.name.span()));
                }
            }
        }
    }
}