Repository navigation
Expand file tree
/
Copy pathMethodInvocation.php
More file actions
143 lines (131 loc) · 3.54 KB
/
Copy pathMethodInvocation.php
File metadata and controls
143 lines (131 loc) · 3.54 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace Okapi\Aop\Invocation;
use Okapi\Aop\Advice\AdviceType;
/**
* # Method invocation
*
* Base class for all method invocations.
*/
abstract class MethodInvocation
{
/**
* MethodInvocation constructor.
*
* @param object|null $subject
* @param string $className
* @param string $methodName
* @param mixed $result
* @param array $arguments
*/
public function __construct(
private readonly ?object $subject,
private readonly string $className,
private readonly string $methodName,
protected mixed $result,
private array &$arguments,
) {}
/**
* Get an argument by name or index.
*
* @param int|string $nameOrIndex
*
* @return mixed
*/
public function getArgument(int|string $nameOrIndex): mixed
{
if (is_int($nameOrIndex)) {
return array_values($this->arguments)[$nameOrIndex] ?? null;
} else {
return $this->arguments[$nameOrIndex] ?? null;
}
}
/**
* Set an argument by name or index.
*
* @param int|string $nameOrIndex
* @param mixed $value
*
* @return void
*/
public function setArgument(int|string $nameOrIndex, mixed $value): void
{
if (is_int($nameOrIndex)) {
$this->arguments[array_keys($this->arguments)[$nameOrIndex]] = $value;
} else {
$this->arguments[$nameOrIndex] = $value;
}
}
/**
* Get all arguments.
*
* @return array<string, mixed>
*/
public function getArguments(): array
{
return $this->arguments;
}
/**
* Set new arguments.
*
* @param array<string, mixed> $arguments
*
* @return void
*/
public function setArguments(array $arguments): void
{
$this->arguments = $arguments;
}
/**
* Get the advice type of the invocation.
*
* @return AdviceType
*
* @noinspection PhpUndefinedClassInspection
*/
public function getAdviceType(): AdviceType
{
return match (true) {
$this instanceof AroundMethodInvocation => AdviceType::Around,
$this instanceof BeforeMethodInvocation => AdviceType::Before,
$this instanceof AfterMethodInvocation => AdviceType::After,
// TODO: Implement
$this instanceof AfterReturningMethodInvocation => AdviceType::AfterReturning,
// TODO: Implement
$this instanceof AfterThrowingMethodInvocation => AdviceType::AfterThrowing,
};
}
/**
* Get the original subject class of the invocation.
*
* If the subject is a static method, this will return {@see null}.
*
* @return object|null
*/
public function getSubject(): ?object
{
return $this->subject;
}
/** Access the subject's properties, optionally in an original declaring scope. */
public function properties(?string $declaringClass = null): PropertyAccessor
{
return new PropertyAccessor($this->subject ?? $this->className, $declaringClass);
}
/**
* Get the original subject class name of the invocation.
*
* @return string
*/
public function getClassName(): string
{
return $this->className;
}
/**
* Get the original subject method name of the invocation.
*
* @return string
*/
public function getMethodName(): string
{
return $this->methodName;
}
}