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 6439e7f
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/MethodSignatureString.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
use function var_export;

use const PHP_EOL;
use const PHP_MAJOR_VERSION;

final class MethodSignatureString
{
private const NULLABLE_PHP8 = 'null|';
private const NULLABLE_PHP7 = '?';
private const INDENT = ' ';

private TypeString $typeString;
/** @var TypeString */
private $typeString;

public function __construct(int $phpVersion)
{
Expand Down Expand Up @@ -61,6 +63,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 +161,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 6439e7f

Please sign in to comment.