Skip to main content

mago_codex/metadata/
constant.rs

1use mago_php_version::PHPVersion;
2use mago_php_version::PHPVersionRange;
3
4use mago_reporting::Issue;
5use mago_span::HasSpan;
6use mago_span::Span;
7use mago_word::Word;
8
9use crate::metadata::attribute::AttributeMetadata;
10use crate::metadata::flags::MetadataFlags;
11use crate::metadata::patch::PATCHABLE_COMMON_DOCBLOCK_FLAGS;
12use crate::metadata::patch::merge_explicit_docblock_flags;
13use crate::metadata::ttype::TypeMetadata;
14use crate::metadata::version_constraint::VersionConstraint;
15use crate::ttype::union::TUnion;
16
17/// Contains metadata associated with a global constant defined using `const`.
18///
19/// Represents a single constant declaration item, potentially within a grouped declaration,
20/// like `MAX_RETRIES = 3` in `const MAX_RETRIES = 3;` or `B = 2` in `const A = 1, B = 2;`.
21#[derive(Clone, Debug, PartialEq, Eq)]
22#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
23#[non_exhaustive]
24pub struct ConstantMetadata {
25    pub attributes: Vec<AttributeMetadata>,
26    pub name: Word,
27    pub span: Span,
28    pub type_metadata: Option<TypeMetadata>,
29    pub inferred_type: Option<TUnion>,
30    pub flags: MetadataFlags,
31    #[cfg_attr(feature = "serde", serde(default))]
32    pub(crate) explicit_docblock_flags: MetadataFlags,
33    pub issues: Vec<Issue>,
34    pub version_constraint: VersionConstraint,
35}
36
37impl ConstantMetadata {
38    /// Creates new `ConstantMetadata` for a non-deprecated, non-internal global constant item.
39    ///
40    /// # Arguments
41    ///
42    /// * `name`: The identifier (name) of the constant.
43    /// * `span`: The source code location of this specific constant's definition item (`NAME = value`).
44    #[inline]
45    #[must_use]
46    pub fn new(name: Word, span: Span, flags: MetadataFlags) -> Self {
47        Self {
48            attributes: Vec::new(),
49            name,
50            span,
51            flags,
52            explicit_docblock_flags: MetadataFlags::empty(),
53            type_metadata: None,
54            inferred_type: None,
55            issues: Vec::new(),
56            version_constraint: VersionConstraint::unconstrained(),
57        }
58    }
59
60    /// Returns a mutable slice of docblock issues.
61    #[inline]
62    pub fn take_issues(&mut self) -> Vec<Issue> {
63        std::mem::take(&mut self.issues)
64    }
65
66    /// Returns `true` when this constant is available in the given PHP version.
67    #[inline]
68    #[must_use]
69    pub fn is_available_in_version(&self, version: PHPVersion) -> bool {
70        self.version_constraint.allows_version(version)
71    }
72
73    /// Returns `true` when this constant is available across the entire
74    /// supplied [`PHPVersionRange`].
75    #[inline]
76    #[must_use]
77    pub fn is_available_in_version_range(&self, range: PHPVersionRange) -> bool {
78        self.version_constraint.allows_version_range(range)
79    }
80
81    /// Applies a patch to this entry in place, refining type information and supported docblock flags.
82    pub fn apply_patch(&mut self, patch: &ConstantMetadata) {
83        if patch.type_metadata.is_some() {
84            self.type_metadata.clone_from(&patch.type_metadata);
85        }
86        merge_explicit_docblock_flags(
87            &mut self.flags,
88            patch.flags,
89            patch.explicit_docblock_flags,
90            PATCHABLE_COMMON_DOCBLOCK_FLAGS,
91        );
92    }
93}
94
95impl HasSpan for ConstantMetadata {
96    fn span(&self) -> Span {
97        self.span
98    }
99}