Skip to content

Commit effa74a

Browse files
committed
Refactor - Enhance method body generation and improve code readability in StopwatchDecorator
1 parent 4118296 commit effa74a

File tree

2 files changed

+62
-35
lines changed

2 files changed

+62
-35
lines changed

ecs.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use PhpCsFixer\Fixer\Phpdoc\PhpdocSummaryFixer;
2929
use PhpCsFixer\Fixer\Phpdoc\PhpdocToCommentFixer;
3030
use PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer;
31+
use PhpCsFixer\Fixer\StringNotation\ExplicitStringVariableFixer;
3132
use PhpCsFixer\Fixer\Whitespace\BlankLineBeforeStatementFixer;
3233
use Symplify\EasyCodingStandard\Config\ECSConfig;
3334
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;
@@ -43,11 +44,15 @@
4344
$ruleConfigurations = [
4445
[
4546
IncrementStyleFixer::class,
46-
['style' => 'post'],
47+
[
48+
'style' => 'post',
49+
],
4750
],
4851
[
4952
CastSpacesFixer::class,
50-
['space' => 'none'],
53+
[
54+
'space' => 'none',
55+
],
5156
],
5257
[
5358
YodaStyleFixer::class,
@@ -59,15 +64,21 @@
5964
],
6065
[
6166
ConcatSpaceFixer::class,
62-
['spacing' => 'one'],
67+
[
68+
'spacing' => 'one',
69+
],
6370
],
6471
[
6572
CastSpacesFixer::class,
66-
['space' => 'none'],
73+
[
74+
'space' => 'none',
75+
],
6776
],
6877
[
6978
OrderedImportsFixer::class,
70-
['imports_order' => ['class', 'function', 'const']],
79+
[
80+
'imports_order' => ['class', 'function', 'const'],
81+
],
7182
],
7283
[
7384
NoSuperfluousPhpdocTagsFixer::class,
@@ -79,15 +90,23 @@
7990
],
8091
[
8192
DeclareEqualNormalizeFixer::class,
82-
['space' => 'single'],
93+
[
94+
'space' => 'single',
95+
],
8396
],
8497
[
8598
BlankLineBeforeStatementFixer::class,
86-
['statements' => ['continue', 'declare', 'return', 'throw', 'try']],
99+
[
100+
'statements' => ['continue', 'declare', 'return', 'throw', 'try'],
101+
],
87102
],
88103
[
89104
BinaryOperatorSpacesFixer::class,
90-
['operators' => ['&' => 'align']],
105+
[
106+
'operators' => [
107+
'&' => 'align',
108+
],
109+
],
91110
],
92111
];
93112

@@ -106,5 +125,6 @@
106125
PhpdocAlignFixer::class => null,
107126
PhpdocToCommentFixer::class => null,
108127
NativeFunctionInvocationFixer::class => null,
128+
ExplicitStringVariableFixer::class => null,
109129
]);
110130
};

src/Decorator/StopwatchDecorator.php

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,9 @@ private function generateProxyClass(
207207
$methods = $this->getProxyableMethods($reflection);
208208
$methodsCode = $this->generateProxyMethods($methods);
209209

210-
return "
211-
class {$proxyClassName} extends {$originalClassName} {
210+
return <<<CODE
211+
212+
class $proxyClassName extends $originalClassName {
212213
private object \$wrappedInstance;
213214
private array \$prefixInterceptors;
214215
private array \$suffixInterceptors;
@@ -218,8 +219,9 @@ public function __construct(object \$wrappedInstance, array \$prefixInterceptors
218219
\$this->prefixInterceptors = \$prefixInterceptors;
219220
\$this->suffixInterceptors = \$suffixInterceptors;
220221
}
221-
{$methodsCode}
222-
}";
222+
$methodsCode
223+
}
224+
CODE;
223225
}
224226

225227
/**
@@ -241,15 +243,17 @@ private function generateProxyMethod(ReflectionMethod $method): string
241243
$methodName = $method->getName();
242244
[$paramsList, $argsList] = $this->buildMethodParameters($method);
243245
[$returnType, $isVoid] = $this->getMethodReturnType($method);
246+
$methodBody = $this->generateMethodBody($methodName, $argsList, $isVoid);
247+
248+
return <<<CODE
244249
245-
$code = "\n public function {$methodName}({$paramsList}){$returnType} {\n";
246-
$code .= " if (isset(\$this->prefixInterceptors['{$methodName}'])) {\n";
247-
$code .= " (\$this->prefixInterceptors['{$methodName}'])();\n";
248-
$code .= " }\n";
249-
$code .= $this->generateMethodBody($methodName, $argsList, $isVoid);
250-
$code .= " }\n";
250+
public function $methodName($paramsList)$returnType {
251+
if (isset(\$this->prefixInterceptors['$methodName'])) {
252+
(\$this->prefixInterceptors['$methodName'])();
253+
}
254+
$methodBody }
251255
252-
return $code;
256+
CODE;
253257
}
254258

255259
private function generateMethodBody(string $methodName, string $argsList, bool $isVoid): string
@@ -261,26 +265,29 @@ private function generateMethodBody(string $methodName, string $argsList, bool $
261265

262266
private function generateVoidMethodBody(string $methodName, string $argsList): string
263267
{
264-
$code = " \$this->wrappedInstance->{$methodName}({$argsList});\n";
265-
$code .= " if (isset(\$this->suffixInterceptors['{$methodName}'])) {\n";
266-
$code .= " \$returnValue = null;\n";
267-
$code .= " (\$this->suffixInterceptors['{$methodName}'])";
268-
$code .= "(null, null, null, func_get_args(), \$returnValue);\n";
269-
$code .= " }\n";
270-
271-
return $code;
268+
return <<<CODE
269+
\$this->wrappedInstance->$methodName($argsList);
270+
271+
if (isset(\$this->suffixInterceptors['$methodName'])) {
272+
\$returnValue = null;
273+
(\$this->suffixInterceptors['$methodName'])(null, null, null, func_get_args(), \$returnValue);
274+
}
275+
276+
CODE;
272277
}
273278

274279
private function generateNonVoidMethodBody(string $methodName, string $argsList): string
275280
{
276-
$code = " \$returnValue = \$this->wrappedInstance->{$methodName}({$argsList});\n";
277-
$code .= " if (isset(\$this->suffixInterceptors['{$methodName}'])) {\n";
278-
$code .= " (\$this->suffixInterceptors['{$methodName}'])";
279-
$code .= "(null, null, null, func_get_args(), \$returnValue);\n";
280-
$code .= " }\n";
281-
$code .= " return \$returnValue;\n";
282-
283-
return $code;
281+
return <<<CODE
282+
\$returnValue = \$this->wrappedInstance->$methodName($argsList);
283+
284+
if (isset(\$this->suffixInterceptors['$methodName'])) {
285+
(\$this->suffixInterceptors['$methodName'])(null, null, null, func_get_args(), \$returnValue);
286+
}
287+
288+
return \$returnValue;
289+
290+
CODE;
284291
}
285292

286293
// Method parameter handling

0 commit comments

Comments
 (0)