Skip to content

Commit d521d1b

Browse files
committed
Refactored how lines combined into block of text.
1 parent f944b35 commit d521d1b

File tree

1 file changed

+49
-36
lines changed

1 file changed

+49
-36
lines changed

src/Utils/SchemaPrinter.php

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use function array_filter;
3232
use function array_keys;
3333
use function array_map;
34+
use function array_sum;
3435
use function array_values;
3536
use function count;
3637
use function explode;
@@ -40,6 +41,7 @@
4041
use function ksort;
4142
use function ltrim;
4243
use function mb_strlen;
44+
use function mb_strpos;
4345
use function self;
4446
use function sprintf;
4547
use function str_replace;
@@ -239,7 +241,7 @@ protected static function printDescription(array $options, $def, string $indenta
239241
return static::printDescriptionWithComments($description, $indentation, $firstInBlock);
240242
}
241243

242-
$preferMultipleLines = static::isMultiline($description);
244+
$preferMultipleLines = static::isLineTooLong($description);
243245
$blockString = BlockString::print($description, '', $preferMultipleLines);
244246
$prefix = $indentation !== '' && ! $firstInBlock
245247
? "\n" . $indentation
@@ -286,16 +288,8 @@ protected static function printArgs(array $options, array $args, string $indenta
286288
$arguments[] = $value;
287289
}
288290

289-
// Multiline?
290-
$serialized = '';
291-
292-
if ($description || static::isMultiline($length)) {
293-
$serialized = "(\n" . implode("\n", $arguments) . "\n{$indentation})";
294-
} else {
295-
$serialized = '(' . implode(', ', array_map('trim', $arguments)) . ')';
296-
}
297-
298-
return $serialized;
291+
// Return
292+
return static::printLines($arguments, '(', ')', $description || static::isLineTooLong($length), $indentation);
299293
}
300294

301295
/**
@@ -304,13 +298,10 @@ protected static function printArgs(array $options, array $args, string $indenta
304298
*/
305299
protected static function printArg(FieldArgument $type, array $options, string $indentation = '', bool $firstInBlock = true): string
306300
{
307-
$field = static::printDescription($options, $type, $indentation, $firstInBlock) .
301+
return static::printDescription($options, $type, $indentation, $firstInBlock) .
308302
$indentation .
309303
static::printInputValue($type) .
310304
static::printTypeDirectives($type, $options, $indentation);
311-
$field = static::printLine($field, $firstInBlock);
312-
313-
return $field;
314305
}
315306

316307
/**
@@ -375,16 +366,13 @@ static function (FieldDefinition $f, int $i) use ($options): string {
375366
*/
376367
protected static function printField(FieldDefinition $type, array $options, string $indentation = '', bool $firstInBlock = true): string
377368
{
378-
$field = static::printDescription($options, $type, $indentation, $firstInBlock) .
369+
return static::printDescription($options, $type, $indentation, $firstInBlock) .
379370
' ' .
380371
$type->name .
381372
static::printArgs($options, $type->args, ' ') .
382373
': ' .
383374
(string) $type->getType() .
384375
static::printTypeDirectives($type, $options, $indentation);
385-
$field = static::printLine($field, $firstInBlock);
386-
387-
return $field;
388376
}
389377

390378
/**
@@ -483,13 +471,10 @@ static function (EnumValueDefinition $value, int $i) use ($options): string {
483471
*/
484472
protected static function printEnumValue(EnumValueDefinition $type, array $options, string $indentation = '', bool $firstInBlock = false): string
485473
{
486-
$value = static::printDescription($options, $type, $indentation, $firstInBlock) .
474+
return static::printDescription($options, $type, $indentation, $firstInBlock) .
487475
' ' .
488476
$type->name .
489477
static::printTypeDirectives($type, $options, $indentation);
490-
$value = static::printLine($value, $firstInBlock);
491-
492-
return $value;
493478
}
494479

495480
/**
@@ -519,23 +504,18 @@ static function (InputObjectField $f, $i) use ($options): string {
519504
*/
520505
protected static function printInputObjectField(InputObjectField $type, array $options, string $indentation = '', bool $firstInBlock = true): string
521506
{
522-
$field = static::printDescription($options, $type, $indentation, $firstInBlock) .
507+
return static::printDescription($options, $type, $indentation, $firstInBlock) .
523508
' ' .
524509
static::printInputValue($type) .
525510
static::printTypeDirectives($type, $options, $indentation);
526-
$field = static::printLine($field, $firstInBlock);
527-
528-
return $field;
529511
}
530512

531513
/**
532514
* @param array<string> $items
533515
*/
534516
protected static function printBlock(array $items): string
535517
{
536-
return count($items) > 0
537-
? " {\n" . implode("\n", $items) . "\n}"
538-
: '';
518+
return static::printLines($items, ' {', '}', true);
539519
}
540520

541521
/**
@@ -588,7 +568,7 @@ protected static function printTypeDirectives($type, array $options, string $ind
588568
$serialized = '';
589569

590570
if ($directives) {
591-
$delimiter = static::isMultiline($length) ? "\n{$indentation}" : ' ';
571+
$delimiter = static::isLineTooLong($length) ? "\n{$indentation}" : ' ';
592572
$serialized = $delimiter.implode($delimiter, $directives);
593573
}
594574

@@ -605,18 +585,51 @@ protected static function printTypeDirective(DirectiveNode $directive, array $op
605585
return Printer::doPrint($directive);
606586
}
607587

608-
protected static function printLine(string $string, bool $firstInBlock = true): string {
609-
if (!$firstInBlock && static::isMultiline($string)) {
610-
$string = "\n".ltrim($string, "\n");
588+
/**
589+
* @param array<string> $lines
590+
*/
591+
protected static function printLines(array $lines, string $begin, string $end, bool $multiline, string $indentation = ''): string
592+
{
593+
$block = '';
594+
595+
if ($lines) {
596+
$multiline = $multiline || static::isLineTooLong(array_sum(array_map('mb_strlen', $lines)));
597+
598+
if ($multiline) {
599+
$wrapped = false;
600+
601+
for ($i = 0, $c = count($lines); $i < $c; $i++) {
602+
// If line too long and contains LF we wrap it by empty lines
603+
$line = trim($lines[$i], "\n");
604+
$wrap = static::isLineTooLong($line) || mb_strpos($line, "\n") !== false;
605+
606+
if ($i === 0) {
607+
$line = "\n{$line}";
608+
}
609+
610+
if (($wrap && $i > 0) || $wrapped) {
611+
$line = "\n{$line}";
612+
}
613+
614+
$block .= "{$line}\n";
615+
$wrapped = $wrap;
616+
}
617+
618+
$block .= $indentation;
619+
} else {
620+
$block = implode(', ', array_map('trim', $lines));
621+
}
622+
623+
$block = $begin.$block.$end;
611624
}
612625

613-
return $string;
626+
return $block;
614627
}
615628

616629
/**
617630
* @param string|int $string
618631
*/
619-
protected static function isMultiline($string): bool {
632+
protected static function isLineTooLong($string): bool {
620633
return (is_string($string) ? mb_strlen($string) : $string) > static::LINE_LENGTH;
621634
}
622635
}

0 commit comments

Comments
 (0)