Skip to main content

mago_codex/metadata/
class_like_constant.rs

1use mago_php_version::PHPVersion;
2use mago_php_version::PHPVersionRange;
3use serde::Deserialize;
4use serde::Serialize;
5
6use mago_atom::Atom;
7use mago_span::HasSpan;
8use mago_span::Span;
9
10use crate::metadata::attribute::AttributeMetadata;
11use crate::metadata::flags::MetadataFlags;
12use crate::metadata::ttype::TypeMetadata;
13use crate::metadata::version_constraint::VersionConstraint;
14use crate::ttype::atomic::TAtomic;
15use crate::visibility::Visibility;
16
17#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
18#[non_exhaustive]
19pub struct ClassLikeConstantMetadata {
20    pub attributes: Vec<AttributeMetadata>,
21    pub name: Atom,
22    pub span: Span,
23    pub visibility: Visibility,
24    pub type_declaration: Option<TypeMetadata>,
25    pub type_metadata: Option<TypeMetadata>,
26    pub inferred_type: Option<TAtomic>,
27    pub flags: MetadataFlags,
28    pub version_constraint: VersionConstraint,
29}
30
31impl ClassLikeConstantMetadata {
32    #[must_use]
33    pub fn new(name: Atom, span: Span, visibility: Visibility, flags: MetadataFlags) -> Self {
34        Self {
35            attributes: Vec::new(),
36            name,
37            span,
38            visibility,
39            type_declaration: None,
40            type_metadata: None,
41            inferred_type: None,
42            flags,
43            version_constraint: VersionConstraint::unconstrained(),
44        }
45    }
46
47    pub fn set_type_declaration(&mut self, type_declaration: TypeMetadata) {
48        if self.type_metadata.is_none() {
49            self.type_metadata = Some(type_declaration.clone());
50        }
51
52        self.type_declaration = Some(type_declaration);
53    }
54
55    /// Returns `true` when this class constant is available in the given PHP
56    /// version.
57    #[inline]
58    #[must_use]
59    pub fn is_available_in_version(&self, version: PHPVersion) -> bool {
60        self.version_constraint.allows_version(version)
61    }
62
63    /// Returns `true` when this class constant is available across the entire
64    /// supplied [`PHPVersionRange`].
65    #[inline]
66    #[must_use]
67    pub fn is_available_in_version_range(&self, range: PHPVersionRange) -> bool {
68        self.version_constraint.allows_version_range(range)
69    }
70}
71
72impl HasSpan for ClassLikeConstantMetadata {
73    fn span(&self) -> Span {
74        self.span
75    }
76}