Skip to content

Commit 170a2e0

Browse files
committed
Implement implicitly inherited interfaces
1 parent cd1ffac commit 170a2e0

4 files changed

Lines changed: 531 additions & 2 deletions

File tree

example.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ enum Priority: int
8888
case High = 3;
8989
}
9090

91+
enum Mode
92+
{
93+
case Automatic;
94+
case Manual;
95+
}
96+
9197
// ─── Base / Parent Class ────────────────────────────────────────────────────
9298

9399
/**

src/parser.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use std::collections::HashMap;
88

99
use bumpalo::Bump;
10+
1011
use mago_syntax::ast::*;
1112
use mago_syntax::parser::parse_file_content;
1213

@@ -683,9 +684,22 @@ impl Backend {
683684
Statement::Enum(enum_def) => {
684685
let enum_name = enum_def.name.value.to_string();
685686

686-
let (mut methods, mut properties, constants, used_traits) =
687+
let (mut methods, mut properties, constants, mut used_traits) =
687688
Self::extract_class_like_members(enum_def.members.iter(), doc_ctx);
688689

690+
// Enums implicitly implement UnitEnum or BackedEnum.
691+
// We add the interface as a fully-qualified name (leading
692+
// backslash) so that `resolve_name` does not prepend the
693+
// current namespace. The class_loader / merge_traits_into
694+
// path will pick up the interface from the SPL stubs and
695+
// merge its methods (cases, from, tryFrom, …) automatically.
696+
let implicit_interface = if enum_def.backing_type_hint.is_some() {
697+
"\\BackedEnum"
698+
} else {
699+
"\\UnitEnum"
700+
};
701+
used_traits.push(implicit_interface.to_string());
702+
689703
// Extract @property, @method, and @mixin tags from the enum-level docblock.
690704
let mut mixins = Vec::new();
691705
if let Some(ctx) = doc_ctx
@@ -1034,7 +1048,7 @@ impl Backend {
10341048
/// otherwise prepend current namespace
10351049
/// 3. Unqualified (`Bar`) → check use_map; otherwise prepend namespace
10361050
/// 4. No namespace and not in use_map → keep as-is
1037-
pub(crate) fn resolve_parent_class_names(
1051+
pub fn resolve_parent_class_names(
10381052
classes: &mut [ClassInfo],
10391053
use_map: &HashMap<String, String>,
10401054
namespace: &Option<String>,

0 commit comments

Comments
 (0)