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 explicit type declaration (type hint) or docblock type (`@param`).
25    ///
26    /// Can be `None` if no type is specified.
27    pub type_metadata: Option<TypeMetadata>,
28
29    /// The type specified by a `@param-out` docblock tag.
30    ///
31    /// This indicates the expected type of a pass-by-reference parameter *after* the function executes.
32    pub out_type: Option<TypeMetadata>,
33
34    /// The inferred type of the parameter's default value, if `has_default` is true and the
35    /// type could be determined.
36    ///
37    /// `None` if there is no default or the default value's type couldn't be inferred.
38    pub default_type: Option<TypeMetadata>,
39
40    /// The source code location (span) covering the entire parameter declaration.
41    pub span: Span,
42
43    /// The specific source code location (span) of the parameter's name identifier.
44    pub name_span: Span,
45
46    /// Flags indicating various properties of the parameter.
47    pub flags: MetadataFlags,
48}
49
50/// Contains metadata associated with a single parameter within a function, method, or closure signature.
51///
52/// This captures details like the parameter's name, type hint, attributes, default value,
53/// pass-by-reference status, variadic nature, and other PHP features like property promotion.
54impl FunctionLikeParameterMetadata {
55    /// Creates new `FunctionLikeParameterMetadata` for a basic parameter.
56    /// Initializes most flags to false and optional fields to None.
57    ///
58    /// # Arguments
59    ///
60    /// * `name`: The identifier (name) of the parameter (e.g., `$userId`).
61    /// * `span`: The source code location covering the entire parameter declaration.
62    /// * `name_span`: The source code location of the parameter's name identifier (`$userId`).
63    pub fn new(name: VariableIdentifier, span: Span, name_span: Span, flags: MetadataFlags) -> Self {
64        Self {
65            attributes: Vec::new(),
66            name,
67            flags,
68            span,
69            name_span,
70            type_metadata: None,
71            out_type: None,
72            default_type: None,
73        }
74    }
75
76    /// Returns a reference to the parameter's name identifier (e.g., `$userId`).
77    #[inline]
78    pub fn get_name(&self) -> &VariableIdentifier {
79        &self.name
80    }
81
82    /// Returns the span covering the entire parameter declaration.
83    #[inline]
84    pub fn get_span(&self) -> Span {
85        self.span
86    }
87
88    /// Returns the span covering the parameter's name identifier.
89    #[inline]
90    pub fn get_name_span(&self) -> Span {
91        self.name_span
92    }
93
94    /// Returns a reference to the explicit type signature, if available.
95    #[inline]
96    pub fn get_type_metadata(&self) -> Option<&TypeMetadata> {
97        self.type_metadata.as_ref()
98    }
99
100    /// Returns a reference to the inferred type of the default value, if known.
101    #[inline]
102    pub fn get_default_type(&self) -> Option<&TypeMetadata> {
103        self.default_type.as_ref()
104    }
105
106    /// Sets the attributes, replacing any existing ones.
107    pub fn set_attributes(&mut self, attributes: impl IntoIterator<Item = AttributeMetadata>) {
108        self.attributes = attributes.into_iter().collect();
109    }
110
111    /// Returns a new instance with the attributes replaced.
112    pub fn with_attributes(mut self, attributes: impl IntoIterator<Item = AttributeMetadata>) -> Self {
113        self.set_attributes(attributes);
114        self
115    }
116
117    /// Sets the explicit type signature (type hint or `@param` type).
118    pub fn set_type_signature(&mut self, type_signature: Option<TypeMetadata>) {
119        self.type_metadata = type_signature;
120    }
121
122    /// Returns a new instance with the explicit type signature set.
123    pub fn with_type_signature(mut self, type_signature: Option<TypeMetadata>) -> Self {
124        self.set_type_signature(type_signature);
125        self
126    }
127}
128
129impl HasSpan for FunctionLikeParameterMetadata {
130    fn span(&self) -> Span {
131        self.span
132    }
133}