Skip to content

Commit

Permalink
Add support for PHP 8 attributes in method signatures
Browse files Browse the repository at this point in the history
Enhanced MethodSignatureString to include attribute handling if PHP version is 8 or higher. Additional fake attributes and test cases were introduced to validate the new functionality.
  • Loading branch information
koriym committed Nov 15, 2024
1 parent 5790b6d commit 7f1328f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/MethodSignatureString.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ private function formatArg($name, $value): string

private function generateParameterCode(ReflectionParameter $param): string
{
// Support attributes
$attributesStr = '';
if (PHP_MAJOR_VERSION >= 8) {
$attributesStr = $this->getAttributeStr($param);
}

$typeStr = ($this->typeString)($param->getType());
$typeStrWithSpace = $typeStr ? $typeStr . ' ' : $typeStr;
$variadicStr = $param->isVariadic() ? '...' : '';
Expand All @@ -149,6 +155,26 @@ private function generateParameterCode(ReflectionParameter $param): string
$defaultStr = ' = ' . str_replace(["\r", "\n"], '', $default);
}

return "{$typeStrWithSpace}{$referenceStr}{$variadicStr}\${$param->getName()}{$defaultStr}";
return "{$attributesStr}{$typeStrWithSpace}{$referenceStr}{$variadicStr}\${$param->getName()}{$defaultStr}";
}

public function getAttributeStr(ReflectionParameter $param): string
{
if (PHP_MAJOR_VERSION < 8) {
return '';
}

$attributesStr = '';
$attributes = $param->getAttributes();
if (! empty($attributes)) {
$attributeStrings = [];
foreach ($attributes as $attribute) {
$attributeStrings[] = sprintf('#[%s]', $attribute->getName());
}

$attributesStr = implode(' ', $attributeStrings) . ' ';
}

return $attributesStr;
}
}
3 changes: 2 additions & 1 deletion tests/AopCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function testReturnType(): void
public function testVariousMethodSignature(): void
{
$bind = new Bind();
for ($i = 1; $i <= 24; $i++) {
for ($i = 1; $i <= 25; $i++) {
$bind->bindInterceptors('method' . (string) $i, []);
}

Expand Down Expand Up @@ -91,6 +91,7 @@ public function method22()', $code);
public function method23()', $code);
$this->assertStringContainsString('#[\\Ray\\Aop\\Annotation\\FakeMarker6(fruit1: \\Ray\\Aop\\FakePhp81Enum::Apple, fruit2: \\Ray\\Aop\\FakePhp81Enum::Orange)]
public function method24()', $code);
$this->assertStringContainsString('public function method25(#[Ray\Aop\Attribute\FakeAttr1] $a, #[Ray\Aop\FakeAttri1] #[Ray\Aop\FakeAttr2] $b): void', $code);
}

/** @requires PHP 8.2 */
Expand Down
7 changes: 7 additions & 0 deletions tests/Fake/Attribute/FakeAttr1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Ray\Aop\Attribute;

final class FakeAttr1
{
}
7 changes: 7 additions & 0 deletions tests/Fake/Attribute/FakeAttr2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Ray\Aop\Attribute;

final class FakeAttr2
{
}
9 changes: 9 additions & 0 deletions tests/Fake/FakePhp8Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Ray\Aop\Annotation\FakeMarker5;
use Ray\Aop\Annotation\FakeMarker6;
use Ray\Aop\Annotation\FakeMarkerName;
use Ray\Aop\Attribute\FakeAttr1;

class FakePhp8Types implements FakeNullInterface, \Ray\Aop\FakeNullInterface1
{
Expand Down Expand Up @@ -69,4 +70,12 @@ public function method23() {}

#[FakeMarker6(fruit1: FakePhp81Enum::Apple, fruit2: FakePhp81Enum::Orange)]
public function method24() {}

// Method with attribute
public function method25(
#[FakeAttr1]
$a,
#[FakeAttri1, FakeAttr2]
$b
): void {}
}

0 comments on commit 7f1328f

Please sign in to comment.