Skip to content

Commit

Permalink
Refactor method signature handling to support attributes
Browse files Browse the repository at this point in the history
Refactor `MethodSignatureString` to leverage `ReflectionAttribute` for handling attributes. Updated attribute formatting to use a dedicated method, enhancing readability and maintainability. Removed outdated PHP version checks and improved type declarations.
  • Loading branch information
koriym committed Nov 15, 2024
1 parent 8dbea5a commit db92e2a
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions src/MethodSignatureString.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Ray\Aop;

use Reflection;
use ReflectionAttribute;
use ReflectionMethod;
use ReflectionParameter;
use UnitEnum;
Expand All @@ -19,21 +20,18 @@
use function var_export;

use const PHP_EOL;
use const PHP_MAJOR_VERSION;

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;
private TypeString $typeString;

public function __construct(int $phpVersion)
{
$nullableStr = $phpVersion >= self::PHP_VERSION_8 ? self::NULLABLE_PHP8 : self::NULLABLE_PHP7;
$nullableStr = $phpVersion >= 80000 ? self::NULLABLE_PHP8 : self::NULLABLE_PHP7;
$this->typeString = new TypeString($nullableStr);
}

Expand Down Expand Up @@ -63,20 +61,9 @@ private function getDocComment(ReflectionMethod $method): array
/** @param array<string> $signatureParts */
private function addAttributes(ReflectionMethod $method, array &$signatureParts): void
{
if (PHP_MAJOR_VERSION < 8) {
return;
}

$attributes = $method->getAttributes();
foreach ($attributes as $attribute) {
$argsList = $attribute->getArguments();
$formattedArgs = [];
/** @var mixed $value */
foreach ($argsList as $name => $value) {
$formattedArgs[] = $this->formatArg($name, $value);
}

$signatureParts[] = sprintf(' #[\\%s(%s)]', $attribute->getName(), implode(', ', $formattedArgs)) . PHP_EOL;
$signatureParts[] = sprintf(' #[%s]', $this->formatAttributeStr($attribute)) . PHP_EOL;
}

if (empty($signatureParts)) {
Expand All @@ -86,6 +73,19 @@ private function addAttributes(ReflectionMethod $method, array &$signatureParts)
$signatureParts[] = self::INDENT;
}

/** @param ReflectionAttribute<object> $attribute */
private function formatAttributeStr(ReflectionAttribute $attribute): string
{
$argsList = $attribute->getArguments();
$formattedArgs = [];
/** @var scalar $value */
foreach ($argsList as $name => $value) {
$formattedArgs[] = $this->formatArg($name, $value);
}

return sprintf('\\%s(%s)', $attribute->getName(), implode(', ', $formattedArgs));
}

/**
* @param array<string> $signatureParts
*
Expand Down Expand Up @@ -139,7 +139,6 @@ private function formatArg($name, $value): string

private function generateParameterCode(ReflectionParameter $param): string
{
// Support attributes
$attributesStr = $this->getAttributeStr($param);
$typeStr = ($this->typeString)($param->getType());
$typeStrWithSpace = $typeStr ? $typeStr . ' ' : $typeStr;
Expand All @@ -161,7 +160,7 @@ public function getAttributeStr(ReflectionParameter $param): string
if (! empty($attributes)) {
$attributeStrings = [];
foreach ($attributes as $attribute) {
$attributeStrings[] = sprintf('#[\%s]', $attribute->getName());
$attributeStrings[] = sprintf('#[%s]', $this->formatAttributeStr($attribute));
}

$attributesStr = implode(' ', $attributeStrings) . ' ';
Expand Down

0 comments on commit db92e2a

Please sign in to comment.