-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParameterSource.php
124 lines (98 loc) · 2.57 KB
/
ParameterSource.php
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
<?php declare(strict_types = 1);
namespace Orisai\SourceMap;
use DateTimeImmutable;
use Orisai\SourceMap\Exception\InvalidSource;
use ReflectionException;
use ReflectionFunction;
use ReflectionMethod;
use ReflectionParameter;
use Throwable;
use function assert;
/**
* @readonly
*/
final class ParameterSource implements ReflectorSource
{
private ReflectionParameter $reflector;
private ?Throwable $failure;
public function __construct(ReflectionParameter $reflector)
{
$this->reflector = $reflector;
$this->failure = null;
}
/**
* @return FunctionSource|MethodSource
*/
public function getFunction(): Source
{
$this->throwIfInvalid();
$reflector = $this->reflector->getDeclaringFunction();
if ($reflector instanceof ReflectionMethod) {
return new MethodSource($reflector);
}
assert($reflector instanceof ReflectionFunction);
return new FunctionSource($reflector);
}
public function getReflector(): ReflectionParameter
{
$this->throwIfInvalid();
return $this->reflector;
}
public function toString(): string
{
return $this->getFunction()->toString(
[
$this->reflector->getName(),
],
);
}
public function isValid(): bool
{
return $this->failure === null;
}
private function throwIfInvalid(): void
{
if ($this->failure === null) {
return;
}
throw $this->failure;
}
public function getLastChange(): DateTimeImmutable
{
return $this->getFunction()->getLastChange();
}
public function __toString(): string
{
return $this->toString();
}
public function __serialize(): array
{
$this->throwIfInvalid();
$class = $this->reflector->getDeclaringClass();
return [
'class' => $class !== null ? $class->getName() : null,
'function' => $this->reflector->getDeclaringFunction()->getName(),
'parameter' => $this->reflector->getName(),
];
}
public function __unserialize(array $data): void
{
$class = $data['class'];
$function = $data['function'];
$parameter = $data['parameter'];
try {
$this->reflector = $class !== null
? new ReflectionParameter([$class, $function], $parameter)
: new ReflectionParameter($function, $parameter);
$this->failure = null;
} catch (ReflectionException $exception) {
$message = $exception->getMessage();
if ($message === 'The parameter specified by its name could not be found') {
$message = "Parameter {$data['class']}::{$data['function']}({$data['parameter']}) does not exist";
}
$this->failure = InvalidSource::create($this)
->withMessage("Deserialization failed due to following error:\n$message")
->withPrevious($exception);
}
}
}