From 9ed785128f167c62f3b29973f9eaf5f0f9c61f41 Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Sat, 18 May 2024 16:38:39 +0900 Subject: [PATCH 01/19] test: Add red test --- tests/AopCodeTest.php | 18 ++++++++++++++++++ tests/Fake/Annotation/FakeMarker5.php | 20 ++++++++++++++++++++ tests/Fake/FakePhp81Enum.php | 12 ++++++++++++ tests/Fake/FakePhp81Types.php | 13 +++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 tests/Fake/Annotation/FakeMarker5.php create mode 100644 tests/Fake/FakePhp81Enum.php create mode 100644 tests/Fake/FakePhp81Types.php diff --git a/tests/AopCodeTest.php b/tests/AopCodeTest.php index 71e0d92f..075c773b 100644 --- a/tests/AopCodeTest.php +++ b/tests/AopCodeTest.php @@ -89,6 +89,24 @@ public function method21()', $code); public function method22()', $code); } + /** @requires PHP 8.1 */ + public function testVariousMethodSignaturePhp81(): void + { + $bind = new Bind(); + for ($i = 200; $i <= 200; $i++) { + $bind->bindInterceptors('method' . (string) $i, []); + } + + $code = $this->codeGen->generate(new ReflectionClass(FakePhp81Types::class), $bind, '_test'); + $tempFile = tempnam(sys_get_temp_dir(), 'tmp_') . '.php'; + file_put_contents($tempFile, $code); + require $tempFile; + unlink($tempFile); + $this->assertTrue(class_exists('\Ray\Aop\FakePhp81Types_test')); + $this->assertStringContainsString('#[\\Ray\\Aop\\Annotation\\FakeMarker5(\\Ray\\Aop\\FakePhp81Enum::Apple)] + public function method200()', $code); + } + /** @requires PHP 8.2 */ public function testVariousMethodSignaturePhp82(): void { diff --git a/tests/Fake/Annotation/FakeMarker5.php b/tests/Fake/Annotation/FakeMarker5.php new file mode 100644 index 00000000..1ef3d12b --- /dev/null +++ b/tests/Fake/Annotation/FakeMarker5.php @@ -0,0 +1,20 @@ + Date: Sat, 18 May 2024 16:54:15 +0900 Subject: [PATCH 02/19] fix: Method signature string --- src/MethodSignatureString.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/MethodSignatureString.php b/src/MethodSignatureString.php index c0dbea73..4177922c 100644 --- a/src/MethodSignatureString.php +++ b/src/MethodSignatureString.php @@ -7,6 +7,7 @@ use Reflection; use ReflectionMethod; use ReflectionParameter; +use UnitEnum; use function implode; use function is_numeric; @@ -53,6 +54,14 @@ public function get(ReflectionMethod $method): string $formattedArgs = []; foreach ($argsList as $name => $value) { + if ($value instanceof UnitEnum) { + $formattedValue = preg_replace('/\s+/', ' ', '\\' . var_export($value, true)); + $argRepresentation = is_numeric($name) ? $formattedValue : "{$name}: {$formattedValue}"; + $formattedArgs[] = $argRepresentation; + + continue; + } + $formattedValue = preg_replace('/\s+/', ' ', var_export($value, true)); $argRepresentation = is_numeric($name) ? $formattedValue : "{$name}: {$formattedValue}"; $formattedArgs[] = $argRepresentation; From e97ba3eb79bd5e6c0a3fd3128f7629123188193f Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Sat, 18 May 2024 16:54:55 +0900 Subject: [PATCH 03/19] test: --- tests/Fake/Annotation/FakeMarker5.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Fake/Annotation/FakeMarker5.php b/tests/Fake/Annotation/FakeMarker5.php index 1ef3d12b..48b58436 100644 --- a/tests/Fake/Annotation/FakeMarker5.php +++ b/tests/Fake/Annotation/FakeMarker5.php @@ -14,7 +14,7 @@ #[Attribute(Attribute::TARGET_METHOD)] final class FakeMarker5 { - public function __construct(private readonly FakePhp81Enum $fruits) + public function __construct(public readonly FakePhp81Enum $fruits) { } } From 1998244e2f09dc6a51c265d93fa9bb888ffde72b Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Sat, 18 May 2024 17:05:29 +0900 Subject: [PATCH 04/19] test: --- tests/AopCodeTest.php | 20 ++------------------ tests/Fake/FakePhp81Types.php | 13 ------------- tests/Fake/FakePhp8Types.php | 3 +++ 3 files changed, 5 insertions(+), 31 deletions(-) delete mode 100644 tests/Fake/FakePhp81Types.php diff --git a/tests/AopCodeTest.php b/tests/AopCodeTest.php index 075c773b..6351b115 100644 --- a/tests/AopCodeTest.php +++ b/tests/AopCodeTest.php @@ -49,7 +49,7 @@ public function testReturnType(): void public function testVariousMethodSignature(): void { $bind = new Bind(); - for ($i = 1; $i <= 22; $i++) { + for ($i = 1; $i <= 23; $i++) { $bind->bindInterceptors('method' . (string) $i, []); } @@ -87,24 +87,8 @@ public function testVariousMethodSignature(): void public function method21()', $code); $this->assertStringContainsString('#[\\Ray\\Aop\\Annotation\\FakeMarkerName(a: 1, b: \'string\', c: true)] public function method22()', $code); - } - - /** @requires PHP 8.1 */ - public function testVariousMethodSignaturePhp81(): void - { - $bind = new Bind(); - for ($i = 200; $i <= 200; $i++) { - $bind->bindInterceptors('method' . (string) $i, []); - } - - $code = $this->codeGen->generate(new ReflectionClass(FakePhp81Types::class), $bind, '_test'); - $tempFile = tempnam(sys_get_temp_dir(), 'tmp_') . '.php'; - file_put_contents($tempFile, $code); - require $tempFile; - unlink($tempFile); - $this->assertTrue(class_exists('\Ray\Aop\FakePhp81Types_test')); $this->assertStringContainsString('#[\\Ray\\Aop\\Annotation\\FakeMarker5(\\Ray\\Aop\\FakePhp81Enum::Apple)] - public function method200()', $code); + public function method23()', $code); } /** @requires PHP 8.2 */ diff --git a/tests/Fake/FakePhp81Types.php b/tests/Fake/FakePhp81Types.php deleted file mode 100644 index 7fb1957f..00000000 --- a/tests/Fake/FakePhp81Types.php +++ /dev/null @@ -1,13 +0,0 @@ - Date: Sat, 18 May 2024 17:26:14 +0900 Subject: [PATCH 05/19] refactor: Method signature string --- src/MethodSignatureString.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MethodSignatureString.php b/src/MethodSignatureString.php index 4177922c..8ee9f4d0 100644 --- a/src/MethodSignatureString.php +++ b/src/MethodSignatureString.php @@ -55,7 +55,7 @@ public function get(ReflectionMethod $method): string foreach ($argsList as $name => $value) { if ($value instanceof UnitEnum) { - $formattedValue = preg_replace('/\s+/', ' ', '\\' . var_export($value, true)); + $formattedValue = '\\' . var_export($value, true); $argRepresentation = is_numeric($name) ? $formattedValue : "{$name}: {$formattedValue}"; $formattedArgs[] = $argRepresentation; From e99a40a8c89b9bf37444a633850ae3718c0f0f79 Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Sat, 18 May 2024 17:33:51 +0900 Subject: [PATCH 06/19] test: --- tests/Fake/Annotation/FakeMarker5.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Fake/Annotation/FakeMarker5.php b/tests/Fake/Annotation/FakeMarker5.php index 48b58436..83e4e8cf 100644 --- a/tests/Fake/Annotation/FakeMarker5.php +++ b/tests/Fake/Annotation/FakeMarker5.php @@ -14,7 +14,7 @@ #[Attribute(Attribute::TARGET_METHOD)] final class FakeMarker5 { - public function __construct(public readonly FakePhp81Enum $fruits) + public function __construct(public readonly FakePhp81Enum $fruit) { } } From ede1515e2070aa0034171504a36daef4df45f9b7 Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Sun, 19 May 2024 14:07:51 +0900 Subject: [PATCH 07/19] test: --- tests/AopCodeTest.php | 4 +++- tests/Fake/Annotation/FakeMarker6.php | 22 ++++++++++++++++++++++ tests/Fake/FakePhp8Types.php | 4 ++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/Fake/Annotation/FakeMarker6.php diff --git a/tests/AopCodeTest.php b/tests/AopCodeTest.php index 6351b115..678b2424 100644 --- a/tests/AopCodeTest.php +++ b/tests/AopCodeTest.php @@ -49,7 +49,7 @@ public function testReturnType(): void public function testVariousMethodSignature(): void { $bind = new Bind(); - for ($i = 1; $i <= 23; $i++) { + for ($i = 1; $i <= 24; $i++) { $bind->bindInterceptors('method' . (string) $i, []); } @@ -89,6 +89,8 @@ public function method21()', $code); public function method22()', $code); $this->assertStringContainsString('#[\\Ray\\Aop\\Annotation\\FakeMarker5(\\Ray\\Aop\\FakePhp81Enum::Apple)] public function method23()', $code); + $this->assertStringContainsString('#[\\Ray\\Aop\\Annotation\\FakeMarker6(fruit1: \\Ray\\Aop\\FakePhp81Enum::Apple, fruit2: \\Ray\\Aop\\FakePhp81Enum::Orange)] + public function method24()', $code); } /** @requires PHP 8.2 */ diff --git a/tests/Fake/Annotation/FakeMarker6.php b/tests/Fake/Annotation/FakeMarker6.php new file mode 100644 index 00000000..0adb2e77 --- /dev/null +++ b/tests/Fake/Annotation/FakeMarker6.php @@ -0,0 +1,22 @@ + Date: Sun, 19 May 2024 14:08:28 +0900 Subject: [PATCH 08/19] refactor: Method signature string --- src/MethodSignatureString.php | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/MethodSignatureString.php b/src/MethodSignatureString.php index 8ee9f4d0..a9f0b2a5 100644 --- a/src/MethodSignatureString.php +++ b/src/MethodSignatureString.php @@ -54,17 +54,7 @@ public function get(ReflectionMethod $method): string $formattedArgs = []; foreach ($argsList as $name => $value) { - if ($value instanceof UnitEnum) { - $formattedValue = '\\' . var_export($value, true); - $argRepresentation = is_numeric($name) ? $formattedValue : "{$name}: {$formattedValue}"; - $formattedArgs[] = $argRepresentation; - - continue; - } - - $formattedValue = preg_replace('/\s+/', ' ', var_export($value, true)); - $argRepresentation = is_numeric($name) ? $formattedValue : "{$name}: {$formattedValue}"; - $formattedArgs[] = $argRepresentation; + $formattedArgs[] = $this->representArg($name, $value); } $signatureParts[] = sprintf(' #[\\%s(%s)]', $attribute->getName(), implode(', ', $formattedArgs)) . PHP_EOL; @@ -98,6 +88,23 @@ public function get(ReflectionMethod $method): string return implode(' ', $signatureParts); } + /** + * @param string|int $name + * @param mixed $value + */ + private function representArg($name, $value): string + { + if ($value instanceof UnitEnum) { + $formattedValue = '\\' . var_export($value, true); + + return is_numeric($name) ? $formattedValue : "{$name}: {$formattedValue}"; + } + + $formattedValue = preg_replace('/\s+/', ' ', var_export($value, true)); + + return is_numeric($name) ? $formattedValue : "{$name}: {$formattedValue}"; + } + public function generateParameterCode(ReflectionParameter $param): string { $typeStr = ($this->typeString)($param->getType()); From f7710d8450a8c985d284705059078ab2689220a8 Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Sun, 19 May 2024 14:14:44 +0900 Subject: [PATCH 09/19] refactor: Method signature string --- src/MethodSignatureString.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MethodSignatureString.php b/src/MethodSignatureString.php index a9f0b2a5..674b3abc 100644 --- a/src/MethodSignatureString.php +++ b/src/MethodSignatureString.php @@ -92,7 +92,7 @@ public function get(ReflectionMethod $method): string * @param string|int $name * @param mixed $value */ - private function representArg($name, $value): string + private function representArg($name, $value): ?string { if ($value instanceof UnitEnum) { $formattedValue = '\\' . var_export($value, true); From 74acb784ddbf7778103a1fd56af28b1f98481d91 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Mon, 20 May 2024 02:04:59 +0900 Subject: [PATCH 10/19] Refactor representArg method in MethodSignatureString Simplified the representArg method logic for handling UnitEnum instances. The refactoring involved removing duplicate code to improve readability and maintainability. --- src/MethodSignatureString.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/MethodSignatureString.php b/src/MethodSignatureString.php index 674b3abc..ccf90b53 100644 --- a/src/MethodSignatureString.php +++ b/src/MethodSignatureString.php @@ -94,13 +94,9 @@ public function get(ReflectionMethod $method): string */ private function representArg($name, $value): ?string { - if ($value instanceof UnitEnum) { - $formattedValue = '\\' . var_export($value, true); - - return is_numeric($name) ? $formattedValue : "{$name}: {$formattedValue}"; - } - - $formattedValue = preg_replace('/\s+/', ' ', var_export($value, true)); + $formattedValue = $value instanceof UnitEnum ? + '\\' . var_export($value, true) + : preg_replace('/\s+/', ' ', var_export($value, true)); return is_numeric($name) ? $formattedValue : "{$name}: {$formattedValue}"; } From a23b2c48a569ed82d37fd1e9f69ff238cdbc29d5 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Mon, 20 May 2024 02:52:04 +0900 Subject: [PATCH 11/19] Refactor magic values in MethodSignatureString Replaced magic values in MethodSignatureString class with class constants for better readability and maintainability. This includes PHP version check and the indent string. --- src/MethodSignatureString.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/MethodSignatureString.php b/src/MethodSignatureString.php index ccf90b53..1b116bc4 100644 --- a/src/MethodSignatureString.php +++ b/src/MethodSignatureString.php @@ -23,12 +23,17 @@ /** @SuppressWarnings(PHPMD.CyclomaticComplexity) */ final class MethodSignatureString { + private const PHP_VERSION_8 = 80000; + private const NULLABLE_PHP8 = 'null|'; + private const NULLABLE_PHP7 = '?'; + private const INDENT = ' '; + /** @var TypeString */ private $typeString; public function __construct(int $phpVersion) { - $nullableStr = $phpVersion >= 80000 ? 'null|' : '?'; + $nullableStr = $phpVersion >= self::PHP_VERSION_8 ? self::NULLABLE_PHP8 : self::NULLABLE_PHP7; $this->typeString = new TypeString($nullableStr); } @@ -62,7 +67,7 @@ public function get(ReflectionMethod $method): string } if ($signatureParts) { - $signatureParts[] = ' '; // インデント追加 + $signatureParts[] = self::INDENT; } // アクセス修飾子を取得 From 2dd4b33fba82c53cbc0e35a8fb480cda77ec68aa Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Mon, 20 May 2024 03:10:39 +0900 Subject: [PATCH 12/19] Refactor method signature string generation The method signature string generation was refactored for improved readability and maintainability. Separate helper methods were created to handle the generation of doc comments, attributes, access modifiers, and the method signature itself. This reduces the complexity and length of the main 'get' method. --- src/MethodSignatureString.php | 75 ++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/src/MethodSignatureString.php b/src/MethodSignatureString.php index 1b116bc4..554c74c7 100644 --- a/src/MethodSignatureString.php +++ b/src/MethodSignatureString.php @@ -43,54 +43,68 @@ public function __construct(int $phpVersion) */ public function get(ReflectionMethod $method): string { - $signatureParts = []; + $signatureParts = $this->getDocComment($method); + $this->addAttributes($method, $signatureParts); + $this->addAccessModifiers($method, $signatureParts); + $this->addMethodSignature($method, $signatureParts); - // PHPDocを取得 + return implode(' ', $signatureParts); + } + + /** @return array */ + private function getDocComment(ReflectionMethod $method): array + { $docComment = $method->getDocComment(); - if (is_string($docComment)) { - $signatureParts[] = $docComment . PHP_EOL; - } - // アトリビュートを取得 (PHP 8.0+ の場合のみ) - if (PHP_MAJOR_VERSION >= 8) { - /** @psalm-suppress MixedAssignment */ - foreach ($method->getAttributes() as $attribute) { - $argsList = $attribute->getArguments(); - $formattedArgs = []; + return is_string($docComment) ? [$docComment . PHP_EOL] : []; + } - foreach ($argsList as $name => $value) { - $formattedArgs[] = $this->representArg($name, $value); - } + /** @param array $signatureParts */ + private function addAttributes(ReflectionMethod $method, array &$signatureParts): void + { + if (PHP_MAJOR_VERSION < 8) { + return; + } - $signatureParts[] = sprintf(' #[\\%s(%s)]', $attribute->getName(), implode(', ', $formattedArgs)) . PHP_EOL; + /** @psalm-suppress MixedAssignment */ + foreach ($method->getAttributes() as $attribute) { + $argsList = $attribute->getArguments(); + $formattedArgs = []; + foreach ($argsList as $name => $value) { + $formattedArgs[] = $this->representArg($name, $value); } + + $signatureParts[] = sprintf(' #[\\%s(%s)]', $attribute->getName(), implode(', ', $formattedArgs)) . PHP_EOL; } - if ($signatureParts) { - $signatureParts[] = self::INDENT; + if (empty($signatureParts)) { + return; } - // アクセス修飾子を取得 + $signatureParts[] = self::INDENT; + } + + /** @param array $signatureParts */ + private function addAccessModifiers(ReflectionMethod $method, array &$signatureParts): void + { $modifier = implode(' ', Reflection::getModifierNames($method->getModifiers())); + $signatureParts[] = $modifier; + } - // メソッド名とパラメータを取得 + /** @param array $signatureParts */ + private function addMethodSignature(ReflectionMethod $method, array &$signatureParts): void + { $params = []; foreach ($method->getParameters() as $param) { $params[] = $this->generateParameterCode($param); } - $returnType = ''; - $rType = $method->getReturnType(); - if ($rType) { - $returnType = ': ' . ($this->typeString)($rType); - } - $parmsList = implode(', ', $params); + $rType = $method->getReturnType(); + $return = $rType ? ': ' . ($this->typeString)($rType) : ''; - $signatureParts[] = sprintf('function %s(%s)%s', $method->getName(), $parmsList, $returnType); - - return implode(' ', $signatureParts); + $signatureParts[] = sprintf('function %s(%s)%s', $method->getName(), $parmsList, $return); } /** @@ -110,13 +124,8 @@ public function generateParameterCode(ReflectionParameter $param): string { $typeStr = ($this->typeString)($param->getType()); $typeStrWithSpace = $typeStr ? $typeStr . ' ' : $typeStr; - // Variadicのチェック $variadicStr = $param->isVariadic() ? '...' : ''; - - // 参照渡しのチェック $referenceStr = $param->isPassedByReference() ? '&' : ''; - - // デフォルト値のチェック $defaultStr = ''; if ($param->isDefaultValueAvailable()) { $default = var_export($param->getDefaultValue(), true); From ba415c6aaaac808046e9de895f2d712d1063e47a Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Mon, 20 May 2024 03:13:03 +0900 Subject: [PATCH 13/19] Remove SuppressWarnings annotation from MethodSignatureString class The SuppressWarnings PHPMD.CyclomaticComplexity annotation used on the MethodSignatureString class was not necessary and has now been removed. The removal of this annotation will ensure that the cyclomatic complexity app metric is appropriately analyzed and followed for this class. --- src/MethodSignatureString.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/MethodSignatureString.php b/src/MethodSignatureString.php index 554c74c7..771bb59f 100644 --- a/src/MethodSignatureString.php +++ b/src/MethodSignatureString.php @@ -20,7 +20,6 @@ use const PHP_EOL; use const PHP_MAJOR_VERSION; -/** @SuppressWarnings(PHPMD.CyclomaticComplexity) */ final class MethodSignatureString { private const PHP_VERSION_8 = 80000; From 2c8d2de1773116d2e4663ef2ce106f39090f6435 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Mon, 20 May 2024 03:13:35 +0900 Subject: [PATCH 14/19] Change visibility of generateParameterCode method to private The generateParameterCode method in MethodSignatureString.php has been changed from public to private for encapsulation purposes. This change increases the integrity of the data and ensures that --- src/MethodSignatureString.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MethodSignatureString.php b/src/MethodSignatureString.php index 771bb59f..fb210baf 100644 --- a/src/MethodSignatureString.php +++ b/src/MethodSignatureString.php @@ -119,7 +119,7 @@ private function representArg($name, $value): ?string return is_numeric($name) ? $formattedValue : "{$name}: {$formattedValue}"; } - public function generateParameterCode(ReflectionParameter $param): string + private function generateParameterCode(ReflectionParameter $param): string { $typeStr = ($this->typeString)($param->getType()); $typeStrWithSpace = $typeStr ? $typeStr . ' ' : $typeStr; From bf071a6b13c6bd0837e00abe123234281efb94b5 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Mon, 20 May 2024 03:33:35 +0900 Subject: [PATCH 15/19] Update representArg method return type The return type of the representArg function in MethodSignatureString.php was updated. The previous nullable string type has been replaced with a non-nullable string type to ensure it always returns a string. --- src/MethodSignatureString.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MethodSignatureString.php b/src/MethodSignatureString.php index fb210baf..92482da9 100644 --- a/src/MethodSignatureString.php +++ b/src/MethodSignatureString.php @@ -110,7 +110,7 @@ private function addMethodSignature(ReflectionMethod $method, array &$signatureP * @param string|int $name * @param mixed $value */ - private function representArg($name, $value): ?string + private function representArg($name, $value): string { $formattedValue = $value instanceof UnitEnum ? '\\' . var_export($value, true) From c84d4bfa9bdc634bb6da521aa72cf11a8e68326e Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Mon, 20 May 2024 03:35:01 +0900 Subject: [PATCH 16/19] Rename method from "representArg" to "formatArg" The method in the MethodSignatureString class has been renamed from "representArg" to "formatArg" for better understanding of its function. This change improves readability and provides clearer naming convention in the codebase. --- src/MethodSignatureString.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MethodSignatureString.php b/src/MethodSignatureString.php index 92482da9..e5d855b4 100644 --- a/src/MethodSignatureString.php +++ b/src/MethodSignatureString.php @@ -70,7 +70,7 @@ private function addAttributes(ReflectionMethod $method, array &$signatureParts) $argsList = $attribute->getArguments(); $formattedArgs = []; foreach ($argsList as $name => $value) { - $formattedArgs[] = $this->representArg($name, $value); + $formattedArgs[] = $this->formatArg($name, $value); } $signatureParts[] = sprintf(' #[\\%s(%s)]', $attribute->getName(), implode(', ', $formattedArgs)) . PHP_EOL; @@ -110,7 +110,7 @@ private function addMethodSignature(ReflectionMethod $method, array &$signatureP * @param string|int $name * @param mixed $value */ - private function representArg($name, $value): string + private function formatArg($name, $value): string { $formattedValue = $value instanceof UnitEnum ? '\\' . var_export($value, true) From 74dd1350a316fbea3d004850326f1552e18d4e07 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Mon, 20 May 2024 03:44:49 +0900 Subject: [PATCH 17/19] Soothe phpstan Update MethodSignatureString to enhance argument formatting This commit updates the formatArg function in MethodSignatureString to ensure strict string output. It also removes unnecessary string casting in the foreach loop that processes argument lists. The changes provide a more consistent and reliable argument format for use in function signatures. --- src/MethodSignatureString.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MethodSignatureString.php b/src/MethodSignatureString.php index e5d855b4..f5aedc27 100644 --- a/src/MethodSignatureString.php +++ b/src/MethodSignatureString.php @@ -116,7 +116,7 @@ private function formatArg($name, $value): string '\\' . var_export($value, true) : preg_replace('/\s+/', ' ', var_export($value, true)); - return is_numeric($name) ? $formattedValue : "{$name}: {$formattedValue}"; + return is_numeric($name) ? (string) $formattedValue : "{$name}: {$formattedValue}"; } private function generateParameterCode(ReflectionParameter $param): string From ecee7cf98b04db8c5621049462aa6b3a6f20d7d9 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Mon, 20 May 2024 11:55:59 +0900 Subject: [PATCH 18/19] Update array formatting and whitespace handling in code Removed unnecessary spaces in array definitions in tests and updated the method of handling whitespace in the MethodSignatureString. This change enhances code readability by ensuring consistent formatting standards across the codebase. --- src/MethodSignatureString.php | 6 +----- tests/AopCodeTest.php | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/MethodSignatureString.php b/src/MethodSignatureString.php index f5aedc27..9640f84f 100644 --- a/src/MethodSignatureString.php +++ b/src/MethodSignatureString.php @@ -36,10 +36,6 @@ public function __construct(int $phpVersion) $this->typeString = new TypeString($nullableStr); } - /** - * @psalm-suppress MixedArgument - * @psalm-suppress MixedMethodCall - */ public function get(ReflectionMethod $method): string { $signatureParts = $this->getDocComment($method); @@ -114,7 +110,7 @@ private function formatArg($name, $value): string { $formattedValue = $value instanceof UnitEnum ? '\\' . var_export($value, true) - : preg_replace('/\s+/', ' ', var_export($value, true)); + : preg_replace('/\s+/', '', var_export($value, true)); return is_numeric($name) ? (string) $formattedValue : "{$name}: {$formattedValue}"; } diff --git a/tests/AopCodeTest.php b/tests/AopCodeTest.php index 678b2424..afcf7ff0 100644 --- a/tests/AopCodeTest.php +++ b/tests/AopCodeTest.php @@ -83,7 +83,7 @@ public function testVariousMethodSignature(): void $this->assertStringContainsString(' /** * PHPDoc */ - #[\\Ray\\Aop\\Annotation\\FakeMarker4(array ( 0 => 1, 1 => 2, ), 3)] + #[\\Ray\\Aop\\Annotation\\FakeMarker4(array(0=>1,1=>2,), 3)] public function method21()', $code); $this->assertStringContainsString('#[\\Ray\\Aop\\Annotation\\FakeMarkerName(a: 1, b: \'string\', c: true)] public function method22()', $code); From 1d15b6dc741342af80ca8b9233af9b9a3d65f1df Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Mon, 20 May 2024 12:09:13 +0900 Subject: [PATCH 19/19] Refactor method attribute fetching process The method attribute fetching process has been refactored to improve readability and maintainability. The method attributes are now fetched once and stored in a variable before being iterated over, rather than fetching them directly during the iteration. The 'psalm-suppress MixedAssignment' annotation has been replaced by a 'var mixed' comment, providing easier understanding of the variable's expected value format. --- src/MethodSignatureString.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/MethodSignatureString.php b/src/MethodSignatureString.php index 9640f84f..a70ecb14 100644 --- a/src/MethodSignatureString.php +++ b/src/MethodSignatureString.php @@ -61,10 +61,11 @@ private function addAttributes(ReflectionMethod $method, array &$signatureParts) return; } - /** @psalm-suppress MixedAssignment */ - foreach ($method->getAttributes() as $attribute) { + $attributes = $method->getAttributes(); + foreach ($attributes as $attribute) { $argsList = $attribute->getArguments(); $formattedArgs = []; + /** @var mixed $value */ foreach ($argsList as $name => $value) { $formattedArgs[] = $this->formatArg($name, $value); }