Skip to content

Commit 7df216e

Browse files
committed
Refactor - Improve default value handling and return type detection in StopwatchDecorator
1 parent 4648e56 commit 7df216e

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

src/Decorator/StopwatchDecorator.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -231,39 +231,41 @@ private function getParameterDefaultValue(\ReflectionParameter $param): string
231231

232232
private function getDefaultValueString(\ReflectionParameter $param): string
233233
{
234+
$result = '';
235+
234236
try {
235-
if (!$param->isDefaultValueAvailable()) {
236-
return '';
237+
if ($param->isDefaultValueAvailable()) {
238+
/** @psalm-suppress MixedAssignment */
239+
$defaultValue = $param->getDefaultValue();
240+
$result = ' = ' . var_export($defaultValue, true);
237241
}
238-
239-
/** @psalm-suppress MixedAssignment */
240-
$defaultValue = $param->getDefaultValue();
241-
242-
return ' = ' . var_export($defaultValue, true);
243242
} catch (Throwable) {
244243
// Default value cannot be determined for internal classes or certain parameter types
245-
return '';
244+
$result = '';
246245
}
246+
247+
return $result;
247248
}
248249

249250
/**
250251
* @return array{0: string, 1: bool}
251252
*/
252253
private function getMethodReturnType(ReflectionMethod $method): array
253254
{
254-
if (!$method->hasReturnType()) {
255-
return ['', false];
256-
}
255+
$returnType = '';
256+
$isVoid = false;
257257

258-
$type = $method->getReturnType();
258+
if ($method->hasReturnType()) {
259+
$type = $method->getReturnType();
259260

260-
if ($type === null) {
261-
return ['', false];
261+
if ($type !== null) {
262+
$typeString = (string)$type;
263+
$returnType = ': ' . $typeString;
264+
$isVoid = $typeString === 'void';
265+
}
262266
}
263267

264-
$typeString = (string)$type;
265-
266-
return [': ' . $typeString, $typeString === 'void'];
268+
return [$returnType, $isVoid];
267269
}
268270

269271
private function generateMethodBody(string $methodName, string $argsList, bool $isVoid): string

tests/Integration/Decorator/StopwatchDecoratorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ public function testThatDecoratorDoesNotTryToDecorateEntityObjects(): void
156156
}
157157

158158
/**
159-
* @return Generator<array{0: string, 1: object}>
159+
* @return Generator<array-key, array{0: class-string, 1: object}>
160160
*/
161161
public static function dataProviderTestThatDecorateMethodReturnsExpected(): Generator
162162
{
163-
yield ['object', new EntityReferenceExists()];
163+
yield [EntityReferenceExists::class, new EntityReferenceExists()];
164164
yield [stdClass::class, new stdClass()];
165165
}
166166
}

0 commit comments

Comments
 (0)