mago_codex/metadata/
parameter.rs

1use serde::Deserialize;
2use serde::Serialize;
3
4use mago_span::HasSpan;
5use mago_span::Span;
6
7use crate::metadata::attribute::AttributeMetadata;
8use crate::metadata::flags::MetadataFlags;
9use crate::metadata::ttype::TypeMetadata;
10use crate::misc::VariableIdentifier;
11
12/// Contains metadata associated with a single parameter within a function, method, or closure signature.
13///
14/// This captures details like the parameter's name, type hint, attributes, default value,
15/// pass-by-reference status, variadic nature, and other PHP features like property promotion.
16#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
17pub struct FunctionLikeParameterMetadata {
18    /// Attributes attached to the parameter declaration.
19    pub attributes: Vec<AttributeMetadata>,
20
21    /// The identifier (name) of the parameter, including the leading '$'.
22    pub name: VariableIdentifier,
23
24    /// The native type declaration from the function signature.
25    ///
26    /// This is the type hint specified in the code (e.g., `string $name`), not from docblocks.
27    /// Can be `None` if no type hint is specified in the signature.
28    pub type_declaration_metadata: Option<TypeMetadata>,
29
30    /// The explicit type declaration (type hint) or docblock type (`@param`).
31    ///
32    /// If there's a docblock `@param` annotation, this will contain that type (with `from_docblock=true`).
33    /// Otherwise, this will be the same as `type_declaration_metadata`.
34    /// Can be `None` if no type is specified.
35    pub type_metadata: Option<TypeMetadata>,
36
37    /// The type specified by a `@param-out` docblock tag.
38    ///
39    /// This indicates the expected type of a pass-by-reference parameter *after* the function executes.
40    pub out_type: Option<TypeMetadata>,
41
42    /// The inferred type of the parameter's default value, if `has_default` is true and the
43    /// type could be determined.
44    ///
45    /// `None` if there is no default or the default value's type couldn't be inferred.
46    pub default_type: Option<TypeMetadata>,
47
48    /// The source code location (span) covering the entire parameter declaration.
49    pub span: Span,
50
51    /// The specific source code location (span) of the parameter's name identifier.
52    pub name_span: Span,
53
54    /// Flags indicating various properties of the parameter.
55    pub flags: MetadataFlags,
56}
57
58/// Contains metadata associated with a single parameter within a function, method, or closure signature.
59///
60/// This captures details like the parameter's name, type hint, attributes, default value,
61/// pass-by-reference status, variadic nature, and other PHP features like property promotion.
62impl FunctionLikeParameterMetadata {
63    /// Creates new `FunctionLikeParameterMetadata` for a basic parameter.
64    /// Initializes most flags to false and optional fields to None.
65    ///
66    /// # Arguments
67    ///
68    /// * `name`: The identifier (name) of the parameter (e.g., `$userId`).
69    /// * `span`: The source code location covering the entire parameter declaration.
70    /// * `name_span`: The source code location of the parameter's name identifier (`$userId`).
71    #[must_use]
72    pub fn new(name: VariableIdentifier, span: Span, name_span: Span, flags: MetadataFlags) -> Self {
73        Self {
74            attributes: Vec::new(),
75            name,
76            flags,
77            span,
78            name_span,
79            type_declaration_metadata: None,
80            type_metadata: None,
81            out_type: None,
82            default_type: None,
83        }
84    }
85
86    /// Returns a reference to the parameter's name identifier (e.g., `$userId`).
87    #[inline]
88    #[must_use]
89    pub fn get_name(&self) -> &VariableIdentifier {
90        &self.name
91    }
92
93    /// Returns the span covering the entire parameter declaration.
94    #[inline]
95    #[must_use]
96    pub fn get_span(&self) -> Span {
97        self.span
98    }
99
100    /// Returns the span covering the parameter's name identifier.
101    #[inline]
102    #[must_use]
103    pub fn get_name_span(&self) -> Span {
104        self.name_span
105    }
106
107    /// Returns a reference to the parameter's type metadata (effective type with docblock).
108    #[inline]
109    #[must_use]
110    pub fn get_type_metadata(&self) -> Option<&TypeMetadata> {
111        self.type_metadata.as_ref()
112    }
113
114    /// Returns a reference to the parameter's native type declaration metadata.
115    #[inline]
116    #[must_use]
117    pub fn get_type_declaration_metadata(&self) -> Option<&TypeMetadata> {
118        self.type_declaration_metadata.as_ref()
119    }
120
121    /// Returns a reference to the inferred type of the default value, if known.
122    #[inline]
123    #[must_use]
124    pub fn get_default_type(&self) -> Option<&TypeMetadata> {
125        self.default_type.as_ref()
126    }
127
128    /// Sets the attributes, replacing any existing ones.
129    pub fn set_attributes(&mut self, attributes: impl IntoIterator<Item = AttributeMetadata>) {
130        self.attributes = attributes.into_iter().collect();
131    }
132
133    /// Returns a new instance with the attributes replaced.
134    pub fn with_attributes(mut self, attributes: impl IntoIterator<Item = AttributeMetadata>) -> Self {
135        self.set_attributes(attributes);
136        self
137    }
138
139    /// Sets the parameter's type metadata (effective type with docblock).
140    #[inline]
141    pub fn set_type_metadata(&mut self, type_metadata: Option<TypeMetadata>) {
142        self.type_metadata = type_metadata;
143    }
144
145    /// Sets the parameter's native type declaration metadata.
146    ///
147    /// If `type_metadata` is not set, it will be initialized with the same value.
148    #[inline]
149    pub fn set_type_declaration_metadata(&mut self, type_declaration: Option<TypeMetadata>) {
150        if self.type_metadata.is_none() {
151            self.type_metadata.clone_from(&type_declaration);
152        }
153
154        self.type_declaration_metadata = type_declaration;
155    }
156}
157
158impl HasSpan for FunctionLikeParameterMetadata {
159    fn span(&self) -> Span {
160        self.span
161    }
162}