mago-semantics 1.17.0

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
#![allow(clippy::too_many_arguments)]

use mago_reporting::Annotation;
use mago_reporting::Issue;
use mago_span::HasSpan;
use mago_span::Span;
use mago_syntax::ast::Hint;
use mago_syntax::ast::Method;
use mago_syntax::ast::MethodBody;
use mago_syntax::ast::Modifier;

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

#[inline]
pub fn check_method<'ast, 'arena>(
    method: &'ast Method<'arena>,
    method_name: &str,
    class_like_span: Span,
    class_like_name: &str,
    class_like_fqcn: &str,
    class_like_kind: &str,
    class_like_is_interface: bool,
    context: &mut Context<'_, 'ast, 'arena>,
) {
    let mut last_static: Option<Span> = None;
    let mut last_final: Option<Span> = None;
    let mut last_abstract: Option<Span> = None;
    let mut last_visibility: Option<Span> = None;
    let mut is_public = true;
    for modifier in &method.modifiers {
        match modifier {
            Modifier::Static(_) => {
                if let Some(last_static) = last_static {
                    context.report(
                        Issue::error(format!(
                            "duplicate `static` modifier on method `{class_like_name}::{method_name}`"
                        ))
                        .with_annotation(
                            Annotation::primary(modifier.span()).with_message("duplicate `static` modifier"),
                        )
                        .with_annotation(Annotation::primary(last_static).with_message("previous `static` modifier"))
                        .with_annotation(
                            Annotation::secondary(method.span())
                                .with_message(format!("method `{class_like_name}::{method_name}` defined here.",)),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` is defined here.")),
                        ),
                    );
                }

                last_static = Some(modifier.span());
            }
            Modifier::Final(_) => {
                if let Some(abstract_modifier) = last_abstract {
                    context.report(
                        Issue::error(format!(
                            "method `{class_like_name}::{method_name}` cannot be both `final` and `abstract`"
                        ))
                        .with_annotation(Annotation::primary(modifier.span()).with_message("`final` modifier"))
                        .with_annotation(Annotation::primary(abstract_modifier).with_message("`abstract` modifier"))
                        .with_annotation(
                            Annotation::secondary(method.span())
                                .with_message(format!("method `{class_like_name}::{method_name}` defined here.",)),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` is defined here.")),
                        ),
                    );
                }

                if let Some(last_final) = last_final {
                    context.report(
                        Issue::error(format!(
                            "duplicate `final` modifier on method `{class_like_name}::{method_name}`"
                        ))
                        .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(method.span())
                                .with_message(format!("method `{class_like_name}::{method_name}` defined here.",)),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` is defined here.")),
                        ),
                    );
                }

                last_final = Some(modifier.span());
            }
            Modifier::Abstract(_) => {
                if let Some(final_modifier) = last_final {
                    context.report(
                        Issue::error(format!(
                            "method `{class_like_name}::{method_name}` cannot be both `final` and `abstract`"
                        ))
                        .with_annotation(Annotation::primary(modifier.span()).with_message("`abstract` modifier"))
                        .with_annotation(Annotation::primary(final_modifier).with_message("`final` modifier"))
                        .with_annotation(
                            Annotation::secondary(method.span())
                                .with_message(format!("method `{class_like_name}::{method_name}` defined here.",)),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` is defined here.")),
                        ),
                    );
                }

                if let Some(last_abstract) = last_abstract {
                    context.report(
                        Issue::error(format!(
                            "duplicate `abstract` modifier on method `{class_like_name}::{method_name}`"
                        ))
                        .with_annotation(
                            Annotation::primary(modifier.span()).with_message("duplicate `abstract` modifier"),
                        )
                        .with_annotation(
                            Annotation::primary(last_abstract).with_message("previous `abstract` modifier"),
                        )
                        .with_annotation(
                            Annotation::secondary(method.span())
                                .with_message(format!("method `{class_like_name}::{method_name}` defined here.",)),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` is defined here.")),
                        ),
                    );
                }

                last_abstract = Some(modifier.span());
            }
            Modifier::Readonly(_) => {
                context.report(
                    Issue::error("`readonly` modifier is not allowed on methods".to_string())
                        .with_annotation(Annotation::primary(modifier.span()).with_message("`readonly` modifier"))
                        .with_annotation(
                            Annotation::secondary(method.span())
                                .with_message(format!("method `{class_like_name}::{method_name}` defined here.",)),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` is defined here.")),
                        ),
                );
            }
            Modifier::Private(_) | Modifier::Protected(_) | Modifier::Public(_) => {
                if let Some(last_visibility) = last_visibility {
                    context.report(
                        Issue::error(format!(
                            "duplicate visibility modifier on method `{class_like_name}::{method_name}`"
                        ))
                        .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(method.span())
                                .with_message(format!("method `{class_like_name}::{method_name}` defined here.",)),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` is defined here.")),
                        ),
                    );
                } else {
                    if !matches!(modifier, Modifier::Public(_)) {
                        is_public = false;
                    }

                    last_visibility = Some(modifier.span());
                }
            }
            Modifier::PrivateSet(k) | Modifier::ProtectedSet(k) | Modifier::PublicSet(k) => {
                let modifier_name = k.value;

                context.report(
                    Issue::error(format!("`{modifier_name}` modifier is not allowed on methods"))
                        .with_annotation(
                            Annotation::primary(modifier.span()).with_message(format!("`{modifier_name}` modifier")),
                        )
                        .with_annotation(
                            Annotation::secondary(method.span())
                                .with_message(format!("method `{class_like_name}::{method_name}` defined here.",)),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` is defined here.")),
                        ),
                );
            }
        }
    }

    for (magic_method, parameter_count, must_be_public, must_be_static, can_have_return_type) in MAGIC_METHOD_SEMANTICS
    {
        if method_name.eq_ignore_ascii_case(magic_method) {
            if let Some(count) = parameter_count {
                let mut found_count = 0;
                let mut found_variadic = false;
                for param in &method.parameter_list.parameters {
                    found_count += 1;

                    if param.ellipsis.is_some() {
                        found_variadic = true;
                    }
                }

                if found_variadic || found_count.ne(count) {
                    let message = if found_variadic {
                        format!(
                            "Magic method `{class_like_name}::{method_name}` must have exactly {count} parameters, found more than {found_count} due to variadic parameter."
                        )
                    } else {
                        format!(
                            "Magic method `{class_like_name}::{method_name}` must have exactly {count} parameters, found {found_count}."
                        )
                    };

                    context.report(
                        Issue::error(message)
                            .with_annotation(Annotation::primary(method.parameter_list.span()))
                            .with_annotation(
                                Annotation::secondary(method.span())
                                    .with_message(format!("Method `{class_like_name}::{method_name}` defined here.",)),
                            )
                            .with_annotation(
                                Annotation::secondary(class_like_span)
                                    .with_message(format!("{class_like_kind} `{class_like_fqcn}` is defined here.")),
                            ),
                    );
                }
            }

            if *must_be_public && !is_public {
                context.report(
                    Issue::error(format!("Magic method `{class_like_name}::{method_name}` must be public."))
                        .with_annotation(
                            Annotation::primary(last_visibility.unwrap())
                                .with_message("Non-Public visibility modifier."),
                        )
                        .with_annotation(
                            Annotation::secondary(method.span())
                                .with_message(format!("Method `{class_like_name}::{method_name}` defined here.",)),
                        )
                        .with_annotation(
                            Annotation::secondary(class_like_span)
                                .with_message(format!("{class_like_kind} `{class_like_fqcn}` is defined here.")),
                        ),
                );
            }

            match last_static.as_ref() {
                Some(span) if !*must_be_static => {
                    context.report(
                        Issue::error(format!("Magic method `{class_like_name}::{method_name}` cannot be static."))
                            .with_annotation(Annotation::primary(*span).with_message("`static` modifier"))
                            .with_annotation(
                                Annotation::secondary(method.span())
                                    .with_message(format!("Method `{class_like_name}::{method_name}` defined here.",)),
                            )
                            .with_annotation(
                                Annotation::secondary(class_like_span)
                                    .with_message(format!("{class_like_kind} `{class_like_fqcn}` is defined here.")),
                            ),
                    );
                }
                None if *must_be_static => {
                    context.report(
                        Issue::error(format!("Magic method `{class_like_name}::{method_name}` must be static."))
                            .with_annotation(Annotation::primary(method.name.span()))
                            .with_annotation(
                                Annotation::secondary(class_like_span)
                                    .with_message(format!("{class_like_kind} `{class_like_fqcn}`")),
                            )
                            .with_annotation(
                                Annotation::secondary(method.span())
                                    .with_message(format!("Method `{class_like_name}::{method_name}` defined here.",)),
                            ),
                    );
                }
                _ => {}
            }

            if !*can_have_return_type && let Some(hint) = &method.return_type_hint {
                context.report(
                    Issue::error(format!(
                        "Magic method `{class_like_name}::{method_name}` cannot have a return type hint."
                    ))
                    .with_annotation(Annotation::primary(hint.span()))
                    .with_annotation(
                        Annotation::secondary(method.span())
                            .with_message(format!("Method `{class_like_name}::{method_name}` defined here.",)),
                    )
                    .with_annotation(
                        Annotation::secondary(class_like_span)
                            .with_message(format!("{class_like_kind} `{class_like_fqcn}` is defined here.")),
                    ),
                );
            }
        }
    }

    let mut is_abstract = false;
    match &method.body {
        MethodBody::Abstract(method_abstract_body) => {
            if !class_like_is_interface && last_abstract.is_none() {
                context.report(
                    Issue::error(format!(
                        "Non-Abstract method `{class_like_name}::{method_name}` must have a concrete body.",
                    ))
                    .with_annotation(Annotation::primary(method_abstract_body.span()))
                    .with_annotations([
                        Annotation::secondary(class_like_span)
                            .with_message(format!("{class_like_kind} `{class_like_fqcn}` is defined here.")),
                        Annotation::secondary(method.span())
                            .with_message(format!("Method `{class_like_name}::{method_name}` defined here.")),
                    ]),
                );
            }

            is_abstract = true;
        }
        MethodBody::Concrete(body) => {
            if let Some(abstract_modifier) = last_abstract {
                is_abstract = true;

                context.report(
                    Issue::error(format!(
                        "Method `{class_like_name}::{method_name}` is abstract and cannot have a concrete body.",
                    ))
                    .with_annotation(Annotation::primary(body.span()))
                    .with_annotations([
                        Annotation::primary(abstract_modifier.span()),
                        Annotation::secondary(class_like_span)
                            .with_message(format!("{class_like_kind} `{class_like_fqcn}` is defined here.")),
                        Annotation::secondary(method.span())
                            .with_message(format!("Method `{class_like_name}::{method_name}` defined here.")),
                    ]),
                );
            } else if class_like_is_interface {
                context.report(
                    Issue::error(format!(
                        "Interface method `{class_like_name}::{method_name}` is implicitly abstract and cannot have a concrete body.",
                    ))
                    .with_annotation(Annotation::primary(body.span()))
                    .with_annotations([
                        Annotation::secondary(class_like_span)
                            .with_message(format!("{class_like_kind} `{class_like_fqcn}` is defined here.")),
                        Annotation::secondary(method.span())
                            .with_message(format!("Method `{class_like_name}::{method_name}` defined here.")),
                    ]),
                );
            }

            let hint = if let Some(return_hint) = &method.return_type_hint {
                &return_hint.hint
            } else {
                return;
            };

            let returns = mago_syntax::utils::find_returns_in_block(body);

            match &hint {
                Hint::Void(_) => {
                    for r#return in returns {
                        if let Some(val) = &r#return.value {
                            context.report(
                                Issue::error(format!(
                                    "Method `{class_like_name}::{method_name}` with return type of `void` must not return a value.",
                                ))
                                .with_annotation(Annotation::primary(val.span()))
                                .with_annotations([
                                    Annotation::secondary(class_like_span).with_message(format!(
                                        "{class_like_kind} `{class_like_fqcn}` is defined here."
                                    )),
                                    Annotation::secondary(method.span()).with_message(format!(
                                        "Method `{class_like_name}::{method_name}` defined here.",
                                    )),
                                ])
                                .with_help("Remove the return type hint, or remove the return value."),
                            );
                        }
                    }
                }
                Hint::Never(_) => {
                    for r#return in returns {
                        context.report(
                            Issue::error(format!(
                                "Function `{class_like_name}::{method_name}` with return type of `never` must not return.",
                            ))
                            .with_annotation(Annotation::primary(r#return.span()))
                            .with_annotations([
                                Annotation::secondary(class_like_span).with_message(format!(
                                    "{class_like_kind} `{class_like_fqcn}` is defined here."
                                )),
                                Annotation::secondary(method.span()).with_message(format!(
                                    "Method `{class_like_name}::{method_name}` defined here.",
                                )),
                            ])
                            .with_help("Remove the return type hint, or remove the return statement."),
                        );
                    }
                }
                _ if !returns_generator(context, body, hint) => {
                    for r#return in returns {
                        if r#return.value.is_none() {
                            context.report(
                                Issue::error(format!(
                                    "Method `{class_like_name}::{method_name}` with return type must return a value.",
                                ))
                                .with_annotation(Annotation::primary(r#return.span()))
                                .with_annotations([
                                    Annotation::secondary(class_like_span).with_message(format!(
                                        "{class_like_kind} `{class_like_fqcn}` is defined here."
                                    )),
                                    Annotation::secondary(method.span()).with_message(format!(
                                        "Method `{class_like_name}::{method_name}` defined here.",
                                    )),
                                ])
                                .with_note("Did you mean `return null;` instead of `return;`?")
                                .with_help("Add a return value to the statement."),
                            );
                        }
                    }
                }
                _ => {}
            }
        }
    }

    if !method_name.eq_ignore_ascii_case("__construct") {
        check_for_promoted_properties_outside_constructor(&method.parameter_list, context);
    } else if is_abstract {
        for parameter in &method.parameter_list.parameters {
            if parameter.is_promoted_property() {
                context.report(
                    Issue::error("Promoted properties are not allowed in abstract constructors.")
                        .with_annotation(
                            Annotation::primary(parameter.span()).with_message("Promoted property used here."),
                        )
                        .with_help(
                            "Remove the promoted property from the constructor or make the constructor concrete.",
                        ),
                );
            }
        }
    }
}