mago_reflection/function_like/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
use serde::Deserialize;
use serde::Serialize;
use mago_span::Span;
use crate::attribute::AttributeReflection;
use crate::class_like::member::ClassLikeMemberVisibilityReflection;
use crate::function_like::parameter::FunctionLikeParameterReflection;
use crate::function_like::r#return::FunctionLikeReturnTypeReflection;
use crate::identifier::FunctionLikeName;
use crate::r#type::kind::Template;
pub mod parameter;
pub mod r#return;
/// Represents reflection data for a function-like entity, such as a function or method.
///
/// This includes details about its parameters, return type, attributes, and various properties
/// like visibility, overrides, and whether it supports specific PHP features.
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
pub struct FunctionLikeReflection {
/// Attributes associated with this function-like entity.
pub attribute_reflections: Vec<AttributeReflection>,
/// Visibility information for this function-like if it is a class member.
pub visibility_reflection: Option<ClassLikeMemberVisibilityReflection>,
/// The unique identifier for this function or method.
pub name: FunctionLikeName,
/// The list of templates accepted by this function or method.
pub templates: Vec<Template>,
/// The list of parameters accepted by this function or method, including their types and attributes.
pub parameters: Vec<FunctionLikeParameterReflection>,
/// The return type of this function or method, if specified.
pub return_type_reflection: Option<FunctionLikeReturnTypeReflection>,
/// Indicates whether the function or method returns by reference.
pub returns_by_reference: bool,
/// Flags if the function or method contains a `yield` expression, indicating it is a generator.
pub has_yield: bool,
/// Flags if the function or method has the potential to throw an exception.
pub has_throws: bool,
/// Indicates if this function-like entity is anonymous (i.e., a closure or an anonymous function).
///
/// For functions and methods, this is always `false`.
pub is_anonymous: bool,
/// Indicates if this function or method is static.
///
/// This is always `false` for functions; for closures, arrow functions, and methods, it depends on their declaration.
pub is_static: bool,
/// Indicates if this function or method is declared as final.
///
/// This is always `true` for functions, arrow functions, and closures. For methods, it depends on the declaration.
pub is_final: bool,
/// Indicates if this function or method is abstract.
///
/// Always `false` for functions, arrow functions, and closures. For methods, it depends on the declaration.
pub is_abstract: bool,
/// Indicates if this function or method is pure.
pub is_pure: bool,
/// Flags if this function or method overrides a method from a parent class.
///
/// Always `false` for functions, arrow functions, and closures. For methods,
/// it depends on whether they override a parent method.
pub is_overriding: bool,
/// The span in the source code where this function or method is defined.
pub span: Span,
/// Indicate if this function-like entity is populated.
pub is_populated: bool,
}
impl FunctionLikeReflection {
pub fn is_function(&self) -> bool {
matches!(self.name, FunctionLikeName::Function(_))
}
pub fn is_method(&self) -> bool {
matches!(self.name, FunctionLikeName::Method(_, _))
}
pub fn is_property_hook(&self) -> bool {
matches!(self.name, FunctionLikeName::PropertyHook(_, _, _))
}
pub fn is_closure(&self) -> bool {
matches!(self.name, FunctionLikeName::Closure(_))
}
pub fn is_arrow_function(&self) -> bool {
matches!(self.name, FunctionLikeName::ArrowFunction(_))
}
}