-
Notifications
You must be signed in to change notification settings - Fork 1
/
MethodParameterStaticTargetPoint.php
170 lines (145 loc) · 4.37 KB
/
MethodParameterStaticTargetPoint.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* This file is part of the opportus/object-mapper project.
*
* Copyright (c) 2018-2022 Clément Cazaud <clement.cazaud@gmail.com>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
namespace Opportus\ObjectMapper\Point;
use Opportus\ObjectMapper\Exception\InvalidArgumentException;
use ReflectionException;
use ReflectionParameter;
/**
* The method parameter static target point.
*
* @package Opportus\ObjectMapper\Point
* @author Clément Cazaud <clement.cazaud@gmail.com>
* @license https://github.com/opportus/object-mapper/blob/master/LICENSE MIT
*/
class MethodParameterStaticTargetPoint extends TargetPoint implements StaticTargetPointInterface
{
private const FQN_REGEX_PATTERN = '/^#?([A-Za-z0-9\\\_]+)::([A-Za-z0-9_]+)\(\)::\$([A-Za-z0-9_]+)$/';
/**
* @var string $methodName
*/
private $methodName;
/**
* @var string[] $valuePhpTypes
*/
private $valuePhpTypes;
/**
* @var string[] $valuePhpDocTypes
*/
private $valuePhpDocTypes;
/**
* Constructs the method parameter static target point.
*
* @param string $fqn
* @throws InvalidArgumentException
*/
public function __construct(string $fqn)
{
if (!\preg_match(self::FQN_REGEX_PATTERN, $fqn, $matches)) {
$message = \sprintf(
'%s is not a method parameter static target point as FQN of such is expected to have the following syntax: %s.',
$fqn,
self::FQN_REGEX_PATTERN
);
throw new InvalidArgumentException(1, $message);
}
[
$matchedFqn,
$matchedTargetFqn,
$matchedMethodName,
$matchedName
] = $matches;
try {
/** @noinspection PhpParamsInspection */
$reflection = new ReflectionParameter(
[$matchedTargetFqn, $matchedMethodName],
$matchedName
);
} catch (ReflectionException $exception) {
$message = \sprintf(
'%s is not a method parameter static target point. %s.',
$fqn,
$exception->getMessage()
);
throw new InvalidArgumentException(1, $message);
}
$this->fqn = \sprintf('#%s', \ltrim($matchedFqn, '#'));
$this->targetFqn = $matchedTargetFqn;
$this->name = $matchedName;
$this->methodName = $matchedMethodName;
$this->valuePhpTypes = $this->resolveValuePhpTypes($reflection);
$this->valuePhpDocTypes = $this->resolveValuePhpDocTypes($reflection);
}
/**
* Gets the name of the method of the point.
*
* @return string
*/
public function getMethodName(): string
{
return $this->methodName;
}
/**
* {@inheritdoc}
*/
public static function getFqnRegexPattern(): string
{
return self::FQN_REGEX_PATTERN;
}
/**
* {@inheritdoc}
*/
public function getValuePhpTypes(): array
{
return $this->valuePhpTypes;
}
/**
* {@inheritdoc}
*/
public function getValuePhpDocTypes(): array
{
return $this->valuePhpDocTypes;
}
/**
* @param ReflectionParameter $reflection
* @return string[]
*/
private function resolveValuePhpTypes(ReflectionParameter $reflection): array
{
if (null === $reflection->getType()) {
return [];
}
$types = [];
foreach (\explode('|', $reflection->getType()->getName()) as $type) {
if (0 === \strpos($type, '?')) {
$types['null'] = 'null';
$types[\ltrim($type, '?')] = \ltrim($type, '?');
continue;
}
$types[$type] = $type;
}
return \array_values($types);
}
/**
* @param ReflectionParameter $reflection
* @return string[]
*/
private function resolveValuePhpDocTypes(ReflectionParameter $reflection): array
{
\preg_match_all(
'/@param ([^\r\n ]+)/s',
$reflection->getDeclaringFunction()->getDocComment(),
$matches
);
if (false === isset($matches[1][$reflection->getPosition()])) {
return [];
}
return \explode('|', $matches[1][$reflection->getPosition()]);
}
}