Skip to main content

mago_codex/metadata/
enum_case.rs

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