Skip to content

Commit b673fb8

Browse files
committed
added automatic multiline attribute conversions for long annotations
1 parent c2c16b5 commit b673fb8

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

app/helpers/MetaFormats/AnnotationToAttributeConverter.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ private static function shortenClass(string $className)
3232
return end($tokens);
3333
}
3434

35+
private static array $attributeParenthesesBuilders = [];
36+
3537
/**
3638
* Converts an array of preg_replace_callback matches to an attribute string.
3739
* @param array $matches An array of matches, with empty captures as NULL (PREG_UNMATCHED_AS_NULL flag).
@@ -117,7 +119,8 @@ private static function regexCaptureToAttributeCallback(array $matches)
117119
}
118120

119121
$paramAttributeClass = self::shortenClass(Param::class);
120-
return "#[{$paramAttributeClass}{$parenthesesBuilder->toString()}]";
122+
self::$attributeParenthesesBuilders[] = $parenthesesBuilder;
123+
return "#[{$paramAttributeClass}]";
121124
}
122125

123126
private static function checkValidationNullability(string $validation): bool
@@ -230,6 +233,7 @@ public static function convertFile(string $path)
230233
{
231234
// read file and replace @Param annotations with attributes
232235
$content = file_get_contents($path);
236+
self::$attributeParenthesesBuilders = [];
233237
$withInterleavedAttributes = preg_replace_callback(self::$postRegex, function ($matches) {
234238
return self::regexCaptureToAttributeCallback($matches);
235239
}, $content, -1, $count, PREG_UNMATCHED_AS_NULL);
@@ -257,10 +261,16 @@ public static function convertFile(string $path)
257261
// detected the end of the comment block "*/", flush attribute lines
258262
} elseif (trim($line) === "*/") {
259263
$lines[] = $line;
260-
foreach ($attributeLinesBuffer as $attributeLine) {
261-
// the attribute lines are shifted by one space to the right (due to the comment block origin)
262-
$lines[] = substr($attributeLine, 1);
264+
for ($i = 0; $i < count($attributeLinesBuffer); $i++) {
265+
$parenthesesBuilder = self::$attributeParenthesesBuilders[$i];
266+
$attributeLine = " #[{$paramAttributeClass}{$parenthesesBuilder->toString()}]";
267+
// change to multiline if the line is too long
268+
if (strlen($attributeLine) > 120) {
269+
$attributeLine = " #[{$paramAttributeClass}{$parenthesesBuilder->toMultilineString(4)}]";
270+
}
271+
$lines[] = $attributeLine;
263272
}
273+
264274
$attributeLinesBuffer = [];
265275
} else {
266276
$lines[] = $line;

app/helpers/Swagger/ParenthesesBuilder.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ public function addValue(string $value): ParenthesesBuilder
4848

4949
public function toString(): string
5050
{
51-
return '(' . implode(', ', $this->tokens) . ')';
51+
return "(" . implode(", ", $this->tokens) . ")";
52+
}
53+
54+
private static function spaces(int $count): string
55+
{
56+
return str_repeat(" ", $count);
57+
}
58+
59+
private const CODEBASE_INDENTATION = 4;
60+
public function toMultilineString(int $initialIndentation): string
61+
{
62+
// do not add indentation to the first line
63+
$str = "(\n";
64+
foreach ($this->tokens as $token) {
65+
$str .= self::spaces($initialIndentation + self::CODEBASE_INDENTATION) . $token . ",\n";
66+
}
67+
$str .= self::spaces($initialIndentation) . ")";
68+
return $str;
5269
}
5370
}

0 commit comments

Comments
 (0)