Skip to content

Commit

Permalink
Refactor AopCodeGen, InterceptTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
koriym committed Sep 18, 2023
1 parent 893d10e commit e1486de
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
7 changes: 3 additions & 4 deletions src/AopCodeGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

final class AopCodeGen
{
const INTERCEPT_STATEMENT = '\$this->_intercept(__FUNCTION__, func_get_args());';

Check failure on line 29 in src/AopCodeGen.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Constant \Ray\Aop\AopCodeGen::INTERCEPT_STATEMENT visibility missing.

Check failure on line 29 in src/AopCodeGen.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Constant \Ray\Aop\AopCodeGen::INTERCEPT_STATEMENT visibility missing.
/** @var AopCodeGenMethodSignature */
private $methodSignature;

Expand All @@ -44,7 +45,7 @@ public function __construct()
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function generate(ReflectionClass $sourceClass, BindInterface $bind, string $postfix = '_aop'): string
public function generate(ReflectionClass $sourceClass, BindInterface $bind, string $postfix): string
{
$fileName = (string) $sourceClass->getFileName();
if (! file_exists($fileName)) {
Expand Down Expand Up @@ -93,7 +94,6 @@ public function generate(ReflectionClass $sourceClass, BindInterface $bind, stri

$newCode->implementsInterface(WeavedInterface::class);
$this->addMethods($newCode, $sourceClass, $bind);
$newCode->getCodeText();

return $newCode->getCodeText();
}
Expand All @@ -104,7 +104,6 @@ private function addMethods(AopCodeGenNewCode $newCode, ReflectionClass $class,
$bindings = array_keys($bind->getBindings());

$parentMethods = $class->getMethods();
$statement = '\$this->_intercept(func_get_args(), __FUNCTION__);';
$additionalMethods = [];
foreach ($parentMethods as $method) {
if (! in_array($method->getName(), $bindings)) {
Expand All @@ -119,7 +118,7 @@ private function addMethods(AopCodeGenNewCode $newCode, ReflectionClass $class,
}

$return = $isVoid ? '' : 'return ';
$additionalMethods[] = sprintf(" %s\n {\n %s%s\n }\n", $signature, $return, $statement);
$additionalMethods[] = sprintf(" %s\n {\n %s%s\n }\n", $signature, $return, self::INTERCEPT_STATEMENT);
}

if ($additionalMethods) {
Expand Down
1 change: 1 addition & 0 deletions src/AopCodeGenNewCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function implementsInterface(string $interfaceName): void

public function getCodeText(): string
{
// close opened curly brace
while ($this->curlyBraceCount !== 0) {
$this->code .= '}';
$this->curlyBraceCount--;
Expand Down
2 changes: 1 addition & 1 deletion src/InterceptTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ trait InterceptTrait
*
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
*/
private function _intercept(array $args, string $func) // phpcs:ignore
private function _intercept(string $func, array $args) // phpcs:ignore
{
if (! $this->isAspect) {
$this->isAspect = true;
Expand Down
10 changes: 5 additions & 5 deletions tests/AopCodeGenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testTypeDeclarations(): void
{
$bind = new Bind();
$bind->bindInterceptors('run', []);
$code = $this->codeGen->generate(new ReflectionClass(FakePhp7Class::class), $bind);
$code = $this->codeGen->generate(new ReflectionClass(FakePhp7Class::class), $bind, '_test');

Check failure on line 32 in tests/AopCodeGenTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Expected 1 space after comma in argument list; 2 found

Check failure on line 32 in tests/AopCodeGenTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Expected 1 space after comma in argument list; 2 found
$expected = 'function run(string $a, int $b, float $c, bool $d): array';
$this->assertStringContainsString($expected, $code);
}
Expand All @@ -38,7 +38,7 @@ public function testReturnType(): void
{
$bind = new Bind();
$bind->bindInterceptors('returnTypeArray', []);
$code = $this->codeGen->generate(new ReflectionClass(FakePhp7ReturnTypeClass::class), $bind);
$code = $this->codeGen->generate(new ReflectionClass(FakePhp7ReturnTypeClass::class), $bind, '_test');

Check failure on line 41 in tests/AopCodeGenTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Expected 1 space after comma in argument list; 2 found

Check failure on line 41 in tests/AopCodeGenTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Expected 1 space after comma in argument list; 2 found
$expected = 'function returnTypeArray(): array';
$this->assertStringContainsString($expected, $code);
}
Expand All @@ -51,12 +51,12 @@ public function testUnionType(): void
$bind->bindInterceptors('method' . (string) $i, []);
}

$code = $this->codeGen->generate(new ReflectionClass(FakePhp8Types::class), $bind);
$code = $this->codeGen->generate(new ReflectionClass(FakePhp8Types::class), $bind, '_test');

Check failure on line 54 in tests/AopCodeGenTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Expected 1 space after comma in argument list; 2 found

Check failure on line 54 in tests/AopCodeGenTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Expected 1 space after comma in argument list; 2 found
$tempFile = tempnam(sys_get_temp_dir(), 'tmp_') . '.php';
file_put_contents($tempFile, $code);
require $tempFile;
unlink($tempFile);
$this->assertTrue(class_exists('\Ray\Aop\FakePhp8Types_aop'));
$this->assertTrue(class_exists('\Ray\Aop\FakePhp8Types_test'));

$this->assertStringContainsString('public function method1($param1)', $code);
$this->assertStringContainsString('public function method2(string $param1)', $code);
Expand Down Expand Up @@ -84,6 +84,6 @@ public function testUnionType(): void
public function testInvalidSourceClass(): void
{
$this->expectException(InvalidSourceClassException::class);
$this->codeGen->generate(new ReflectionClass(stdClass::class), new Bind());
$this->codeGen->generate(new ReflectionClass(stdClass::class), new Bind(), '_test');

Check failure on line 87 in tests/AopCodeGenTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Expected 1 space after comma in argument list; 2 found

Check failure on line 87 in tests/AopCodeGenTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Expected 1 space after comma in argument list; 2 found
}
}

0 comments on commit e1486de

Please sign in to comment.