mago_codex/metadata/property.rs
1use mago_php_version::PHPVersion;
2use mago_php_version::PHPVersionRange;
3
4use mago_span::Span;
5use mago_word::WordMap;
6
7use crate::metadata::attribute::AttributeMetadata;
8use crate::metadata::flags::MetadataFlags;
9use crate::metadata::property_hook::PropertyHookMetadata;
10use crate::metadata::ttype::TypeMetadata;
11use crate::metadata::version_constraint::VersionConstraint;
12use crate::misc::VariableIdentifier;
13use crate::visibility::Visibility;
14
15/// Contains metadata associated with a declared class property in PHP.
16///
17/// This includes information about its name, location, visibility (potentially asymmetric),
18/// type hints, default values, and various modifiers (`static`, `readonly`, `abstract`, etc.).
19#[derive(Clone, Debug, PartialEq, Eq)]
20#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
21#[non_exhaustive]
22pub struct PropertyMetadata {
23 /// The identifier (name) of the property, including the leading '$'.
24 pub name: VariableIdentifier,
25
26 /// The specific source code location (span) of the property's name identifier itself.
27 /// `None` if the location is unknown or not relevant (e.g., for synthetic properties).
28 pub name_span: Option<Span>,
29
30 /// The source code location (span) covering the entire property declaration statement.
31 /// `None` if the location is unknown or not relevant.
32 pub span: Option<Span>,
33
34 /// The visibility level required for reading the property's value.
35 ///
36 /// In PHP, this corresponds to the primary visibility keyword specified
37 /// (e.g., the `public` in `public private(set) string $prop;`).
38 ///
39 /// If no asymmetric visibility is specified (e.g., `public string $prop`),
40 /// this level applies to both reading and writing. Defaults to `Public`.
41 pub read_visibility: Visibility,
42
43 /// The visibility level required for writing/modifying the property's value.
44 ///
45 /// In PHP, this can differ from `read_visibility` using asymmetric visibility syntax
46 /// like `private(set)` (e.g., `public private(set) string $prop;`).
47 ///
48 /// If asymmetric visibility is not used, this implicitly matches `read_visibility`.
49 /// Defaults to `Public`.
50 pub write_visibility: Visibility,
51
52 /// The explicit type declaration (type hint) associated with the property, if any.
53 ///
54 /// e.g., for `public string $name;`, this would contain the metadata for `string`.
55 pub type_declaration_metadata: Option<TypeMetadata>,
56
57 /// The type metadata for the property's type, if any.
58 ///
59 /// This is either the same as `type_declaration_metadata` or the type provided
60 /// in a docblock comment (e.g., `@var string`).
61 pub type_metadata: Option<TypeMetadata>,
62
63 /// The type accepted when writing to the property, when it differs from `type_metadata`.
64 ///
65 /// Only set for magic properties declaring split `@property-read` / `@property-write`
66 /// types; `None` means writes accept the same type that reads produce (`type_metadata`).
67 pub write_type_metadata: Option<TypeMetadata>,
68
69 /// The type inferred from the property's default value, if it has one.
70 ///
71 /// e.g., for `public $count = 0;`, this would contain the metadata for `int(0)`.
72 /// This can be used to compare against `type_signature` for consistency checks.
73 pub default_type_metadata: Option<TypeMetadata>,
74
75 /// Flags indicating various properties of the property.
76 pub flags: MetadataFlags,
77
78 #[cfg_attr(feature = "serde", serde(default))]
79 pub(crate) explicit_docblock_flags: MetadataFlags,
80
81 /// Attributes attached to the property declaration.
82 pub attributes: Vec<AttributeMetadata>,
83
84 /// Metadata for property hooks (get/set).
85 ///
86 /// Key is the hook name atom ("get" or "set").
87 /// Only present for PHP 8.4+ hooked properties.
88 pub hooks: WordMap<PropertyHookMetadata>,
89
90 /// PHP version range in which this property is available, derived from
91 /// `Mago\AvailableSince` / `Mago\AvailableUntil` attributes during
92 /// scanning.
93 pub version_constraint: VersionConstraint,
94}
95
96impl PropertyMetadata {
97 /// Creates new `PropertyMetadata` with basic defaults (public, non-static, non-readonly, etc.).
98 /// Name is mandatory. Spans, types, and flags can be set using modifier methods.
99 #[inline]
100 #[must_use]
101 pub fn new(name: VariableIdentifier, flags: MetadataFlags) -> Self {
102 Self {
103 name,
104 name_span: None,
105 span: None,
106 read_visibility: Visibility::Public,
107 write_visibility: Visibility::Public,
108 type_declaration_metadata: None,
109 type_metadata: None,
110 write_type_metadata: None,
111 default_type_metadata: None,
112 flags,
113 explicit_docblock_flags: MetadataFlags::empty(),
114 attributes: Vec::new(),
115 hooks: WordMap::default(),
116 version_constraint: VersionConstraint::unconstrained(),
117 }
118 }
119
120 /// Returns `true` when this property is available in the given PHP
121 /// version.
122 #[inline]
123 #[must_use]
124 pub fn is_available_in_version(&self, version: PHPVersion) -> bool {
125 self.version_constraint.allows_version(version)
126 }
127
128 /// Returns `true` when this property is available across the entire
129 /// supplied [`PHPVersionRange`].
130 #[inline]
131 #[must_use]
132 pub fn is_available_in_version_range(&self, range: PHPVersionRange) -> bool {
133 self.version_constraint.allows_version_range(range)
134 }
135
136 #[inline]
137 pub fn set_default_type_metadata(&mut self, default_type_metadata: Option<TypeMetadata>) {
138 self.default_type_metadata = default_type_metadata;
139 }
140
141 #[inline]
142 pub fn set_type_declaration_metadata(&mut self, type_declaration_metadata: Option<TypeMetadata>) {
143 if self.type_metadata.is_none() {
144 self.type_metadata.clone_from(&type_declaration_metadata);
145 }
146
147 self.type_declaration_metadata = type_declaration_metadata;
148 }
149
150 #[inline]
151 pub fn set_type_metadata(&mut self, type_metadata: Option<TypeMetadata>) {
152 self.type_metadata = type_metadata;
153 }
154
155 /// Returns the type accepted when writing to the property: the distinct write type
156 /// when one is declared, the read type otherwise.
157 #[inline]
158 #[must_use]
159 pub fn get_write_type_metadata(&self) -> Option<&TypeMetadata> {
160 self.write_type_metadata.as_ref().or(self.type_metadata.as_ref())
161 }
162
163 /// Returns a reference to the property's name identifier.
164 #[inline]
165 #[must_use]
166 pub fn get_name(&self) -> &VariableIdentifier {
167 &self.name
168 }
169
170 /// Checks if the property is effectively final (private read access).
171 ///
172 /// A property with `private(set)` (private write but public read) is NOT final
173 /// because child classes can still read and override it.
174 #[inline]
175 #[must_use]
176 pub fn is_final(&self) -> bool {
177 self.read_visibility.is_private()
178 }
179
180 /// Sets the span for the property name identifier.
181 #[inline]
182 pub fn set_name_span(&mut self, name_span: Option<Span>) {
183 self.name_span = name_span;
184 }
185
186 /// Sets the overall span for the property declaration.
187 #[inline]
188 pub fn set_span(&mut self, span: Option<Span>) {
189 self.span = span;
190 }
191
192 /// Sets both read and write visibility levels. Updates `is_asymmetric`. Ensures virtual properties remain symmetric.
193 #[inline]
194 pub fn set_visibility(&mut self, read: Visibility, write: Visibility) {
195 self.read_visibility = read;
196 self.write_visibility = write;
197 self.update_asymmetric();
198 }
199
200 /// Sets whether the property uses property hooks. Updates `is_asymmetric`.
201 #[inline]
202 pub fn set_is_virtual(&mut self, is_virtual: bool) {
203 self.flags.set(MetadataFlags::VIRTUAL_PROPERTY, is_virtual);
204
205 self.update_asymmetric();
206 }
207
208 /// Also ensures virtual properties are not asymmetric.
209 #[inline]
210 fn update_asymmetric(&mut self) {
211 if self.flags.is_virtual_property() {
212 if self.read_visibility != self.write_visibility {
213 // If virtual and somehow asymmetric, force symmetry (prefer read)
214 self.write_visibility = self.read_visibility;
215 }
216
217 self.flags &= !MetadataFlags::ASYMMETRIC_PROPERTY;
218 } else if self.read_visibility == self.write_visibility {
219 // If both visibilities are the same, ensure no asymmetric flag is set
220 self.flags &= !MetadataFlags::ASYMMETRIC_PROPERTY;
221 } else {
222 // Otherwise, set the asymmetric flag
223 self.flags |= MetadataFlags::ASYMMETRIC_PROPERTY;
224 }
225 }
226}