mago_reflection/
attribute.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
use serde::Deserialize;
use serde::Serialize;

use mago_source::HasSource;
use mago_source::SourceIdentifier;
use mago_span::HasSpan;
use mago_span::Span;

use crate::identifier::Name;
use crate::r#type::TypeReflection;

/// Represents an attribute applied to a class, function, or other PHP constructs.
///
/// Attributes provide metadata for the construct they are applied to, often used
/// for annotations or configuration in frameworks and libraries.
///
/// Example:
///
/// ```php
/// #[Foo, Bar]
/// class Example {}
/// ```
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
pub struct AttributeReflection {
    /// The name of the attribute.
    pub name: Name,

    /// Optional list of arguments provided to the attribute.
    pub arguments: Option<AttributeArgumentListReflection>,

    /// The span of the attribute in the source code.
    pub span: Span,
}

/// Represents a list of arguments for an attribute.
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
pub struct AttributeArgumentListReflection {
    /// The list of arguments for the attribute.
    pub arguments: Vec<AttributeArgumentReflection>,
}

/// Represents a single argument in an attribute.
///
/// Arguments can either be positional (e.g., `#[Foo("value")]`) or named (e.g., `#[Foo(name: "value")]`).
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
pub enum AttributeArgumentReflection {
    /// A positional argument, such as `#[Foo("value")]`.
    Positional {
        /// The type reflection of the argument's value.
        value_type_reflection: TypeReflection,

        /// The span of the argument in the source code.
        span: Span,
    },

    /// A named argument, such as `#[Foo(name: "value")]`.
    Named {
        /// The name of the argument.
        name: Name,

        /// The type reflection of the argument's value.
        value_type_reflection: TypeReflection,

        /// The span of the argument in the source code.
        span: Span,
    },
}

impl HasSource for AttributeReflection {
    /// Returns the source identifier of the file containing this attribute.
    fn source(&self) -> SourceIdentifier {
        self.span.source()
    }
}

impl HasSpan for AttributeReflection {
    /// Returns the span of the attribute in the source code.
    ///
    /// This includes the entire range of the attribute, including its arguments if present.
    fn span(&self) -> Span {
        self.span
    }
}