Skip to content

Commit

Permalink
Add PHP version check for attributes handling
Browse files Browse the repository at this point in the history
Ensure compatibility with PHP versions below 8 by adding conditional checks before processing attributes. This prevents runtime errors in environments using earlier PHP versions.
  • Loading branch information
koriym committed Nov 15, 2024
1 parent f4790bd commit 602ef32
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/MethodSignatureString.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use function var_export;

use const PHP_EOL;
use const PHP_MAJOR_VERSION;

final class MethodSignatureString
{
Expand Down Expand Up @@ -61,6 +62,10 @@ 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) {
$signatureParts[] = sprintf(' #[%s]', $this->formatAttributeStr($attribute)) . PHP_EOL;
Expand Down Expand Up @@ -155,6 +160,10 @@ private function generateParameterCode(ReflectionParameter $param): string

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

$attributesStr = '';
$attributes = $param->getAttributes();
if (! empty($attributes)) {
Expand Down

0 comments on commit 602ef32

Please sign in to comment.