Skip to main content

mago_codex/metadata/
enum_case.rs

1use mago_span::HasSpan;
2use mago_span::Span;
3use mago_word::Word;
4
5use crate::metadata::attribute::AttributeMetadata;
6use crate::metadata::flags::MetadataFlags;
7use crate::metadata::version_constraint::VersionConstraint;
8use crate::ttype::atomic::TAtomic;
9
10/// Contains metadata associated with a specific `case` within a PHP `enum`.
11///
12/// Represents enum cases in both "pure" enums (e.g., `case Pending;` in `enum Status`)
13/// and "backed" enums (e.g., `case Ok = 200;` in `enum HttpStatus: int`),
14/// including associated attributes, values, and source locations.
15#[derive(Clone, Debug, PartialEq, Eq, Hash)]
16#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
17#[non_exhaustive]
18pub struct EnumCaseMetadata {
19    pub attributes: Vec<AttributeMetadata>,
20    pub name: Word,
21    pub name_span: Span,
22    pub span: Span,
23    pub value_type: Option<TAtomic>,
24    pub flags: MetadataFlags,
25    /// PHP version range in which this enum case is available, derived from
26    /// `Mago\AvailableSince` / `Mago\AvailableUntil` attributes during
27    /// scanning.
28    pub version_constraint: VersionConstraint,
29}
30
31impl EnumCaseMetadata {
32    /// Creates new `EnumCaseMetadata` for a case assumed initially to be non-backed (pure).
33    ///
34    /// Use modifier methods (`set_is_backed`, `with_is_backed`) later during analysis
35    /// if the enum is determined to be backed.
36    ///
37    /// # Arguments
38    /// * `name`: The identifier (name) of the enum case (e.g., `PENDING`).
39    /// * `name_span`: The source code location of the name identifier.
40    /// * `span`: The source code location of the entire case declaration.
41    #[inline]
42    #[must_use]
43    pub fn new(name: Word, name_span: Span, span: Span, flags: MetadataFlags) -> Self {
44        Self {
45            attributes: Vec::new(),
46            name,
47            name_span,
48            span,
49            flags,
50            value_type: None,
51            version_constraint: VersionConstraint::unconstrained(),
52        }
53    }
54
55    /// Returns `true` when this enum case is available in the given PHP version.
56    #[inline]
57    #[must_use]
58    pub fn is_available_in_version(&self, version: mago_php_version::PHPVersion) -> bool {
59        self.version_constraint.allows_version(version)
60    }
61
62    /// Returns `true` when this enum case is available across the entire
63    /// supplied [`PHPVersionRange`].
64    #[inline]
65    #[must_use]
66    pub fn is_available_in_version_range(&self, range: mago_php_version::PHPVersionRange) -> bool {
67        self.version_constraint.allows_version_range(range)
68    }
69}
70
71impl HasSpan for EnumCaseMetadata {
72    fn span(&self) -> Span {
73        self.span
74    }
75}