Skip to content

Commit f944b35

Browse files
committed
Code cleanup.
1 parent f9220f3 commit f944b35

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

src/Utils/SchemaPrinter.php

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use function explode;
3737
use function implode;
3838
use function is_callable;
39+
use function is_string;
3940
use function ksort;
4041
use function ltrim;
4142
use function mb_strlen;
@@ -238,7 +239,7 @@ protected static function printDescription(array $options, $def, string $indenta
238239
return static::printDescriptionWithComments($description, $indentation, $firstInBlock);
239240
}
240241

241-
$preferMultipleLines = mb_strlen($description) > static::LINE_LENGTH;
242+
$preferMultipleLines = static::isMultiline($description);
242243
$blockString = BlockString::print($description, '', $preferMultipleLines);
243244
$prefix = $indentation !== '' && ! $firstInBlock
244245
? "\n" . $indentation
@@ -288,7 +289,7 @@ protected static function printArgs(array $options, array $args, string $indenta
288289
// Multiline?
289290
$serialized = '';
290291

291-
if ($length > static::LINE_LENGTH || $description) {
292+
if ($description || static::isMultiline($length)) {
292293
$serialized = "(\n" . implode("\n", $arguments) . "\n{$indentation})";
293294
} else {
294295
$serialized = '(' . implode(', ', array_map('trim', $arguments)) . ')';
@@ -307,10 +308,7 @@ protected static function printArg(FieldArgument $type, array $options, string $
307308
$indentation .
308309
static::printInputValue($type) .
309310
static::printTypeDirectives($type, $options, $indentation);
310-
311-
if (!$firstInBlock && mb_strlen($field) > static::LINE_LENGTH) {
312-
$field = "\n".ltrim($field, "\n");
313-
}
311+
$field = static::printLine($field, $firstInBlock);
314312

315313
return $field;
316314
}
@@ -384,10 +382,7 @@ protected static function printField(FieldDefinition $type, array $options, stri
384382
': ' .
385383
(string) $type->getType() .
386384
static::printTypeDirectives($type, $options, $indentation);
387-
388-
if (!$firstInBlock && mb_strlen($field) > static::LINE_LENGTH) {
389-
$field = "\n".ltrim($field, "\n");
390-
}
385+
$field = static::printLine($field, $firstInBlock);
391386

392387
return $field;
393388
}
@@ -492,10 +487,7 @@ protected static function printEnumValue(EnumValueDefinition $type, array $optio
492487
' ' .
493488
$type->name .
494489
static::printTypeDirectives($type, $options, $indentation);
495-
496-
if (!$firstInBlock && mb_strlen($value) > static::LINE_LENGTH) {
497-
$value = "\n".ltrim($value, "\n");
498-
}
490+
$value = static::printLine($value, $firstInBlock);
499491

500492
return $value;
501493
}
@@ -531,10 +523,7 @@ protected static function printInputObjectField(InputObjectField $type, array $o
531523
' ' .
532524
static::printInputValue($type) .
533525
static::printTypeDirectives($type, $options, $indentation);
534-
535-
if (!$firstInBlock && mb_strlen($field) > static::LINE_LENGTH) {
536-
$field = "\n".ltrim($field, "\n");
537-
}
526+
$field = static::printLine($field, $firstInBlock);
538527

539528
return $field;
540529
}
@@ -599,7 +588,7 @@ protected static function printTypeDirectives($type, array $options, string $ind
599588
$serialized = '';
600589

601590
if ($directives) {
602-
$delimiter = $length > static::LINE_LENGTH ? "\n{$indentation}" : ' ';
591+
$delimiter = static::isMultiline($length) ? "\n{$indentation}" : ' ';
603592
$serialized = $delimiter.implode($delimiter, $directives);
604593
}
605594

@@ -615,4 +604,19 @@ protected static function printTypeDirective(DirectiveNode $directive, array $op
615604
{
616605
return Printer::doPrint($directive);
617606
}
607+
608+
protected static function printLine(string $string, bool $firstInBlock = true): string {
609+
if (!$firstInBlock && static::isMultiline($string)) {
610+
$string = "\n".ltrim($string, "\n");
611+
}
612+
613+
return $string;
614+
}
615+
616+
/**
617+
* @param string|int $string
618+
*/
619+
protected static function isMultiline($string): bool {
620+
return (is_string($string) ? mb_strlen($string) : $string) > static::LINE_LENGTH;
621+
}
618622
}

0 commit comments

Comments
 (0)