Skip to content

Commit 267bb6d

Browse files
committed
ValidationCallback: simplify internal reflection
1 parent 6f4dd7d commit 267bb6d

File tree

3 files changed

+19
-56
lines changed

3 files changed

+19
-56
lines changed

src/Callbacks/AfterValidationCallback.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Orisai\ObjectMapper\Callbacks;
44

55
use Orisai\Exceptions\Logic\InvalidArgument;
6-
use ReflectionClass;
76
use ReflectionMethod;
87
use ReflectionParameter;
98
use function in_array;
@@ -13,7 +12,6 @@ final class AfterValidationCallback extends ValidationCallback
1312
{
1413

1514
protected static function validateClassMethodDataParam(
16-
ReflectionClass $class,
1715
ReflectionMethod $method,
1816
ReflectionParameter $paramData
1917
): void
@@ -27,13 +25,13 @@ protected static function validateClassMethodDataParam(
2725
throw InvalidArgument::create()
2826
->withMessage(sprintf(
2927
'First parameter of class callback method %s::%s should have "array" type instead of %s',
30-
$class->getName(),
28+
$method->getDeclaringClass()->getName(),
3129
$method->getName(),
3230
$type ?? 'none',
3331
));
3432
}
3533

36-
protected static function validateClassMethodReturn(ReflectionClass $class, ReflectionMethod $method): void
34+
protected static function validateClassMethodReturn(ReflectionMethod $method): void
3735
{
3836
$type = self::getTypeName($method->getReturnType());
3937

@@ -44,14 +42,13 @@ protected static function validateClassMethodReturn(ReflectionClass $class, Refl
4442
throw InvalidArgument::create()
4543
->withMessage(sprintf(
4644
'Return type of class callback method %s::%s should be "array", "void" or "never" instead of %s',
47-
$class->getName(),
45+
$method->getDeclaringClass()->getName(),
4846
$method->getName(),
4947
$type ?? 'none',
5048
));
5149
}
5250

5351
protected static function validatePropertyMethodDataParam(
54-
ReflectionClass $class,
5552
ReflectionMethod $method,
5653
ReflectionParameter $paramData
5754
): void

src/Callbacks/BeforeValidationCallback.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Orisai\ObjectMapper\Callbacks;
44

55
use Orisai\Exceptions\Logic\InvalidArgument;
6-
use ReflectionClass;
76
use ReflectionMethod;
87
use ReflectionParameter;
98
use function in_array;
@@ -13,7 +12,6 @@ final class BeforeValidationCallback extends ValidationCallback
1312
{
1413

1514
protected static function validateClassMethodDataParam(
16-
ReflectionClass $class,
1715
ReflectionMethod $method,
1816
ReflectionParameter $paramData
1917
): void
@@ -27,19 +25,18 @@ protected static function validateClassMethodDataParam(
2725
throw InvalidArgument::create()
2826
->withMessage(sprintf(
2927
'First parameter of class callback method %s::%s should have "mixed" or none type instead of %s',
30-
$class->getName(),
28+
$method->getDeclaringClass()->getName(),
3129
$method->getName(),
3230
$type,
3331
));
3432
}
3533

36-
protected static function validateClassMethodReturn(ReflectionClass $class, ReflectionMethod $method): void
34+
protected static function validateClassMethodReturn(ReflectionMethod $method): void
3735
{
3836
// Any type is okay
3937
}
4038

4139
protected static function validatePropertyMethodDataParam(
42-
ReflectionClass $class,
4340
ReflectionMethod $method,
4441
ReflectionParameter $paramData
4542
): void
@@ -53,7 +50,7 @@ protected static function validatePropertyMethodDataParam(
5350
throw InvalidArgument::create()
5451
->withMessage(sprintf(
5552
'First parameter of before field callback method %s::%s should have none or "mixed" type instead of %s',
56-
$class->getName(),
53+
$method->getDeclaringClass()->getName(),
5754
$method->getName(),
5855
$type,
5956
));

src/Callbacks/ValidationCallback.php

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private static function validateMethod(
101101
{
102102
$method = self::validateMethodExistence($class, $methodName);
103103

104-
self::validateMethodSignature($method, $class, $property);
104+
self::validateMethodSignature($method, $property);
105105

106106
return $method;
107107
}
@@ -132,34 +132,29 @@ private static function validateMethodExistence(ReflectionClass $class, string $
132132
return $class->getMethod($methodName);
133133
}
134134

135-
/**
136-
* @param ReflectionClass<MappedObject> $class
137-
*/
138135
private static function validateMethodSignature(
139136
ReflectionMethod $method,
140-
ReflectionClass $class,
141137
?ReflectionProperty $property
142138
): void
143139
{
144-
[$paramData, $paramContext] = self::validateParametersCount($class, $method);
140+
[$paramData, $paramContext] = self::validateParametersCount($method);
145141

146142
$property === null
147-
? self::validateClassMethodSignature($class, $method, $paramData, $paramContext)
148-
: self::validatePropertyMethodSignature($class, $method, $paramData, $paramContext);
143+
? self::validateClassMethodSignature($method, $paramData, $paramContext)
144+
: self::validatePropertyMethodSignature($method, $paramData, $paramContext);
149145
}
150146

151147
/**
152-
* @param ReflectionClass<MappedObject> $class
153148
* @return array{ReflectionParameter|null, ReflectionParameter|null}
154149
*/
155-
private static function validateParametersCount(ReflectionClass $class, ReflectionMethod $method): array
150+
private static function validateParametersCount(ReflectionMethod $method): array
156151
{
157152
$requiredCount = $method->getNumberOfRequiredParameters();
158153
if ($requiredCount > 2) {
159154
throw InvalidArgument::create()
160155
->withMessage(sprintf(
161156
'Callback method %s::%s should have only 2 required parameters, %s required parameters given',
162-
$class->getName(),
157+
$method->getDeclaringClass()->getName(),
163158
$method->getName(),
164159
$requiredCount,
165160
));
@@ -176,49 +171,34 @@ private static function validateParametersCount(ReflectionClass $class, Reflecti
176171
/**
177172
* beforeClass(<nothing>|mixed $data, MappedObjectContext $context): <anything>
178173
* afterClass(array $data, MappedObjectContext $context): array|void|never
179-
*
180-
* @param ReflectionClass<MappedObject> $class
181174
*/
182175
private static function validateClassMethodSignature(
183-
ReflectionClass $class,
184176
ReflectionMethod $method,
185177
?ReflectionParameter $paramData,
186178
?ReflectionParameter $paramContext
187179
): void
188180
{
189181
if ($paramData !== null) {
190-
static::validateClassMethodDataParam($class, $method, $paramData);
182+
static::validateClassMethodDataParam($method, $paramData);
191183
}
192184

193185
if ($paramContext !== null) {
194-
self::validateClassMethodContextParam($class, $method, $paramContext);
186+
self::validateClassMethodContextParam($method, $paramContext);
195187
}
196188

197-
static::validateClassMethodReturn($class, $method);
189+
static::validateClassMethodReturn($method);
198190
}
199191

200-
/**
201-
* @param ReflectionClass<MappedObject> $class
202-
*/
203192
abstract protected static function validateClassMethodDataParam(
204-
ReflectionClass $class,
205193
ReflectionMethod $method,
206194
ReflectionParameter $paramData
207195
): void;
208196

209-
/**
210-
* @param ReflectionClass<MappedObject> $class
211-
*/
212197
abstract protected static function validateClassMethodReturn(
213-
ReflectionClass $class,
214198
ReflectionMethod $method
215199
): void;
216200

217-
/**
218-
* @param ReflectionClass<MappedObject> $class
219-
*/
220201
private static function validateClassMethodContextParam(
221-
ReflectionClass $class,
222202
ReflectionMethod $method,
223203
ReflectionParameter $paramContext
224204
): void
@@ -230,7 +210,7 @@ private static function validateClassMethodContextParam(
230210
throw InvalidArgument::create()
231211
->withMessage(sprintf(
232212
'Second parameter of class callback method %s::%s should have "%s" type instead of %s',
233-
$class->getName(),
213+
$method->getDeclaringClass()->getName(),
234214
$method->getName(),
235215
ObjectContext::class,
236216
$type ?? 'none',
@@ -241,39 +221,28 @@ private static function validateClassMethodContextParam(
241221
/**
242222
* beforeField(<nothing>|mixed $data, FieldContext $context): <anything>
243223
* afterField(<anything> $data, FieldContext $context): <anything>
244-
*
245-
* @param ReflectionClass<MappedObject> $class
246224
*/
247225
private static function validatePropertyMethodSignature(
248-
ReflectionClass $class,
249226
ReflectionMethod $method,
250227
?ReflectionParameter $paramData,
251228
?ReflectionParameter $paramContext
252229
): void
253230
{
254231
if ($paramData !== null) {
255-
static::validatePropertyMethodDataParam($class, $method, $paramData);
232+
static::validatePropertyMethodDataParam($method, $paramData);
256233
}
257234

258235
if ($paramContext !== null) {
259-
self::validatePropertyMethodContextParam($class, $method, $paramContext);
236+
self::validatePropertyMethodContextParam($method, $paramContext);
260237
}
261238
}
262239

263-
/**
264-
* @param ReflectionClass<MappedObject> $class
265-
*/
266240
abstract protected static function validatePropertyMethodDataParam(
267-
ReflectionClass $class,
268241
ReflectionMethod $method,
269242
ReflectionParameter $paramData
270243
): void;
271244

272-
/**
273-
* @param ReflectionClass<MappedObject> $class
274-
*/
275245
private static function validatePropertyMethodContextParam(
276-
ReflectionClass $class,
277246
ReflectionMethod $method,
278247
ReflectionParameter $paramContext
279248
): void
@@ -287,7 +256,7 @@ private static function validatePropertyMethodContextParam(
287256
throw InvalidArgument::create()
288257
->withMessage(sprintf(
289258
'Second parameter of field callback method %s::%s should have "%s" type instead of %s',
290-
$class->getName(),
259+
$method->getDeclaringClass()->getName(),
291260
$method->getName(),
292261
FieldContext::class,
293262
$type ?? 'none',

0 commit comments

Comments
 (0)