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
use super::*;
impl<'a> ClassAnalyzer<'a> {
pub(super) fn check_overrides(
&self,
fqcn: &Arc<str>,
_cls_location: Option<&Location>,
issues: &mut Vec<Issue>,
) {
let here = crate::db::Fqcn::from_str(self.db, fqcn.as_ref());
let Some(class) = crate::db::find_class_like(self.db, here) else {
return;
};
let own_methods: Vec<(Arc<str>, Arc<mir_codebase::storage::MethodDef>)> = class
.own_methods()
.iter()
.map(|(k, m)| (k.clone(), m.clone()))
.collect();
for (_, own) in own_methods {
let method_name: Arc<str> = own.name.clone();
// PHP does not enforce constructor signature compatibility
if method_name.as_ref() == "__construct" {
continue;
}
// Find parent definition (if any) — search ancestor chain
let method_name_lower: Arc<str> = if method_name.chars().all(|c| !c.is_uppercase()) {
method_name.clone()
} else {
Arc::from(method_name.to_lowercase().as_str())
};
// Collect ALL ancestors (skipping self) that define this method.
// The first one is the "primary parent" for structural checks (final,
// visibility, static, abstract). All are checked for signature
// compatibility (return type, param types) so that conflicts across
// multiple interfaces are caught.
let all_parent_methods: Vec<(Arc<str>, Arc<mir_codebase::storage::MethodDef>)> =
crate::db::class_ancestors_by_fqcn(self.db, here)
.iter()
.skip(1)
.filter_map(|anc| {
let here2 = crate::db::Fqcn::from_str(self.db, anc.as_ref());
crate::db::find_method_in_class(self.db, here2, method_name_lower.as_ref())
.map(|m| (anc.clone(), m))
})
.collect();
let parent_method = all_parent_methods.first().cloned();
let own_location = own.location.clone();
let loc = issue_location(
own_location.as_ref(),
own_location
.as_ref()
.and_then(|l| self.sources.get(&l.file).copied()),
);
let (parent_fqcn, parent) = match parent_method {
Some(m) => m,
None => {
// #[Override] declared but no parent method exists.
if own.is_override {
let mut issue = Issue::new(
IssueKind::InvalidOverride {
class: fqcn.to_string(),
method: method_name_lower.to_string(),
detail: "no parent method exists to override".to_string(),
},
loc,
);
if let Some(snippet) = extract_snippet(own_location.as_ref(), &self.sources)
{
issue = issue.with_snippet(snippet);
}
issues.push(issue);
}
continue;
}
};
// #[Override] with a private parent method — private methods are
// not visible to subclasses and cannot be overridden.
if own.is_override && parent.visibility == Visibility::Private {
let mut issue = Issue::new(
IssueKind::InvalidOverride {
class: fqcn.to_string(),
method: method_name_lower.to_string(),
detail: format!(
"parent method {}::{}() is private",
parent_fqcn, method_name_lower
),
},
loc.clone(),
);
if let Some(snippet) = extract_snippet(own_location.as_ref(), &self.sources) {
issue = issue.with_snippet(snippet);
}
issues.push(issue);
}
// ---- a0. Cannot re-declare a concrete method as abstract --------
// PHP rejects making a concrete parent method abstract in a subclass.
if own.is_abstract && !parent.is_abstract {
issues.push(
Issue::new(
IssueKind::MethodSignatureMismatch {
class: fqcn.to_string(),
method: method_name_lower.to_string(),
detail: format!(
"cannot make non-abstract method {}::{}() abstract",
parent_fqcn, method_name_lower
),
},
loc.clone(),
)
.with_snippet(method_name_lower.to_string()),
);
}
// ---- a. Cannot override a final method -------------------------
if parent.is_final {
let mut issue = Issue::new(
IssueKind::FinalMethodOverridden {
class: fqcn.to_string(),
method: method_name_lower.to_string(),
parent: parent_fqcn.to_string(),
},
loc.clone(),
);
if let Some(snippet) = extract_snippet(own_location.as_ref(), &self.sources) {
issue = issue.with_snippet(snippet);
}
issues.push(issue);
}
// ---- b. Static/non-static mismatch --------------------------------
// A non-static child method cannot override a static parent method
// and vice versa — PHP treats these as different methods in practice
// but the static contract is part of the signature.
if parent.is_static != own.is_static {
let detail = if parent.is_static {
format!(
"cannot override static method {}::{}() with a non-static method",
parent_fqcn, method_name_lower
)
} else {
format!(
"cannot override non-static method {}::{}() with a static method",
parent_fqcn, method_name_lower
)
};
let mut issue = Issue::new(
IssueKind::MethodSignatureMismatch {
class: fqcn.to_string(),
method: method_name_lower.to_string(),
detail,
},
loc.clone(),
);
if let Some(snippet) = extract_snippet(own_location.as_ref(), &self.sources) {
issue = issue.with_snippet(snippet);
}
issues.push(issue);
}
// ---- c. Visibility must not be reduced -------------------------
if visibility_reduced(own.visibility, parent.visibility) {
let mut issue = Issue::new(
IssueKind::OverriddenMethodAccess {
class: fqcn.to_string(),
method: method_name_lower.to_string(),
},
loc.clone(),
);
if let Some(snippet) = extract_snippet(own_location.as_ref(), &self.sources) {
issue = issue.with_snippet(snippet);
}
issues.push(issue);
}
// ---- c. Return type must be covariant (check ALL ancestors) -----
// Check every ancestor that defines this method: a class implementing
// two interfaces with conflicting return types must be flagged even if
// it satisfies the first interface's contract.
let own_return_type = own.return_type.as_deref().cloned();
if let Some(child_ret) = own_return_type.as_ref() {
let child_file = own_location.as_ref().map(|l| l.file.as_ref()).unwrap_or("");
for (idx, (p_fqcn, p)) in all_parent_methods.iter().enumerate() {
let Some(parent_ret) = p.return_type.as_deref() else {
continue;
};
if parent_ret.from_docblock
|| parent_ret.is_mixed()
|| child_ret.is_mixed()
|| self.return_type_has_template(parent_ret)
{
continue;
}
let involves_named_objects = Self::type_has_named_objects(child_ret)
|| Self::type_has_named_objects(parent_ret);
let involves_self_static = self.type_has_self_or_static(child_ret)
|| self.type_has_self_or_static(parent_ret);
let compatible = if (involves_named_objects || involves_self_static)
&& self.type_has_only_object_atoms(child_ret)
&& self.type_has_only_object_atoms(parent_ret)
{
crate::stmt::named_object_return_compatible(
child_ret, parent_ret, self.db, child_file,
)
} else if involves_named_objects || involves_self_static {
true // mixed scalar+object union — skip (G5 gap)
} else {
Self::scalar_return_types_compatible(child_ret, parent_ret)
};
if !compatible {
// Primary parent uses the original message format for
// backwards-compatibility with existing fixtures. Additional
// ancestors include the declaring class to clarify which
// contract is violated.
let detail = if idx == 0 {
format!(
"return type '{child_ret}' is not a subtype of parent '{parent_ret}'"
)
} else {
format!(
"return type '{child_ret}' is not a subtype of {p_fqcn}::{}() '{parent_ret}'",
method_name_lower
)
};
issues.push(
Issue::new(
IssueKind::MethodSignatureMismatch {
class: fqcn.to_string(),
method: method_name_lower.to_string(),
detail,
},
loc.clone(),
)
.with_snippet(method_name_lower.to_string()),
);
}
}
}
// ---- d. Required param count must not increase -----------------
let parent_params = parent.params.clone();
let own_params = own.params.clone();
let parent_required = parent_params
.iter()
.filter(|p| !p.is_optional && !p.is_variadic)
.count();
let child_required = own_params
.iter()
.filter(|p| !p.is_optional && !p.is_variadic)
.count();
if child_required > parent_required {
issues.push(
Issue::new(
IssueKind::MethodSignatureMismatch {
class: fqcn.to_string(),
method: method_name_lower.to_string(),
detail: format!(
"overriding method requires {child_required} argument(s) but parent requires {parent_required}"
),
},
loc.clone(),
)
.with_snippet(method_name_lower.to_string()),
);
}
// ---- d2. Child must not declare fewer parameters than parent -----
// A child accepting fewer positional params cannot handle every call
// the parent could (an LSP violation PHP rejects). A trailing
// variadic absorbs the extras, so it is exempt. Constructors are
// exempt from signature compatibility in PHP, and private parent
// methods are not real overrides.
if method_name_lower.as_ref() != "__construct"
&& parent.visibility != Visibility::Private
&& own_params.len() < parent_params.len()
&& !own_params.iter().any(|p| p.is_variadic)
{
issues.push(
Issue::new(
IssueKind::MethodSignatureMismatch {
class: fqcn.to_string(),
method: method_name_lower.to_string(),
detail: format!(
"method has fewer parameters ({}) than parent {}::{}() ({})",
own_params.len(),
parent_fqcn,
method_name_lower,
parent_params.len()
),
},
loc.clone(),
)
.with_snippet(method_name_lower.to_string()),
);
}
// ---- d3. by-reference-ness of shared params must match -----------
// A parameter that is by-value in the parent but by-reference in the
// child (or vice versa) changes the calling contract — PHP rejects
// the override. Constructors are exempt.
if method_name_lower.as_ref() != "__construct" {
let shared = parent_params.len().min(own_params.len());
if let Some(i) =
(0..shared).find(|&i| parent_params[i].is_byref != own_params[i].is_byref)
{
issues.push(
Issue::new(
IssueKind::MethodSignatureMismatch {
class: fqcn.to_string(),
method: method_name_lower.to_string(),
detail: format!(
"parameter ${} must {}be passed by reference to match parent {}::{}()",
own_params[i].name.as_ref().trim_start_matches('$'),
if parent_params[i].is_byref { "" } else { "not " },
parent_fqcn,
method_name_lower
),
},
loc.clone(),
)
.with_snippet(method_name_lower.to_string()),
);
}
}
// ---- e. Param types must not be narrowed (contravariance) --------
// For each positional param present in both parent and child:
// parent_param_type must be a subtype of child_param_type.
// (Child may widen; it must not narrow.)
// Skip when:
// - Either side has no type hint
// - Either type is mixed
// - Either type contains a named object (needs codebase for inheritance check)
// - Either type contains TSelf/TStaticObject
// - Either type contains a template param
let shared_len = parent_params.len().min(own_params.len());
for i in 0..shared_len {
let parent_param = &parent_params[i];
let child_param = &own_params[i];
let (parent_ty, child_ty) = match (&parent_param.ty, &child_param.ty) {
(Some(p), Some(c)) => (p, c),
_ => continue,
};
if parent_ty.is_mixed()
|| child_ty.is_mixed()
|| Self::type_has_named_objects(parent_ty)
|| Self::type_has_named_objects(child_ty)
|| self.type_has_self_or_static(parent_ty)
|| self.type_has_self_or_static(child_ty)
|| self.return_type_has_template(parent_ty)
|| self.return_type_has_template(child_ty)
{
continue;
}
if Self::scalar_param_type_narrowed(parent_ty, child_ty) {
issues.push(
Issue::new(
IssueKind::MethodSignatureMismatch {
class: fqcn.to_string(),
method: method_name_lower.to_string(),
detail: format!(
"parameter ${} type '{}' is narrower than parent type '{}'",
child_param.name, child_ty, parent_ty
),
},
loc.clone(),
)
.with_snippet(method_name_lower.to_string()),
);
break; // one issue per method is enough
}
}
}
// ---- Property visibility must not be reduced -------------------------
let own_properties: Vec<(Arc<str>, mir_codebase::storage::PropertyDef)> = class
.own_properties()
.map(|props| props.iter().map(|(k, p)| (k.clone(), p.clone())).collect())
.unwrap_or_default();
for (_, own_prop) in own_properties {
let prop_name = own_prop.name.clone();
// Look up the same property name in ancestors (skip self = first entry)
let parent_prop = crate::db::class_ancestors_by_fqcn(self.db, here)
.iter()
.skip(1)
.find_map(|anc| {
let anc_here = crate::db::Fqcn::from_str(self.db, anc.as_ref());
crate::db::find_property_in_class(self.db, anc_here, prop_name.as_ref())
.map(|p| (anc.clone(), p))
});
let Some((_parent_fqcn, parent_prop)) = parent_prop else {
continue;
};
if visibility_reduced(own_prop.visibility, parent_prop.visibility) {
let loc = issue_location(
own_prop.location.as_ref(),
own_prop
.location
.as_ref()
.and_then(|l| self.sources.get(&l.file).copied()),
);
let mut issue = Issue::new(
IssueKind::OverriddenPropertyAccess {
class: fqcn.to_string(),
property: prop_name.to_string(),
},
loc,
);
if let Some(snippet) = extract_snippet(own_prop.location.as_ref(), &self.sources) {
issue = issue.with_snippet(snippet);
}
issues.push(issue);
}
}
}
// -----------------------------------------------------------------------
// Helpers
// -----------------------------------------------------------------------
/// Returns true if the type contains template params or class-strings with unknown types.
/// Used to suppress MethodSignatureMismatch on generic parent return types.
/// Checks recursively into array key/value types.
fn return_type_has_template(&self, ty: &mir_types::Type) -> bool {
use mir_types::Atomic;
ty.types.iter().any(|atomic| match atomic {
Atomic::TTemplateParam { .. } => true,
Atomic::TClassString(Some(inner)) => !crate::db::class_exists(self.db, inner.as_ref()),
Atomic::TNamedObject { fqcn, type_params } => {
// Bare name with no namespace separator is likely a template param
(!fqcn.contains('\\') && !crate::db::class_exists(self.db, fqcn.as_ref()))
// Also check if any type params are templates
|| type_params.iter().any(|tp| self.return_type_has_template(tp))
}
Atomic::TArray { key, value } | Atomic::TNonEmptyArray { key, value } => {
self.return_type_has_template(key) || self.return_type_has_template(value)
}
Atomic::TList { value } | Atomic::TNonEmptyList { value } => {
self.return_type_has_template(value)
}
_ => false,
})
}
/// Returns true if the type contains any named-object atomics (TNamedObject)
/// at any level (including inside array key/value types).
/// Named-object subtyping requires codebase inheritance lookup, so we skip
/// the simple structural check for these.
fn type_has_named_objects(ty: &mir_types::Type) -> bool {
use mir_types::Atomic;
ty.types.iter().any(|a| match a {
Atomic::TNamedObject { .. } => true,
Atomic::TArray { key, value } | Atomic::TNonEmptyArray { key, value } => {
Self::type_has_named_objects(key) || Self::type_has_named_objects(value)
}
Atomic::TList { value } | Atomic::TNonEmptyList { value } => {
Self::type_has_named_objects(value)
}
_ => false,
})
}
/// Returns true if the type contains TSelf or TStaticObject (late-static types).
/// These are always considered compatible with their bound class type.
fn type_has_self_or_static(&self, ty: &mir_types::Type) -> bool {
use mir_types::Atomic;
ty.types
.iter()
.any(|a| matches!(a, Atomic::TSelf { .. } | Atomic::TStaticObject { .. }))
}
/// Returns true if every atom in the union is handled by `named_object_return_compatible`:
/// object types (named/self/static/parent), null, void, never, and class-string variants.
/// Unions that also contain scalar atoms (int, string, …) are not fully handled there
/// and must fall back to the skip path (G5 gap).
fn type_has_only_object_atoms(&self, ty: &mir_types::Type) -> bool {
use mir_types::Atomic;
ty.types.iter().all(|a| {
matches!(
a,
Atomic::TNamedObject { .. }
| Atomic::TSelf { .. }
| Atomic::TStaticObject { .. }
| Atomic::TParent { .. }
| Atomic::TNull
| Atomic::TVoid
| Atomic::TNever
| Atomic::TClassString(_)
)
})
}
// -----------------------------------------------------------------------
// Check: circular class inheritance (class A extends B extends A)
}