mago_reflection/function_like/
parameter.rs

1use serde::Deserialize;
2use serde::Serialize;
3
4use mago_interner::StringIdentifier;
5use mago_source::HasSource;
6use mago_source::SourceIdentifier;
7use mago_span::HasSpan;
8use mago_span::Span;
9
10use crate::attribute::AttributeReflection;
11use crate::r#type::TypeReflection;
12
13/// Represents a parameter in a function-like entity (such as a function or method),
14/// including its type, attributes, and various properties.
15#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
16pub struct FunctionLikeParameterReflection {
17    /// Attributes associated with the parameter, such as annotations or metadata.
18    pub attribute_reflections: Vec<AttributeReflection>,
19
20    /// The type of the parameter, if specified.
21    pub type_reflection: Option<TypeReflection>,
22
23    /// The name identifier of the parameter.
24    pub name: StringIdentifier,
25
26    /// Indicates whether the parameter accepts a variable number of arguments.
27    pub is_variadic: bool,
28
29    /// Indicates whether the parameter is passed by reference.
30    pub is_passed_by_reference: bool,
31
32    /// Indicates whether the parameter promotes a property in a constructor, typically used in PHP class constructors.
33    pub is_promoted_property: bool,
34
35    /// The default value of the parameter, if any, including its type and span in the source code.
36    pub default: Option<FunctionLikeParameterDefaultValueReflection>,
37
38    /// The span of the parameter in the source code.
39    pub span: Span,
40}
41
42impl HasSpan for FunctionLikeParameterReflection {
43    /// Returns the combined span of the parameter in the source code.
44    fn span(&self) -> Span {
45        self.span
46    }
47}
48
49impl HasSource for FunctionLikeParameterReflection {
50    /// Returns the source identifier of the file containing this parameter.
51    fn source(&self) -> SourceIdentifier {
52        self.span().source()
53    }
54}
55
56/// Represents the default value of a function-like parameter, including its inferred type
57/// and the source code span where it is defined.
58#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
59pub struct FunctionLikeParameterDefaultValueReflection {
60    /// The inferred type of the default value.
61    /// This type is determined based on the default value itself.
62    pub type_reflection: TypeReflection,
63
64    /// The span in the source code where the default value is located.
65    pub span: Span,
66}
67
68impl HasSpan for FunctionLikeParameterDefaultValueReflection {
69    /// Returns the span of the default value in the source code.
70    ///
71    /// This span indicates where the default value is defined within the parameter declaration.
72    fn span(&self) -> Span {
73        self.span
74    }
75}
76
77impl HasSource for FunctionLikeParameterDefaultValueReflection {
78    /// Returns the source identifier of the file containing this default value.
79    fn source(&self) -> SourceIdentifier {
80        self.span.source()
81    }
82}