Skip to main content

mago_codex/metadata/
property_hook.rs

1use mago_reporting::Issue;
2use mago_span::Span;
3use mago_word::Word;
4
5use crate::metadata::attribute::AttributeMetadata;
6use crate::metadata::flags::MetadataFlags;
7use crate::metadata::parameter::FunctionLikeParameterMetadata;
8use crate::metadata::ttype::TypeMetadata;
9
10/// Metadata for a property hook (get or set).
11///
12/// PHP 8.4 introduced property hooks, which allow defining custom get/set behavior
13/// for properties. This struct stores the metadata for a single hook.
14#[derive(Clone, Debug, PartialEq, Eq)]
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16#[non_exhaustive]
17pub struct PropertyHookMetadata {
18    /// The hook name ("get" or "set").
19    pub name: Word,
20
21    /// Span of the hook declaration.
22    pub span: Span,
23
24    /// Hook modifiers (final, etc.).
25    pub flags: MetadataFlags,
26
27    /// For set hooks: the parameter (explicit or implicit $value).
28    /// None for get hooks.
29    pub parameter: Option<FunctionLikeParameterMetadata>,
30
31    /// Whether the hook returns by reference (&get).
32    pub returns_by_ref: bool,
33
34    /// Whether this is an abstract hook (no body, just semicolon).
35    pub is_abstract: bool,
36
37    /// Attributes on the hook.
38    pub attributes: Vec<AttributeMetadata>,
39
40    /// Return type from @return docblock (for get hooks).
41    pub return_type_metadata: Option<TypeMetadata>,
42
43    /// Whether this hook has a docblock comment.
44    pub has_docblock: bool,
45
46    /// Issues from parsing the docblock.
47    pub issues: Vec<Issue>,
48}
49
50impl PropertyHookMetadata {
51    /// Returns whether this is a get hook.
52    #[inline]
53    #[must_use]
54    pub fn is_get(&self) -> bool {
55        self.name.as_bytes() == b"get"
56    }
57
58    /// Takes the issues, leaving an empty vector.
59    #[inline]
60    pub fn take_issues(&mut self) -> Vec<Issue> {
61        std::mem::take(&mut self.issues)
62    }
63}