From 9130abaa16369449c048ffd4e393469834fcf05a Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 18 Dec 2021 14:15:23 +0400 Subject: [PATCH 01/90] Settings. --- src/Printer/Settings.php | 73 +++++++++++++++++++++++ src/Printer/Settings/DefaultSettings.php | 76 ++++++++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 src/Printer/Settings.php create mode 100644 src/Printer/Settings/DefaultSettings.php diff --git a/src/Printer/Settings.php b/src/Printer/Settings.php new file mode 100644 index 00000000..9b021082 --- /dev/null +++ b/src/Printer/Settings.php @@ -0,0 +1,73 @@ + Date: Sat, 18 Dec 2021 17:25:44 +0400 Subject: [PATCH 02/90] "Description" block implementation. --- src/Printer/Blocks/Block.php | 59 +++++++ src/Printer/Blocks/Description.php | 76 +++++++++ src/Printer/Blocks/DescriptionTest.php | 228 +++++++++++++++++++++++++ 3 files changed, 363 insertions(+) create mode 100644 src/Printer/Blocks/Block.php create mode 100644 src/Printer/Blocks/Description.php create mode 100644 src/Printer/Blocks/DescriptionTest.php diff --git a/src/Printer/Blocks/Block.php b/src/Printer/Blocks/Block.php new file mode 100644 index 00000000..7f7e2220 --- /dev/null +++ b/src/Printer/Blocks/Block.php @@ -0,0 +1,59 @@ +getContent(); + } + + protected function getContent(): string { + if ($this->content === null) { + $this->content = $this->serialize(); + $this->length = mb_strlen($this->content); + } + + return $this->content; + } + + protected function getLength(): int { + return $this->length ?? mb_strlen($this->getContent()); + } + + protected function isMultiline(): bool { + return $this->isLineTooLong($this->getLength()); + } + + protected function reset(): void { + $this->content = null; + $this->length = null; + } + + abstract protected function serialize(): string; + + protected function indent(): string { + return str_repeat($this->settings->getIndent(), $this->level); + } + + protected function isLineTooLong(int $length): bool { + return $length > $this->settings->getLineLength(); + } +} diff --git a/src/Printer/Blocks/Description.php b/src/Printer/Blocks/Description.php new file mode 100644 index 00000000..0048040d --- /dev/null +++ b/src/Printer/Blocks/Description.php @@ -0,0 +1,76 @@ +settings->getLineEnd(); + $indent = $this->indent(); + $wrapper = '"""'; + $description = $this->description; + + // Standardize line endings + $description = str_replace(["\r\n", "\n\r", "\n", "\r"], $eol, $description); + + // Normalize? + if ($this->settings->isNormalizeDescription()) { + $description = rtrim(trim($description, $eol)); + + if (!$description) { + return $description; + } + } + + // Whitespace only? + if (preg_match('/^\p{Zs}+$/u', $description)) { + return "\"{$description}\""; + } + + // Multiline? (markdown) + $length = mb_strlen($indent) + 2 * mb_strlen($wrapper) + mb_strlen($description); + $isMultiline = $this->isLineTooLong($length) + || mb_strpos($description, $eol) + || str_starts_with($description, ' ') + || str_starts_with($description, "\t") + || str_ends_with($description, '"') + || str_ends_with($description, '\\\\'); + + if ($isMultiline) { + $description = $eol.$indent.str_replace($eol, "{$eol}{$indent}", $description).$eol.$indent; + } + + // Wrap && Escape + $description = str_replace($wrapper, "\\{$wrapper}", $description); + $description = "{$indent}{$wrapper}{$description}{$wrapper}"; + + // Return + return $description; + } +} diff --git a/src/Printer/Blocks/DescriptionTest.php b/src/Printer/Blocks/DescriptionTest.php new file mode 100644 index 00000000..ff72cd6f --- /dev/null +++ b/src/Printer/Blocks/DescriptionTest.php @@ -0,0 +1,228 @@ + + // ========================================================================= + /** + * @covers ::__toString + * + * @dataProvider dataProviderToString + */ + public function testToString(string $expected, Settings $settings, int $level, string $description): void { + self::assertEquals($expected, (string)(new Description($settings, $level, $description))); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderToString(): array { + return [ + 'Prints an empty description' => [ + '""""""', + new class() extends DefaultSettings { + public function isNormalizeDescription(): bool { + return false; + } + }, + 0, + '', + ], + 'Prints an empty description (normalized)' => [ + '', + new class() extends DefaultSettings { + public function isNormalizeDescription(): bool { + return true; + } + }, + 0, + '', + ], + 'Prints an description with only whitespace' => [ + '" "', + new class() extends DefaultSettings { + public function isNormalizeDescription(): bool { + return false; + } + }, + 0, + ' ', + ], + 'Prints an description with only whitespace (normalized)' => [ + '', + new class() extends DefaultSettings { + public function isNormalizeDescription(): bool { + return true; + } + }, + 0, + ' ', + ], + 'One-line prints a short description' => [ + '"""Short description"""', + new DefaultSettings(), + 0, + 'Short description', + ], + 'One-line prints a long description' => [ + <<<'STRING' + """ + Long description + """ + STRING, + new class() extends DefaultSettings { + public function getLineLength(): int { + return 4; + } + }, + 0, + 'Long description', + ], + 'Description is short' => [ + <<<'STRING' + """description""" + STRING, + new class() extends DefaultSettings { + public function getLineLength(): int { + return 17; + } + }, + 0, + 'description', + ], + 'Description is short (indent)' => [ + <<<'STRING' + """description""" + STRING, + new class() extends DefaultSettings { + public function getLineLength(): int { + return 21; + } + + public function getIndent(): string { + return ' '; + } + }, + 2, + 'description', + ], + 'Description is long (indent)' => [ + <<<'STRING' + """ + description + """ + STRING, + new class() extends DefaultSettings { + public function getLineLength(): int { + return 21 - 1; + } + + public function getIndent(): string { + return ' '; + } + }, + 2, + 'description', + ], + 'Multi-line description' => [ + "\"\"\"\nMulti-line\n description \n\n\n\"\"\"", + new class() extends DefaultSettings { + public function isNormalizeDescription(): bool { + return false; + } + }, + 0, + "Multi-line\n description \n\n", + ], + 'Multi-line description (normalized)' => [ + <<<'STRING' + """ + Multi-line + description + """ + STRING, + new class() extends DefaultSettings { + public function isNormalizeDescription(): bool { + return true; + } + }, + 0, + "Multi-line\n description \n\n", + ], + 'Leading space' => [ + <<<'STRING' + """ + Leading space + """ + STRING, + new DefaultSettings(), + 0, + " Leading space", + ], + 'Leading tab' => [ + "\"\"\"\n\tLeading tab\n\"\"\"", + new DefaultSettings(), + 0, + "\tLeading tab", + ], + 'Trailing "' => [ + <<<'STRING' + """ + Trailing " + """ + STRING, + new DefaultSettings(), + 0, + 'Trailing "', + ], + 'Trailing backslashes' => [ + <<<'STRING' + """ + Trailing \\ + """ + STRING, + new DefaultSettings(), + 0, + 'Trailing \\\\', + ], + 'Escape wrapper' => [ + <<<'STRING' + """String with \""" wrapper""" + STRING, + new DefaultSettings(), + 0, + 'String with """ wrapper', + ], + 'Indent' => [ + <<<'STRING' + """ + Multi-line + description + """ + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 2, + <<<'STRING' + Multi-line + description + STRING, + ], + ]; + } + // +} From 4c11a6ccebae57c3b6ca60a83a0b92ea5af43eee Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 19 Dec 2021 12:26:09 +0400 Subject: [PATCH 03/90] 'BlockList' implementation + improvements. --- src/Printer/Blocks/Block.php | 35 ++++- src/Printer/Blocks/BlockList.php | 139 +++++++++++++++++ src/Printer/Blocks/BlockListTest.php | 203 +++++++++++++++++++++++++ src/Printer/Blocks/BlockTest.php | 133 ++++++++++++++++ src/Printer/Blocks/Description.php | 13 +- src/Printer/Blocks/DescriptionTest.php | 4 +- 6 files changed, 512 insertions(+), 15 deletions(-) create mode 100644 src/Printer/Blocks/BlockList.php create mode 100644 src/Printer/Blocks/BlockListTest.php create mode 100644 src/Printer/Blocks/BlockTest.php diff --git a/src/Printer/Blocks/Block.php b/src/Printer/Blocks/Block.php index 7f7e2220..32159404 100644 --- a/src/Printer/Blocks/Block.php +++ b/src/Printer/Blocks/Block.php @@ -1,4 +1,4 @@ -content === null) { - $this->content = $this->serialize(); - $this->length = mb_strlen($this->content); + $this->content = $this->serialize(); + $this->length = mb_strlen($this->content); + $this->multiline = $this->isLineTooLong($this->length) + || $this->isStringMultiline($this->content); } return $this->content; @@ -39,16 +42,27 @@ protected function getLength(): int { } protected function isMultiline(): bool { - return $this->isLineTooLong($this->getLength()); + return $this->getContent() && $this->multiline; } protected function reset(): void { - $this->content = null; - $this->length = null; + $this->multiline = null; + $this->content = null; + $this->length = null; } abstract protected function serialize(): string; + abstract protected function isNormalized(): bool; + + protected function eol(): string { + return $this->settings->getLineEnd(); + } + + protected function space(): string { + return $this->settings->getSpace(); + } + protected function indent(): string { return str_repeat($this->settings->getIndent(), $this->level); } @@ -56,4 +70,9 @@ protected function indent(): string { protected function isLineTooLong(int $length): bool { return $length > $this->settings->getLineLength(); } + + protected function isStringMultiline(string $string): bool { + return mb_strpos($string, "\n") !== false + || mb_strpos($string, "\r") !== false; + } } diff --git a/src/Printer/Blocks/BlockList.php b/src/Printer/Blocks/BlockList.php new file mode 100644 index 00000000..b35701d6 --- /dev/null +++ b/src/Printer/Blocks/BlockList.php @@ -0,0 +1,139 @@ + + */ +abstract class BlockList extends Block implements ArrayAccess { + /** + * @var array + */ + private array $blocks = []; + + /** + * @var array + */ + private array $multiline = []; + private int $length = 0; + private int $reserved; + + public function __construct( + Settings $settings, + int $reserved, + ) { + parent::__construct($settings); + + $this->reserved = $reserved; + } + + protected function isMultiline(): bool { + return count($this->multiline) > 0 || parent::isMultiline(); + } + + protected function serialize(): string { + // Blocks? + $blocks = $this->blocks; + $count = count($blocks); + + if ($this->isNormalized()) { + ksort($blocks, SORT_NATURAL); + } + + if (!$count) { + return ''; + } + + // Join + $eol = $this->eol(); + $separator = ",{$this->space()}"; + $isMultiline = count($this->multiline) > 0 || $this->isLineTooLong( + $this->reserved + $this->length + mb_strlen($separator) * ($count - 1), + ); + $content = ''; + + if ($isMultiline) { + $last = $count - 1; + $index = 0; + $previous = false; + + foreach ($blocks as $block) { + // Multiline block should be wrapped by empty lines + $multiline = $block->isMultiline(); + + if (($multiline && $index > 0) || $previous) { + $content .= $eol; + } + + $content .= $block; + + if ($index < $last) { + $content .= $eol; + } + + $previous = $multiline; + $index = $index + 1; + } + } else { + $content = implode($separator, $blocks); + } + + // Return + return $content; + } + + // + // ========================================================================= + /** + * @param string $offset + */ + public function offsetExists(mixed $offset): bool { + return isset($this->blocks[$offset]); + } + + /** + * @param string $offset + */ + public function offsetGet(mixed $offset): Block { + return $this->blocks[$offset]; + } + + /** + * @param string $offset + * @param Block $value + */ + public function offsetSet(mixed $offset, mixed $value): void { + $this->blocks[$offset] = $value; + $this->length += $value->getLength(); + + if ($value->isMultiline()) { + $this->multiline[$offset] = true; + } + + $this->reset(); + } + + /** + * @param string $offset + */ + public function offsetUnset(mixed $offset): void { + if (isset($this->blocks[$offset])) { + $this->length -= $this->blocks[$offset]->getLength(); + } + + unset($this->blocks[$offset]); + unset($this->multiline[$offset]); + + $this->reset(); + } + // +} diff --git a/src/Printer/Blocks/BlockListTest.php b/src/Printer/Blocks/BlockListTest.php new file mode 100644 index 00000000..7ccffcae --- /dev/null +++ b/src/Printer/Blocks/BlockListTest.php @@ -0,0 +1,203 @@ + + // ========================================================================= + /** + * @covers ::__toString + * + * @dataProvider dataProviderToString + * + * @param array $blocks + */ + public function testToString( + string $expected, + Settings $settings, + int $reserved, + bool $normalized, + array $blocks, + ): void { + $list = new class($settings, $reserved, $normalized) extends BlockList { + public function __construct( + Settings $settings, + int $reserved, + protected bool $normalized, + ) { + parent::__construct($settings, $reserved); + } + + protected function isNormalized(): bool { + return $this->normalized; + } + }; + + foreach ($blocks as $name => $block) { + $list[$name] = $block; + } + + self::assertEquals($expected, (string) $list); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderToString(): array { + return [ + 'one single-line block' => [ + <<<'STRING' + block a + STRING, + new DefaultSettings(), + 0, + false, + [ + 'a' => new BlockListTest__Block(false, 'block a'), + ], + ], + 'one multi-line block' => [ + <<<'STRING' + block a + STRING, + new DefaultSettings(), + 0, + false, + [ + 'a' => new BlockListTest__Block(true, 'block a'), + ], + ], + 'short block list' => [ + <<<'STRING' + block a, block b + STRING, + new DefaultSettings(), + 0, + false, + [ + 'a' => new BlockListTest__Block(false, 'block a'), + 'b' => new BlockListTest__Block(false, 'block b'), + ], + ], + 'long block list' => [ + <<<'STRING' + block b + block a + STRING, + new class() extends DefaultSettings { + public function getLineLength(): int { + return 20; + } + }, + 5, + false, + [ + 'b' => new BlockListTest__Block(false, 'block b'), + 'a' => new BlockListTest__Block(false, 'block a'), + ], + ], + 'short block list with multiline block' => [ + <<<'STRING' + block a + + block b + STRING, + new DefaultSettings(), + 0, + false, + [ + 'a' => new BlockListTest__Block(false, 'block a'), + 'b' => new BlockListTest__Block(true, 'block b'), + ], + ], + 'block list with multiline blocks' => [ + <<<'STRING' + block a + + block b + block c + + block d + + block e + block f + + block g + STRING, + new DefaultSettings(), + 0, + false, + [ + 'a' => new BlockListTest__Block(true, 'block a'), + 'b' => new BlockListTest__Block(false, 'block b'), + 'c' => new BlockListTest__Block(false, 'block c'), + 'd' => new BlockListTest__Block(true, 'block d'), + 'e' => new BlockListTest__Block(false, 'block e'), + 'f' => new BlockListTest__Block(false, 'block f'), + 'g' => new BlockListTest__Block(true, 'block g'), + ], + ], + 'normalized block list' => [ + <<<'STRING' + block a, block b + STRING, + new DefaultSettings(), + 0, + true, + [ + 'b' => new BlockListTest__Block(false, 'block b'), + 'a' => new BlockListTest__Block(false, 'block a'), + ], + ], + ]; + } + // +} + +// @phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses +// @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps + +/** + * @internal + * @noinspection PhpMultipleClassesDeclarationsInOneFile + */ +class BlockListTest__Block extends Block { + /** @noinspection PhpMissingParentConstructorInspection */ + public function __construct( + protected bool $multiline, + protected string $content, + ) { + // empty + } + + protected function getContent(): string { + return $this->content; + } + + protected function getLength(): int { + return mb_strlen($this->getContent()); + } + + public function isMultiline(): bool { + return $this->multiline; + } + + protected function serialize(): string { + return ''; + } + + protected function isNormalized(): bool { + return false; + } +} diff --git a/src/Printer/Blocks/BlockTest.php b/src/Printer/Blocks/BlockTest.php new file mode 100644 index 00000000..2edffe84 --- /dev/null +++ b/src/Printer/Blocks/BlockTest.php @@ -0,0 +1,133 @@ + + // ========================================================================= + /** + * @covers ::getContent + */ + public function testGetContent(): void { + $content = 'content'; + $block = Mockery::mock(BlockTest__Block::class, [new DefaultSettings()]); + $block->shouldAllowMockingProtectedMethods(); + $block->makePartial(); + $block + ->shouldReceive('serialize') + ->once() + ->andReturn($content); + + self::assertEquals($content, $block->getContent()); + self::assertEquals($content, $block->getContent()); + } + + /** + * @covers ::getLength + */ + public function testGetLength(): void { + $content = 'content'; + $length = mb_strlen($content); + $block = Mockery::mock(BlockTest__Block::class, [new DefaultSettings()]); + $block->shouldAllowMockingProtectedMethods(); + $block->makePartial(); + $block + ->shouldReceive('serialize') + ->once() + ->andReturn($content); + + self::assertEquals($length, $block->getLength()); + self::assertEquals($length, $block->getLength()); + } + + /** + * @covers ::isMultiline + * + * @dataProvider dataProviderIsMultiline + */ + public function testIsMultiline(bool $expected, Settings $settings, string $content): void { + $block = Mockery::mock(BlockTest__Block::class, [$settings]); + $block->shouldAllowMockingProtectedMethods(); + $block->makePartial(); + $block + ->shouldReceive('serialize') + ->once() + ->andReturn($content); + + self::assertEquals($expected, $block->isMultiline()); + self::assertEquals($expected, $block->isMultiline()); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderIsMultiline(): array { + return [ + 'single short line' => [ + false, + new DefaultSettings(), + 'short line', + ], + 'single long line' => [ + true, + new class() extends DefaultSettings { + public function getLineLength(): int { + return 5; + } + }, + 'long line', + ], + 'multi line' => [ + true, + new DefaultSettings(), + "multi\nline", + ], + ]; + } + // +} + +// @phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses +// @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps + +/** + * @internal + * @noinspection PhpMultipleClassesDeclarationsInOneFile + */ +class BlockTest__Block extends Block { + public function __construct(Settings $settings, int $level = 0) { + parent::__construct($settings, $level); + } + + public function getContent(): string { + return parent::getContent(); + } + + public function getLength(): int { + return parent::getLength(); + } + + public function isMultiline(): bool { + return parent::isMultiline(); + } + + protected function serialize(): string { + return ''; + } + + protected function isNormalized(): bool { + return false; + } +} diff --git a/src/Printer/Blocks/Description.php b/src/Printer/Blocks/Description.php index 0048040d..d4da5a03 100644 --- a/src/Printer/Blocks/Description.php +++ b/src/Printer/Blocks/Description.php @@ -1,10 +1,9 @@ -settings->isNormalizeDescription(); + } + protected function serialize(): string { // Begin - $eol = $this->settings->getLineEnd(); + $eol = $this->eol(); $indent = $this->indent(); $wrapper = '"""'; $description = $this->description; @@ -40,7 +43,7 @@ protected function serialize(): string { $description = str_replace(["\r\n", "\n\r", "\n", "\r"], $eol, $description); // Normalize? - if ($this->settings->isNormalizeDescription()) { + if ($this->isNormalized()) { $description = rtrim(trim($description, $eol)); if (!$description) { @@ -56,7 +59,7 @@ protected function serialize(): string { // Multiline? (markdown) $length = mb_strlen($indent) + 2 * mb_strlen($wrapper) + mb_strlen($description); $isMultiline = $this->isLineTooLong($length) - || mb_strpos($description, $eol) + || $this->isStringMultiline($description) || str_starts_with($description, ' ') || str_starts_with($description, "\t") || str_ends_with($description, '"') diff --git a/src/Printer/Blocks/DescriptionTest.php b/src/Printer/Blocks/DescriptionTest.php index ff72cd6f..d7357f89 100644 --- a/src/Printer/Blocks/DescriptionTest.php +++ b/src/Printer/Blocks/DescriptionTest.php @@ -1,4 +1,4 @@ - From 0b024a70a0867ebc94711a6e0f8ca0effe8c9cbd Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 19 Dec 2021 13:16:14 +0400 Subject: [PATCH 04/90] `BlockList` will support `$level`. --- src/Printer/Blocks/BlockList.php | 16 ++++++++++----- src/Printer/Blocks/BlockListTest.php | 29 ++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/Printer/Blocks/BlockList.php b/src/Printer/Blocks/BlockList.php index b35701d6..3533ddb9 100644 --- a/src/Printer/Blocks/BlockList.php +++ b/src/Printer/Blocks/BlockList.php @@ -14,7 +14,7 @@ * @internal * @implements ArrayAccess */ -abstract class BlockList extends Block implements ArrayAccess { +class BlockList extends Block implements ArrayAccess { /** * @var array */ @@ -29,9 +29,10 @@ abstract class BlockList extends Block implements ArrayAccess { public function __construct( Settings $settings, + int $level, int $reserved, ) { - parent::__construct($settings); + parent::__construct($settings, $level); $this->reserved = $reserved; } @@ -40,6 +41,10 @@ protected function isMultiline(): bool { return count($this->multiline) > 0 || parent::isMultiline(); } + protected function isNormalized(): bool { + return false; + } + protected function serialize(): string { // Blocks? $blocks = $this->blocks; @@ -54,7 +59,6 @@ protected function serialize(): string { } // Join - $eol = $this->eol(); $separator = ",{$this->space()}"; $isMultiline = count($this->multiline) > 0 || $this->isLineTooLong( $this->reserved + $this->length + mb_strlen($separator) * ($count - 1), @@ -62,8 +66,10 @@ protected function serialize(): string { $content = ''; if ($isMultiline) { + $eol = $this->eol(); $last = $count - 1; $index = 0; + $indent = $this->indent(); $previous = false; foreach ($blocks as $block) { @@ -74,7 +80,7 @@ protected function serialize(): string { $content .= $eol; } - $content .= $block; + $content .= "{$indent}{$block}"; if ($index < $last) { $content .= $eol; @@ -113,7 +119,7 @@ public function offsetGet(mixed $offset): Block { */ public function offsetSet(mixed $offset, mixed $value): void { $this->blocks[$offset] = $value; - $this->length += $value->getLength(); + $this->length += $value->getLength(); if ($value->isMultiline()) { $this->multiline[$offset] = true; diff --git a/src/Printer/Blocks/BlockListTest.php b/src/Printer/Blocks/BlockListTest.php index 7ccffcae..a3fefeea 100644 --- a/src/Printer/Blocks/BlockListTest.php +++ b/src/Printer/Blocks/BlockListTest.php @@ -23,17 +23,19 @@ class BlockListTest extends TestCase { public function testToString( string $expected, Settings $settings, + int $level, int $reserved, bool $normalized, array $blocks, ): void { - $list = new class($settings, $reserved, $normalized) extends BlockList { + $list = new class($settings, $level, $reserved, $normalized) extends BlockList { public function __construct( Settings $settings, + int $level, int $reserved, protected bool $normalized, ) { - parent::__construct($settings, $reserved); + parent::__construct($settings, $level, $reserved); } protected function isNormalized(): bool { @@ -62,6 +64,7 @@ public function dataProviderToString(): array { STRING, new DefaultSettings(), 0, + 0, false, [ 'a' => new BlockListTest__Block(false, 'block a'), @@ -73,6 +76,7 @@ public function dataProviderToString(): array { STRING, new DefaultSettings(), 0, + 0, false, [ 'a' => new BlockListTest__Block(true, 'block a'), @@ -84,6 +88,7 @@ public function dataProviderToString(): array { STRING, new DefaultSettings(), 0, + 0, false, [ 'a' => new BlockListTest__Block(false, 'block a'), @@ -100,6 +105,7 @@ public function getLineLength(): int { return 20; } }, + 0, 5, false, [ @@ -115,6 +121,7 @@ public function getLineLength(): int { STRING, new DefaultSettings(), 0, + 0, false, [ 'a' => new BlockListTest__Block(false, 'block a'), @@ -137,6 +144,7 @@ public function getLineLength(): int { STRING, new DefaultSettings(), 0, + 0, false, [ 'a' => new BlockListTest__Block(true, 'block a'), @@ -154,12 +162,29 @@ public function getLineLength(): int { STRING, new DefaultSettings(), 0, + 0, true, [ 'b' => new BlockListTest__Block(false, 'block b'), 'a' => new BlockListTest__Block(false, 'block a'), ], ], + 'multi-line with level' => [ + <<<'STRING' + block a + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 2, + 0, + false, + [ + 'a' => new BlockListTest__Block(true, 'block a'), + ], + ], ]; } // From 990182ce014961d163a88a4bebcc0dda14296d52 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 19 Dec 2021 16:07:00 +0400 Subject: [PATCH 05/90] `BlockList` allows disabling wrap multiline blocks with empty lines. --- src/Printer/Blocks/BlockList.php | 17 +++++---- src/Printer/Blocks/BlockListTest.php | 57 +++++++++++++++++----------- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/src/Printer/Blocks/BlockList.php b/src/Printer/Blocks/BlockList.php index 3533ddb9..7c846529 100644 --- a/src/Printer/Blocks/BlockList.php +++ b/src/Printer/Blocks/BlockList.php @@ -25,16 +25,15 @@ class BlockList extends Block implements ArrayAccess { */ private array $multiline = []; private int $length = 0; - private int $reserved; public function __construct( Settings $settings, int $level, - int $reserved, + private int $reserved, + private bool $normalized = false, + private bool $wrapped = false, ) { parent::__construct($settings, $level); - - $this->reserved = $reserved; } protected function isMultiline(): bool { @@ -42,7 +41,11 @@ protected function isMultiline(): bool { } protected function isNormalized(): bool { - return false; + return $this->normalized; + } + + protected function isWrapped(): bool { + return $this->wrapped; } protected function serialize(): string { @@ -70,11 +73,11 @@ protected function serialize(): string { $last = $count - 1; $index = 0; $indent = $this->indent(); + $wrapped = $this->isWrapped(); $previous = false; foreach ($blocks as $block) { - // Multiline block should be wrapped by empty lines - $multiline = $block->isMultiline(); + $multiline = $wrapped && $block->isMultiline(); if (($multiline && $index > 0) || $previous) { $content .= $eol; diff --git a/src/Printer/Blocks/BlockListTest.php b/src/Printer/Blocks/BlockListTest.php index a3fefeea..a61d2b64 100644 --- a/src/Printer/Blocks/BlockListTest.php +++ b/src/Printer/Blocks/BlockListTest.php @@ -26,22 +26,10 @@ public function testToString( int $level, int $reserved, bool $normalized, + bool $wrapped, array $blocks, ): void { - $list = new class($settings, $level, $reserved, $normalized) extends BlockList { - public function __construct( - Settings $settings, - int $level, - int $reserved, - protected bool $normalized, - ) { - parent::__construct($settings, $level, $reserved); - } - - protected function isNormalized(): bool { - return $this->normalized; - } - }; + $list = new BlockList($settings, $level, $reserved, $normalized, $wrapped); foreach ($blocks as $name => $block) { $list[$name] = $block; @@ -58,7 +46,7 @@ protected function isNormalized(): bool { */ public function dataProviderToString(): array { return [ - 'one single-line block' => [ + 'one single-line block' => [ <<<'STRING' block a STRING, @@ -66,11 +54,12 @@ public function dataProviderToString(): array { 0, 0, false, + true, [ 'a' => new BlockListTest__Block(false, 'block a'), ], ], - 'one multi-line block' => [ + 'one multi-line block' => [ <<<'STRING' block a STRING, @@ -78,11 +67,12 @@ public function dataProviderToString(): array { 0, 0, false, + true, [ 'a' => new BlockListTest__Block(true, 'block a'), ], ], - 'short block list' => [ + 'short block list' => [ <<<'STRING' block a, block b STRING, @@ -90,12 +80,13 @@ public function dataProviderToString(): array { 0, 0, false, + true, [ 'a' => new BlockListTest__Block(false, 'block a'), 'b' => new BlockListTest__Block(false, 'block b'), ], ], - 'long block list' => [ + 'long block list' => [ <<<'STRING' block b block a @@ -108,12 +99,13 @@ public function getLineLength(): int { 0, 5, false, + true, [ 'b' => new BlockListTest__Block(false, 'block b'), 'a' => new BlockListTest__Block(false, 'block a'), ], ], - 'short block list with multiline block' => [ + 'short block list with multiline block' => [ <<<'STRING' block a @@ -123,12 +115,13 @@ public function getLineLength(): int { 0, 0, false, + true, [ 'a' => new BlockListTest__Block(false, 'block a'), 'b' => new BlockListTest__Block(true, 'block b'), ], ], - 'block list with multiline blocks' => [ + 'block list with multiline blocks' => [ <<<'STRING' block a @@ -146,6 +139,7 @@ public function getLineLength(): int { 0, 0, false, + true, [ 'a' => new BlockListTest__Block(true, 'block a'), 'b' => new BlockListTest__Block(false, 'block b'), @@ -156,7 +150,24 @@ public function getLineLength(): int { 'g' => new BlockListTest__Block(true, 'block g'), ], ], - 'normalized block list' => [ + 'block list with multiline blocks without wrap' => [ + <<<'STRING' + block c + block b + block a + STRING, + new DefaultSettings(), + 0, + 0, + false, + false, + [ + 'c' => new BlockListTest__Block(true, 'block c'), + 'b' => new BlockListTest__Block(false, 'block b'), + 'a' => new BlockListTest__Block(true, 'block a'), + ], + ], + 'normalized block list' => [ <<<'STRING' block a, block b STRING, @@ -164,12 +175,13 @@ public function getLineLength(): int { 0, 0, true, + true, [ 'b' => new BlockListTest__Block(false, 'block b'), 'a' => new BlockListTest__Block(false, 'block a'), ], ], - 'multi-line with level' => [ + 'multi-line with level' => [ <<<'STRING' block a STRING, @@ -181,6 +193,7 @@ public function getIndent(): string { 2, 0, false, + false, [ 'a' => new BlockListTest__Block(true, 'block a'), ], From 03b60e33210b2af28219c6046e7cc6c2c1ab2b59 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 19 Dec 2021 17:16:40 +0400 Subject: [PATCH 06/90] `BlockString` implemented (required to set proper indent and EOL). --- src/Printer/Blocks/BlockString.php | 53 ++++++ src/Printer/Blocks/BlockStringTest.php | 213 +++++++++++++++++++++++++ 2 files changed, 266 insertions(+) create mode 100644 src/Printer/Blocks/BlockString.php create mode 100644 src/Printer/Blocks/BlockStringTest.php diff --git a/src/Printer/Blocks/BlockString.php b/src/Printer/Blocks/BlockString.php new file mode 100644 index 00000000..c08ceec3 --- /dev/null +++ b/src/Printer/Blocks/BlockString.php @@ -0,0 +1,53 @@ +eol(); + $indent = $this->indent(); + $wrapper = '"""'; + $content = $this->string; + + // Whitespace only? (seems it is not possible, so just for the case) + if (preg_match('/^\p{Zs}+$/u', $content)) { + return "\"{$content}\""; + } + + // Multiline? (markdown) + $length = $this->reserved + mb_strlen($indent) + 2 * mb_strlen($wrapper) + mb_strlen($content); + $isMultiline = $this->isLineTooLong($length) + || $this->isStringMultiline($content) + || str_ends_with($content, '"') + || str_ends_with($content, '\\\\'); + + if ($isMultiline) { + $content = $eol.$indent.preg_replace('/(\R)/u', "\$1{$indent}", $content).$eol.$indent; + } + + // Wrap && Escape + $content = str_replace($wrapper, "\\{$wrapper}", $content); + $content = "{$indent}{$wrapper}{$content}{$wrapper}"; + + // Return + return $content; + } +} diff --git a/src/Printer/Blocks/BlockStringTest.php b/src/Printer/Blocks/BlockStringTest.php new file mode 100644 index 00000000..53d226ab --- /dev/null +++ b/src/Printer/Blocks/BlockStringTest.php @@ -0,0 +1,213 @@ + + // ========================================================================= + /** + * @covers ::__toString + * + * @dataProvider dataProviderToString + */ + public function testToString(string $expected, Settings $settings, int $level, int $reserved, string $string): void { + $actual = (string) (new BlockString($settings, $level, $reserved, $string)); + $parsed = Parser::valueLiteral($actual)->value; + + self::assertEquals($expected, $actual); + self::assertEquals($string, $parsed); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderToString(): array { + return [ + 'Prints an empty string' => [ + '""""""', + new DefaultSettings(), + 0, + 0, + '', + ], + 'Prints an string with only whitespace' => [ + '" "', + new DefaultSettings(), + 0, + 0, + ' ', + ], + 'One-line prints a short string' => [ + '"""Short string"""', + new DefaultSettings(), + 0, + 0, + 'Short string', + ], + 'One-line prints a long string' => [ + <<<'STRING' + """ + Long string + """ + STRING, + new class() extends DefaultSettings { + public function getLineLength(): int { + return 4; + } + }, + 0, + 0, + 'Long string', + ], + 'String is short (indent)' => [ + <<<'STRING' + """string""" + STRING, + new class() extends DefaultSettings { + public function getLineLength(): int { + return 21; + } + + public function getIndent(): string { + return ' '; + } + }, + 2, + 0, + 'string', + ], + 'String is long (indent)' => [ + <<<'STRING' + """ + string + """ + STRING, + new class() extends DefaultSettings { + public function getLineLength(): int { + return 22; + } + + public function getIndent(): string { + return ' '; + } + }, + 2, + 20, + 'string', + ], + 'Multi-line string' => [ + <<<'STRING' + """ + aaa + bbb + """ + STRING, + new DefaultSettings(), + 0, + 0, + <<<'STRING' + aaa + bbb + STRING, + ], + 'Leading space' => [ + <<<'STRING' + """ Leading space""" + STRING, + new DefaultSettings(), + 0, + 0, + ' Leading space', + ], + 'Leading tab' => [ + "\"\"\"\tLeading tab\"\"\"", + new DefaultSettings(), + 0, + 0, + "\tLeading tab", + ], + 'Trailing "' => [ + <<<'STRING' + """ + Trailing " + """ + STRING, + new DefaultSettings(), + 0, + 0, + 'Trailing "', + ], + 'Leading whitespace and trailing "' => [ + <<<'STRING' + """ + Leading whitespace and trailing " + abc + """ + STRING, + new DefaultSettings(), + 0, + 0, + <<<'STRING' + Leading whitespace and trailing " + abc + STRING, + ], + 'Trailing backslash' => [ + <<<'STRING' + """ + Trailing \\ + """ + STRING, + new DefaultSettings(), + 0, + 0, + 'Trailing \\\\', + ], + 'Escape wrapper' => [ + <<<'STRING' + """String with \""" wrapper""" + STRING, + new DefaultSettings(), + 0, + 0, + 'String with """ wrapper', + ], + 'Indent' => [ + implode("\n", [ + ' """', + ' aaa', + ' bbb ', + ' ccc ', + ' ddd ', + ' """', + ]), + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 2, + 0, + implode("\n", [ + ' aaa', + ' bbb ', + 'ccc ', + ' ddd ', + ]), + ], + ]; + } + // +} From 58c1a4ea098b961daf80aa953193f038c3097003 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 19 Dec 2021 17:17:58 +0400 Subject: [PATCH 07/90] `Block::isNormalized()` removed. --- src/Printer/Blocks/Block.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Printer/Blocks/Block.php b/src/Printer/Blocks/Block.php index 32159404..4c43f223 100644 --- a/src/Printer/Blocks/Block.php +++ b/src/Printer/Blocks/Block.php @@ -53,8 +53,6 @@ protected function reset(): void { abstract protected function serialize(): string; - abstract protected function isNormalized(): bool; - protected function eol(): string { return $this->settings->getLineEnd(); } From 7e4ecb77d70141d554cdebe90d7347f4fc0b3939 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 26 Dec 2021 15:04:35 +0400 Subject: [PATCH 08/90] Added general string block (`StringBlock`), `Description` will use it. --- src/Printer/Blocks/BlockString.php | 53 ---- src/Printer/Blocks/Description.php | 66 ++--- src/Printer/Blocks/DescriptionTest.php | 236 +++++++++++++----- src/Printer/Blocks/StringBlock.php | 73 ++++++ ...lockStringTest.php => StringBlockTest.php} | 88 +++++-- 5 files changed, 335 insertions(+), 181 deletions(-) delete mode 100644 src/Printer/Blocks/BlockString.php create mode 100644 src/Printer/Blocks/StringBlock.php rename src/Printer/Blocks/{BlockStringTest.php => StringBlockTest.php} (72%) diff --git a/src/Printer/Blocks/BlockString.php b/src/Printer/Blocks/BlockString.php deleted file mode 100644 index c08ceec3..00000000 --- a/src/Printer/Blocks/BlockString.php +++ /dev/null @@ -1,53 +0,0 @@ -eol(); - $indent = $this->indent(); - $wrapper = '"""'; - $content = $this->string; - - // Whitespace only? (seems it is not possible, so just for the case) - if (preg_match('/^\p{Zs}+$/u', $content)) { - return "\"{$content}\""; - } - - // Multiline? (markdown) - $length = $this->reserved + mb_strlen($indent) + 2 * mb_strlen($wrapper) + mb_strlen($content); - $isMultiline = $this->isLineTooLong($length) - || $this->isStringMultiline($content) - || str_ends_with($content, '"') - || str_ends_with($content, '\\\\'); - - if ($isMultiline) { - $content = $eol.$indent.preg_replace('/(\R)/u', "\$1{$indent}", $content).$eol.$indent; - } - - // Wrap && Escape - $content = str_replace($wrapper, "\\{$wrapper}", $content); - $content = "{$indent}{$wrapper}{$content}{$wrapper}"; - - // Return - return $content; - } -} diff --git a/src/Printer/Blocks/Description.php b/src/Printer/Blocks/Description.php index d4da5a03..b0d39c69 100644 --- a/src/Printer/Blocks/Description.php +++ b/src/Printer/Blocks/Description.php @@ -3,77 +3,53 @@ namespace LastDragon_ru\LaraASP\GraphQL\Printer\Blocks; use LastDragon_ru\LaraASP\GraphQL\Printer\Settings; -use function mb_strlen; -use function preg_match; +use function preg_replace; use function rtrim; -use function str_ends_with; use function str_replace; -use function str_starts_with; use function trim; /** * @internal */ -class Description extends Block { +class Description extends StringBlock { public function __construct( Settings $settings, int $level, + int $reserved, protected string $description, ) { - parent::__construct($settings, $level); - } - - protected function isMultiline(): bool { - // Needed to convert any item with description into multiline. - return true; + parent::__construct($settings, $level, $reserved); } protected function isNormalized(): bool { return $this->settings->isNormalizeDescription(); } - protected function serialize(): string { - // Begin - $eol = $this->eol(); - $indent = $this->indent(); - $wrapper = '"""'; - $description = $this->description; + protected function isBlock(): bool { + return true; + } - // Standardize line endings - $description = str_replace(["\r\n", "\n\r", "\n", "\r"], $eol, $description); + protected function getString(): string { + $string = $this->description; - // Normalize? if ($this->isNormalized()) { - $description = rtrim(trim($description, $eol)); - - if (!$description) { - return $description; - } + $eol = $this->eol(); + $string = str_replace(["\r\n", "\n\r", "\n", "\r"], $eol, $string); + $string = rtrim(trim($string, $eol)); + $string = preg_replace('/\R{2,}/u', "{$eol}{$eol}", $string); + $string = preg_replace('/^(.*?)\h+$/mu', '$1', $string); } - // Whitespace only? - if (preg_match('/^\p{Zs}+$/u', $description)) { - return "\"{$description}\""; - } + return $string; + } - // Multiline? (markdown) - $length = mb_strlen($indent) + 2 * mb_strlen($wrapper) + mb_strlen($description); - $isMultiline = $this->isLineTooLong($length) - || $this->isStringMultiline($description) - || str_starts_with($description, ' ') - || str_starts_with($description, "\t") - || str_ends_with($description, '"') - || str_ends_with($description, '\\\\'); + protected function serialize(): string { + $content = parent::serialize(); - if ($isMultiline) { - $description = $eol.$indent.str_replace($eol, "{$eol}{$indent}", $description).$eol.$indent; + if ($content === '""""""') { + $content = ''; } - // Wrap && Escape - $description = str_replace($wrapper, "\\{$wrapper}", $description); - $description = "{$indent}{$wrapper}{$description}{$wrapper}"; - - // Return - return $description; + return $content; } } diff --git a/src/Printer/Blocks/DescriptionTest.php b/src/Printer/Blocks/DescriptionTest.php index d7357f89..965addbb 100644 --- a/src/Printer/Blocks/DescriptionTest.php +++ b/src/Printer/Blocks/DescriptionTest.php @@ -2,9 +2,11 @@ namespace LastDragon_ru\LaraASP\GraphQL\Printer\Blocks; +use GraphQL\Language\Parser; use LastDragon_ru\LaraASP\GraphQL\Printer\Settings; use LastDragon_ru\LaraASP\GraphQL\Printer\Settings\DefaultSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; +use function implode; /** * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\Printer\Blocks\Description @@ -17,8 +19,20 @@ class DescriptionTest extends TestCase { * * @dataProvider dataProviderToString */ - public function testToString(string $expected, Settings $settings, int $level, string $description): void { - self::assertEquals($expected, (string) (new Description($settings, $level, $description))); + public function testToString( + string $expected, + Settings $settings, + int $level, + int $reserved, + string $description, + ): void { + $actual = (string) (new Description($settings, $level, $reserved, $description)); + + self::assertEquals($expected, $actual); + + if ($expected) { + self::assertNotNull(Parser::valueLiteral($actual)); + } } // @@ -29,17 +43,18 @@ public function testToString(string $expected, Settings $settings, int $level, s */ public function dataProviderToString(): array { return [ - 'Prints an empty description' => [ - '""""""', + 'Prints an empty string' => [ + '', new class() extends DefaultSettings { public function isNormalizeDescription(): bool { return false; } }, 0, + 0, '', ], - 'Prints an empty description (normalized)' => [ + 'Prints an empty string (normalized)' => [ '', new class() extends DefaultSettings { public function isNormalizeDescription(): bool { @@ -47,9 +62,10 @@ public function isNormalizeDescription(): bool { } }, 0, + 0, '', ], - 'Prints an description with only whitespace' => [ + 'Prints an empty string with only whitespace' => [ '" "', new class() extends DefaultSettings { public function isNormalizeDescription(): bool { @@ -57,9 +73,10 @@ public function isNormalizeDescription(): bool { } }, 0, + 0, ' ', ], - 'Prints an description with only whitespace (normalized)' => [ + 'Prints an empty string with only whitespace (normalized)' => [ '', new class() extends DefaultSettings { public function isNormalizeDescription(): bool { @@ -67,18 +84,24 @@ public function isNormalizeDescription(): bool { } }, 0, + 0, ' ', ], - 'One-line prints a short description' => [ - '"""Short description"""', + 'One-line prints a short string' => [ + <<<'STRING' + """ + Short string + """ + STRING, new DefaultSettings(), 0, - 'Short description', + 0, + 'Short string', ], - 'One-line prints a long description' => [ + 'One-line prints a long string' => [ <<<'STRING' """ - Long description + Long string """ STRING, new class() extends DefaultSettings { @@ -87,27 +110,18 @@ public function getLineLength(): int { } }, 0, - 'Long description', - ], - 'Description is short' => [ - <<<'STRING' - """description""" - STRING, - new class() extends DefaultSettings { - public function getLineLength(): int { - return 17; - } - }, 0, - 'description', + 'Long string', ], - 'Description is short (indent)' => [ + 'String is short (indent)' => [ <<<'STRING' - """description""" + """ + string + """ STRING, new class() extends DefaultSettings { public function getLineLength(): int { - return 21; + return 2; } public function getIndent(): string { @@ -115,17 +129,18 @@ public function getIndent(): string { } }, 2, - 'description', + 0, + 'string', ], - 'Description is long (indent)' => [ + 'String is long (indent)' => [ <<<'STRING' """ - description + string """ STRING, new class() extends DefaultSettings { public function getLineLength(): int { - return 21 - 1; + return 22; } public function getIndent(): string { @@ -133,23 +148,43 @@ public function getIndent(): string { } }, 2, - 'description', + 20, + 'string', ], - 'Multi-line description' => [ - "\"\"\"\nMulti-line\n description \n\n\n\"\"\"", + 'Multi-line string' => [ + <<<'STRING' + """ + aaa + bbb + + + + ccc + """ + STRING, new class() extends DefaultSettings { public function isNormalizeDescription(): bool { return false; } }, 0, - "Multi-line\n description \n\n", + 0, + <<<'STRING' + aaa + bbb + + + + ccc + STRING, ], - 'Multi-line description (normalized)' => [ + 'Multi-line string (normalized)' => [ <<<'STRING' """ - Multi-line - description + aaa + bbb + + ccc """ STRING, new class() extends DefaultSettings { @@ -158,25 +193,32 @@ public function isNormalizeDescription(): bool { } }, 0, - "Multi-line\n description \n\n", + 0, + <<<'STRING' + aaa + bbb + + + ccc + STRING, ], - 'Leading space' => [ + 'Leading space' => [ <<<'STRING' - """ - Leading space - """ + """ Leading space""" STRING, new DefaultSettings(), 0, - " Leading space", + 0, + ' Leading space', ], - 'Leading tab' => [ - "\"\"\"\n\tLeading tab\n\"\"\"", + 'Leading tab' => [ + "\"\"\"\tLeading tab\"\"\"", new DefaultSettings(), 0, + 0, "\tLeading tab", ], - 'Trailing "' => [ + 'Trailing "' => [ <<<'STRING' """ Trailing " @@ -184,9 +226,25 @@ public function isNormalizeDescription(): bool { STRING, new DefaultSettings(), 0, + 0, 'Trailing "', ], - 'Trailing backslashes' => [ + 'Leading whitespace and trailing "' => [ + <<<'STRING' + """ + Leading whitespace and trailing " + abc + """ + STRING, + new DefaultSettings(), + 0, + 0, + <<<'STRING' + Leading whitespace and trailing " + abc + STRING, + ], + 'Trailing backslash' => [ <<<'STRING' """ Trailing \\ @@ -194,33 +252,89 @@ public function isNormalizeDescription(): bool { STRING, new DefaultSettings(), 0, + 0, 'Trailing \\\\', ], - 'Escape wrapper' => [ + 'Escape wrapper' => [ <<<'STRING' - """String with \""" wrapper""" + """ + String with \""" wrapper + """ STRING, new DefaultSettings(), 0, + 0, 'String with """ wrapper', ], - 'Indent' => [ - <<<'STRING' - """ - Multi-line - description - """ - STRING, + 'Indent' => [ + implode( + "\n", + [ + ' """', + ' aaa', + '', + ' bbb ', + ' ccc ', + ' ', + ' ddd ', + ' """', + ], + ), new class() extends DefaultSettings { public function getIndent(): string { return ' '; } }, 2, - <<<'STRING' - Multi-line - description - STRING, + 0, + implode( + "\n", + [ + ' aaa', + '', + ' bbb ', + 'ccc ', + ' ', + ' ddd ', + ], + ), + ], + 'Indent (normalized)' => [ + implode( + "\n", + [ + ' """', + ' aaa', + '', + ' bbb', + ' ccc', + '', + ' ddd', + ' """', + ], + ), + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + + public function isNormalizeDescription(): bool { + return true; + } + }, + 2, + 0, + implode( + "\n", + [ + ' aaa', + '', + ' bbb ', + 'ccc ', + ' ', + ' ddd ', + ], + ), ], ]; } diff --git a/src/Printer/Blocks/StringBlock.php b/src/Printer/Blocks/StringBlock.php new file mode 100644 index 00000000..b8a676b2 --- /dev/null +++ b/src/Printer/Blocks/StringBlock.php @@ -0,0 +1,73 @@ +eol(); + $indent = $this->indent(); + $wrapper = '"""'; + $content = $this->getString(); + + // Whitespace only? (it cannot be rendered as BlockString) + if (preg_match('/^\h+$/u', $content)) { + return Printer::doPrint( + new StringValueNode([ + 'value' => $content, + 'block' => false, + ]), + ); + } + + // Multiline? + $length = $this->reserved + mb_strlen($indent) + 2 * mb_strlen($wrapper) + mb_strlen($content); + $isOneliner = !$this->isStringMultiline($content); + $isMultiline = $this->isBlock() + || !$isOneliner + || $this->isLineTooLong($length) + || str_ends_with($content, '"') + || str_ends_with($content, '\\\\'); + + if ($isOneliner && (bool) preg_match('/^\h+/u', $content)) { + $isMultiline = false; + } + + if ($isMultiline && $content !== '') { + $content = $eol.preg_replace('/(.+)/mu', "{$indent}\$1", $content).$eol.$indent; + } + + // Wrap && Escape + $content = str_replace($wrapper, "\\{$wrapper}", $content); + $content = "{$indent}{$wrapper}{$content}{$wrapper}"; + + // Return + return $content; + } +} diff --git a/src/Printer/Blocks/BlockStringTest.php b/src/Printer/Blocks/StringBlockTest.php similarity index 72% rename from src/Printer/Blocks/BlockStringTest.php rename to src/Printer/Blocks/StringBlockTest.php index 53d226ab..5e6df52c 100644 --- a/src/Printer/Blocks/BlockStringTest.php +++ b/src/Printer/Blocks/StringBlockTest.php @@ -9,9 +9,9 @@ use function implode; /** - * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\Printer\Blocks\BlockString + * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\Printer\Blocks\StringBlock */ -class BlockStringTest extends TestCase { +class StringBlockTest extends TestCase { // // ========================================================================= /** @@ -19,8 +19,27 @@ class BlockStringTest extends TestCase { * * @dataProvider dataProviderToString */ - public function testToString(string $expected, Settings $settings, int $level, int $reserved, string $string): void { - $actual = (string) (new BlockString($settings, $level, $reserved, $string)); + public function testToString( + string $expected, + Settings $settings, + int $level, + int $reserved, + string $string, + ): void { + $actual = (string) (new class($settings, $level, $reserved, $string) extends StringBlock { + public function __construct( + Settings $settings, + int $level, + int $reserved, + protected string $string, + ) { + parent::__construct($settings, $level, $reserved); + } + + protected function getString(): string { + return $this->string; + } + }); $parsed = Parser::valueLiteral($actual)->value; self::assertEquals($expected, $actual); @@ -56,7 +75,7 @@ public function dataProviderToString(): array { 0, 'Short string', ], - 'One-line prints a long string' => [ + 'One-line prints a long string' => [ <<<'STRING' """ Long string @@ -71,7 +90,7 @@ public function getLineLength(): int { 0, 'Long string', ], - 'String is short (indent)' => [ + 'String is short (indent)' => [ <<<'STRING' """string""" STRING, @@ -88,7 +107,7 @@ public function getIndent(): string { 0, 'string', ], - 'String is long (indent)' => [ + 'String is long (indent)' => [ <<<'STRING' """ string @@ -107,11 +126,13 @@ public function getIndent(): string { 20, 'string', ], - 'Multi-line string' => [ + 'Multi-line string' => [ <<<'STRING' """ aaa bbb + + ccc """ STRING, new DefaultSettings(), @@ -120,6 +141,8 @@ public function getIndent(): string { <<<'STRING' aaa bbb + + ccc STRING, ], 'Leading space' => [ @@ -138,6 +161,17 @@ public function getIndent(): string { 0, "\tLeading tab", ], + 'Leading whitespace (single line)' => [ + "\"\"\"\tLeading tab\"\"\"", + new class() extends DefaultSettings { + public function getLineLength(): int { + return 1; + } + }, + 0, + 0, + "\tLeading tab", + ], 'Trailing "' => [ <<<'STRING' """ @@ -185,14 +219,19 @@ public function getIndent(): string { 'String with """ wrapper', ], 'Indent' => [ - implode("\n", [ - ' """', - ' aaa', - ' bbb ', - ' ccc ', - ' ddd ', - ' """', - ]), + implode( + "\n", + [ + ' """', + ' aaa', + '', + ' bbb ', + ' ccc ', + ' ', + ' ddd ', + ' """', + ], + ), new class() extends DefaultSettings { public function getIndent(): string { return ' '; @@ -200,12 +239,17 @@ public function getIndent(): string { }, 2, 0, - implode("\n", [ - ' aaa', - ' bbb ', - 'ccc ', - ' ddd ', - ]), + implode( + "\n", + [ + ' aaa', + '', + ' bbb ', + 'ccc ', + ' ', + ' ddd ', + ], + ), ], ]; } From e9d048b5ef6ffe09669344f55c486d7aabb1602a Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 26 Dec 2021 15:12:47 +0400 Subject: [PATCH 09/90] Added `Block::$used` instead of `$reserved`. --- src/Printer/Blocks/Block.php | 6 ++++-- src/Printer/Blocks/BlockList.php | 6 +++--- src/Printer/Blocks/BlockListTest.php | 4 ++-- src/Printer/Blocks/BlockTest.php | 4 ---- src/Printer/Blocks/Description.php | 4 ++-- src/Printer/Blocks/DescriptionTest.php | 4 ++-- src/Printer/Blocks/StringBlock.php | 11 +---------- src/Printer/Blocks/StringBlockTest.php | 8 ++++---- 8 files changed, 18 insertions(+), 29 deletions(-) diff --git a/src/Printer/Blocks/Block.php b/src/Printer/Blocks/Block.php index 4c43f223..e54e74e9 100644 --- a/src/Printer/Blocks/Block.php +++ b/src/Printer/Blocks/Block.php @@ -5,6 +5,7 @@ use LastDragon_ru\LaraASP\GraphQL\Printer\Settings; use Stringable; use function mb_strlen; +use function mb_strpos; use function str_repeat; /** @@ -15,9 +16,10 @@ abstract class Block implements Stringable { private ?int $length = null; private ?bool $multiline = null; - protected function __construct( + public function __construct( protected Settings $settings, protected int $level = 0, + protected int $used = 0, ) { // empty } @@ -30,7 +32,7 @@ protected function getContent(): string { if ($this->content === null) { $this->content = $this->serialize(); $this->length = mb_strlen($this->content); - $this->multiline = $this->isLineTooLong($this->length) + $this->multiline = $this->isLineTooLong($this->length + $this->used) || $this->isStringMultiline($this->content); } diff --git a/src/Printer/Blocks/BlockList.php b/src/Printer/Blocks/BlockList.php index 7c846529..26bb3f7f 100644 --- a/src/Printer/Blocks/BlockList.php +++ b/src/Printer/Blocks/BlockList.php @@ -29,11 +29,11 @@ class BlockList extends Block implements ArrayAccess { public function __construct( Settings $settings, int $level, - private int $reserved, + int $used, private bool $normalized = false, private bool $wrapped = false, ) { - parent::__construct($settings, $level); + parent::__construct($settings, $level, $used); } protected function isMultiline(): bool { @@ -64,7 +64,7 @@ protected function serialize(): string { // Join $separator = ",{$this->space()}"; $isMultiline = count($this->multiline) > 0 || $this->isLineTooLong( - $this->reserved + $this->length + mb_strlen($separator) * ($count - 1), + $this->used + $this->length + mb_strlen($separator) * ($count - 1), ); $content = ''; diff --git a/src/Printer/Blocks/BlockListTest.php b/src/Printer/Blocks/BlockListTest.php index a61d2b64..f432ebbf 100644 --- a/src/Printer/Blocks/BlockListTest.php +++ b/src/Printer/Blocks/BlockListTest.php @@ -24,12 +24,12 @@ public function testToString( string $expected, Settings $settings, int $level, - int $reserved, + int $used, bool $normalized, bool $wrapped, array $blocks, ): void { - $list = new BlockList($settings, $level, $reserved, $normalized, $wrapped); + $list = new BlockList($settings, $level, $used, $normalized, $wrapped); foreach ($blocks as $name => $block) { $list[$name] = $block; diff --git a/src/Printer/Blocks/BlockTest.php b/src/Printer/Blocks/BlockTest.php index 2edffe84..f292e756 100644 --- a/src/Printer/Blocks/BlockTest.php +++ b/src/Printer/Blocks/BlockTest.php @@ -107,10 +107,6 @@ public function getLineLength(): int { * @noinspection PhpMultipleClassesDeclarationsInOneFile */ class BlockTest__Block extends Block { - public function __construct(Settings $settings, int $level = 0) { - parent::__construct($settings, $level); - } - public function getContent(): string { return parent::getContent(); } diff --git a/src/Printer/Blocks/Description.php b/src/Printer/Blocks/Description.php index b0d39c69..f166b5fe 100644 --- a/src/Printer/Blocks/Description.php +++ b/src/Printer/Blocks/Description.php @@ -15,10 +15,10 @@ class Description extends StringBlock { public function __construct( Settings $settings, int $level, - int $reserved, + int $used, protected string $description, ) { - parent::__construct($settings, $level, $reserved); + parent::__construct($settings, $level, $used); } protected function isNormalized(): bool { diff --git a/src/Printer/Blocks/DescriptionTest.php b/src/Printer/Blocks/DescriptionTest.php index 965addbb..5f9fc5e8 100644 --- a/src/Printer/Blocks/DescriptionTest.php +++ b/src/Printer/Blocks/DescriptionTest.php @@ -23,10 +23,10 @@ public function testToString( string $expected, Settings $settings, int $level, - int $reserved, + int $used, string $description, ): void { - $actual = (string) (new Description($settings, $level, $reserved, $description)); + $actual = (string) (new Description($settings, $level, $used, $description)); self::assertEquals($expected, $actual); diff --git a/src/Printer/Blocks/StringBlock.php b/src/Printer/Blocks/StringBlock.php index b8a676b2..803e085d 100644 --- a/src/Printer/Blocks/StringBlock.php +++ b/src/Printer/Blocks/StringBlock.php @@ -4,7 +4,6 @@ use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\Printer; -use LastDragon_ru\LaraASP\GraphQL\Printer\Settings; use function mb_strlen; use function preg_match; use function preg_replace; @@ -15,14 +14,6 @@ * @internal */ abstract class StringBlock extends Block { - public function __construct( - Settings $settings, - int $level, - protected int $reserved, - ) { - parent::__construct($settings, $level); - } - abstract protected function getString(): string; protected function isBlock(): bool { @@ -47,7 +38,7 @@ protected function serialize(): string { } // Multiline? - $length = $this->reserved + mb_strlen($indent) + 2 * mb_strlen($wrapper) + mb_strlen($content); + $length = $this->used + mb_strlen($indent) + 2 * mb_strlen($wrapper) + mb_strlen($content); $isOneliner = !$this->isStringMultiline($content); $isMultiline = $this->isBlock() || !$isOneliner diff --git a/src/Printer/Blocks/StringBlockTest.php b/src/Printer/Blocks/StringBlockTest.php index 5e6df52c..893d2943 100644 --- a/src/Printer/Blocks/StringBlockTest.php +++ b/src/Printer/Blocks/StringBlockTest.php @@ -23,17 +23,17 @@ public function testToString( string $expected, Settings $settings, int $level, - int $reserved, + int $used, string $string, ): void { - $actual = (string) (new class($settings, $level, $reserved, $string) extends StringBlock { + $actual = (string) (new class($settings, $level, $used, $string) extends StringBlock { public function __construct( Settings $settings, int $level, - int $reserved, + int $used, protected string $string, ) { - parent::__construct($settings, $level, $reserved); + parent::__construct($settings, $level, $used); } protected function getString(): string { From 8043cf417fe9776322cc5cd62300d73ecb25bf14 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Fri, 31 Dec 2021 10:53:06 +0400 Subject: [PATCH 10/90] Code cleanup & simplification. --- src/Printer/Blocks/BlockList.php | 6 +++--- src/Printer/Blocks/BlockListTest.php | 5 +---- src/Printer/Blocks/BlockTest.php | 5 +---- src/Printer/Blocks/Description.php | 11 ++++------- src/Printer/Blocks/StringBlock.php | 20 +++++++++++++++++--- src/Printer/Blocks/StringBlockTest.php | 15 +-------------- 6 files changed, 27 insertions(+), 35 deletions(-) diff --git a/src/Printer/Blocks/BlockList.php b/src/Printer/Blocks/BlockList.php index 26bb3f7f..1c4b20da 100644 --- a/src/Printer/Blocks/BlockList.php +++ b/src/Printer/Blocks/BlockList.php @@ -64,8 +64,8 @@ protected function serialize(): string { // Join $separator = ",{$this->space()}"; $isMultiline = count($this->multiline) > 0 || $this->isLineTooLong( - $this->used + $this->length + mb_strlen($separator) * ($count - 1), - ); + $this->used + $this->length + mb_strlen($separator) * ($count - 1), + ); $content = ''; if ($isMultiline) { @@ -122,7 +122,7 @@ public function offsetGet(mixed $offset): Block { */ public function offsetSet(mixed $offset, mixed $value): void { $this->blocks[$offset] = $value; - $this->length += $value->getLength(); + $this->length += $value->getLength(); if ($value->isMultiline()) { $this->multiline[$offset] = true; diff --git a/src/Printer/Blocks/BlockListTest.php b/src/Printer/Blocks/BlockListTest.php index f432ebbf..bdadf0ad 100644 --- a/src/Printer/Blocks/BlockListTest.php +++ b/src/Printer/Blocks/BlockListTest.php @@ -5,6 +5,7 @@ use LastDragon_ru\LaraASP\GraphQL\Printer\Settings; use LastDragon_ru\LaraASP\GraphQL\Printer\Settings\DefaultSettings; use PHPUnit\Framework\TestCase; + use function mb_strlen; /** @@ -234,8 +235,4 @@ public function isMultiline(): bool { protected function serialize(): string { return ''; } - - protected function isNormalized(): bool { - return false; - } } diff --git a/src/Printer/Blocks/BlockTest.php b/src/Printer/Blocks/BlockTest.php index f292e756..7c486b91 100644 --- a/src/Printer/Blocks/BlockTest.php +++ b/src/Printer/Blocks/BlockTest.php @@ -6,6 +6,7 @@ use LastDragon_ru\LaraASP\GraphQL\Printer\Settings\DefaultSettings; use Mockery; use PHPUnit\Framework\TestCase; + use function mb_strlen; /** @@ -122,8 +123,4 @@ public function isMultiline(): bool { protected function serialize(): string { return ''; } - - protected function isNormalized(): bool { - return false; - } } diff --git a/src/Printer/Blocks/Description.php b/src/Printer/Blocks/Description.php index f166b5fe..f1a2a80a 100644 --- a/src/Printer/Blocks/Description.php +++ b/src/Printer/Blocks/Description.php @@ -3,6 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\Printer\Blocks; use LastDragon_ru\LaraASP\GraphQL\Printer\Settings; + use function preg_replace; use function rtrim; use function str_replace; @@ -16,21 +17,17 @@ public function __construct( Settings $settings, int $level, int $used, - protected string $description, + string $string, ) { - parent::__construct($settings, $level, $used); + parent::__construct($settings, $level, $used, $string, true); } protected function isNormalized(): bool { return $this->settings->isNormalizeDescription(); } - protected function isBlock(): bool { - return true; - } - protected function getString(): string { - $string = $this->description; + $string = parent::getString(); if ($this->isNormalized()) { $eol = $this->eol(); diff --git a/src/Printer/Blocks/StringBlock.php b/src/Printer/Blocks/StringBlock.php index 803e085d..be516956 100644 --- a/src/Printer/Blocks/StringBlock.php +++ b/src/Printer/Blocks/StringBlock.php @@ -4,6 +4,8 @@ use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\Printer; +use LastDragon_ru\LaraASP\GraphQL\Printer\Settings; + use function mb_strlen; use function preg_match; use function preg_replace; @@ -13,11 +15,23 @@ /** * @internal */ -abstract class StringBlock extends Block { - abstract protected function getString(): string; +class StringBlock extends Block { + public function __construct( + Settings $settings, + int $level, + int $used, + protected string $string, + protected bool $block = false, + ) { + parent::__construct($settings, $level, $used); + } + + protected function getString(): string { + return $this->string; + } protected function isBlock(): bool { - return false; + return $this->block; } protected function serialize(): string { diff --git a/src/Printer/Blocks/StringBlockTest.php b/src/Printer/Blocks/StringBlockTest.php index 893d2943..978aa588 100644 --- a/src/Printer/Blocks/StringBlockTest.php +++ b/src/Printer/Blocks/StringBlockTest.php @@ -26,20 +26,7 @@ public function testToString( int $used, string $string, ): void { - $actual = (string) (new class($settings, $level, $used, $string) extends StringBlock { - public function __construct( - Settings $settings, - int $level, - int $used, - protected string $string, - ) { - parent::__construct($settings, $level, $used); - } - - protected function getString(): string { - return $this->string; - } - }); + $actual = (string) new StringBlock($settings, $level, $used, $string); $parsed = Parser::valueLiteral($actual)->value; self::assertEquals($expected, $actual); From 4b1b71a3dfc433bcfddf506af231ab40b80c0c13 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Fri, 31 Dec 2021 11:26:51 +0400 Subject: [PATCH 11/90] `BlockList` will support prefix and suffix. --- src/Printer/Blocks/Block.php | 5 +- src/Printer/Blocks/BlockList.php | 47 ++++++-- src/Printer/Blocks/BlockListTest.php | 164 +++++++++++++++++++++++++-- 3 files changed, 195 insertions(+), 21 deletions(-) diff --git a/src/Printer/Blocks/Block.php b/src/Printer/Blocks/Block.php index e54e74e9..4cddc372 100644 --- a/src/Printer/Blocks/Block.php +++ b/src/Printer/Blocks/Block.php @@ -4,6 +4,7 @@ use LastDragon_ru\LaraASP\GraphQL\Printer\Settings; use Stringable; + use function mb_strlen; use function mb_strpos; use function str_repeat; @@ -63,8 +64,8 @@ protected function space(): string { return $this->settings->getSpace(); } - protected function indent(): string { - return str_repeat($this->settings->getIndent(), $this->level); + protected function indent(int $level = null): string { + return str_repeat($this->settings->getIndent(), $level ?? $this->level); } protected function isLineTooLong(int $length): bool { diff --git a/src/Printer/Blocks/BlockList.php b/src/Printer/Blocks/BlockList.php index 1c4b20da..9a45139d 100644 --- a/src/Printer/Blocks/BlockList.php +++ b/src/Printer/Blocks/BlockList.php @@ -4,10 +4,12 @@ use ArrayAccess; use LastDragon_ru\LaraASP\GraphQL\Printer\Settings; + use function count; use function implode; use function ksort; use function mb_strlen; + use const SORT_NATURAL; /** @@ -32,14 +34,13 @@ public function __construct( int $used, private bool $normalized = false, private bool $wrapped = false, + private string $prefix = '', + private string $suffix = '', + private string $separator = ',', ) { parent::__construct($settings, $level, $used); } - protected function isMultiline(): bool { - return count($this->multiline) > 0 || parent::isMultiline(); - } - protected function isNormalized(): bool { return $this->normalized; } @@ -48,6 +49,22 @@ protected function isWrapped(): bool { return $this->wrapped; } + public function getPrefix(): ?string { + return $this->prefix; + } + + public function getSuffix(): ?string { + return $this->suffix; + } + + public function getSeparator(): string { + return $this->separator; + } + + protected function isMultiline(): bool { + return count($this->multiline) > 0 || parent::isMultiline(); + } + protected function serialize(): string { // Blocks? $blocks = $this->blocks; @@ -62,17 +79,23 @@ protected function serialize(): string { } // Join - $separator = ",{$this->space()}"; - $isMultiline = count($this->multiline) > 0 || $this->isLineTooLong( - $this->used + $this->length + mb_strlen($separator) * ($count - 1), - ); + $eol = ''; + $prefix = $this->getPrefix(); + $suffix = $this->getSuffix(); + $separator = "{$this->getSeparator()}{$this->space()}"; + $length = $this->used + + $this->length + + mb_strlen($suffix) + + mb_strlen($prefix) + + mb_strlen($separator) * ($count - 1); + $isMultiline = count($this->multiline) > 0 || $this->isLineTooLong($length); $content = ''; if ($isMultiline) { $eol = $this->eol(); $last = $count - 1; $index = 0; - $indent = $this->indent(); + $indent = $this->indent($this->level + (int) ($prefix || $suffix)); $wrapped = $this->isWrapped(); $previous = false; @@ -96,6 +119,12 @@ protected function serialize(): string { $content = implode($separator, $blocks); } + // Prefix & Suffix + if ($prefix || $suffix) { + $indent = $this->indent(); + $content = "{$prefix}{$eol}{$content}{$eol}{$indent}{$suffix}"; + } + // Return return $content; } diff --git a/src/Printer/Blocks/BlockListTest.php b/src/Printer/Blocks/BlockListTest.php index bdadf0ad..2ca0f3ea 100644 --- a/src/Printer/Blocks/BlockListTest.php +++ b/src/Printer/Blocks/BlockListTest.php @@ -28,9 +28,11 @@ public function testToString( int $used, bool $normalized, bool $wrapped, + string $prefix, + string $suffix, array $blocks, ): void { - $list = new BlockList($settings, $level, $used, $normalized, $wrapped); + $list = new BlockList($settings, $level, $used, $normalized, $wrapped, $prefix, $suffix); foreach ($blocks as $name => $block) { $list[$name] = $block; @@ -47,7 +49,7 @@ public function testToString( */ public function dataProviderToString(): array { return [ - 'one single-line block' => [ + 'one single-line block' => [ <<<'STRING' block a STRING, @@ -56,11 +58,13 @@ public function dataProviderToString(): array { 0, false, true, + '', + '', [ 'a' => new BlockListTest__Block(false, 'block a'), ], ], - 'one multi-line block' => [ + 'one multi-line block' => [ <<<'STRING' block a STRING, @@ -69,11 +73,13 @@ public function dataProviderToString(): array { 0, false, true, + '', + '', [ 'a' => new BlockListTest__Block(true, 'block a'), ], ], - 'short block list' => [ + 'short block list' => [ <<<'STRING' block a, block b STRING, @@ -82,12 +88,14 @@ public function dataProviderToString(): array { 0, false, true, + '', + '', [ 'a' => new BlockListTest__Block(false, 'block a'), 'b' => new BlockListTest__Block(false, 'block b'), ], ], - 'long block list' => [ + 'long block list' => [ <<<'STRING' block b block a @@ -101,12 +109,14 @@ public function getLineLength(): int { 5, false, true, + '', + '', [ 'b' => new BlockListTest__Block(false, 'block b'), 'a' => new BlockListTest__Block(false, 'block a'), ], ], - 'short block list with multiline block' => [ + 'short block list with multiline block' => [ <<<'STRING' block a @@ -117,12 +127,14 @@ public function getLineLength(): int { 0, false, true, + '', + '', [ 'a' => new BlockListTest__Block(false, 'block a'), 'b' => new BlockListTest__Block(true, 'block b'), ], ], - 'block list with multiline blocks' => [ + 'block list with multiline blocks' => [ <<<'STRING' block a @@ -141,6 +153,8 @@ public function getLineLength(): int { 0, false, true, + '', + '', [ 'a' => new BlockListTest__Block(true, 'block a'), 'b' => new BlockListTest__Block(false, 'block b'), @@ -151,7 +165,7 @@ public function getLineLength(): int { 'g' => new BlockListTest__Block(true, 'block g'), ], ], - 'block list with multiline blocks without wrap' => [ + 'block list with multiline blocks without wrap' => [ <<<'STRING' block c block b @@ -162,13 +176,15 @@ public function getLineLength(): int { 0, false, false, + '', + '', [ 'c' => new BlockListTest__Block(true, 'block c'), 'b' => new BlockListTest__Block(false, 'block b'), 'a' => new BlockListTest__Block(true, 'block a'), ], ], - 'normalized block list' => [ + 'normalized block list' => [ <<<'STRING' block a, block b STRING, @@ -177,12 +193,14 @@ public function getLineLength(): int { 0, true, true, + '', + '', [ 'b' => new BlockListTest__Block(false, 'block b'), 'a' => new BlockListTest__Block(false, 'block a'), ], ], - 'multi-line with level' => [ + 'multi-line with level' => [ <<<'STRING' block a STRING, @@ -195,6 +213,132 @@ public function getIndent(): string { 0, false, false, + '', + '', + [ + 'a' => new BlockListTest__Block(true, 'block a'), + ], + ], + '[prefix & suffix] one single-line block' => [ + <<<'STRING' + [block a] + STRING, + new DefaultSettings(), + 0, + 0, + false, + true, + '[', + ']', + [ + 'a' => new BlockListTest__Block(false, 'block a'), + ], + ], + '[prefix & suffix] one multi-line block' => [ + <<<'STRING' + [ + block a + ] + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + false, + true, + '[', + ']', + [ + 'a' => new BlockListTest__Block(true, 'block a'), + ], + ], + '[prefix & suffix] short block list' => [ + <<<'STRING' + [block a, block b] + STRING, + new DefaultSettings(), + 0, + 0, + false, + true, + '[', + ']', + [ + 'a' => new BlockListTest__Block(false, 'block a'), + 'b' => new BlockListTest__Block(false, 'block b'), + ], + ], + '[prefix & suffix] long block list' => [ + <<<'STRING' + [ + block b + block a + ] + STRING, + new class() extends DefaultSettings { + public function getLineLength(): int { + return 20; + } + + public function getIndent(): string { + return ' '; + } + }, + 0, + 5, + false, + true, + '[', + ']', + [ + 'b' => new BlockListTest__Block(false, 'block b'), + 'a' => new BlockListTest__Block(false, 'block a'), + ], + ], + '[prefix & suffix] short block list with multiline block' => [ + <<<'STRING' + [ + block a + + block b + ] + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + false, + true, + '[', + ']', + [ + 'a' => new BlockListTest__Block(false, 'block a'), + 'b' => new BlockListTest__Block(true, 'block b'), + ], + ], + '[prefix & suffix] multi-line with level' => [ + <<<'STRING' + [ + block a + ] + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 2, + 0, + false, + false, + '[', + ']', [ 'a' => new BlockListTest__Block(true, 'block a'), ], From 68f1079f3472ea5cfd3617ab8727333fefb80aef Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Fri, 31 Dec 2021 14:59:05 +0400 Subject: [PATCH 12/90] `StringBlock` will not indent the first line. --- src/Printer/Blocks/StringBlock.php | 2 +- src/Printer/Blocks/StringBlockTest.php | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Printer/Blocks/StringBlock.php b/src/Printer/Blocks/StringBlock.php index be516956..6446d5b4 100644 --- a/src/Printer/Blocks/StringBlock.php +++ b/src/Printer/Blocks/StringBlock.php @@ -70,7 +70,7 @@ protected function serialize(): string { // Wrap && Escape $content = str_replace($wrapper, "\\{$wrapper}", $content); - $content = "{$indent}{$wrapper}{$content}{$wrapper}"; + $content = "{$wrapper}{$content}{$wrapper}"; // Return return $content; diff --git a/src/Printer/Blocks/StringBlockTest.php b/src/Printer/Blocks/StringBlockTest.php index 978aa588..93d900b9 100644 --- a/src/Printer/Blocks/StringBlockTest.php +++ b/src/Printer/Blocks/StringBlockTest.php @@ -6,6 +6,7 @@ use LastDragon_ru\LaraASP\GraphQL\Printer\Settings; use LastDragon_ru\LaraASP\GraphQL\Printer\Settings\DefaultSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; + use function implode; /** @@ -79,7 +80,7 @@ public function getLineLength(): int { ], 'String is short (indent)' => [ <<<'STRING' - """string""" + """string""" STRING, new class() extends DefaultSettings { public function getLineLength(): int { @@ -87,7 +88,7 @@ public function getLineLength(): int { } public function getIndent(): string { - return ' '; + return ' '; } }, 2, @@ -96,7 +97,7 @@ public function getIndent(): string { ], 'String is long (indent)' => [ <<<'STRING' - """ + """ string """ STRING, @@ -209,7 +210,7 @@ public function getLineLength(): int { implode( "\n", [ - ' """', + '"""', ' aaa', '', ' bbb ', From 5522bb35c837f453cb6fc3c7c9b6e00aa88bb2a6 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 2 Jan 2022 17:44:02 +0400 Subject: [PATCH 13/90] Added `NamedBlock`. --- src/Printer/Blocks/Block.php | 60 +- src/Printer/Blocks/BlockList.php | 106 ++-- src/Printer/Blocks/BlockListTest.php | 733 +++++++++++++++---------- src/Printer/Blocks/BlockTest.php | 8 +- src/Printer/Blocks/Description.php | 6 +- src/Printer/Blocks/DescriptionTest.php | 9 +- src/Printer/Blocks/NamedBlock.php | 39 ++ src/Printer/Blocks/NamedBlockTest.php | 70 +++ src/Printer/Blocks/StringBlock.php | 4 +- 9 files changed, 695 insertions(+), 340 deletions(-) create mode 100644 src/Printer/Blocks/NamedBlock.php create mode 100644 src/Printer/Blocks/NamedBlockTest.php diff --git a/src/Printer/Blocks/Block.php b/src/Printer/Blocks/Block.php index 4cddc372..b281d1c6 100644 --- a/src/Printer/Blocks/Block.php +++ b/src/Printer/Blocks/Block.php @@ -18,62 +18,86 @@ abstract class Block implements Stringable { private ?bool $multiline = null; public function __construct( - protected Settings $settings, - protected int $level = 0, - protected int $used = 0, + private Settings $settings, + private int $level = 0, + private int $used = 0, ) { // empty } + // + // ========================================================================= + protected function getSettings(): Settings { + return $this->settings; + } + + protected function getLevel(): int { + return $this->level; + } + + protected function getUsed(): int { + return $this->used; + } + // + + // + // ========================================================================= + public function getLength(): int { + return $this->length ?? mb_strlen($this->getContent()); + } + + public function isMultiline(): bool { + return $this->getContent() && $this->multiline; + } + public function __toString(): string { return $this->getContent(); } + // + // + // ========================================================================= protected function getContent(): string { if ($this->content === null) { - $this->content = $this->serialize(); + $this->content = $this->content(); $this->length = mb_strlen($this->content); - $this->multiline = $this->isLineTooLong($this->length + $this->used) + $this->multiline = $this->isLineTooLong($this->length + $this->getUsed()) || $this->isStringMultiline($this->content); } return $this->content; } - protected function getLength(): int { - return $this->length ?? mb_strlen($this->getContent()); - } - - protected function isMultiline(): bool { - return $this->getContent() && $this->multiline; - } - protected function reset(): void { $this->multiline = null; $this->content = null; $this->length = null; } - abstract protected function serialize(): string; + abstract protected function content(): string; + // + // + // ========================================================================= protected function eol(): string { - return $this->settings->getLineEnd(); + return $this->getSettings()->getLineEnd(); } protected function space(): string { - return $this->settings->getSpace(); + return $this->getSettings()->getSpace(); } protected function indent(int $level = null): string { - return str_repeat($this->settings->getIndent(), $level ?? $this->level); + return str_repeat($this->getSettings()->getIndent(), $level ?? $this->getLevel()); } protected function isLineTooLong(int $length): bool { - return $length > $this->settings->getLineLength(); + return $length > $this->getSettings()->getLineLength(); } protected function isStringMultiline(string $string): bool { return mb_strpos($string, "\n") !== false || mb_strpos($string, "\r") !== false; } + // } diff --git a/src/Printer/Blocks/BlockList.php b/src/Printer/Blocks/BlockList.php index 9a45139d..983a3670 100644 --- a/src/Printer/Blocks/BlockList.php +++ b/src/Printer/Blocks/BlockList.php @@ -7,6 +7,7 @@ use function count; use function implode; +use function is_numeric; use function ksort; use function mb_strlen; @@ -18,7 +19,7 @@ */ class BlockList extends Block implements ArrayAccess { /** - * @var array + * @var array */ private array $blocks = []; @@ -49,11 +50,11 @@ protected function isWrapped(): bool { return $this->wrapped; } - public function getPrefix(): ?string { + public function getPrefix(): string { return $this->prefix; } - public function getSuffix(): ?string { + public function getSuffix(): string { return $this->suffix; } @@ -61,41 +62,50 @@ public function getSeparator(): string { return $this->separator; } - protected function isMultiline(): bool { + public function isMultiline(): bool { return count($this->multiline) > 0 || parent::isMultiline(); } - protected function serialize(): string { - // Blocks? + /** + * @return array + */ + protected function getBlocks(): array { $blocks = $this->blocks; - $count = count($blocks); if ($this->isNormalized()) { ksort($blocks, SORT_NATURAL); } + return $blocks; + } + + protected function content(): string { + // Blocks? + $content = ''; + $blocks = $this->getBlocks(); + $count = count($blocks); + if (!$count) { - return ''; + return $content; } // Join - $eol = ''; - $prefix = $this->getPrefix(); - $suffix = $this->getSuffix(); - $separator = "{$this->getSeparator()}{$this->space()}"; - $length = $this->used - + $this->length - + mb_strlen($suffix) - + mb_strlen($prefix) - + mb_strlen($separator) * ($count - 1); - $isMultiline = count($this->multiline) > 0 || $this->isLineTooLong($length); - $content = ''; + $eol = ''; + $listPrefix = $this->getPrefix(); + $listSuffix = $this->getSuffix(); + $itemSeparator = "{$this->getSeparator()}{$this->space()}"; + $isMultiline = $this->isMultilineContent( + $blocks, + $listSuffix, + $listPrefix, + $itemSeparator, + ); if ($isMultiline) { $eol = $this->eol(); $last = $count - 1; $index = 0; - $indent = $this->indent($this->level + (int) ($prefix || $suffix)); + $indent = $this->indent($this->getLevel() + (int) ($listPrefix || $listSuffix)); $wrapped = $this->isWrapped(); $previous = false; @@ -116,42 +126,76 @@ protected function serialize(): string { $index = $index + 1; } } else { - $content = implode($separator, $blocks); + $content = implode($itemSeparator, $blocks); } // Prefix & Suffix - if ($prefix || $suffix) { - $indent = $this->indent(); - $content = "{$prefix}{$eol}{$content}{$eol}{$indent}{$suffix}"; + if ($listPrefix || $listSuffix) { + $indent = $isMultiline ? $this->indent() : ''; + $content = "{$listPrefix}{$eol}{$content}{$eol}{$indent}{$listSuffix}"; } // Return return $content; } + /** + * @param array $blocks + */ + protected function isMultilineContent( + array $blocks, + string $suffix, + string $prefix, + string $itemSeparator, + ): bool { + // Any multiline block? + if ($this->multiline) { + return true; + } + + // Length? + $count = count($blocks); + $length = $this->getUsed() + + $this->length + + mb_strlen($suffix) + + mb_strlen($prefix) + + mb_strlen($itemSeparator) * ($count - 1); + + return $this->isLineTooLong($length); + } + // // ========================================================================= /** - * @param string $offset + * @param int|string $offset */ public function offsetExists(mixed $offset): bool { return isset($this->blocks[$offset]); } /** - * @param string $offset + * @param int|string $offset */ public function offsetGet(mixed $offset): Block { return $this->blocks[$offset]; } /** - * @param string $offset - * @param Block $value + * @param int|string|null $offset + * @param Block $value */ public function offsetSet(mixed $offset, mixed $value): void { - $this->blocks[$offset] = $value; - $this->length += $value->getLength(); + if ($offset !== null) { + if (!is_numeric($offset)) { + $value = new NamedBlock($this->getSettings(), $offset, $value); + } + + $this->blocks[$offset] = $value; + } else { + $this->blocks[] = $value; + } + + $this->length += $value->getLength(); if ($value->isMultiline()) { $this->multiline[$offset] = true; @@ -161,7 +205,7 @@ public function offsetSet(mixed $offset, mixed $value): void { } /** - * @param string $offset + * @param int|string $offset */ public function offsetUnset(mixed $offset): void { if (isset($this->blocks[$offset])) { diff --git a/src/Printer/Blocks/BlockListTest.php b/src/Printer/Blocks/BlockListTest.php index 2ca0f3ea..93887619 100644 --- a/src/Printer/Blocks/BlockListTest.php +++ b/src/Printer/Blocks/BlockListTest.php @@ -4,6 +4,8 @@ use LastDragon_ru\LaraASP\GraphQL\Printer\Settings; use LastDragon_ru\LaraASP\GraphQL\Printer\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; +use LastDragon_ru\LaraASP\Testing\Providers\MergeDataProvider; use PHPUnit\Framework\TestCase; use function mb_strlen; @@ -48,302 +50,478 @@ public function testToString( * @return array */ public function dataProviderToString(): array { - return [ - 'one single-line block' => [ - <<<'STRING' - block a - STRING, - new DefaultSettings(), - 0, - 0, - false, - true, - '', - '', - [ - 'a' => new BlockListTest__Block(false, 'block a'), + return (new MergeDataProvider([ + 'index' => new ArrayDataProvider([ + 'one single-line block' => [ + <<<'STRING' + block a + STRING, + new DefaultSettings(), + 0, + 0, + false, + true, + '', + '', + [ + new BlockListTest__Block(false, 'block a'), + ], ], - ], - 'one multi-line block' => [ - <<<'STRING' - block a - STRING, - new DefaultSettings(), - 0, - 0, - false, - true, - '', - '', - [ - 'a' => new BlockListTest__Block(true, 'block a'), + 'one multi-line block' => [ + <<<'STRING' + block a + STRING, + new DefaultSettings(), + 0, + 0, + false, + true, + '', + '', + [ + new BlockListTest__Block(true, 'block a'), + ], ], - ], - 'short block list' => [ - <<<'STRING' - block a, block b - STRING, - new DefaultSettings(), - 0, - 0, - false, - true, - '', - '', - [ - 'a' => new BlockListTest__Block(false, 'block a'), - 'b' => new BlockListTest__Block(false, 'block b'), + 'short block list' => [ + <<<'STRING' + block a, block b + STRING, + new DefaultSettings(), + 0, + 0, + false, + true, + '', + '', + [ + new BlockListTest__Block(false, 'block a'), + new BlockListTest__Block(false, 'block b'), + ], ], - ], - 'long block list' => [ - <<<'STRING' - block b - block a - STRING, - new class() extends DefaultSettings { - public function getLineLength(): int { - return 20; - } - }, - 0, - 5, - false, - true, - '', - '', - [ - 'b' => new BlockListTest__Block(false, 'block b'), - 'a' => new BlockListTest__Block(false, 'block a'), + 'long block list' => [ + <<<'STRING' + block b + block a + STRING, + new class() extends DefaultSettings { + public function getLineLength(): int { + return 20; + } + }, + 0, + 5, + false, + true, + '', + '', + [ + new BlockListTest__Block(false, 'block b'), + new BlockListTest__Block(false, 'block a'), + ], ], - ], - 'short block list with multiline block' => [ - <<<'STRING' - block a + 'short block list with multiline block' => [ + <<<'STRING' + block a - block b - STRING, - new DefaultSettings(), - 0, - 0, - false, - true, - '', - '', - [ - 'a' => new BlockListTest__Block(false, 'block a'), - 'b' => new BlockListTest__Block(true, 'block b'), + block b + STRING, + new DefaultSettings(), + 0, + 0, + false, + true, + '', + '', + [ + new BlockListTest__Block(false, 'block a'), + new BlockListTest__Block(true, 'block b'), + ], ], - ], - 'block list with multiline blocks' => [ - <<<'STRING' - block a + 'block list with multiline blocks' => [ + <<<'STRING' + block a - block b - block c + block b + block c - block d + block d - block e - block f + block e + block f - block g - STRING, - new DefaultSettings(), - 0, - 0, - false, - true, - '', - '', - [ - 'a' => new BlockListTest__Block(true, 'block a'), - 'b' => new BlockListTest__Block(false, 'block b'), - 'c' => new BlockListTest__Block(false, 'block c'), - 'd' => new BlockListTest__Block(true, 'block d'), - 'e' => new BlockListTest__Block(false, 'block e'), - 'f' => new BlockListTest__Block(false, 'block f'), - 'g' => new BlockListTest__Block(true, 'block g'), + block g + STRING, + new DefaultSettings(), + 0, + 0, + false, + true, + '', + '', + [ + new BlockListTest__Block(true, 'block a'), + new BlockListTest__Block(false, 'block b'), + new BlockListTest__Block(false, 'block c'), + new BlockListTest__Block(true, 'block d'), + new BlockListTest__Block(false, 'block e'), + new BlockListTest__Block(false, 'block f'), + new BlockListTest__Block(true, 'block g'), + ], ], - ], - 'block list with multiline blocks without wrap' => [ - <<<'STRING' - block c - block b - block a - STRING, - new DefaultSettings(), - 0, - 0, - false, - false, - '', - '', - [ - 'c' => new BlockListTest__Block(true, 'block c'), - 'b' => new BlockListTest__Block(false, 'block b'), - 'a' => new BlockListTest__Block(true, 'block a'), + 'block list with multiline blocks without wrap' => [ + <<<'STRING' + block c + block b + block a + STRING, + new DefaultSettings(), + 0, + 0, + false, + false, + '', + '', + [ + new BlockListTest__Block(true, 'block c'), + new BlockListTest__Block(false, 'block b'), + new BlockListTest__Block(true, 'block a'), + ], ], - ], - 'normalized block list' => [ - <<<'STRING' - block a, block b - STRING, - new DefaultSettings(), - 0, - 0, - true, - true, - '', - '', - [ - 'b' => new BlockListTest__Block(false, 'block b'), - 'a' => new BlockListTest__Block(false, 'block a'), + 'normalized block list' => [ + <<<'STRING' + block b, block a + STRING, + new DefaultSettings(), + 0, + 0, + true, + true, + '', + '', + [ + new BlockListTest__Block(false, 'block b'), + new BlockListTest__Block(false, 'block a'), + ], ], - ], - 'multi-line with level' => [ - <<<'STRING' - block a - STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, - 2, - 0, - false, - false, - '', - '', - [ - 'a' => new BlockListTest__Block(true, 'block a'), + 'multi-line with level' => [ + <<<'STRING' + block a + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 2, + 0, + false, + false, + '', + '', + [ + new BlockListTest__Block(true, 'block a'), + ], ], - ], - '[prefix & suffix] one single-line block' => [ - <<<'STRING' - [block a] - STRING, - new DefaultSettings(), - 0, - 0, - false, - true, - '[', - ']', - [ - 'a' => new BlockListTest__Block(false, 'block a'), + ]), + 'named' => new ArrayDataProvider([ + 'one single-line block' => [ + <<<'STRING' + a: block a + STRING, + new DefaultSettings(), + 0, + 0, + false, + true, + '', + '', + [ + 'a' => new BlockListTest__Block(false, 'block a'), + ], ], - ], - '[prefix & suffix] one multi-line block' => [ - <<<'STRING' - [ - block a - ] - STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, - 0, - 0, - false, - true, - '[', - ']', - [ - 'a' => new BlockListTest__Block(true, 'block a'), + 'one multi-line block' => [ + <<<'STRING' + a: block a + STRING, + new DefaultSettings(), + 0, + 0, + false, + true, + '', + '', + [ + 'a' => new BlockListTest__Block(true, 'block a'), + ], ], - ], - '[prefix & suffix] short block list' => [ - <<<'STRING' - [block a, block b] - STRING, - new DefaultSettings(), - 0, - 0, - false, - true, - '[', - ']', - [ - 'a' => new BlockListTest__Block(false, 'block a'), - 'b' => new BlockListTest__Block(false, 'block b'), + 'short block list' => [ + <<<'STRING' + a: block a, b: block b + STRING, + new DefaultSettings(), + 0, + 0, + false, + true, + '', + '', + [ + 'a' => new BlockListTest__Block(false, 'block a'), + 'b' => new BlockListTest__Block(false, 'block b'), + ], ], - ], - '[prefix & suffix] long block list' => [ - <<<'STRING' - [ - block b - block a - ] - STRING, - new class() extends DefaultSettings { - public function getLineLength(): int { - return 20; - } + 'long block list' => [ + <<<'STRING' + b: block b + a: block a + STRING, + new class() extends DefaultSettings { + public function getLineLength(): int { + return 20; + } + }, + 0, + 5, + false, + true, + '', + '', + [ + 'b' => new BlockListTest__Block(false, 'block b'), + 'a' => new BlockListTest__Block(false, 'block a'), + ], + ], + 'short block list with multiline block' => [ + <<<'STRING' + a: block a - public function getIndent(): string { - return ' '; - } - }, - 0, - 5, - false, - true, - '[', - ']', - [ - 'b' => new BlockListTest__Block(false, 'block b'), - 'a' => new BlockListTest__Block(false, 'block a'), + b: block b + STRING, + new DefaultSettings(), + 0, + 0, + false, + true, + '', + '', + [ + 'a' => new BlockListTest__Block(false, 'block a'), + 'b' => new BlockListTest__Block(true, 'block b'), + ], ], - ], - '[prefix & suffix] short block list with multiline block' => [ - <<<'STRING' - [ - block a + 'block list with multiline blocks' => [ + <<<'STRING' + a: block a - block b - ] - STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, - 0, - 0, - false, - true, - '[', - ']', - [ - 'a' => new BlockListTest__Block(false, 'block a'), - 'b' => new BlockListTest__Block(true, 'block b'), + b: block b + c: block c + + d: block d + + e: block e + f: block f + + g: block g + STRING, + new DefaultSettings(), + 0, + 0, + false, + true, + '', + '', + [ + 'a' => new BlockListTest__Block(true, 'block a'), + 'b' => new BlockListTest__Block(false, 'block b'), + 'c' => new BlockListTest__Block(false, 'block c'), + 'd' => new BlockListTest__Block(true, 'block d'), + 'e' => new BlockListTest__Block(false, 'block e'), + 'f' => new BlockListTest__Block(false, 'block f'), + 'g' => new BlockListTest__Block(true, 'block g'), + ], + ], + 'block list with multiline blocks without wrap' => [ + <<<'STRING' + c: block c + b: block b + a: block a + STRING, + new DefaultSettings(), + 0, + 0, + false, + false, + '', + '', + [ + 'c' => new BlockListTest__Block(true, 'block c'), + 'b' => new BlockListTest__Block(false, 'block b'), + 'a' => new BlockListTest__Block(true, 'block a'), + ], + ], + 'normalized block list' => [ + <<<'STRING' + a: block a, b: block b + STRING, + new DefaultSettings(), + 0, + 0, + true, + true, + '', + '', + [ + 'b' => new BlockListTest__Block(false, 'block b'), + 'a' => new BlockListTest__Block(false, 'block a'), + ], + ], + 'multi-line with level' => [ + <<<'STRING' + a: block a + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 2, + 0, + false, + false, + '', + '', + [ + 'a' => new BlockListTest__Block(true, 'block a'), + ], + ], + ]), + 'prefix & suffix' => new ArrayDataProvider([ + 'one single-line block' => [ + <<<'STRING' + [a: block a] + STRING, + new DefaultSettings(), + 0, + 0, + false, + true, + '[', + ']', + [ + 'a' => new BlockListTest__Block(false, 'block a'), + ], + ], + 'one multi-line block' => [ + <<<'STRING' + [ + a: block a + ] + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + false, + true, + '[', + ']', + [ + 'a' => new BlockListTest__Block(true, 'block a'), + ], + ], + 'short block list' => [ + <<<'STRING' + [block a, b: block b] + STRING, + new DefaultSettings(), + 0, + 0, + false, + true, + '[', + ']', + [ + 0 => new BlockListTest__Block(false, 'block a'), + 'b' => new BlockListTest__Block(false, 'block b'), + ], + ], + 'long block list' => [ + <<<'STRING' + [ + b: block b + a: block a + ] + STRING, + new class() extends DefaultSettings { + public function getLineLength(): int { + return 20; + } + + public function getIndent(): string { + return ' '; + } + }, + 0, + 5, + false, + true, + '[', + ']', + [ + 'b' => new BlockListTest__Block(false, 'block b'), + 'a' => new BlockListTest__Block(false, 'block a'), + ], + ], + 'short block list with multiline block' => [ + <<<'STRING' + [ + block a + + block b + ] + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + false, + true, + '[', + ']', + [ + new BlockListTest__Block(false, 'block a'), + new BlockListTest__Block(true, 'block b'), + ], ], - ], - '[prefix & suffix] multi-line with level' => [ - <<<'STRING' - [ - block a - ] - STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, - 2, - 0, - false, - false, - '[', - ']', - [ - 'a' => new BlockListTest__Block(true, 'block a'), + 'multi-line with level' => [ + <<<'STRING' + [ + block a + ] + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 2, + 0, + false, + false, + '[', + ']', + [ + new BlockListTest__Block(true, 'block a'), + ], ], - ], - ]; + ]), + ]))->getData(); } // } @@ -356,19 +534,18 @@ public function getIndent(): string { * @noinspection PhpMultipleClassesDeclarationsInOneFile */ class BlockListTest__Block extends Block { - /** @noinspection PhpMissingParentConstructorInspection */ public function __construct( protected bool $multiline, protected string $content, ) { - // empty + parent::__construct(new DefaultSettings()); } protected function getContent(): string { return $this->content; } - protected function getLength(): int { + public function getLength(): int { return mb_strlen($this->getContent()); } @@ -376,7 +553,7 @@ public function isMultiline(): bool { return $this->multiline; } - protected function serialize(): string { + protected function content(): string { return ''; } } diff --git a/src/Printer/Blocks/BlockTest.php b/src/Printer/Blocks/BlockTest.php index 7c486b91..0b116f59 100644 --- a/src/Printer/Blocks/BlockTest.php +++ b/src/Printer/Blocks/BlockTest.php @@ -24,7 +24,7 @@ public function testGetContent(): void { $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); $block - ->shouldReceive('serialize') + ->shouldReceive('content') ->once() ->andReturn($content); @@ -42,7 +42,7 @@ public function testGetLength(): void { $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); $block - ->shouldReceive('serialize') + ->shouldReceive('content') ->once() ->andReturn($content); @@ -60,7 +60,7 @@ public function testIsMultiline(bool $expected, Settings $settings, string $cont $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); $block - ->shouldReceive('serialize') + ->shouldReceive('content') ->once() ->andReturn($content); @@ -120,7 +120,7 @@ public function isMultiline(): bool { return parent::isMultiline(); } - protected function serialize(): string { + protected function content(): string { return ''; } } diff --git a/src/Printer/Blocks/Description.php b/src/Printer/Blocks/Description.php index f1a2a80a..576cda42 100644 --- a/src/Printer/Blocks/Description.php +++ b/src/Printer/Blocks/Description.php @@ -23,7 +23,7 @@ public function __construct( } protected function isNormalized(): bool { - return $this->settings->isNormalizeDescription(); + return $this->getSettings()->isNormalizeDescription(); } protected function getString(): string { @@ -40,8 +40,8 @@ protected function getString(): string { return $string; } - protected function serialize(): string { - $content = parent::serialize(); + protected function content(): string { + $content = parent::content(); if ($content === '""""""') { $content = ''; diff --git a/src/Printer/Blocks/DescriptionTest.php b/src/Printer/Blocks/DescriptionTest.php index 5f9fc5e8..e7921699 100644 --- a/src/Printer/Blocks/DescriptionTest.php +++ b/src/Printer/Blocks/DescriptionTest.php @@ -6,6 +6,7 @@ use LastDragon_ru\LaraASP\GraphQL\Printer\Settings; use LastDragon_ru\LaraASP\GraphQL\Printer\Settings\DefaultSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; + use function implode; /** @@ -115,7 +116,7 @@ public function getLineLength(): int { ], 'String is short (indent)' => [ <<<'STRING' - """ + """ string """ STRING, @@ -134,7 +135,7 @@ public function getIndent(): string { ], 'String is long (indent)' => [ <<<'STRING' - """ + """ string """ STRING, @@ -270,7 +271,7 @@ public function isNormalizeDescription(): bool { implode( "\n", [ - ' """', + '"""', ' aaa', '', ' bbb ', @@ -303,7 +304,7 @@ public function getIndent(): string { implode( "\n", [ - ' """', + '"""', ' aaa', '', ' bbb', diff --git a/src/Printer/Blocks/NamedBlock.php b/src/Printer/Blocks/NamedBlock.php new file mode 100644 index 00000000..d173d02c --- /dev/null +++ b/src/Printer/Blocks/NamedBlock.php @@ -0,0 +1,39 @@ +getLevel(), $block->getUsed()); + } + + public function getName(): string { + return $this->name; + } + + public function isMultiline(): bool { + return $this->getBlock()->isMultiline() || parent::isMultiline(); + } + + protected function getBlock(): Block { + return $this->block; + } + + protected function getSeparator(): string { + return $this->separator; + } + + protected function content(): string { + return "{$this->getName()}{$this->getSeparator()}{$this->space()}{$this->getBlock()}"; + } +} diff --git a/src/Printer/Blocks/NamedBlockTest.php b/src/Printer/Blocks/NamedBlockTest.php new file mode 100644 index 00000000..95bbae3c --- /dev/null +++ b/src/Printer/Blocks/NamedBlockTest.php @@ -0,0 +1,70 @@ +space; + } + }; + $block = new class($settings, $level, $used, $content) extends Block { + public function __construct( + Settings $settings, + int $level, + int $used, + protected string $content, + ) { + parent::__construct($settings, $level, $used); + } + + protected function content(): string { + return $this->content; + } + }; + $named = new class($settings, $name, $block, $separator) extends NamedBlock { + public function getUsed(): int { + return parent::getUsed(); + } + + public function getLevel(): int { + return parent::getLevel(); + } + }; + $expected = "{$name}{$separator}{$space}{$content}"; + + self::assertEquals($used, $named->getUsed()); + self::assertEquals($level, $named->getLevel()); + self::assertEquals($expected, (string) $named); + self::assertEquals(mb_strlen($expected), mb_strlen((string) $named)); + self::assertEquals(mb_strlen($expected), $named->getLength()); + } +} diff --git a/src/Printer/Blocks/StringBlock.php b/src/Printer/Blocks/StringBlock.php index 6446d5b4..a5f6c1bb 100644 --- a/src/Printer/Blocks/StringBlock.php +++ b/src/Printer/Blocks/StringBlock.php @@ -34,7 +34,7 @@ protected function isBlock(): bool { return $this->block; } - protected function serialize(): string { + protected function content(): string { // Begin $eol = $this->eol(); $indent = $this->indent(); @@ -52,7 +52,7 @@ protected function serialize(): string { } // Multiline? - $length = $this->used + mb_strlen($indent) + 2 * mb_strlen($wrapper) + mb_strlen($content); + $length = $this->getUsed() + mb_strlen($indent) + 2 * mb_strlen($wrapper) + mb_strlen($content); $isOneliner = !$this->isStringMultiline($content); $isMultiline = $this->isBlock() || !$isOneliner From 906a640141c0a507c12c9ebb7fd1db049940d6d8 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Wed, 5 Jan 2022 09:34:39 +0400 Subject: [PATCH 14/90] Added `Value` block. --- src/Printer/Blocks/BlockList.php | 44 ++--- src/Printer/Blocks/BlockListTest.php | 41 +++- src/Printer/Blocks/ListBlockList.php | 13 ++ src/Printer/Blocks/ObjectBlockList.php | 17 ++ src/Printer/Blocks/Value.php | 56 ++++++ src/Printer/Blocks/ValueTest.php | 261 +++++++++++++++++++++++++ 6 files changed, 404 insertions(+), 28 deletions(-) create mode 100644 src/Printer/Blocks/ListBlockList.php create mode 100644 src/Printer/Blocks/ObjectBlockList.php create mode 100644 src/Printer/Blocks/Value.php create mode 100644 src/Printer/Blocks/ValueTest.php diff --git a/src/Printer/Blocks/BlockList.php b/src/Printer/Blocks/BlockList.php index 983a3670..6e768bd1 100644 --- a/src/Printer/Blocks/BlockList.php +++ b/src/Printer/Blocks/BlockList.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\Printer\Blocks; use ArrayAccess; -use LastDragon_ru\LaraASP\GraphQL\Printer\Settings; use function count; use function implode; @@ -17,7 +16,7 @@ * @internal * @implements ArrayAccess */ -class BlockList extends Block implements ArrayAccess { +abstract class BlockList extends Block implements ArrayAccess { /** * @var array */ @@ -29,37 +28,28 @@ class BlockList extends Block implements ArrayAccess { private array $multiline = []; private int $length = 0; - public function __construct( - Settings $settings, - int $level, - int $used, - private bool $normalized = false, - private bool $wrapped = false, - private string $prefix = '', - private string $suffix = '', - private string $separator = ',', - ) { - parent::__construct($settings, $level, $used); + protected function isWrapped(): bool { + return false; } protected function isNormalized(): bool { - return $this->normalized; + return false; } - protected function isWrapped(): bool { - return $this->wrapped; + protected function isAlwaysMultiline(): bool { + return false; } - public function getPrefix(): string { - return $this->prefix; + protected function getPrefix(): string { + return ''; } - public function getSuffix(): string { - return $this->suffix; + protected function getSuffix(): string { + return ''; } - public function getSeparator(): string { - return $this->separator; + protected function getSeparator(): string { + return ','; } public function isMultiline(): bool { @@ -142,14 +132,14 @@ protected function content(): string { /** * @param array $blocks */ - protected function isMultilineContent( + private function isMultilineContent( array $blocks, string $suffix, string $prefix, - string $itemSeparator, + string $separator, ): bool { - // Any multiline block? - if ($this->multiline) { + // Always or Any multiline block? + if ($this->isAlwaysMultiline() || $this->multiline) { return true; } @@ -159,7 +149,7 @@ protected function isMultilineContent( + $this->length + mb_strlen($suffix) + mb_strlen($prefix) - + mb_strlen($itemSeparator) * ($count - 1); + + mb_strlen($separator) * ($count - 1); return $this->isLineTooLong($length); } diff --git a/src/Printer/Blocks/BlockListTest.php b/src/Printer/Blocks/BlockListTest.php index 93887619..b62eb997 100644 --- a/src/Printer/Blocks/BlockListTest.php +++ b/src/Printer/Blocks/BlockListTest.php @@ -34,7 +34,7 @@ public function testToString( string $suffix, array $blocks, ): void { - $list = new BlockList($settings, $level, $used, $normalized, $wrapped, $prefix, $suffix); + $list = new BlockListTest__BlockList($settings, $level, $used, $normalized, $wrapped, $prefix, $suffix); foreach ($blocks as $name => $block) { $list[$name] = $block; @@ -529,6 +529,45 @@ public function getIndent(): string { // @phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses // @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps +/** + * @internal + * @noinspection PhpMultipleClassesDeclarationsInOneFile + */ +class BlockListTest__BlockList extends BlockList { + public function __construct( + Settings $settings, + int $level, + int $used, + private bool $normalized = false, + private bool $wrapped = false, + private string $prefix = '', + private string $suffix = '', + private string $separator = ',', + ) { + parent::__construct($settings, $level, $used); + } + + protected function isNormalized(): bool { + return $this->normalized; + } + + protected function isWrapped(): bool { + return $this->wrapped; + } + + protected function getPrefix(): string { + return $this->prefix; + } + + protected function getSuffix(): string { + return $this->suffix; + } + + protected function getSeparator(): string { + return $this->separator; + } +} + /** * @internal * @noinspection PhpMultipleClassesDeclarationsInOneFile diff --git a/src/Printer/Blocks/ListBlockList.php b/src/Printer/Blocks/ListBlockList.php new file mode 100644 index 00000000..3642d60e --- /dev/null +++ b/src/Printer/Blocks/ListBlockList.php @@ -0,0 +1,13 @@ +node instanceof ListValueNode) { + $content = new ListBlockList($this->getSettings(), $this->getLevel(), $this->getUsed()); + + foreach ($this->node->values as $value) { + $content[] = new Value($this->getSettings(), $this->getLevel() + 1, $this->getUsed(), $value); + } + } elseif ($this->node instanceof ObjectValueNode) { + $content = new ObjectBlockList($this->getSettings(), $this->getLevel(), $this->getUsed()); + + foreach ($this->node->fields as $field) { + $content[$field->name->value] = new Value( + $this->getSettings(), + $this->getLevel() + 1 + (int) ($field->value instanceof StringValueNode), + $this->getUsed(), + $field->value, + ); + } + } elseif ($this->node instanceof StringValueNode) { + $content = $this->node->block + ? new StringBlock($this->getSettings(), $this->getLevel(), 0, $this->node->value, true) + : Printer::doPrint($this->node); + } else { + $content = Printer::doPrint($this->node); + } + + return (string) $content; + } +} diff --git a/src/Printer/Blocks/ValueTest.php b/src/Printer/Blocks/ValueTest.php new file mode 100644 index 00000000..afbc3aa6 --- /dev/null +++ b/src/Printer/Blocks/ValueTest.php @@ -0,0 +1,261 @@ + + // ========================================================================= + /** + * @covers ::__toString + * + * @dataProvider dataProviderToString + * + * @param ValueNode&Node $node + */ + public function testToString( + string $expected, + Settings $settings, + int $level, + int $used, + ValueNode $node, + ): void { + $actual = (string) (new Value($settings, $level, $used, $node)); + $parsed = Parser::valueLiteral($actual); + + self::assertEquals($expected, $actual); + self::assertInstanceOf(ValueNode::class, $parsed); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderToString(): array { + return [ + NullValueNode::class => [ + 'null', + new DefaultSettings(), + 0, + 0, + Parser::valueLiteral('null'), + ], + IntValueNode::class => [ + '123', + new DefaultSettings(), + 0, + 0, + Parser::valueLiteral('123'), + ], + FloatValueNode::class => [ + '123.45', + new DefaultSettings(), + 0, + 0, + Parser::valueLiteral('123.45'), + ], + BooleanValueNode::class => [ + 'true', + new DefaultSettings(), + 0, + 0, + Parser::valueLiteral('true'), + ], + StringValueNode::class => [ + '"true"', + new DefaultSettings(), + 0, + 0, + Parser::valueLiteral('"true"'), + ], + EnumValueNode::class => [ + 'Value', + new DefaultSettings(), + 0, + 0, + Parser::valueLiteral('Value'), + ], + VariableNode::class => [ + '$variable', + new DefaultSettings(), + 0, + 0, + Parser::valueLiteral('$variable'), + ], + ListValueNode::class.' (short)' => [ + '["a", "b", "c"]', + new DefaultSettings(), + 0, + 0, + Parser::valueLiteral('["a", "b", "c"]'), + ], + ListValueNode::class.' (with block string)' => [ + <<<'STRING' + [ + "string" + """ + Block + string + """ + ] + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + Parser::valueLiteral( + <<<'STRING' + [ + "string" + """ + Block + string + """ + ] + STRING, + ), + ], + ListValueNode::class.' (with block string and level)' => [ + <<<'STRING' + [ + "string" + """ + Block + string + """ + ] + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 1, + 0, + Parser::valueLiteral( + <<<'STRING' + [ + "string" + """ + Block + string + """ + ] + STRING, + ), + ], + ObjectValueNode::class => [ + <<<'STRING' + { + object: { + a: "a" + b: "b" + } + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + Parser::valueLiteral( + <<<'STRING' + { + object: { + a: "a" + b: "b" + } + } + STRING, + ), + ], + 'all' => [ + <<<'STRING' + { + int: 123 + bool: true + string: "string" + blockString: """ + Block + string + """ + array: [1, 2, 3] + object: { + a: "a" + b: { + b: null + array: [3] + nested: { + a: 123 + } + } + } + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + Parser::valueLiteral( + <<<'STRING' + { + int: 123 + bool: true + string: "string" + blockString: """ + Block + string + """ + array: [ + 1 + 2 + 3 + ] + object: { + a: "a" + b: { + b: null + array: [ + 3 + ] + nested: { + a: 123 + } + } + } + } + STRING, + ), + ], + ]; + } + // +} From 74a6731b6164260b115096c5fc3986850f3f2dc4 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Wed, 5 Jan 2022 09:37:04 +0400 Subject: [PATCH 15/90] `DefaultSettings` will use ` ` (4 spaces) for indent. --- src/Printer/Settings/DefaultSettings.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Printer/Settings/DefaultSettings.php b/src/Printer/Settings/DefaultSettings.php index cc1aab47..4969eff5 100644 --- a/src/Printer/Settings/DefaultSettings.php +++ b/src/Printer/Settings/DefaultSettings.php @@ -6,12 +6,16 @@ use LastDragon_ru\LaraASP\GraphQL\Printer\Settings; class DefaultSettings implements Settings { + public function __construct() { + // empty + } + public function getSpace(): string { return ' '; } public function getIndent(): string { - return ' '; + return ' '; } public function getFileEnd(): string { From 07bf1937212665b9413abebc08983e6ace913359 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Wed, 5 Jan 2022 09:49:41 +0400 Subject: [PATCH 16/90] GraphQL blocks moved to own namespace. --- src/Printer/Blocks/{ => Nodes}/Description.php | 2 +- src/Printer/Blocks/{ => Nodes}/DescriptionTest.php | 4 ++-- src/Printer/Blocks/{ => Nodes}/StringBlock.php | 3 ++- src/Printer/Blocks/{ => Nodes}/StringBlockTest.php | 4 ++-- src/Printer/Blocks/{ => Nodes}/Value.php | 5 ++++- src/Printer/Blocks/{ => Nodes}/ValueTest.php | 4 ++-- 6 files changed, 13 insertions(+), 9 deletions(-) rename src/Printer/Blocks/{ => Nodes}/Description.php (95%) rename src/Printer/Blocks/{ => Nodes}/DescriptionTest.php (99%) rename src/Printer/Blocks/{ => Nodes}/StringBlock.php (94%) rename src/Printer/Blocks/{ => Nodes}/StringBlockTest.php (98%) rename src/Printer/Blocks/{ => Nodes}/Value.php (88%) rename src/Printer/Blocks/{ => Nodes}/ValueTest.php (99%) diff --git a/src/Printer/Blocks/Description.php b/src/Printer/Blocks/Nodes/Description.php similarity index 95% rename from src/Printer/Blocks/Description.php rename to src/Printer/Blocks/Nodes/Description.php index 576cda42..504850c2 100644 --- a/src/Printer/Blocks/Description.php +++ b/src/Printer/Blocks/Nodes/Description.php @@ -1,6 +1,6 @@ diff --git a/src/Printer/Blocks/StringBlock.php b/src/Printer/Blocks/Nodes/StringBlock.php similarity index 94% rename from src/Printer/Blocks/StringBlock.php rename to src/Printer/Blocks/Nodes/StringBlock.php index a5f6c1bb..be08394b 100644 --- a/src/Printer/Blocks/StringBlock.php +++ b/src/Printer/Blocks/Nodes/StringBlock.php @@ -1,9 +1,10 @@ diff --git a/src/Printer/Blocks/Value.php b/src/Printer/Blocks/Nodes/Value.php similarity index 88% rename from src/Printer/Blocks/Value.php rename to src/Printer/Blocks/Nodes/Value.php index 826b2389..753d91ed 100644 --- a/src/Printer/Blocks/Value.php +++ b/src/Printer/Blocks/Nodes/Value.php @@ -1,6 +1,6 @@ From adbbee391ed60926d9659673f611dd9cf1b8d0f4 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Wed, 5 Jan 2022 11:17:31 +0400 Subject: [PATCH 17/90] Added `Directive`. --- src/Printer/Blocks/ArgumentsBlockList.php | 20 ++++ src/Printer/Blocks/Nodes/Arguments.php | 41 ++++++++ src/Printer/Blocks/Nodes/Directive.php | 36 +++++++ src/Printer/Blocks/Nodes/DirectiveTest.php | 103 +++++++++++++++++++++ 4 files changed, 200 insertions(+) create mode 100644 src/Printer/Blocks/ArgumentsBlockList.php create mode 100644 src/Printer/Blocks/Nodes/Arguments.php create mode 100644 src/Printer/Blocks/Nodes/Directive.php create mode 100644 src/Printer/Blocks/Nodes/DirectiveTest.php diff --git a/src/Printer/Blocks/ArgumentsBlockList.php b/src/Printer/Blocks/ArgumentsBlockList.php new file mode 100644 index 00000000..951d79f2 --- /dev/null +++ b/src/Printer/Blocks/ArgumentsBlockList.php @@ -0,0 +1,20 @@ +getSettings()->isNormalizeArguments(); + } +} diff --git a/src/Printer/Blocks/Nodes/Arguments.php b/src/Printer/Blocks/Nodes/Arguments.php new file mode 100644 index 00000000..d08374f8 --- /dev/null +++ b/src/Printer/Blocks/Nodes/Arguments.php @@ -0,0 +1,41 @@ + $arguments + */ + public function __construct( + Settings $settings, + int $level, + int $used, + protected NodeList $arguments, + ) { + parent::__construct($settings, $level, $used); + } + + protected function content(): string { + $arguments = new ArgumentsBlockList($this->getSettings(), $this->getLevel(), $this->getUsed()); + + foreach ($this->arguments as $argument) { + $arguments[$argument->name->value] = new Value( + $this->getSettings(), + $this->getLevel() + 1, + $this->getUsed(), + $argument->value, + ); + } + + return (string) $arguments; + } +} diff --git a/src/Printer/Blocks/Nodes/Directive.php b/src/Printer/Blocks/Nodes/Directive.php new file mode 100644 index 00000000..cee76bec --- /dev/null +++ b/src/Printer/Blocks/Nodes/Directive.php @@ -0,0 +1,36 @@ +directive->name->value}"; + $used = mb_strlen($name); + $args = new Arguments( + $this->getSettings(), + $this->getLevel(), + $this->getUsed() + $used, + $this->directive->arguments, + ); + + return "{$name}{$args}"; + } +} diff --git a/src/Printer/Blocks/Nodes/DirectiveTest.php b/src/Printer/Blocks/Nodes/DirectiveTest.php new file mode 100644 index 00000000..1f86adf0 --- /dev/null +++ b/src/Printer/Blocks/Nodes/DirectiveTest.php @@ -0,0 +1,103 @@ + + // ========================================================================= + /** + * @covers ::__toString + * + * @dataProvider dataProviderToString + */ + public function testToString( + string $expected, + Settings $settings, + int $level, + int $used, + DirectiveNode $node, + ): void { + $actual = (string) (new Directive($settings, $level, $used, $node)); + $parsed = Parser::directive($actual); + + self::assertEquals($expected, $actual); + self::assertInstanceOf(DirectiveNode::class, $parsed); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderToString(): array { + return [ + 'without arguments' => [ + '@directive', + new DefaultSettings(), + 0, + 0, + Parser::directive('@directive'), + ], + 'without arguments (level)' => [ + '@directive', + new DefaultSettings(), + 0, + 0, + Parser::directive('@directive'), + ], + 'with arguments (short)' => [ + '@directive(a: "a", b: "b")', + new DefaultSettings(), + 0, + 0, + Parser::directive('@directive(a: "a", b: "b")'), + ], + 'with arguments (long)' => [ + <<<'STRING' + @directive( + b: "b" + a: "a" + ) + STRING, + new DefaultSettings(), + 0, + 120, + Parser::directive('@directive(b: "b", a: "a")'), + ], + 'with arguments (normalized)' => [ + '@directive(a: "a", b: "b")', + new class() extends DefaultSettings { + public function isNormalizeArguments(): bool { + return true; + } + }, + 0, + 0, + Parser::directive('@directive(b: "b", a: "a")'), + ], + 'with arguments (level)' => [ + <<<'STRING' + @directive( + b: "b" + a: "a" + ) + STRING, + new DefaultSettings(), + 1, + 120, + Parser::directive('@directive(b: "b", a: "a")'), + ], + ]; + } + // +} From 80f9bf730bff1681820a90891d2a796411d0ee21 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Wed, 5 Jan 2022 11:18:09 +0400 Subject: [PATCH 18/90] Main namespace renamed to `SchemaPrinter`. --- .../Blocks/ArgumentsBlockList.php | 2 +- src/{Printer => SchemaPrinter}/Blocks/Block.php | 4 ++-- src/{Printer => SchemaPrinter}/Blocks/BlockList.php | 2 +- .../Blocks/BlockListTest.php | 8 ++++---- src/{Printer => SchemaPrinter}/Blocks/BlockTest.php | 8 ++++---- .../Blocks/ListBlockList.php | 2 +- src/{Printer => SchemaPrinter}/Blocks/NamedBlock.php | 4 ++-- .../Blocks/NamedBlockTest.php | 8 ++++---- .../Blocks/Nodes/Arguments.php | 8 ++++---- .../Blocks/Nodes/Description.php | 4 ++-- .../Blocks/Nodes/DescriptionTest.php | 8 ++++---- .../Blocks/Nodes/Directive.php | 6 +++--- .../Blocks/Nodes/DirectiveTest.php | 8 ++++---- .../Blocks/Nodes/StringBlock.php | 6 +++--- .../Blocks/Nodes/StringBlockTest.php | 8 ++++---- src/{Printer => SchemaPrinter}/Blocks/Nodes/Value.php | 10 +++++----- .../Blocks/Nodes/ValueTest.php | 8 ++++---- .../Blocks/ObjectBlockList.php | 2 +- src/{Printer => SchemaPrinter}/Settings.php | 2 +- .../Settings/DefaultSettings.php | 4 ++-- 20 files changed, 56 insertions(+), 56 deletions(-) rename src/{Printer => SchemaPrinter}/Blocks/ArgumentsBlockList.php (85%) rename src/{Printer => SchemaPrinter}/Blocks/Block.php (95%) rename src/{Printer => SchemaPrinter}/Blocks/BlockList.php (98%) rename src/{Printer => SchemaPrinter}/Blocks/BlockListTest.php (98%) rename src/{Printer => SchemaPrinter}/Blocks/BlockTest.php (92%) rename src/{Printer => SchemaPrinter}/Blocks/ListBlockList.php (78%) rename src/{Printer => SchemaPrinter}/Blocks/NamedBlock.php (87%) rename src/{Printer => SchemaPrinter}/Blocks/NamedBlockTest.php (87%) rename src/{Printer => SchemaPrinter}/Blocks/Nodes/Arguments.php (77%) rename src/{Printer => SchemaPrinter}/Blocks/Nodes/Description.php (90%) rename src/{Printer => SchemaPrinter}/Blocks/Nodes/DescriptionTest.php (97%) rename src/{Printer => SchemaPrinter}/Blocks/Nodes/Directive.php (79%) rename src/{Printer => SchemaPrinter}/Blocks/Nodes/DirectiveTest.php (91%) rename src/{Printer => SchemaPrinter}/Blocks/Nodes/StringBlock.php (91%) rename src/{Printer => SchemaPrinter}/Blocks/Nodes/StringBlockTest.php (95%) rename src/{Printer => SchemaPrinter}/Blocks/Nodes/Value.php (84%) rename src/{Printer => SchemaPrinter}/Blocks/Nodes/ValueTest.php (96%) rename src/{Printer => SchemaPrinter}/Blocks/ObjectBlockList.php (82%) rename src/{Printer => SchemaPrinter}/Settings.php (97%) rename src/{Printer => SchemaPrinter}/Settings/DefaultSettings.php (92%) diff --git a/src/Printer/Blocks/ArgumentsBlockList.php b/src/SchemaPrinter/Blocks/ArgumentsBlockList.php similarity index 85% rename from src/Printer/Blocks/ArgumentsBlockList.php rename to src/SchemaPrinter/Blocks/ArgumentsBlockList.php index 951d79f2..5a7c7415 100644 --- a/src/Printer/Blocks/ArgumentsBlockList.php +++ b/src/SchemaPrinter/Blocks/ArgumentsBlockList.php @@ -1,6 +1,6 @@ diff --git a/src/Printer/Blocks/BlockTest.php b/src/SchemaPrinter/Blocks/BlockTest.php similarity index 92% rename from src/Printer/Blocks/BlockTest.php rename to src/SchemaPrinter/Blocks/BlockTest.php index 0b116f59..fe4b5785 100644 --- a/src/Printer/Blocks/BlockTest.php +++ b/src/SchemaPrinter/Blocks/BlockTest.php @@ -1,16 +1,16 @@ diff --git a/src/Printer/Blocks/ListBlockList.php b/src/SchemaPrinter/Blocks/ListBlockList.php similarity index 78% rename from src/Printer/Blocks/ListBlockList.php rename to src/SchemaPrinter/Blocks/ListBlockList.php index 3642d60e..e47e73d9 100644 --- a/src/Printer/Blocks/ListBlockList.php +++ b/src/SchemaPrinter/Blocks/ListBlockList.php @@ -1,6 +1,6 @@ diff --git a/src/Printer/Blocks/Nodes/Directive.php b/src/SchemaPrinter/Blocks/Nodes/Directive.php similarity index 79% rename from src/Printer/Blocks/Nodes/Directive.php rename to src/SchemaPrinter/Blocks/Nodes/Directive.php index cee76bec..fa6b50e5 100644 --- a/src/Printer/Blocks/Nodes/Directive.php +++ b/src/SchemaPrinter/Blocks/Nodes/Directive.php @@ -1,10 +1,10 @@ diff --git a/src/Printer/Blocks/Nodes/StringBlock.php b/src/SchemaPrinter/Blocks/Nodes/StringBlock.php similarity index 91% rename from src/Printer/Blocks/Nodes/StringBlock.php rename to src/SchemaPrinter/Blocks/Nodes/StringBlock.php index be08394b..df63b84c 100644 --- a/src/Printer/Blocks/Nodes/StringBlock.php +++ b/src/SchemaPrinter/Blocks/Nodes/StringBlock.php @@ -1,11 +1,11 @@ diff --git a/src/Printer/Blocks/Nodes/Value.php b/src/SchemaPrinter/Blocks/Nodes/Value.php similarity index 84% rename from src/Printer/Blocks/Nodes/Value.php rename to src/SchemaPrinter/Blocks/Nodes/Value.php index 753d91ed..94ff3e5b 100644 --- a/src/Printer/Blocks/Nodes/Value.php +++ b/src/SchemaPrinter/Blocks/Nodes/Value.php @@ -1,6 +1,6 @@ diff --git a/src/Printer/Blocks/ObjectBlockList.php b/src/SchemaPrinter/Blocks/ObjectBlockList.php similarity index 82% rename from src/Printer/Blocks/ObjectBlockList.php rename to src/SchemaPrinter/Blocks/ObjectBlockList.php index 93ef60f1..ab244437 100644 --- a/src/Printer/Blocks/ObjectBlockList.php +++ b/src/SchemaPrinter/Blocks/ObjectBlockList.php @@ -1,6 +1,6 @@ Date: Thu, 6 Jan 2022 10:27:51 +0400 Subject: [PATCH 19/90] Removed `ArgumentsBlockList`. --- .../Blocks/ArgumentsBlockList.php | 20 -------------- src/SchemaPrinter/Blocks/Nodes/Arguments.php | 27 +++++++++++-------- 2 files changed, 16 insertions(+), 31 deletions(-) delete mode 100644 src/SchemaPrinter/Blocks/ArgumentsBlockList.php diff --git a/src/SchemaPrinter/Blocks/ArgumentsBlockList.php b/src/SchemaPrinter/Blocks/ArgumentsBlockList.php deleted file mode 100644 index 5a7c7415..00000000 --- a/src/SchemaPrinter/Blocks/ArgumentsBlockList.php +++ /dev/null @@ -1,20 +0,0 @@ -getSettings()->isNormalizeArguments(); - } -} diff --git a/src/SchemaPrinter/Blocks/Nodes/Arguments.php b/src/SchemaPrinter/Blocks/Nodes/Arguments.php index 2b703572..a60195af 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Arguments.php +++ b/src/SchemaPrinter/Blocks/Nodes/Arguments.php @@ -4,14 +4,13 @@ use GraphQL\Language\AST\ArgumentNode; use GraphQL\Language\AST\NodeList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\ArgumentsBlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; /** * @internal */ -class Arguments extends Block { +class Arguments extends BlockList { /** * @param NodeList $arguments */ @@ -19,23 +18,29 @@ public function __construct( Settings $settings, int $level, int $used, - protected NodeList $arguments, + NodeList $arguments, ) { parent::__construct($settings, $level, $used); - } - - protected function content(): string { - $arguments = new ArgumentsBlockList($this->getSettings(), $this->getLevel(), $this->getUsed()); - foreach ($this->arguments as $argument) { - $arguments[$argument->name->value] = new Value( + foreach ($arguments as $argument) { + $this[$argument->name->value] = new Value( $this->getSettings(), $this->getLevel() + 1, $this->getUsed(), $argument->value, ); } + } + + protected function getPrefix(): string { + return '('; + } + + protected function getSuffix(): string { + return ')'; + } - return (string) $arguments; + protected function isNormalized(): bool { + return $this->getSettings()->isNormalizeArguments(); } } From 5afced6421e99e5ad7f1bf47a24fc5ba7f466190 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Thu, 6 Jan 2022 14:39:37 +0400 Subject: [PATCH 20/90] Better settings. --- .../Contracts/DirectiveFilter.php | 9 +++++++ src/SchemaPrinter/Settings.php | 24 +++++++------------ .../Settings/DefaultSettings.php | 21 +++++++--------- 3 files changed, 26 insertions(+), 28 deletions(-) create mode 100644 src/SchemaPrinter/Contracts/DirectiveFilter.php diff --git a/src/SchemaPrinter/Contracts/DirectiveFilter.php b/src/SchemaPrinter/Contracts/DirectiveFilter.php new file mode 100644 index 00000000..176ea4e9 --- /dev/null +++ b/src/SchemaPrinter/Contracts/DirectiveFilter.php @@ -0,0 +1,9 @@ + Date: Thu, 6 Jan 2022 14:40:28 +0400 Subject: [PATCH 21/90] Added `Dispatcher` for all blocks. --- src/SchemaPrinter/Blocks/Block.php | 6 +++++ src/SchemaPrinter/Blocks/BlockList.php | 15 ++++++----- src/SchemaPrinter/Blocks/BlockListTest.php | 17 ++++++++++--- src/SchemaPrinter/Blocks/BlockTest.php | 7 +++--- src/SchemaPrinter/Blocks/NamedBlock.php | 4 ++- src/SchemaPrinter/Blocks/NamedBlockTest.php | 25 +++++++++++-------- src/SchemaPrinter/Blocks/Nodes/Arguments.php | 12 ++++++--- .../Blocks/Nodes/Description.php | 4 ++- .../Blocks/Nodes/DescriptionTest.php | 3 ++- src/SchemaPrinter/Blocks/Nodes/Directive.php | 16 +++++++++--- .../Blocks/Nodes/DirectiveTest.php | 3 ++- .../Blocks/Nodes/StringBlock.php | 4 ++- .../Blocks/Nodes/StringBlockTest.php | 3 ++- src/SchemaPrinter/Blocks/Nodes/Value.php | 25 ++++++++++++------- src/SchemaPrinter/Blocks/Nodes/ValueTest.php | 3 ++- 15 files changed, 100 insertions(+), 47 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Block.php b/src/SchemaPrinter/Blocks/Block.php index dd8bf3f8..19436a21 100644 --- a/src/SchemaPrinter/Blocks/Block.php +++ b/src/SchemaPrinter/Blocks/Block.php @@ -2,6 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; +use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use Stringable; @@ -18,6 +19,7 @@ abstract class Block implements Stringable { private ?bool $multiline = null; public function __construct( + private Dispatcher $dispatcher, private Settings $settings, private int $level = 0, private int $used = 0, @@ -27,6 +29,10 @@ public function __construct( // // ========================================================================= + protected function getDispatcher(): Dispatcher { + return $this->dispatcher; + } + protected function getSettings(): Settings { return $this->settings; } diff --git a/src/SchemaPrinter/Blocks/BlockList.php b/src/SchemaPrinter/Blocks/BlockList.php index 1122cf82..3c533a32 100644 --- a/src/SchemaPrinter/Blocks/BlockList.php +++ b/src/SchemaPrinter/Blocks/BlockList.php @@ -14,11 +14,12 @@ /** * @internal - * @implements ArrayAccess + * @template TBlock of Block + * @implements ArrayAccess */ abstract class BlockList extends Block implements ArrayAccess { /** - * @var array + * @var array */ private array $blocks = []; @@ -57,7 +58,7 @@ public function isMultiline(): bool { } /** - * @return array + * @return array */ protected function getBlocks(): array { $blocks = $this->blocks; @@ -130,7 +131,7 @@ protected function content(): string { } /** - * @param array $blocks + * @param array $blocks */ private function isMultilineContent( array $blocks, @@ -165,6 +166,8 @@ public function offsetExists(mixed $offset): bool { /** * @param int|string $offset + * + * @return TBlock */ public function offsetGet(mixed $offset): Block { return $this->blocks[$offset]; @@ -172,12 +175,12 @@ public function offsetGet(mixed $offset): Block { /** * @param int|string|null $offset - * @param Block $value + * @param TBlock $value */ public function offsetSet(mixed $offset, mixed $value): void { if ($offset !== null) { if (!is_numeric($offset)) { - $value = new NamedBlock($this->getSettings(), $offset, $value); + $value = new NamedBlock($this->getDispatcher(), $this->getSettings(), $offset, $value); } $this->blocks[$offset] = $value; diff --git a/src/SchemaPrinter/Blocks/BlockListTest.php b/src/SchemaPrinter/Blocks/BlockListTest.php index 1d0bbbb2..d40ed6aa 100644 --- a/src/SchemaPrinter/Blocks/BlockListTest.php +++ b/src/SchemaPrinter/Blocks/BlockListTest.php @@ -2,6 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; +use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; @@ -34,7 +35,16 @@ public function testToString( string $suffix, array $blocks, ): void { - $list = new BlockListTest__BlockList($settings, $level, $used, $normalized, $wrapped, $prefix, $suffix); + $list = new BlockListTest__BlockList( + new Dispatcher(), + $settings, + $level, + $used, + $normalized, + $wrapped, + $prefix, + $suffix, + ); foreach ($blocks as $name => $block) { $list[$name] = $block; @@ -535,6 +545,7 @@ public function getIndent(): string { */ class BlockListTest__BlockList extends BlockList { public function __construct( + Dispatcher $dispatcher, Settings $settings, int $level, int $used, @@ -544,7 +555,7 @@ public function __construct( private string $suffix = '', private string $separator = ',', ) { - parent::__construct($settings, $level, $used); + parent::__construct($dispatcher, $settings, $level, $used); } protected function isNormalized(): bool { @@ -577,7 +588,7 @@ public function __construct( protected bool $multiline, protected string $content, ) { - parent::__construct(new DefaultSettings()); + parent::__construct(new Dispatcher(), new DefaultSettings()); } protected function getContent(): string { diff --git a/src/SchemaPrinter/Blocks/BlockTest.php b/src/SchemaPrinter/Blocks/BlockTest.php index fe4b5785..5f8cecda 100644 --- a/src/SchemaPrinter/Blocks/BlockTest.php +++ b/src/SchemaPrinter/Blocks/BlockTest.php @@ -2,6 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; +use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; use Mockery; @@ -20,7 +21,7 @@ class BlockTest extends TestCase { */ public function testGetContent(): void { $content = 'content'; - $block = Mockery::mock(BlockTest__Block::class, [new DefaultSettings()]); + $block = Mockery::mock(BlockTest__Block::class, [new Dispatcher(), new DefaultSettings()]); $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); $block @@ -38,7 +39,7 @@ public function testGetContent(): void { public function testGetLength(): void { $content = 'content'; $length = mb_strlen($content); - $block = Mockery::mock(BlockTest__Block::class, [new DefaultSettings()]); + $block = Mockery::mock(BlockTest__Block::class, [new Dispatcher(), new DefaultSettings()]); $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); $block @@ -56,7 +57,7 @@ public function testGetLength(): void { * @dataProvider dataProviderIsMultiline */ public function testIsMultiline(bool $expected, Settings $settings, string $content): void { - $block = Mockery::mock(BlockTest__Block::class, [$settings]); + $block = Mockery::mock(BlockTest__Block::class, [new Dispatcher(), $settings]); $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); $block diff --git a/src/SchemaPrinter/Blocks/NamedBlock.php b/src/SchemaPrinter/Blocks/NamedBlock.php index f2f043a7..176b871a 100644 --- a/src/SchemaPrinter/Blocks/NamedBlock.php +++ b/src/SchemaPrinter/Blocks/NamedBlock.php @@ -2,6 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; +use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; /** @@ -9,12 +10,13 @@ */ class NamedBlock extends Block { public function __construct( + Dispatcher $dispatcher, Settings $settings, private string $name, private Block $block, private string $separator = ':', ) { - parent::__construct($settings, $block->getLevel(), $block->getUsed()); + parent::__construct($dispatcher, $settings, $block->getLevel(), $block->getUsed()); } public function getName(): string { diff --git a/src/SchemaPrinter/Blocks/NamedBlockTest.php b/src/SchemaPrinter/Blocks/NamedBlockTest.php index 27edf8ff..f7eece3b 100644 --- a/src/SchemaPrinter/Blocks/NamedBlockTest.php +++ b/src/SchemaPrinter/Blocks/NamedBlockTest.php @@ -2,6 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; +use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; use PHPUnit\Framework\TestCase; @@ -19,13 +20,13 @@ class NamedBlockTest extends TestCase { * @covers ::getUsed */ public function testToString(): void { - $name = 'name'; - $used = 123; - $level = 2; - $space = ' '; - $separator = ':'; - $content = 'abc abcabc abcabc abcabc abc'; - $settings = new class($space) extends DefaultSettings { + $name = 'name'; + $used = 123; + $level = 2; + $space = ' '; + $separator = ':'; + $content = 'abc abcabc abcabc abcabc abc'; + $settings = new class($space) extends DefaultSettings { public function __construct( protected string $space, ) { @@ -36,21 +37,23 @@ public function getSpace(): string { return $this->space; } }; - $block = new class($settings, $level, $used, $content) extends Block { + $dispatcher = new Dispatcher(); + $block = new class($dispatcher, $settings, $level, $used, $content) extends Block { public function __construct( + Dispatcher $dispatcher, Settings $settings, int $level, int $used, protected string $content, ) { - parent::__construct($settings, $level, $used); + parent::__construct($dispatcher, $settings, $level, $used); } protected function content(): string { return $this->content; } }; - $named = new class($settings, $name, $block, $separator) extends NamedBlock { + $named = new class($dispatcher, $settings, $name, $block, $separator) extends NamedBlock { public function getUsed(): int { return parent::getUsed(); } @@ -59,7 +62,7 @@ public function getLevel(): int { return parent::getLevel(); } }; - $expected = "{$name}{$separator}{$space}{$content}"; + $expected = "{$name}{$separator}{$space}{$content}"; self::assertEquals($used, $named->getUsed()); self::assertEquals($level, $named->getLevel()); diff --git a/src/SchemaPrinter/Blocks/Nodes/Arguments.php b/src/SchemaPrinter/Blocks/Nodes/Arguments.php index a60195af..c1160b50 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Arguments.php +++ b/src/SchemaPrinter/Blocks/Nodes/Arguments.php @@ -3,27 +3,31 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Nodes; use GraphQL\Language\AST\ArgumentNode; -use GraphQL\Language\AST\NodeList; +use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use Traversable; /** * @internal + * @extends BlockList */ class Arguments extends BlockList { /** - * @param NodeList $arguments + * @param Traversable|array $arguments */ public function __construct( + Dispatcher $dispatcher, Settings $settings, int $level, int $used, - NodeList $arguments, + Traversable|array $arguments, ) { - parent::__construct($settings, $level, $used); + parent::__construct($dispatcher, $settings, $level, $used); foreach ($arguments as $argument) { $this[$argument->name->value] = new Value( + $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, $this->getUsed(), diff --git a/src/SchemaPrinter/Blocks/Nodes/Description.php b/src/SchemaPrinter/Blocks/Nodes/Description.php index 55028e69..630703a9 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Description.php +++ b/src/SchemaPrinter/Blocks/Nodes/Description.php @@ -2,6 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Nodes; +use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use function preg_replace; @@ -14,12 +15,13 @@ */ class Description extends StringBlock { public function __construct( + Dispatcher $dispatcher, Settings $settings, int $level, int $used, string $string, ) { - parent::__construct($settings, $level, $used, $string, true); + parent::__construct($dispatcher, $settings, $level, $used, $string, true); } protected function isNormalized(): bool { diff --git a/src/SchemaPrinter/Blocks/Nodes/DescriptionTest.php b/src/SchemaPrinter/Blocks/Nodes/DescriptionTest.php index 7f749db3..c611b96c 100644 --- a/src/SchemaPrinter/Blocks/Nodes/DescriptionTest.php +++ b/src/SchemaPrinter/Blocks/Nodes/DescriptionTest.php @@ -3,6 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Nodes; use GraphQL\Language\Parser; +use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -27,7 +28,7 @@ public function testToString( int $used, string $description, ): void { - $actual = (string) (new Description($settings, $level, $used, $description)); + $actual = (string) (new Description(new Dispatcher(), $settings, $level, $used, $description)); self::assertEquals($expected, $actual); diff --git a/src/SchemaPrinter/Blocks/Nodes/Directive.php b/src/SchemaPrinter/Blocks/Nodes/Directive.php index fa6b50e5..746eb672 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Directive.php +++ b/src/SchemaPrinter/Blocks/Nodes/Directive.php @@ -3,6 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Nodes; use GraphQL\Language\AST\DirectiveNode; +use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -13,22 +14,29 @@ */ class Directive extends Block { public function __construct( + Dispatcher $dispatcher, Settings $settings, int $level, int $used, - private DirectiveNode $directive, + private DirectiveNode $node, ) { - parent::__construct($settings, $level, $used); + parent::__construct($dispatcher, $settings, $level, $used); + } + + public function getNode(): DirectiveNode { + return $this->node; } protected function content(): string { - $name = "@{$this->directive->name->value}"; + $node = $this->getNode(); + $name = "@{$node->name->value}"; $used = mb_strlen($name); $args = new Arguments( + $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $this->getUsed() + $used, - $this->directive->arguments, + $node->arguments, ); return "{$name}{$args}"; diff --git a/src/SchemaPrinter/Blocks/Nodes/DirectiveTest.php b/src/SchemaPrinter/Blocks/Nodes/DirectiveTest.php index 4026b1f2..95f6f082 100644 --- a/src/SchemaPrinter/Blocks/Nodes/DirectiveTest.php +++ b/src/SchemaPrinter/Blocks/Nodes/DirectiveTest.php @@ -4,6 +4,7 @@ use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\Parser; +use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; use PHPUnit\Framework\TestCase; @@ -26,7 +27,7 @@ public function testToString( int $used, DirectiveNode $node, ): void { - $actual = (string) (new Directive($settings, $level, $used, $node)); + $actual = (string) (new Directive(new Dispatcher(), $settings, $level, $used, $node)); $parsed = Parser::directive($actual); self::assertEquals($expected, $actual); diff --git a/src/SchemaPrinter/Blocks/Nodes/StringBlock.php b/src/SchemaPrinter/Blocks/Nodes/StringBlock.php index df63b84c..be61ebd9 100644 --- a/src/SchemaPrinter/Blocks/Nodes/StringBlock.php +++ b/src/SchemaPrinter/Blocks/Nodes/StringBlock.php @@ -4,6 +4,7 @@ use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\Printer; +use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -18,13 +19,14 @@ */ class StringBlock extends Block { public function __construct( + Dispatcher $dispatcher, Settings $settings, int $level, int $used, protected string $string, protected bool $block = false, ) { - parent::__construct($settings, $level, $used); + parent::__construct($dispatcher, $settings, $level, $used); } protected function getString(): string { diff --git a/src/SchemaPrinter/Blocks/Nodes/StringBlockTest.php b/src/SchemaPrinter/Blocks/Nodes/StringBlockTest.php index c84e0740..fea06571 100644 --- a/src/SchemaPrinter/Blocks/Nodes/StringBlockTest.php +++ b/src/SchemaPrinter/Blocks/Nodes/StringBlockTest.php @@ -3,6 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Nodes; use GraphQL\Language\Parser; +use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -27,7 +28,7 @@ public function testToString( int $used, string $string, ): void { - $actual = (string) new StringBlock($settings, $level, $used, $string); + $actual = (string) new StringBlock(new Dispatcher(), $settings, $level, $used, $string); $parsed = Parser::valueLiteral($actual)->value; self::assertEquals($expected, $actual); diff --git a/src/SchemaPrinter/Blocks/Nodes/Value.php b/src/SchemaPrinter/Blocks/Nodes/Value.php index 94ff3e5b..1fd6a550 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Value.php +++ b/src/SchemaPrinter/Blocks/Nodes/Value.php @@ -8,6 +8,7 @@ use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\AST\ValueNode; use GraphQL\Language\Printer; +use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\ListBlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\ObjectBlockList; @@ -18,37 +19,43 @@ class Value extends Block { * @param ValueNode&Node $node */ public function __construct( + Dispatcher $dispatcher, Settings $settings, int $level, int $used, protected ValueNode $node, ) { - parent::__construct($settings, $level, $used); + parent::__construct($dispatcher, $settings, $level, $used); } protected function content(): string { - $content = ''; + $content = ''; + $dispatcher = $this->getDispatcher(); + $settings = $this->getSettings(); + $level = $this->getLevel(); + $used = $this->getUsed(); if ($this->node instanceof ListValueNode) { - $content = new ListBlockList($this->getSettings(), $this->getLevel(), $this->getUsed()); + $content = new ListBlockList($dispatcher, $settings, $level, $used); foreach ($this->node->values as $value) { - $content[] = new Value($this->getSettings(), $this->getLevel() + 1, $this->getUsed(), $value); + $content[] = new Value($dispatcher, $settings, $level + 1, $used, $value); } } elseif ($this->node instanceof ObjectValueNode) { - $content = new ObjectBlockList($this->getSettings(), $this->getLevel(), $this->getUsed()); + $content = new ObjectBlockList($dispatcher, $settings, $level, $used); foreach ($this->node->fields as $field) { $content[$field->name->value] = new Value( - $this->getSettings(), - $this->getLevel() + 1 + (int) ($field->value instanceof StringValueNode), - $this->getUsed(), + $dispatcher, + $settings, + $level + 1 + (int) ($field->value instanceof StringValueNode), + $used, $field->value, ); } } elseif ($this->node instanceof StringValueNode) { $content = $this->node->block - ? new StringBlock($this->getSettings(), $this->getLevel(), 0, $this->node->value, true) + ? new StringBlock($dispatcher, $settings, $level, 0, $this->node->value, true) : Printer::doPrint($this->node); } else { $content = Printer::doPrint($this->node); diff --git a/src/SchemaPrinter/Blocks/Nodes/ValueTest.php b/src/SchemaPrinter/Blocks/Nodes/ValueTest.php index 4776660b..20f4bbfb 100644 --- a/src/SchemaPrinter/Blocks/Nodes/ValueTest.php +++ b/src/SchemaPrinter/Blocks/Nodes/ValueTest.php @@ -14,6 +14,7 @@ use GraphQL\Language\AST\ValueNode; use GraphQL\Language\AST\VariableNode; use GraphQL\Language\Parser; +use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; use PHPUnit\Framework\TestCase; @@ -38,7 +39,7 @@ public function testToString( int $used, ValueNode $node, ): void { - $actual = (string) (new Value($settings, $level, $used, $node)); + $actual = (string) (new Value(new Dispatcher(), $settings, $level, $used, $node)); $parsed = Parser::valueLiteral($actual); self::assertEquals($expected, $actual); From cb99cb675e1302c75f3218e8e35b36bef57e69d5 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Thu, 6 Jan 2022 14:58:34 +0400 Subject: [PATCH 22/90] `Directive` will emit `DirectiveUsed` event. --- .../Blocks/Events/DirectiveUsed.php | 16 +++++++++++ src/SchemaPrinter/Blocks/Events/Event.php | 10 +++++++ src/SchemaPrinter/Blocks/Nodes/Directive.php | 8 ++++++ .../Blocks/Nodes/DirectiveTest.php | 28 +++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 src/SchemaPrinter/Blocks/Events/DirectiveUsed.php create mode 100644 src/SchemaPrinter/Blocks/Events/Event.php diff --git a/src/SchemaPrinter/Blocks/Events/DirectiveUsed.php b/src/SchemaPrinter/Blocks/Events/DirectiveUsed.php new file mode 100644 index 00000000..58eeaec4 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Events/DirectiveUsed.php @@ -0,0 +1,16 @@ +getNode(); $name = "@{$node->name->value}"; $used = mb_strlen($name); @@ -39,6 +41,12 @@ protected function content(): string { $node->arguments, ); + // Event + $this->getDispatcher()->notify( + new DirectiveUsed($node), + ); + + // Return return "{$name}{$args}"; } } diff --git a/src/SchemaPrinter/Blocks/Nodes/DirectiveTest.php b/src/SchemaPrinter/Blocks/Nodes/DirectiveTest.php index 95f6f082..0a06ed6e 100644 --- a/src/SchemaPrinter/Blocks/Nodes/DirectiveTest.php +++ b/src/SchemaPrinter/Blocks/Nodes/DirectiveTest.php @@ -2,11 +2,15 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Nodes; +use Closure; use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\Parser; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\DirectiveUsed; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use Mockery; use PHPUnit\Framework\TestCase; /** @@ -33,6 +37,30 @@ public function testToString( self::assertEquals($expected, $actual); self::assertInstanceOf(DirectiveNode::class, $parsed); } + + /** + * @covers ::__toString + */ + public function testToStringEvent(): void { + $spy = Mockery::spy(static fn(Event $event) => null); + $node = Parser::directive('@test'); + $settings = new DefaultSettings(); + $dispatcher = new Dispatcher(); + + $dispatcher->attach(Closure::fromCallable($spy)); + + self::assertNotNull( + (string) (new Directive($dispatcher, $settings, 0, 0, $node)), + ); + + $spy + ->shouldHaveBeenCalled() + ->withArgs(static function (Event $event) use ($node): bool { + return $event instanceof DirectiveUsed + && $event->directive === $node; + }) + ->once(); + } // // From 6d1295d64c5e4f029f3136fd84f7532489a11994 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Thu, 6 Jan 2022 21:32:03 +0400 Subject: [PATCH 23/90] Empty `ListBlockList` will be printed as `[]` instead of empty line. --- src/SchemaPrinter/Blocks/BlockList.php | 6 +++++- src/SchemaPrinter/Blocks/BlockListTest.php | 11 +++++++++++ src/SchemaPrinter/Blocks/ListBlockList.php | 4 ++++ src/SchemaPrinter/Blocks/Nodes/ValueTest.php | 9 +++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/SchemaPrinter/Blocks/BlockList.php b/src/SchemaPrinter/Blocks/BlockList.php index 3c533a32..9bbc6952 100644 --- a/src/SchemaPrinter/Blocks/BlockList.php +++ b/src/SchemaPrinter/Blocks/BlockList.php @@ -53,6 +53,10 @@ protected function getSeparator(): string { return ','; } + protected function getEmptyValue(): string { + return ''; + } + public function isMultiline(): bool { return count($this->multiline) > 0 || parent::isMultiline(); } @@ -77,7 +81,7 @@ protected function content(): string { $count = count($blocks); if (!$count) { - return $content; + return $this->getEmptyValue(); } // Join diff --git a/src/SchemaPrinter/Blocks/BlockListTest.php b/src/SchemaPrinter/Blocks/BlockListTest.php index d40ed6aa..b840b818 100644 --- a/src/SchemaPrinter/Blocks/BlockListTest.php +++ b/src/SchemaPrinter/Blocks/BlockListTest.php @@ -530,6 +530,17 @@ public function getIndent(): string { new BlockListTest__Block(true, 'block a'), ], ], + 'empty' => [ + '', + new DefaultSettings(), + 0, + 0, + false, + false, + '[', + ']', + [], + ], ]), ]))->getData(); } diff --git a/src/SchemaPrinter/Blocks/ListBlockList.php b/src/SchemaPrinter/Blocks/ListBlockList.php index e47e73d9..c7695c9c 100644 --- a/src/SchemaPrinter/Blocks/ListBlockList.php +++ b/src/SchemaPrinter/Blocks/ListBlockList.php @@ -10,4 +10,8 @@ protected function getPrefix(): string { protected function getSuffix(): string { return ']'; } + + protected function getEmptyValue(): string { + return "{$this->getPrefix()}{$this->getSuffix()}"; + } } diff --git a/src/SchemaPrinter/Blocks/Nodes/ValueTest.php b/src/SchemaPrinter/Blocks/Nodes/ValueTest.php index 20f4bbfb..01a8c7db 100644 --- a/src/SchemaPrinter/Blocks/Nodes/ValueTest.php +++ b/src/SchemaPrinter/Blocks/Nodes/ValueTest.php @@ -168,6 +168,15 @@ public function getIndent(): string { STRING, ), ], + ListValueNode::class.' (empty)' => [ + <<<'STRING' + [] + STRING, + new DefaultSettings(), + 1, + 0, + Parser::valueLiteral('[]'), + ], ObjectValueNode::class => [ <<<'STRING' { From 005a9265a45860d771143129206e00d77001b9e0 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Fri, 7 Jan 2022 11:57:13 +0400 Subject: [PATCH 24/90] Added `Directives` (prints list of directives) and added support for it to `Description`. --- .../Blocks/Nodes/Description.php | 23 ++- .../Blocks/Nodes/DescriptionTest.php | 104 +++++++++++- src/SchemaPrinter/Blocks/Nodes/Directives.php | 86 ++++++++++ .../Blocks/Nodes/DirectivesTest.php | 155 ++++++++++++++++++ .../Blocks/Nodes/StringBlock.php | 3 +- 5 files changed, 367 insertions(+), 4 deletions(-) create mode 100644 src/SchemaPrinter/Blocks/Nodes/Directives.php create mode 100644 src/SchemaPrinter/Blocks/Nodes/DirectivesTest.php diff --git a/src/SchemaPrinter/Blocks/Nodes/Description.php b/src/SchemaPrinter/Blocks/Nodes/Description.php index 630703a9..5139190f 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Description.php +++ b/src/SchemaPrinter/Blocks/Nodes/Description.php @@ -20,15 +20,25 @@ public function __construct( int $level, int $used, string $string, + private ?Directives $directives = null, ) { - parent::__construct($dispatcher, $settings, $level, $used, $string, true); + parent::__construct($dispatcher, $settings, $level, $used, $string); + } + + protected function getDirectives(): ?Directives { + return $this->directives; } protected function isNormalized(): bool { return $this->getSettings()->isNormalizeDescription(); } + protected function isBlock(): bool { + return true; + } + protected function getString(): string { + // Normalize $string = parent::getString(); if ($this->isNormalized()) { @@ -39,6 +49,17 @@ protected function getString(): string { $string = preg_replace('/^(.*?)\h+$/mu', '$1', $string); } + // Directives + if ($this->getSettings()->isIncludeDirectivesInDescription()) { + $directives = (string) $this->getDirectives(); + + if ($directives) { + $eol = $this->eol(); + $string = "{$string}{$eol}{$eol}{$directives}"; + } + } + + // Return return $string; } diff --git a/src/SchemaPrinter/Blocks/Nodes/DescriptionTest.php b/src/SchemaPrinter/Blocks/Nodes/DescriptionTest.php index c611b96c..a39d9add 100644 --- a/src/SchemaPrinter/Blocks/Nodes/DescriptionTest.php +++ b/src/SchemaPrinter/Blocks/Nodes/DescriptionTest.php @@ -2,6 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Nodes; +use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\Parser; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -20,6 +21,8 @@ class DescriptionTest extends TestCase { * @covers ::__toString * * @dataProvider dataProviderToString + * + * @param array $directives */ public function testToString( string $expected, @@ -27,8 +30,11 @@ public function testToString( int $level, int $used, string $description, + array $directives, ): void { - $actual = (string) (new Description(new Dispatcher(), $settings, $level, $used, $description)); + $dispatcher = new Dispatcher(); + $directives = new Directives($dispatcher, $settings, $level, $used, $directives); + $actual = (string) (new Description($dispatcher, $settings, $level, $used, $description, $directives)); self::assertEquals($expected, $actual); @@ -55,6 +61,7 @@ public function isNormalizeDescription(): bool { 0, 0, '', + [], ], 'Prints an empty string (normalized)' => [ '', @@ -66,6 +73,7 @@ public function isNormalizeDescription(): bool { 0, 0, '', + [], ], 'Prints an empty string with only whitespace' => [ '" "', @@ -77,6 +85,7 @@ public function isNormalizeDescription(): bool { 0, 0, ' ', + [], ], 'Prints an empty string with only whitespace (normalized)' => [ '', @@ -88,6 +97,7 @@ public function isNormalizeDescription(): bool { 0, 0, ' ', + [], ], 'One-line prints a short string' => [ <<<'STRING' @@ -99,6 +109,7 @@ public function isNormalizeDescription(): bool { 0, 0, 'Short string', + [], ], 'One-line prints a long string' => [ <<<'STRING' @@ -114,6 +125,7 @@ public function getLineLength(): int { 0, 0, 'Long string', + [], ], 'String is short (indent)' => [ <<<'STRING' @@ -133,6 +145,7 @@ public function getIndent(): string { 2, 0, 'string', + [], ], 'String is long (indent)' => [ <<<'STRING' @@ -152,6 +165,7 @@ public function getIndent(): string { 2, 20, 'string', + [], ], 'Multi-line string' => [ <<<'STRING' @@ -179,6 +193,7 @@ public function isNormalizeDescription(): bool { ccc STRING, + [], ], 'Multi-line string (normalized)' => [ <<<'STRING' @@ -203,6 +218,7 @@ public function isNormalizeDescription(): bool { ccc STRING, + [], ], 'Leading space' => [ <<<'STRING' @@ -212,6 +228,7 @@ public function isNormalizeDescription(): bool { 0, 0, ' Leading space', + [], ], 'Leading tab' => [ "\"\"\"\tLeading tab\"\"\"", @@ -219,6 +236,7 @@ public function isNormalizeDescription(): bool { 0, 0, "\tLeading tab", + [], ], 'Trailing "' => [ <<<'STRING' @@ -230,6 +248,7 @@ public function isNormalizeDescription(): bool { 0, 0, 'Trailing "', + [], ], 'Leading whitespace and trailing "' => [ <<<'STRING' @@ -245,6 +264,7 @@ public function isNormalizeDescription(): bool { Leading whitespace and trailing " abc STRING, + [], ], 'Trailing backslash' => [ <<<'STRING' @@ -256,6 +276,7 @@ public function isNormalizeDescription(): bool { 0, 0, 'Trailing \\\\', + [], ], 'Escape wrapper' => [ <<<'STRING' @@ -267,6 +288,7 @@ public function isNormalizeDescription(): bool { 0, 0, 'String with """ wrapper', + [], ], 'Indent' => [ implode( @@ -300,6 +322,7 @@ public function getIndent(): string { ' ddd ', ], ), + [], ], 'Indent (normalized)' => [ implode( @@ -337,6 +360,85 @@ public function isNormalizeDescription(): bool { ' ddd ', ], ), + [], + ], + 'directives (disabled)' => [ + <<<'STRING' + """ + Description + """ + STRING, + new class() extends DefaultSettings { + public function isIncludeDirectivesInDescription(): bool { + return false; + } + }, + 0, + 0, + <<<'STRING' + Description + STRING, + [ + Parser::directive('@a'), + ], + ], + 'directives (enabled)' => [ + <<<'STRING' + """ + Description + + + + @a + @b(test: "abc") + """ + STRING, + new class() extends DefaultSettings { + public function isIncludeDirectivesInDescription(): bool { + return true; + } + }, + 0, + 0, + <<<'STRING' + Description + + + STRING, + [ + Parser::directive('@a'), + Parser::directive('@b(test: "abc")'), + ], + ], + 'directives (enabled) + normalized' => [ + <<<'STRING' + """ + Description + + @a + @b(test: "abc") + """ + STRING, + new class() extends DefaultSettings { + public function isNormalizeDescription(): bool { + return true; + } + + public function isIncludeDirectivesInDescription(): bool { + return true; + } + }, + 0, + 0, + <<<'STRING' + Description + + + STRING, + [ + Parser::directive('@a'), + Parser::directive('@b(test: "abc")'), + ], ], ]; } diff --git a/src/SchemaPrinter/Blocks/Nodes/Directives.php b/src/SchemaPrinter/Blocks/Nodes/Directives.php new file mode 100644 index 00000000..37c0a70b --- /dev/null +++ b/src/SchemaPrinter/Blocks/Nodes/Directives.php @@ -0,0 +1,86 @@ + + */ +class Directives extends BlockList { + /** + * @param Traversable|array $directives + */ + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + Traversable|array|null $directives, + string|null $deprecationReason = null, + ) { + parent::__construct($dispatcher, $settings, $level, $used); + + $deprecated = Directive::DEPRECATED_NAME; + $directives ??= []; + + if ($deprecationReason) { + // todo(graphql): Is there a better way to create directive node? + if ($deprecationReason !== Directive::DEFAULT_DEPRECATION_REASON) { + $reason = json_encode($deprecationReason); + $this[] = $this->block(Parser::directive("@{$deprecated}(reason: {$reason})")); + } else { + $this[] = $this->block(Parser::directive("@{$deprecated}")); + } + } + + foreach ($directives as $directive) { + if ($deprecationReason && $directive->name->value === $deprecated) { + continue; + } + + $this[] = $this->block($directive); + } + } + + protected function isAlwaysMultiline(): bool { + return true; + } + + /** + * @inheritDoc + */ + protected function getBlocks(): array { + $filter = $this->getSettings()->getDirectiveFilter(); + $blocks = parent::getBlocks(); + + if ($filter !== null) { + $blocks = array_filter($blocks, static function (Directive $block) use ($filter): bool { + return $filter->isAllowedDirective($block->getNode()); + }); + } + + return $blocks; + } + + private function block(DirectiveNode $directive): DirectiveBlock { + return new DirectiveBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $directive, + ); + } +} diff --git a/src/SchemaPrinter/Blocks/Nodes/DirectivesTest.php b/src/SchemaPrinter/Blocks/Nodes/DirectivesTest.php new file mode 100644 index 00000000..5dd87496 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Nodes/DirectivesTest.php @@ -0,0 +1,155 @@ + + // ========================================================================= + /** + * @covers ::__toString + * + * @dataProvider dataProviderToString + * + * @param array $directives + */ + public function testToString( + string $expected, + Settings $settings, + int $level, + int $used, + array|null $directives, + string $reason = null, + ): void { + $actual = (string) (new Directives(new Dispatcher(), $settings, $level, $used, $directives, $reason)); + $parsed = Parser::directives($actual); + + self::assertEquals($expected, $actual); + self::assertInstanceOf(NodeList::class, $parsed); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderToString(): array { + return [ + 'null' => [ + '', + new DefaultSettings(), + 0, + 0, + null, + null, + ], + 'empty' => [ + '', + new DefaultSettings(), + 0, + 0, + [], + null, + ], + 'directives' => [ + <<<'STRING' + @b(b: 123) + @a + STRING, + new DefaultSettings(), + 0, + 0, + [ + Parser::directive('@b(b: 123)'), + Parser::directive('@a'), + ], + null, + ], + 'deprecated (default reason)' => [ + <<<'STRING' + @deprecated + STRING, + new DefaultSettings(), + 0, + 0, + null, + Directive::DEFAULT_DEPRECATION_REASON, + ], + 'deprecated (custom reason)' => [ + <<<'STRING' + @deprecated(reason: "reason") + STRING, + new DefaultSettings(), + 0, + 0, + null, + 'reason', + ], + 'directives and deprecated (custom reason)' => [ + <<<'STRING' + @deprecated(reason: "reason") + @b(b: 123) + STRING, + new DefaultSettings(), + 0, + 0, + [ + Parser::directive('@b(b: 123)'), + Parser::directive('@deprecated(reason: "should be ignored")'), + ], + 'reason', + ], + 'line length' => [ + <<<'STRING' + @deprecated( + reason: "very very very long reason" + ) + @a(a: 123) + @b( + b: 1234567890 + ) + STRING, + new DefaultSettings(), + 0, + 70, + [ + Parser::directive('@a(a: 123)'), + Parser::directive('@b(b: 1234567890)'), + ], + 'very very very long reason', + ], + 'indent' => [ + <<<'STRING' + @deprecated( + reason: "very very very long reason" + ) + @a(a: 123) + @b( + b: 1234567890 + ) + STRING, + new DefaultSettings(), + 1, + 70, + [ + Parser::directive('@a(a: 123)'), + Parser::directive('@b(b: 1234567890)'), + ], + 'very very very long reason', + ], + ]; + } + // +} diff --git a/src/SchemaPrinter/Blocks/Nodes/StringBlock.php b/src/SchemaPrinter/Blocks/Nodes/StringBlock.php index be61ebd9..b1d49c86 100644 --- a/src/SchemaPrinter/Blocks/Nodes/StringBlock.php +++ b/src/SchemaPrinter/Blocks/Nodes/StringBlock.php @@ -24,7 +24,6 @@ public function __construct( int $level, int $used, protected string $string, - protected bool $block = false, ) { parent::__construct($dispatcher, $settings, $level, $used); } @@ -34,7 +33,7 @@ protected function getString(): string { } protected function isBlock(): bool { - return $this->block; + return false; } protected function content(): string { From 35acfa9fe1b7b3d8c29b6bd6ef46bdec5ee24455 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Fri, 7 Jan 2022 13:53:57 +0400 Subject: [PATCH 25/90] `NamedBlock` renamed to `Property`. --- src/SchemaPrinter/Blocks/BlockList.php | 17 ++-- src/SchemaPrinter/Blocks/BlockListTest.php | 77 ++++++++++++------- src/SchemaPrinter/Blocks/Named.php | 10 +++ src/SchemaPrinter/Blocks/Nodes/Arguments.php | 15 +++- src/SchemaPrinter/Blocks/Nodes/Directive.php | 11 ++- src/SchemaPrinter/Blocks/Nodes/Directives.php | 17 ++-- .../Blocks/Nodes/DirectivesTest.php | 62 +++++++++++++-- src/SchemaPrinter/Blocks/Nodes/Value.php | 17 ++-- .../Blocks/{NamedBlock.php => Property.php} | 5 +- .../{NamedBlockTest.php => PropertyTest.php} | 30 ++++++-- 10 files changed, 188 insertions(+), 73 deletions(-) create mode 100644 src/SchemaPrinter/Blocks/Named.php rename src/SchemaPrinter/Blocks/{NamedBlock.php => Property.php} (90%) rename src/SchemaPrinter/Blocks/{NamedBlockTest.php => PropertyTest.php} (70%) diff --git a/src/SchemaPrinter/Blocks/BlockList.php b/src/SchemaPrinter/Blocks/BlockList.php index 9bbc6952..3fd41e90 100644 --- a/src/SchemaPrinter/Blocks/BlockList.php +++ b/src/SchemaPrinter/Blocks/BlockList.php @@ -6,11 +6,9 @@ use function count; use function implode; -use function is_numeric; -use function ksort; use function mb_strlen; - -use const SORT_NATURAL; +use function strnatcmp; +use function usort; /** * @internal @@ -68,7 +66,12 @@ protected function getBlocks(): array { $blocks = $this->blocks; if ($this->isNormalized()) { - ksort($blocks, SORT_NATURAL); + usort($blocks, static function (Block $a, Block $b): int { + $aName = $a instanceof Named ? $a->getName() : ''; + $bName = $b instanceof Named ? $b->getName() : ''; + + return strnatcmp($aName, $bName); + }); } return $blocks; @@ -183,10 +186,6 @@ public function offsetGet(mixed $offset): Block { */ public function offsetSet(mixed $offset, mixed $value): void { if ($offset !== null) { - if (!is_numeric($offset)) { - $value = new NamedBlock($this->getDispatcher(), $this->getSettings(), $offset, $value); - } - $this->blocks[$offset] = $value; } else { $this->blocks[] = $value; diff --git a/src/SchemaPrinter/Blocks/BlockListTest.php b/src/SchemaPrinter/Blocks/BlockListTest.php index b840b818..0a3062c7 100644 --- a/src/SchemaPrinter/Blocks/BlockListTest.php +++ b/src/SchemaPrinter/Blocks/BlockListTest.php @@ -246,7 +246,7 @@ public function getIndent(): string { '', '', [ - 'a' => new BlockListTest__Block(false, 'block a'), + new BlockListTest__NamedBlock('a', false, 'block a'), ], ], 'one multi-line block' => [ @@ -261,7 +261,7 @@ public function getIndent(): string { '', '', [ - 'a' => new BlockListTest__Block(true, 'block a'), + new BlockListTest__NamedBlock('a', true, 'block a'), ], ], 'short block list' => [ @@ -276,8 +276,8 @@ public function getIndent(): string { '', '', [ - 'a' => new BlockListTest__Block(false, 'block a'), - 'b' => new BlockListTest__Block(false, 'block b'), + new BlockListTest__NamedBlock('a', false, 'block a'), + new BlockListTest__NamedBlock('b', false, 'block b'), ], ], 'long block list' => [ @@ -297,8 +297,8 @@ public function getLineLength(): int { '', '', [ - 'b' => new BlockListTest__Block(false, 'block b'), - 'a' => new BlockListTest__Block(false, 'block a'), + new BlockListTest__NamedBlock('b', false, 'block b'), + new BlockListTest__NamedBlock('a', false, 'block a'), ], ], 'short block list with multiline block' => [ @@ -315,8 +315,8 @@ public function getLineLength(): int { '', '', [ - 'a' => new BlockListTest__Block(false, 'block a'), - 'b' => new BlockListTest__Block(true, 'block b'), + new BlockListTest__NamedBlock('a', false, 'block a'), + new BlockListTest__NamedBlock('b', true, 'block b'), ], ], 'block list with multiline blocks' => [ @@ -341,13 +341,13 @@ public function getLineLength(): int { '', '', [ - 'a' => new BlockListTest__Block(true, 'block a'), - 'b' => new BlockListTest__Block(false, 'block b'), - 'c' => new BlockListTest__Block(false, 'block c'), - 'd' => new BlockListTest__Block(true, 'block d'), - 'e' => new BlockListTest__Block(false, 'block e'), - 'f' => new BlockListTest__Block(false, 'block f'), - 'g' => new BlockListTest__Block(true, 'block g'), + new BlockListTest__NamedBlock('a', true, 'block a'), + new BlockListTest__NamedBlock('b', false, 'block b'), + new BlockListTest__NamedBlock('c', false, 'block c'), + new BlockListTest__NamedBlock('d', true, 'block d'), + new BlockListTest__NamedBlock('e', false, 'block e'), + new BlockListTest__NamedBlock('f', false, 'block f'), + new BlockListTest__NamedBlock('g', true, 'block g'), ], ], 'block list with multiline blocks without wrap' => [ @@ -364,9 +364,9 @@ public function getLineLength(): int { '', '', [ - 'c' => new BlockListTest__Block(true, 'block c'), - 'b' => new BlockListTest__Block(false, 'block b'), - 'a' => new BlockListTest__Block(true, 'block a'), + new BlockListTest__NamedBlock('c', true, 'block c'), + new BlockListTest__NamedBlock('b', false, 'block b'), + new BlockListTest__NamedBlock('a', true, 'block a'), ], ], 'normalized block list' => [ @@ -381,8 +381,8 @@ public function getLineLength(): int { '', '', [ - 'b' => new BlockListTest__Block(false, 'block b'), - 'a' => new BlockListTest__Block(false, 'block a'), + new BlockListTest__NamedBlock('b', false, 'block b'), + new BlockListTest__NamedBlock('a', false, 'block a'), ], ], 'multi-line with level' => [ @@ -401,7 +401,7 @@ public function getIndent(): string { '', '', [ - 'a' => new BlockListTest__Block(true, 'block a'), + new BlockListTest__NamedBlock('a', true, 'block a'), ], ], ]), @@ -418,7 +418,7 @@ public function getIndent(): string { '[', ']', [ - 'a' => new BlockListTest__Block(false, 'block a'), + new BlockListTest__NamedBlock('a', false, 'block a'), ], ], 'one multi-line block' => [ @@ -439,7 +439,7 @@ public function getIndent(): string { '[', ']', [ - 'a' => new BlockListTest__Block(true, 'block a'), + new BlockListTest__NamedBlock('a', true, 'block a'), ], ], 'short block list' => [ @@ -454,8 +454,8 @@ public function getIndent(): string { '[', ']', [ - 0 => new BlockListTest__Block(false, 'block a'), - 'b' => new BlockListTest__Block(false, 'block b'), + new BlockListTest__Block(false, 'block a'), + new BlockListTest__NamedBlock('b', false, 'block b'), ], ], 'long block list' => [ @@ -481,8 +481,8 @@ public function getIndent(): string { '[', ']', [ - 'b' => new BlockListTest__Block(false, 'block b'), - 'a' => new BlockListTest__Block(false, 'block a'), + new BlockListTest__NamedBlock('b', false, 'block b'), + new BlockListTest__NamedBlock('a', false, 'block a'), ], ], 'short block list with multiline block' => [ @@ -618,3 +618,26 @@ protected function content(): string { return ''; } } + +/** + * @internal + * @noinspection PhpMultipleClassesDeclarationsInOneFile + */ +class BlockListTest__NamedBlock extends Property { + public function __construct( + protected string $name, + bool $multiline, + string $content, + ) { + parent::__construct( + new Dispatcher(), + new DefaultSettings(), + $name, + new BlockListTest__Block($multiline, $content), + ); + } + + public function getName(): string { + return $this->name; + } +} diff --git a/src/SchemaPrinter/Blocks/Named.php b/src/SchemaPrinter/Blocks/Named.php new file mode 100644 index 00000000..de2b0193 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Named.php @@ -0,0 +1,10 @@ +name->value] = new Value( + $name = $argument->name->value; + $this[$name] = new Property( $this->getDispatcher(), $this->getSettings(), - $this->getLevel() + 1, - $this->getUsed(), - $argument->value, + $name, + new Value( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel() + 1, + $this->getUsed(), + $argument->value, + ), ); } } diff --git a/src/SchemaPrinter/Blocks/Nodes/Directive.php b/src/SchemaPrinter/Blocks/Nodes/Directive.php index 0fa76e39..fed514ee 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Directive.php +++ b/src/SchemaPrinter/Blocks/Nodes/Directive.php @@ -6,6 +6,7 @@ use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\DirectiveUsed; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use function mb_strlen; @@ -13,7 +14,7 @@ /** * @internal */ -class Directive extends Block { +class Directive extends Block implements Named { public function __construct( Dispatcher $dispatcher, Settings $settings, @@ -24,14 +25,18 @@ public function __construct( parent::__construct($dispatcher, $settings, $level, $used); } - public function getNode(): DirectiveNode { + public function getName(): string { + return "@{$this->getNode()->name->value}"; + } + + protected function getNode(): DirectiveNode { return $this->node; } protected function content(): string { // Convert $node = $this->getNode(); - $name = "@{$node->name->value}"; + $name = $this->getName(); $used = mb_strlen($name); $args = new Arguments( $this->getDispatcher(), diff --git a/src/SchemaPrinter/Blocks/Nodes/Directives.php b/src/SchemaPrinter/Blocks/Nodes/Directives.php index 37c0a70b..5c7c73dc 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Directives.php +++ b/src/SchemaPrinter/Blocks/Nodes/Directives.php @@ -62,13 +62,18 @@ protected function isAlwaysMultiline(): bool { * @inheritDoc */ protected function getBlocks(): array { - $filter = $this->getSettings()->getDirectiveFilter(); - $blocks = parent::getBlocks(); + $blocks = []; + $enabled = $this->getSettings()->isIncludeDirectives(); - if ($filter !== null) { - $blocks = array_filter($blocks, static function (Directive $block) use ($filter): bool { - return $filter->isAllowedDirective($block->getNode()); - }); + if ($enabled) { + $filter = $this->getSettings()->getDirectiveFilter(); + $blocks = parent::getBlocks(); + + if ($filter !== null) { + $blocks = array_filter($blocks, static function (Directive $block) use ($filter): bool { + return $filter->isAllowedDirective($block->getNode()); + }); + } } return $blocks; diff --git a/src/SchemaPrinter/Blocks/Nodes/DirectivesTest.php b/src/SchemaPrinter/Blocks/Nodes/DirectivesTest.php index 5dd87496..0804f13b 100644 --- a/src/SchemaPrinter/Blocks/Nodes/DirectivesTest.php +++ b/src/SchemaPrinter/Blocks/Nodes/DirectivesTest.php @@ -49,7 +49,11 @@ public function dataProviderToString(): array { return [ 'null' => [ '', - new DefaultSettings(), + new class() extends DefaultSettings { + public function isIncludeDirectives(): bool { + return true; + } + }, 0, 0, null, @@ -57,18 +61,40 @@ public function dataProviderToString(): array { ], 'empty' => [ '', - new DefaultSettings(), + new class() extends DefaultSettings { + public function isIncludeDirectives(): bool { + return true; + } + }, 0, 0, [], null, ], + 'disabled' => [ + '', + new class() extends DefaultSettings { + public function isIncludeDirectives(): bool { + return false; + } + }, + 0, + 0, + [ + Parser::directive('@a'), + ], + null, + ], 'directives' => [ <<<'STRING' @b(b: 123) @a STRING, - new DefaultSettings(), + new class() extends DefaultSettings { + public function isIncludeDirectives(): bool { + return true; + } + }, 0, 0, [ @@ -81,7 +107,11 @@ public function dataProviderToString(): array { <<<'STRING' @deprecated STRING, - new DefaultSettings(), + new class() extends DefaultSettings { + public function isIncludeDirectives(): bool { + return true; + } + }, 0, 0, null, @@ -91,7 +121,11 @@ public function dataProviderToString(): array { <<<'STRING' @deprecated(reason: "reason") STRING, - new DefaultSettings(), + new class() extends DefaultSettings { + public function isIncludeDirectives(): bool { + return true; + } + }, 0, 0, null, @@ -102,7 +136,11 @@ public function dataProviderToString(): array { @deprecated(reason: "reason") @b(b: 123) STRING, - new DefaultSettings(), + new class() extends DefaultSettings { + public function isIncludeDirectives(): bool { + return true; + } + }, 0, 0, [ @@ -121,7 +159,11 @@ public function dataProviderToString(): array { b: 1234567890 ) STRING, - new DefaultSettings(), + new class() extends DefaultSettings { + public function isIncludeDirectives(): bool { + return true; + } + }, 0, 70, [ @@ -140,7 +182,11 @@ public function dataProviderToString(): array { b: 1234567890 ) STRING, - new DefaultSettings(), + new class() extends DefaultSettings { + public function isIncludeDirectives(): bool { + return true; + } + }, 1, 70, [ diff --git a/src/SchemaPrinter/Blocks/Nodes/Value.php b/src/SchemaPrinter/Blocks/Nodes/Value.php index 1fd6a550..54c80cc4 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Value.php +++ b/src/SchemaPrinter/Blocks/Nodes/Value.php @@ -12,6 +12,7 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\ListBlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\ObjectBlockList; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Property; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; class Value extends Block { @@ -45,17 +46,23 @@ protected function content(): string { $content = new ObjectBlockList($dispatcher, $settings, $level, $used); foreach ($this->node->fields as $field) { - $content[$field->name->value] = new Value( + $name = $field->name->value; + $content[$name] = new Property( $dispatcher, $settings, - $level + 1 + (int) ($field->value instanceof StringValueNode), - $used, - $field->value, + $name, + new Value( + $dispatcher, + $settings, + $level + 1 + (int) ($field->value instanceof StringValueNode), + $used, + $field->value, + ), ); } } elseif ($this->node instanceof StringValueNode) { $content = $this->node->block - ? new StringBlock($dispatcher, $settings, $level, 0, $this->node->value, true) + ? new StringBlock($dispatcher, $settings, $level, 0, $this->node->value) : Printer::doPrint($this->node); } else { $content = Printer::doPrint($this->node); diff --git a/src/SchemaPrinter/Blocks/NamedBlock.php b/src/SchemaPrinter/Blocks/Property.php similarity index 90% rename from src/SchemaPrinter/Blocks/NamedBlock.php rename to src/SchemaPrinter/Blocks/Property.php index 176b871a..250686f1 100644 --- a/src/SchemaPrinter/Blocks/NamedBlock.php +++ b/src/SchemaPrinter/Blocks/Property.php @@ -8,13 +8,12 @@ /** * @internal */ -class NamedBlock extends Block { +class Property extends Block implements Named { public function __construct( Dispatcher $dispatcher, Settings $settings, private string $name, private Block $block, - private string $separator = ':', ) { parent::__construct($dispatcher, $settings, $block->getLevel(), $block->getUsed()); } @@ -32,7 +31,7 @@ protected function getBlock(): Block { } protected function getSeparator(): string { - return $this->separator; + return ':'; } protected function content(): string { diff --git a/src/SchemaPrinter/Blocks/NamedBlockTest.php b/src/SchemaPrinter/Blocks/PropertyTest.php similarity index 70% rename from src/SchemaPrinter/Blocks/NamedBlockTest.php rename to src/SchemaPrinter/Blocks/PropertyTest.php index f7eece3b..057a3153 100644 --- a/src/SchemaPrinter/Blocks/NamedBlockTest.php +++ b/src/SchemaPrinter/Blocks/PropertyTest.php @@ -10,9 +10,9 @@ use function mb_strlen; /** - * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\NamedBlock + * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Property */ -class NamedBlockTest extends TestCase { +class PropertyTest extends TestCase { /** * @covers ::__toString * @covers ::getLength @@ -53,7 +53,17 @@ protected function content(): string { return $this->content; } }; - $named = new class($dispatcher, $settings, $name, $block, $separator) extends NamedBlock { + $property = new class($dispatcher, $settings, $name, $block, $separator) extends Property { + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + string $name, + Block $block, + private string $separator, + ) { + parent::__construct($dispatcher, $settings, $name, $block); + } + public function getUsed(): int { return parent::getUsed(); } @@ -61,13 +71,17 @@ public function getUsed(): int { public function getLevel(): int { return parent::getLevel(); } + + protected function getSeparator(): string { + return $this->separator; + } }; $expected = "{$name}{$separator}{$space}{$content}"; - self::assertEquals($used, $named->getUsed()); - self::assertEquals($level, $named->getLevel()); - self::assertEquals($expected, (string) $named); - self::assertEquals(mb_strlen($expected), mb_strlen((string) $named)); - self::assertEquals(mb_strlen($expected), $named->getLength()); + self::assertEquals($used, $property->getUsed()); + self::assertEquals($level, $property->getLevel()); + self::assertEquals($expected, (string) $property); + self::assertEquals(mb_strlen($expected), mb_strlen((string) $property)); + self::assertEquals(mb_strlen($expected), $property->getLength()); } } From d9c9e891af48bd1714cbaeadf0e26763cbc16d01 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Fri, 7 Jan 2022 15:10:05 +0400 Subject: [PATCH 26/90] `ScalarType` support. --- .../Blocks/Nodes/Description.php | 4 +- .../Blocks/Nodes/DescriptionTest.php | 14 +- src/SchemaPrinter/Blocks/Nodes/Directives.php | 17 +- .../Blocks/Nodes/DirectivesTest.php | 62 +------ src/SchemaPrinter/Blocks/Nodes/Scalar.php | 63 ++++++++ src/SchemaPrinter/Blocks/Nodes/ScalarTest.php | 153 ++++++++++++++++++ 6 files changed, 243 insertions(+), 70 deletions(-) create mode 100644 src/SchemaPrinter/Blocks/Nodes/Scalar.php create mode 100644 src/SchemaPrinter/Blocks/Nodes/ScalarTest.php diff --git a/src/SchemaPrinter/Blocks/Nodes/Description.php b/src/SchemaPrinter/Blocks/Nodes/Description.php index 5139190f..df66b630 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Description.php +++ b/src/SchemaPrinter/Blocks/Nodes/Description.php @@ -19,10 +19,10 @@ public function __construct( Settings $settings, int $level, int $used, - string $string, + ?string $string, private ?Directives $directives = null, ) { - parent::__construct($dispatcher, $settings, $level, $used, $string); + parent::__construct($dispatcher, $settings, $level, $used, (string) $string); } protected function getDirectives(): ?Directives { diff --git a/src/SchemaPrinter/Blocks/Nodes/DescriptionTest.php b/src/SchemaPrinter/Blocks/Nodes/DescriptionTest.php index a39d9add..c606dce1 100644 --- a/src/SchemaPrinter/Blocks/Nodes/DescriptionTest.php +++ b/src/SchemaPrinter/Blocks/Nodes/DescriptionTest.php @@ -22,15 +22,15 @@ class DescriptionTest extends TestCase { * * @dataProvider dataProviderToString * - * @param array $directives + * @param array|null $directives */ public function testToString( string $expected, Settings $settings, int $level, int $used, - string $description, - array $directives, + ?string $description, + ?array $directives, ): void { $dispatcher = new Dispatcher(); $directives = new Directives($dispatcher, $settings, $level, $used, $directives); @@ -51,6 +51,14 @@ public function testToString( */ public function dataProviderToString(): array { return [ + 'null' => [ + '', + new DefaultSettings(), + 0, + 0, + null, + null, + ], 'Prints an empty string' => [ '', new class() extends DefaultSettings { diff --git a/src/SchemaPrinter/Blocks/Nodes/Directives.php b/src/SchemaPrinter/Blocks/Nodes/Directives.php index 5c7c73dc..37c0a70b 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Directives.php +++ b/src/SchemaPrinter/Blocks/Nodes/Directives.php @@ -62,18 +62,13 @@ protected function isAlwaysMultiline(): bool { * @inheritDoc */ protected function getBlocks(): array { - $blocks = []; - $enabled = $this->getSettings()->isIncludeDirectives(); + $filter = $this->getSettings()->getDirectiveFilter(); + $blocks = parent::getBlocks(); - if ($enabled) { - $filter = $this->getSettings()->getDirectiveFilter(); - $blocks = parent::getBlocks(); - - if ($filter !== null) { - $blocks = array_filter($blocks, static function (Directive $block) use ($filter): bool { - return $filter->isAllowedDirective($block->getNode()); - }); - } + if ($filter !== null) { + $blocks = array_filter($blocks, static function (Directive $block) use ($filter): bool { + return $filter->isAllowedDirective($block->getNode()); + }); } return $blocks; diff --git a/src/SchemaPrinter/Blocks/Nodes/DirectivesTest.php b/src/SchemaPrinter/Blocks/Nodes/DirectivesTest.php index 0804f13b..5dd87496 100644 --- a/src/SchemaPrinter/Blocks/Nodes/DirectivesTest.php +++ b/src/SchemaPrinter/Blocks/Nodes/DirectivesTest.php @@ -49,11 +49,7 @@ public function dataProviderToString(): array { return [ 'null' => [ '', - new class() extends DefaultSettings { - public function isIncludeDirectives(): bool { - return true; - } - }, + new DefaultSettings(), 0, 0, null, @@ -61,40 +57,18 @@ public function isIncludeDirectives(): bool { ], 'empty' => [ '', - new class() extends DefaultSettings { - public function isIncludeDirectives(): bool { - return true; - } - }, + new DefaultSettings(), 0, 0, [], null, ], - 'disabled' => [ - '', - new class() extends DefaultSettings { - public function isIncludeDirectives(): bool { - return false; - } - }, - 0, - 0, - [ - Parser::directive('@a'), - ], - null, - ], 'directives' => [ <<<'STRING' @b(b: 123) @a STRING, - new class() extends DefaultSettings { - public function isIncludeDirectives(): bool { - return true; - } - }, + new DefaultSettings(), 0, 0, [ @@ -107,11 +81,7 @@ public function isIncludeDirectives(): bool { <<<'STRING' @deprecated STRING, - new class() extends DefaultSettings { - public function isIncludeDirectives(): bool { - return true; - } - }, + new DefaultSettings(), 0, 0, null, @@ -121,11 +91,7 @@ public function isIncludeDirectives(): bool { <<<'STRING' @deprecated(reason: "reason") STRING, - new class() extends DefaultSettings { - public function isIncludeDirectives(): bool { - return true; - } - }, + new DefaultSettings(), 0, 0, null, @@ -136,11 +102,7 @@ public function isIncludeDirectives(): bool { @deprecated(reason: "reason") @b(b: 123) STRING, - new class() extends DefaultSettings { - public function isIncludeDirectives(): bool { - return true; - } - }, + new DefaultSettings(), 0, 0, [ @@ -159,11 +121,7 @@ public function isIncludeDirectives(): bool { b: 1234567890 ) STRING, - new class() extends DefaultSettings { - public function isIncludeDirectives(): bool { - return true; - } - }, + new DefaultSettings(), 0, 70, [ @@ -182,11 +140,7 @@ public function isIncludeDirectives(): bool { b: 1234567890 ) STRING, - new class() extends DefaultSettings { - public function isIncludeDirectives(): bool { - return true; - } - }, + new DefaultSettings(), 1, 70, [ diff --git a/src/SchemaPrinter/Blocks/Nodes/Scalar.php b/src/SchemaPrinter/Blocks/Nodes/Scalar.php new file mode 100644 index 00000000..56e790dd --- /dev/null +++ b/src/SchemaPrinter/Blocks/Nodes/Scalar.php @@ -0,0 +1,63 @@ +scalar; + } + + public function getName(): string { + return "scalar {$this->getScalar()->name}"; + } + + protected function content(): string { + $name = $this->getName(); + $scalar = $this->getScalar(); + $directives = new Directives( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $scalar->astNode?->directives, + ); + $description = new Description( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $scalar->description, + $directives, + ); + + $eol = $this->eol(); + $indent = $this->indent(); + $content = $name; + + if ($description->getLength()) { + $content = "{$indent}{$description}{$eol}{$indent}{$content}"; + } + + if ($directives->getLength() && $this->getSettings()->isIncludeDirectives()) { + $content = "{$content}{$eol}{$directives}"; + } + + return $content; + } +} diff --git a/src/SchemaPrinter/Blocks/Nodes/ScalarTest.php b/src/SchemaPrinter/Blocks/Nodes/ScalarTest.php new file mode 100644 index 00000000..5031ffbd --- /dev/null +++ b/src/SchemaPrinter/Blocks/Nodes/ScalarTest.php @@ -0,0 +1,153 @@ + + // ========================================================================= + /** + * @covers ::__toString + * + * @dataProvider dataProviderToString + */ + public function testToString( + string $expected, + Settings $settings, + int $level, + int $used, + ScalarType $type, + ): void { + $actual = (string) (new Scalar(new Dispatcher(), $settings, $level, $used, $type)); + $parsed = Parser::scalarTypeDefinition($actual); + + self::assertEquals($expected, $actual); + self::assertInstanceOf(ScalarTypeDefinitionNode::class, $parsed); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderToString(): array { + return [ + 'scalar' => [ + <<<'STRING' + scalar Test + STRING, + new DefaultSettings(), + 0, + 0, + new CustomScalarType([ + 'name' => 'Test', + ]), + ], + 'with description and directives' => [ + <<<'STRING' + """ + Description + """ + scalar Test + @a + STRING, + new class() extends DefaultSettings { + public function isIncludeDirectives(): bool { + return true; + } + + public function isIncludeDirectivesInDescription(): bool { + return false; + } + }, + 0, + 0, + new CustomScalarType([ + 'name' => 'Test', + 'description' => 'Description', + 'astNode' => Parser::scalarTypeDefinition( + <<<'STRING' + scalar Test @a + STRING, + ), + ]), + ], + 'with directives in description' => [ + <<<'STRING' + """ + Description + + @a + """ + scalar Test + STRING, + new class() extends DefaultSettings { + public function isIncludeDirectives(): bool { + return false; + } + + public function isIncludeDirectivesInDescription(): bool { + return true; + } + }, + 0, + 0, + new CustomScalarType([ + 'name' => 'Test', + 'description' => 'Description', + 'astNode' => Parser::scalarTypeDefinition( + <<<'STRING' + scalar Test @a + STRING, + ), + ]), + ], + 'indent' => [ + <<<'STRING' + """ + Description + """ + scalar Test + @a( + value: "very very long value" + ) + @b(value: "b") + STRING, + new class() extends DefaultSettings { + public function isIncludeDirectives(): bool { + return true; + } + + public function isIncludeDirectivesInDescription(): bool { + return false; + } + }, + 1, + 60, + new CustomScalarType([ + 'name' => 'Test', + 'description' => 'Description', + 'astNode' => Parser::scalarTypeDefinition( + <<<'STRING' + scalar Test @a(value: "very very long value") @b(value: "b") + STRING, + ), + ]), + ], + ]; + } + // +} From 07be83f173f50166df8d7009ca9d6cbe9580b0a8 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Fri, 7 Jan 2022 21:12:02 +0400 Subject: [PATCH 27/90] `Union` support. --- src/SchemaPrinter/Blocks/BlockList.php | 59 +++-- src/SchemaPrinter/Blocks/BlockListTest.php | 196 ++++++++++++++- .../Blocks/Events/DirectiveUsed.php | 4 +- src/SchemaPrinter/Blocks/Events/TypeUsed.php | 14 ++ src/SchemaPrinter/Blocks/Nodes/Directive.php | 9 +- .../Blocks/Nodes/DirectiveTest.php | 5 +- .../Blocks/Nodes/DirectivesTest.php | 43 ++++ src/SchemaPrinter/Blocks/Nodes/Scalar.php | 55 +---- src/SchemaPrinter/Blocks/Nodes/ScalarTest.php | 28 +++ src/SchemaPrinter/Blocks/Nodes/TypeBlock.php | 77 ++++++ src/SchemaPrinter/Blocks/Nodes/TypeName.php | 41 ++++ .../Blocks/Nodes/TypeNameTest.php | 88 +++++++ src/SchemaPrinter/Blocks/Nodes/Union.php | 49 ++++ .../Blocks/Nodes/UnionMembers.php | 54 ++++ src/SchemaPrinter/Blocks/Nodes/UnionTest.php | 231 ++++++++++++++++++ src/SchemaPrinter/Settings.php | 6 + .../Settings/DefaultSettings.php | 4 + 17 files changed, 887 insertions(+), 76 deletions(-) create mode 100644 src/SchemaPrinter/Blocks/Events/TypeUsed.php create mode 100644 src/SchemaPrinter/Blocks/Nodes/TypeBlock.php create mode 100644 src/SchemaPrinter/Blocks/Nodes/TypeName.php create mode 100644 src/SchemaPrinter/Blocks/Nodes/TypeNameTest.php create mode 100644 src/SchemaPrinter/Blocks/Nodes/Union.php create mode 100644 src/SchemaPrinter/Blocks/Nodes/UnionMembers.php create mode 100644 src/SchemaPrinter/Blocks/Nodes/UnionTest.php diff --git a/src/SchemaPrinter/Blocks/BlockList.php b/src/SchemaPrinter/Blocks/BlockList.php index 3fd41e90..210b7f9a 100644 --- a/src/SchemaPrinter/Blocks/BlockList.php +++ b/src/SchemaPrinter/Blocks/BlockList.php @@ -27,6 +27,12 @@ abstract class BlockList extends Block implements ArrayAccess { private array $multiline = []; private int $length = 0; + // + // ========================================================================= + protected function isBlock(): bool { + return true; + } + protected function isWrapped(): bool { return false; } @@ -48,17 +54,27 @@ protected function getSuffix(): string { } protected function getSeparator(): string { - return ','; + return ",{$this->space()}"; + } + + protected function getMultilineSeparator(): string { + return ''; } protected function getEmptyValue(): string { return ''; } + // + // + // ========================================================================= public function isMultiline(): bool { return count($this->multiline) > 0 || parent::isMultiline(); } + // + // + // ========================================================================= /** * @return array */ @@ -88,24 +104,26 @@ protected function content(): string { } // Join - $eol = ''; - $listPrefix = $this->getPrefix(); - $listSuffix = $this->getSuffix(); - $itemSeparator = "{$this->getSeparator()}{$this->space()}"; - $isMultiline = $this->isMultilineContent( + $eol = ''; + $listPrefix = $this->getPrefix(); + $listSuffix = $this->getSuffix(); + $separator = $this->getSeparator(); + $isMultiline = $this->isMultilineContent( $blocks, $listSuffix, $listPrefix, - $itemSeparator, + $separator, ); if ($isMultiline) { - $eol = $this->eol(); - $last = $count - 1; - $index = 0; - $indent = $this->indent($this->getLevel() + (int) ($listPrefix || $listSuffix)); - $wrapped = $this->isWrapped(); - $previous = false; + $eol = $this->eol(); + $last = $count - 1; + $index = 0; + $indent = $this->indent($this->getLevel() + (int) ($listPrefix || $listSuffix)); + $wrapped = $this->isWrapped(); + $isBlock = $this->isBlock(); + $previous = false; + $separator = $this->getMultilineSeparator(); foreach ($blocks as $block) { $multiline = $wrapped && $block->isMultiline(); @@ -114,7 +132,15 @@ protected function content(): string { $content .= $eol; } - $content .= "{$indent}{$block}"; + if ($isBlock || $index > 0) { + $content .= $indent; + } + + if ($index > 0) { + $content .= $separator; + } + + $content .= "{$block}"; if ($index < $last) { $content .= $eol; @@ -124,7 +150,7 @@ protected function content(): string { $index = $index + 1; } } else { - $content = implode($itemSeparator, $blocks); + $content = implode($separator, $blocks); } // Prefix & Suffix @@ -161,8 +187,9 @@ private function isMultilineContent( return $this->isLineTooLong($length); } + // - // + // // ========================================================================= /** * @param int|string $offset diff --git a/src/SchemaPrinter/Blocks/BlockListTest.php b/src/SchemaPrinter/Blocks/BlockListTest.php index 0a3062c7..3c70f054 100644 --- a/src/SchemaPrinter/Blocks/BlockListTest.php +++ b/src/SchemaPrinter/Blocks/BlockListTest.php @@ -29,10 +29,13 @@ public function testToString( Settings $settings, int $level, int $used, + bool $block, bool $normalized, bool $wrapped, string $prefix, string $suffix, + string $separator, + string $multilineSeparator, array $blocks, ): void { $list = new BlockListTest__BlockList( @@ -40,10 +43,13 @@ public function testToString( $settings, $level, $used, + $block, $normalized, $wrapped, $prefix, $suffix, + $separator, + $multilineSeparator, ); foreach ($blocks as $name => $block) { @@ -69,10 +75,13 @@ public function dataProviderToString(): array { new DefaultSettings(), 0, 0, + true, false, true, '', '', + ', ', + '', [ new BlockListTest__Block(false, 'block a'), ], @@ -84,10 +93,13 @@ public function dataProviderToString(): array { new DefaultSettings(), 0, 0, + true, false, true, '', '', + ', ', + '', [ new BlockListTest__Block(true, 'block a'), ], @@ -99,10 +111,13 @@ public function dataProviderToString(): array { new DefaultSettings(), 0, 0, + true, false, true, '', '', + ', ', + '', [ new BlockListTest__Block(false, 'block a'), new BlockListTest__Block(false, 'block b'), @@ -120,10 +135,13 @@ public function getLineLength(): int { }, 0, 5, + true, false, true, '', '', + ', ', + '', [ new BlockListTest__Block(false, 'block b'), new BlockListTest__Block(false, 'block a'), @@ -138,10 +156,13 @@ public function getLineLength(): int { new DefaultSettings(), 0, 0, + true, false, true, '', '', + ', ', + '', [ new BlockListTest__Block(false, 'block a'), new BlockListTest__Block(true, 'block b'), @@ -164,10 +185,13 @@ public function getLineLength(): int { new DefaultSettings(), 0, 0, + true, false, true, '', '', + ', ', + '', [ new BlockListTest__Block(true, 'block a'), new BlockListTest__Block(false, 'block b'), @@ -187,10 +211,13 @@ public function getLineLength(): int { new DefaultSettings(), 0, 0, + true, false, false, '', '', + ', ', + '', [ new BlockListTest__Block(true, 'block c'), new BlockListTest__Block(false, 'block b'), @@ -206,8 +233,11 @@ public function getLineLength(): int { 0, true, true, + true, '', '', + ', ', + '', [ new BlockListTest__Block(false, 'block b'), new BlockListTest__Block(false, 'block a'), @@ -224,10 +254,13 @@ public function getIndent(): string { }, 2, 0, + true, false, false, '', '', + ', ', + '', [ new BlockListTest__Block(true, 'block a'), ], @@ -241,10 +274,13 @@ public function getIndent(): string { new DefaultSettings(), 0, 0, + true, false, true, '', '', + ', ', + '', [ new BlockListTest__NamedBlock('a', false, 'block a'), ], @@ -256,10 +292,13 @@ public function getIndent(): string { new DefaultSettings(), 0, 0, + true, false, true, '', '', + ', ', + '', [ new BlockListTest__NamedBlock('a', true, 'block a'), ], @@ -271,10 +310,13 @@ public function getIndent(): string { new DefaultSettings(), 0, 0, + true, false, true, '', '', + ', ', + '', [ new BlockListTest__NamedBlock('a', false, 'block a'), new BlockListTest__NamedBlock('b', false, 'block b'), @@ -292,10 +334,13 @@ public function getLineLength(): int { }, 0, 5, + true, false, true, '', '', + ', ', + '', [ new BlockListTest__NamedBlock('b', false, 'block b'), new BlockListTest__NamedBlock('a', false, 'block a'), @@ -310,10 +355,13 @@ public function getLineLength(): int { new DefaultSettings(), 0, 0, + true, false, true, '', '', + ', ', + '', [ new BlockListTest__NamedBlock('a', false, 'block a'), new BlockListTest__NamedBlock('b', true, 'block b'), @@ -336,10 +384,13 @@ public function getLineLength(): int { new DefaultSettings(), 0, 0, + true, false, true, '', '', + ', ', + '', [ new BlockListTest__NamedBlock('a', true, 'block a'), new BlockListTest__NamedBlock('b', false, 'block b'), @@ -359,10 +410,13 @@ public function getLineLength(): int { new DefaultSettings(), 0, 0, + true, false, false, '', '', + ', ', + '', [ new BlockListTest__NamedBlock('c', true, 'block c'), new BlockListTest__NamedBlock('b', false, 'block b'), @@ -378,8 +432,11 @@ public function getLineLength(): int { 0, true, true, + true, '', '', + ', ', + '', [ new BlockListTest__NamedBlock('b', false, 'block b'), new BlockListTest__NamedBlock('a', false, 'block a'), @@ -396,10 +453,13 @@ public function getIndent(): string { }, 2, 0, + true, false, false, '', '', + ', ', + '', [ new BlockListTest__NamedBlock('a', true, 'block a'), ], @@ -413,10 +473,13 @@ public function getIndent(): string { new DefaultSettings(), 0, 0, + true, false, true, '[', ']', + ', ', + '', [ new BlockListTest__NamedBlock('a', false, 'block a'), ], @@ -434,10 +497,13 @@ public function getIndent(): string { }, 0, 0, + true, false, true, '[', ']', + ', ', + '', [ new BlockListTest__NamedBlock('a', true, 'block a'), ], @@ -449,10 +515,13 @@ public function getIndent(): string { new DefaultSettings(), 0, 0, + true, false, true, '[', ']', + ', ', + '', [ new BlockListTest__Block(false, 'block a'), new BlockListTest__NamedBlock('b', false, 'block b'), @@ -476,10 +545,13 @@ public function getIndent(): string { }, 0, 5, + true, false, true, '[', ']', + ', ', + '', [ new BlockListTest__NamedBlock('b', false, 'block b'), new BlockListTest__NamedBlock('a', false, 'block a'), @@ -500,10 +572,13 @@ public function getIndent(): string { }, 0, 0, + true, false, true, '[', ']', + ', ', + '', [ new BlockListTest__Block(false, 'block a'), new BlockListTest__Block(true, 'block b'), @@ -522,10 +597,13 @@ public function getIndent(): string { }, 2, 0, + true, false, false, '[', ']', + ', ', + '', [ new BlockListTest__Block(true, 'block a'), ], @@ -535,13 +613,107 @@ public function getIndent(): string { new DefaultSettings(), 0, 0, + true, false, false, '[', ']', + ', ', + '', [], ], ]), + 'separators' => new ArrayDataProvider([ + 'single-line' => [ + <<<'STRING' + block a | block b + STRING, + new DefaultSettings(), + 0, + 0, + true, + false, + true, + '', + '', + ' | ', + '||', + [ + new BlockListTest__Block(false, 'block a'), + new BlockListTest__Block(false, 'block b'), + ], + ], + 'multiline' => [ + <<<'STRING' + block a + || block b + STRING, + new DefaultSettings(), + 0, + 120, + true, + false, + true, + '', + '', + '|', + '|| ', + [ + new BlockListTest__Block(false, 'block a'), + new BlockListTest__Block(false, 'block b'), + ], + ], + 'multiline and indent' => [ + <<<'STRING' + block a + || block b + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 1, + 120, + true, + false, + true, + '', + '', + '|', + '|| ', + [ + new BlockListTest__Block(false, 'block a'), + new BlockListTest__Block(false, 'block b'), + ], + ], + ]), + 'not a block' => new ArrayDataProvider([ + 'long block list' => [ + <<<'STRING' + block b + block a + STRING, + new class() extends DefaultSettings { + public function getLineLength(): int { + return 20; + } + }, + 1, + 5, + false, + false, + true, + '', + '', + ', ', + '', + [ + new BlockListTest__Block(false, 'block b'), + new BlockListTest__Block(false, 'block a'), + ], + ], + ]) ]))->getData(); } // @@ -560,23 +732,29 @@ public function __construct( Settings $settings, int $level, int $used, - private bool $normalized = false, - private bool $wrapped = false, - private string $prefix = '', - private string $suffix = '', - private string $separator = ',', + private bool $block, + private bool $normalized, + private bool $wrapped, + private string $prefix, + private string $suffix, + private string $separator, + private string $multilineSeparator, ) { parent::__construct($dispatcher, $settings, $level, $used); } - protected function isNormalized(): bool { - return $this->normalized; + protected function isBlock(): bool { + return $this->block; } protected function isWrapped(): bool { return $this->wrapped; } + protected function isNormalized(): bool { + return $this->normalized; + } + protected function getPrefix(): string { return $this->prefix; } @@ -588,6 +766,10 @@ protected function getSuffix(): string { protected function getSeparator(): string { return $this->separator; } + + protected function getMultilineSeparator(): string { + return $this->multilineSeparator; + } } /** diff --git a/src/SchemaPrinter/Blocks/Events/DirectiveUsed.php b/src/SchemaPrinter/Blocks/Events/DirectiveUsed.php index 58eeaec4..18af398a 100644 --- a/src/SchemaPrinter/Blocks/Events/DirectiveUsed.php +++ b/src/SchemaPrinter/Blocks/Events/DirectiveUsed.php @@ -2,14 +2,12 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events; -use GraphQL\Language\AST\DirectiveNode; - /** * @internal */ class DirectiveUsed implements Event { public function __construct( - public DirectiveNode $directive, + public string $name, ) { // empty } diff --git a/src/SchemaPrinter/Blocks/Events/TypeUsed.php b/src/SchemaPrinter/Blocks/Events/TypeUsed.php new file mode 100644 index 00000000..f116296e --- /dev/null +++ b/src/SchemaPrinter/Blocks/Events/TypeUsed.php @@ -0,0 +1,14 @@ +getNode()->name->value}"; + return $this->getNode()->name->value; } protected function getNode(): DirectiveNode { @@ -35,9 +35,10 @@ protected function getNode(): DirectiveNode { protected function content(): string { // Convert + $at = '@'; $node = $this->getNode(); $name = $this->getName(); - $used = mb_strlen($name); + $used = mb_strlen($name) + mb_strlen($at); $args = new Arguments( $this->getDispatcher(), $this->getSettings(), @@ -48,10 +49,10 @@ protected function content(): string { // Event $this->getDispatcher()->notify( - new DirectiveUsed($node), + new DirectiveUsed($name), ); // Return - return "{$name}{$args}"; + return "{$at}{$name}{$args}"; } } diff --git a/src/SchemaPrinter/Blocks/Nodes/DirectiveTest.php b/src/SchemaPrinter/Blocks/Nodes/DirectiveTest.php index 0a06ed6e..e02e9826 100644 --- a/src/SchemaPrinter/Blocks/Nodes/DirectiveTest.php +++ b/src/SchemaPrinter/Blocks/Nodes/DirectiveTest.php @@ -57,9 +57,12 @@ public function testToStringEvent(): void { ->shouldHaveBeenCalled() ->withArgs(static function (Event $event) use ($node): bool { return $event instanceof DirectiveUsed - && $event->directive === $node; + && $event->name === $node->name->value; }) ->once(); + $spy + ->shouldHaveBeenCalled() + ->once(); } // diff --git a/src/SchemaPrinter/Blocks/Nodes/DirectivesTest.php b/src/SchemaPrinter/Blocks/Nodes/DirectivesTest.php index 5dd87496..3867b45c 100644 --- a/src/SchemaPrinter/Blocks/Nodes/DirectivesTest.php +++ b/src/SchemaPrinter/Blocks/Nodes/DirectivesTest.php @@ -2,13 +2,17 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Nodes; +use Closure; use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\AST\NodeList; use GraphQL\Language\Parser; use GraphQL\Type\Definition\Directive; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\DirectiveUsed; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use Mockery; use PHPUnit\Framework\TestCase; /** @@ -38,6 +42,45 @@ public function testToString( self::assertEquals($expected, $actual); self::assertInstanceOf(NodeList::class, $parsed); } + + /** + * @covers ::__toString + */ + public function testToStringEvent(): void { + $a = Parser::directive('@a'); + $b = Parser::directive('@b'); + $spy = Mockery::spy(static fn (Event $event) => null); + $settings = new class() extends DefaultSettings { + public function isIncludeDirectives(): bool { + return true; + } + }; + $dispatcher = new Dispatcher(); + + $dispatcher->attach(Closure::fromCallable($spy)); + + self::assertNotNull( + (string) (new Directives($dispatcher, $settings, 0, 0, [$a, $b])), + ); + + $spy + ->shouldHaveBeenCalled() + ->withArgs(static function (Event $event) use ($a): bool { + return $event instanceof DirectiveUsed + && $event->name === $a->name->value; + }) + ->once(); + $spy + ->shouldHaveBeenCalled() + ->withArgs(static function (Event $event) use ($b): bool { + return $event instanceof DirectiveUsed + && $event->name === $b->name->value; + }) + ->once(); + $spy + ->shouldHaveBeenCalled() + ->twice(); + } // // diff --git a/src/SchemaPrinter/Blocks/Nodes/Scalar.php b/src/SchemaPrinter/Blocks/Nodes/Scalar.php index 56e790dd..292eba5a 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Scalar.php +++ b/src/SchemaPrinter/Blocks/Nodes/Scalar.php @@ -4,60 +4,25 @@ use GraphQL\Type\Definition\ScalarType; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -class Scalar extends Block implements Named { +/** + * @internal + * + * @extends TypeBlock + */ +class Scalar extends TypeBlock { public function __construct( Dispatcher $dispatcher, Settings $settings, int $level, int $used, - private ScalarType $scalar, + ScalarType $type, ) { - parent::__construct($dispatcher, $settings, $level, $used); + parent::__construct($dispatcher, $settings, $level, $used, $type); } - protected function getScalar(): ScalarType { - return $this->scalar; - } - - public function getName(): string { - return "scalar {$this->getScalar()->name}"; - } - - protected function content(): string { - $name = $this->getName(); - $scalar = $this->getScalar(); - $directives = new Directives( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel(), - $this->getUsed(), - $scalar->astNode?->directives, - ); - $description = new Description( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel(), - $this->getUsed(), - $scalar->description, - $directives, - ); - - $eol = $this->eol(); - $indent = $this->indent(); - $content = $name; - - if ($description->getLength()) { - $content = "{$indent}{$description}{$eol}{$indent}{$content}"; - } - - if ($directives->getLength() && $this->getSettings()->isIncludeDirectives()) { - $content = "{$content}{$eol}{$directives}"; - } - - return $content; + protected function body(int $used): string { + return "scalar{$this->space()}{$this->getName()}"; } } diff --git a/src/SchemaPrinter/Blocks/Nodes/ScalarTest.php b/src/SchemaPrinter/Blocks/Nodes/ScalarTest.php index 5031ffbd..79f49f9c 100644 --- a/src/SchemaPrinter/Blocks/Nodes/ScalarTest.php +++ b/src/SchemaPrinter/Blocks/Nodes/ScalarTest.php @@ -147,6 +147,34 @@ public function isIncludeDirectivesInDescription(): bool { ), ]), ], + 'indent + no description' => [ + <<<'STRING' + scalar Test + @a( + value: "very very long value" + ) + @b(value: "b") + STRING, + new class() extends DefaultSettings { + public function isIncludeDirectives(): bool { + return true; + } + + public function isIncludeDirectivesInDescription(): bool { + return false; + } + }, + 1, + 60, + new CustomScalarType([ + 'name' => 'Test', + 'astNode' => Parser::scalarTypeDefinition( + <<<'STRING' + scalar Test @a(value: "very very long value") @b(value: "b") + STRING, + ), + ]), + ], ]; } // diff --git a/src/SchemaPrinter/Blocks/Nodes/TypeBlock.php b/src/SchemaPrinter/Blocks/Nodes/TypeBlock.php new file mode 100644 index 00000000..fa4dafa4 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Nodes/TypeBlock.php @@ -0,0 +1,77 @@ +getType()->name; + } + + /** + * @return TType + */ + protected function getType(): Type { + return $this->type; + } + + protected function content(): string { + $type = $this->getType(); + $directives = new Directives( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $type->astNode?->directives, + ); + $description = new Description( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $type->description, + $directives, + ); + + $eol = $this->eol(); + $indent = $this->indent(); + $content = $this->body($this->getUsed() + mb_strlen($indent)); + + if ($description->getLength()) { + $content = "{$description}{$eol}{$indent}{$content}"; + } + + if ($directives->getLength() && $this->getSettings()->isIncludeDirectives()) { + $content = "{$content}{$eol}{$directives}"; + } + + return "{$indent}{$content}"; + } + + abstract protected function body(int $used): string; +} diff --git a/src/SchemaPrinter/Blocks/Nodes/TypeName.php b/src/SchemaPrinter/Blocks/Nodes/TypeName.php new file mode 100644 index 00000000..f8eb40a5 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Nodes/TypeName.php @@ -0,0 +1,41 @@ +getType()->name; + } + + protected function getType(): ObjectType { + return $this->type; + } + + protected function content(): string { + $this->getDispatcher()->notify( + new TypeUsed($this->getName()), + ); + + return $this->getName(); + } +} diff --git a/src/SchemaPrinter/Blocks/Nodes/TypeNameTest.php b/src/SchemaPrinter/Blocks/Nodes/TypeNameTest.php new file mode 100644 index 00000000..32298969 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Nodes/TypeNameTest.php @@ -0,0 +1,88 @@ + + // ========================================================================= + /** + * @covers ::__toString + * + * @dataProvider dataProviderToString + */ + public function testToString( + string $expected, + Settings $settings, + int $level, + int $used, + ObjectType $type, + ): void { + $actual = (string) (new TypeName(new Dispatcher(), $settings, $level, $used, $type)); + + self::assertEquals($expected, $actual); + } + + /** + * @covers ::__toString + */ + public function testToStringEvent(): void { + $spy = Mockery::spy(static fn (Event $event) => null); + $node = new ObjectType([ + 'name' => 'Test', + ]); + $settings = new DefaultSettings(); + $dispatcher = new Dispatcher(); + + $dispatcher->attach(Closure::fromCallable($spy)); + + self::assertNotNull( + (string) (new TypeName($dispatcher, $settings, 0, 0, $node)), + ); + + $spy + ->shouldHaveBeenCalled() + ->withArgs(static function (Event $event) use ($node): bool { + return $event instanceof TypeUsed + && $event->name === $node->name; + }) + ->once(); + $spy + ->shouldHaveBeenCalled() + ->once(); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderToString(): array { + return [ + 'object' => [ + 'Test', + new DefaultSettings(), + 0, + 0, + new ObjectType([ + 'name' => 'Test', + ]), + ], + ]; + } + // +} diff --git a/src/SchemaPrinter/Blocks/Nodes/Union.php b/src/SchemaPrinter/Blocks/Nodes/Union.php new file mode 100644 index 00000000..6b36adc6 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Nodes/Union.php @@ -0,0 +1,49 @@ + + */ +class Union extends TypeBlock { + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + UnionType $type, + ) { + parent::__construct($dispatcher, $settings, $level, $used, $type); + } + + protected function body(int $used): string { + $indent = $this->indent(); + $space = $this->space(); + $equal = "{$space}={$space}"; + $body = "union{$space}{$this->getName()}"; + $types = new UnionMembers( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel() + 1, + $used + mb_strlen($body) + mb_strlen($equal), + $this->getType()->getTypes(), + ); + + if ($types->isMultiline()) { + $eol = $this->eol(); + $body = "{$body}{$eol}{$indent}{$this->indent(1)}={$space}{$types}"; + } else { + $body = "{$body}{$equal}{$types}"; + } + + return $body; + } +} diff --git a/src/SchemaPrinter/Blocks/Nodes/UnionMembers.php b/src/SchemaPrinter/Blocks/Nodes/UnionMembers.php new file mode 100644 index 00000000..8273483d --- /dev/null +++ b/src/SchemaPrinter/Blocks/Nodes/UnionMembers.php @@ -0,0 +1,54 @@ + + */ +class UnionMembers extends BlockList { + /** + * @param Traversable|array $types + */ + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + Traversable|array $types, + ) { + parent::__construct($dispatcher, $settings, $level, $used); + + foreach ($types as $type) { + $this[$type->name] = new TypeName( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel() + 1, + $this->getUsed(), + $type, + ); + } + } + + protected function getSeparator(): string { + return "{$this->space()}|{$this->space()}"; + } + + protected function getMultilineSeparator(): string { + return "|{$this->space()}"; + } + + protected function isNormalized(): bool { + return $this->getSettings()->isNormalizeUnions(); + } + + protected function isBlock(): bool { + return false; + } +} diff --git a/src/SchemaPrinter/Blocks/Nodes/UnionTest.php b/src/SchemaPrinter/Blocks/Nodes/UnionTest.php new file mode 100644 index 00000000..20d113b4 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Nodes/UnionTest.php @@ -0,0 +1,231 @@ + + // ========================================================================= + /** + * @covers ::__toString + * + * @dataProvider dataProviderToString + */ + public function testToString( + string $expected, + Settings $settings, + int $level, + int $used, + UnionType $type, + ): void { + $actual = (string) (new Union(new Dispatcher(), $settings, $level, $used, $type)); + $parsed = Parser::unionTypeDefinition($actual); + + self::assertEquals($expected, $actual); + self::assertInstanceOf(UnionTypeDefinitionNode::class, $parsed); + } + + /** + * @covers ::__toString + */ + public function testToStringEvent(): void { + $spy = Mockery::spy(static fn (Event $event) => null); + $union = new UnionType([ + 'name' => 'Test', + 'types' => [ + new ObjectType([ + 'name' => 'A', + ]), + new ObjectType([ + 'name' => 'B', + ]), + ], + ]); + $settings = new DefaultSettings(); + $dispatcher = new Dispatcher(); + + $dispatcher->attach(Closure::fromCallable($spy)); + + self::assertNotNull( + (string) (new Union($dispatcher, $settings, 0, 0, $union)), + ); + + $spy + ->shouldHaveBeenCalled() + ->withArgs(static function (Event $event): bool { + return $event instanceof TypeUsed + && $event->name === 'A'; + }) + ->once(); + $spy + ->shouldHaveBeenCalled() + ->withArgs(static function (Event $event): bool { + return $event instanceof TypeUsed + && $event->name === 'B'; + }) + ->once(); + $spy + ->shouldHaveBeenCalled() + ->twice(); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderToString(): array { + return [ + 'single-line' => [ + <<<'STRING' + union Test = C | B | A + STRING, + new DefaultSettings(), + 0, + 0, + new UnionType([ + 'name' => 'Test', + 'types' => [ + new ObjectType([ + 'name' => 'C', + ]), + new ObjectType([ + 'name' => 'B', + ]), + new ObjectType([ + 'name' => 'A', + ]), + ], + ]), + ], + 'multiline' => [ + <<<'STRING' + union Test + = C + | B + | A + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 120, + new UnionType([ + 'name' => 'Test', + 'types' => [ + new ObjectType([ + 'name' => 'C', + ]), + new ObjectType([ + 'name' => 'B', + ]), + new ObjectType([ + 'name' => 'A', + ]), + ], + ]), + ], + 'indent single-line' => [ + <<<'STRING' + union Test = C | B | A + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 1, + 0, + new UnionType([ + 'name' => 'Test', + 'types' => [ + new ObjectType([ + 'name' => 'C', + ]), + new ObjectType([ + 'name' => 'B', + ]), + new ObjectType([ + 'name' => 'A', + ]), + ], + ]), + ], + 'indent multiline' => [ + <<<'STRING' + union Test + = C + | B + | A + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 1, + 120, + new UnionType([ + 'name' => 'Test', + 'types' => [ + new ObjectType([ + 'name' => 'C', + ]), + new ObjectType([ + 'name' => 'B', + ]), + new ObjectType([ + 'name' => 'A', + ]), + ], + ]), + ], + 'multiline normalized' => [ + <<<'STRING' + union Test = A | B | C + STRING, + new class() extends DefaultSettings { + public function isNormalizeUnions(): bool { + return true; + } + }, + 0, + 0, + new UnionType([ + 'name' => 'Test', + 'types' => [ + new ObjectType([ + 'name' => 'C', + ]), + new ObjectType([ + 'name' => 'B', + ]), + new ObjectType([ + 'name' => 'A', + ]), + ], + ]), + ], + ]; + } + // +} diff --git a/src/SchemaPrinter/Settings.php b/src/SchemaPrinter/Settings.php index 3b8db794..a8d9e4cf 100644 --- a/src/SchemaPrinter/Settings.php +++ b/src/SchemaPrinter/Settings.php @@ -33,6 +33,12 @@ public function isIncludeUnusedDirectiveDefinitions(): bool; */ public function isNormalizeTypes(): bool; + /** + * If `false` members will be printed in the original order if `true` they + * will be sorted by name. + */ + public function isNormalizeUnions(): bool; + /** * If `false` fields will be printed in the original order if `true` they * will be sorted by name. diff --git a/src/SchemaPrinter/Settings/DefaultSettings.php b/src/SchemaPrinter/Settings/DefaultSettings.php index 5c388a0d..2c0d8dae 100644 --- a/src/SchemaPrinter/Settings/DefaultSettings.php +++ b/src/SchemaPrinter/Settings/DefaultSettings.php @@ -51,6 +51,10 @@ public function isNormalizeTypes(): bool { return false; } + public function isNormalizeUnions(): bool { + return false; + } + public function isNormalizeFields(): bool { return false; } From 6bfe7c12ea7870ff90a52ddd568d601409a0acdf Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 8 Jan 2022 09:47:19 +0400 Subject: [PATCH 28/90] `UnionMembers` renamed to `UnionTypes`. --- src/SchemaPrinter/Blocks/Nodes/Union.php | 2 +- src/SchemaPrinter/Blocks/Nodes/UnionTest.php | 1 + src/SchemaPrinter/Blocks/Nodes/UnionTypes.php | 54 +++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/SchemaPrinter/Blocks/Nodes/UnionTypes.php diff --git a/src/SchemaPrinter/Blocks/Nodes/Union.php b/src/SchemaPrinter/Blocks/Nodes/Union.php index 6b36adc6..8d44fbbf 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Union.php +++ b/src/SchemaPrinter/Blocks/Nodes/Union.php @@ -29,7 +29,7 @@ protected function body(int $used): string { $space = $this->space(); $equal = "{$space}={$space}"; $body = "union{$space}{$this->getName()}"; - $types = new UnionMembers( + $types = new UnionTypes( $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, diff --git a/src/SchemaPrinter/Blocks/Nodes/UnionTest.php b/src/SchemaPrinter/Blocks/Nodes/UnionTest.php index 20d113b4..03e7880b 100644 --- a/src/SchemaPrinter/Blocks/Nodes/UnionTest.php +++ b/src/SchemaPrinter/Blocks/Nodes/UnionTest.php @@ -24,6 +24,7 @@ class UnionTest extends TestCase { // ========================================================================= /** * @covers ::__toString + * @covers \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Nodes\UnionTypes::__toString * * @dataProvider dataProviderToString */ diff --git a/src/SchemaPrinter/Blocks/Nodes/UnionTypes.php b/src/SchemaPrinter/Blocks/Nodes/UnionTypes.php new file mode 100644 index 00000000..10977b9a --- /dev/null +++ b/src/SchemaPrinter/Blocks/Nodes/UnionTypes.php @@ -0,0 +1,54 @@ + + */ +class UnionTypes extends BlockList { + /** + * @param Traversable|array $types + */ + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + Traversable|array $types, + ) { + parent::__construct($dispatcher, $settings, $level, $used); + + foreach ($types as $type) { + $this[$type->name] = new TypeName( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel() + 1, + $this->getUsed(), + $type, + ); + } + } + + protected function getSeparator(): string { + return "{$this->space()}|{$this->space()}"; + } + + protected function getMultilineSeparator(): string { + return "|{$this->space()}"; + } + + protected function isNormalized(): bool { + return $this->getSettings()->isNormalizeUnions(); + } + + protected function isBlock(): bool { + return false; + } +} From 7402facad40851a7d9d7045e4f09a28da98cbf5e Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 8 Jan 2022 10:41:18 +0400 Subject: [PATCH 29/90] `Enum` support. --- src/SchemaPrinter/Blocks/ListBlockList.php | 5 + src/SchemaPrinter/Blocks/Nodes/Enum.php | 40 +++++ src/SchemaPrinter/Blocks/Nodes/EnumTest.php | 160 ++++++++++++++++++ src/SchemaPrinter/Blocks/Nodes/EnumValue.php | 31 ++++ .../Blocks/Nodes/EnumValueTest.php | 104 ++++++++++++ .../{UnionMembers.php => EnumValues.php} | 32 ++-- src/SchemaPrinter/Blocks/Nodes/TypeBlock.php | 28 ++- src/SchemaPrinter/Blocks/ObjectBlockList.php | 5 + src/SchemaPrinter/Settings.php | 6 + .../Settings/DefaultSettings.php | 4 + 10 files changed, 389 insertions(+), 26 deletions(-) create mode 100644 src/SchemaPrinter/Blocks/Nodes/Enum.php create mode 100644 src/SchemaPrinter/Blocks/Nodes/EnumTest.php create mode 100644 src/SchemaPrinter/Blocks/Nodes/EnumValue.php create mode 100644 src/SchemaPrinter/Blocks/Nodes/EnumValueTest.php rename src/SchemaPrinter/Blocks/Nodes/{UnionMembers.php => EnumValues.php} (50%) diff --git a/src/SchemaPrinter/Blocks/ListBlockList.php b/src/SchemaPrinter/Blocks/ListBlockList.php index c7695c9c..6f3bf336 100644 --- a/src/SchemaPrinter/Blocks/ListBlockList.php +++ b/src/SchemaPrinter/Blocks/ListBlockList.php @@ -2,6 +2,11 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; +/** + * @internal + * @template TBlock of Block + * @extends BlockList + */ class ListBlockList extends BlockList { protected function getPrefix(): string { return '['; diff --git a/src/SchemaPrinter/Blocks/Nodes/Enum.php b/src/SchemaPrinter/Blocks/Nodes/Enum.php new file mode 100644 index 00000000..fe4d8f83 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Nodes/Enum.php @@ -0,0 +1,40 @@ + + */ +class Enum extends TypeBlock { + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + EnumType $type, + ) { + parent::__construct($dispatcher, $settings, $level, $used, $type); + } + + protected function body(int $used): string { + $space = $this->space(); + $body = "enum{$space}{$this->getName()}{$space}"; + $values = new EnumValues( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $used + mb_strlen($body), + $this->getType()->getValues(), + ); + + return "{$body}{$values}"; + } +} diff --git a/src/SchemaPrinter/Blocks/Nodes/EnumTest.php b/src/SchemaPrinter/Blocks/Nodes/EnumTest.php new file mode 100644 index 00000000..15f664d6 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Nodes/EnumTest.php @@ -0,0 +1,160 @@ + + // ========================================================================= + /** + * @covers ::__toString + * @covers \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Nodes\EnumValues::__toString + * + * @dataProvider dataProviderToString + */ + public function testToString( + string $expected, + Settings $settings, + int $level, + int $used, + Closure|EnumType $type, + ): void { + if ($type instanceof Closure) { + $type = $type(); + } + + $actual = (string) (new Enum(new Dispatcher(), $settings, $level, $used, $type)); + $parsed = Parser::enumTypeDefinition($actual); + + self::assertEquals($expected, $actual); + self::assertInstanceOf(EnumTypeDefinitionNode::class, $parsed); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderToString(): array { + return [ + 'enum' => [ + <<<'STRING' + enum Test { + C + B + A + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + new EnumType([ + 'name' => 'Test', + 'values' => ['C', 'B', 'A'], + ]), + ], + 'indent' => [ + <<<'STRING' + enum Test { + C + B + A + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 1, + 0, + new EnumType([ + 'name' => 'Test', + 'values' => ['C', 'B', 'A'], + ]), + ], + 'normalized' => [ + <<<'STRING' + enum Test { + A + B + C + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + + public function isNormalizeEnums(): bool { + return true; + } + }, + 0, + 0, + new EnumType([ + 'name' => 'Test', + 'values' => ['C', 'B', 'A'], + ]), + ], + 'directives and description' => [ + <<<'STRING' + enum Test { + C + + """ + Description + """ + B + @b + @a + + A + @deprecated + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + static function (): EnumType { + $enum = new EnumType([ + 'name' => 'Test', + 'values' => ['C', 'B', 'A'], + ]); + + $a = $enum->getValue('A'); + $a->deprecationReason = Directive::DEFAULT_DEPRECATION_REASON; + + $b = $enum->getValue('B'); + $b->astNode = Parser::enumValueDefinition('A @b @a'); + $b->description = 'Description'; + + return $enum; + }, + ], + ]; + } + // +} diff --git a/src/SchemaPrinter/Blocks/Nodes/EnumValue.php b/src/SchemaPrinter/Blocks/Nodes/EnumValue.php new file mode 100644 index 00000000..c985cf26 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Nodes/EnumValue.php @@ -0,0 +1,31 @@ + + */ +class EnumValue extends TypeBlock { + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + EnumValueDefinition $value, + ) { + parent::__construct($dispatcher, $settings, $level, $used, $value); + } + + protected function isBlock(): bool { + return false; + } + + protected function body(int $used): string { + return $this->getName(); + } +} diff --git a/src/SchemaPrinter/Blocks/Nodes/EnumValueTest.php b/src/SchemaPrinter/Blocks/Nodes/EnumValueTest.php new file mode 100644 index 00000000..fbdb0713 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Nodes/EnumValueTest.php @@ -0,0 +1,104 @@ + + // ========================================================================= + /** + * @covers ::__toString + * + * @dataProvider dataProviderToString + */ + public function testToString( + string $expected, + Settings $settings, + int $level, + int $used, + EnumValueDefinition $type, + ): void { + $actual = (string) (new EnumValue(new Dispatcher(), $settings, $level, $used, $type)); + $parsed = Parser::enumValueDefinition($actual); + + self::assertEquals($expected, $actual); + self::assertInstanceOf(EnumValueDefinitionNode::class, $parsed); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderToString(): array { + return [ + 'value' => [ + <<<'STRING' + A + STRING, + new DefaultSettings(), + 0, + 0, + new EnumValueDefinition([ + 'name' => 'A', + 'value' => 'A', + ]), + ], + 'indent' => [ + <<<'STRING' + A + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 1, + 0, + new EnumValueDefinition([ + 'name' => 'A', + 'value' => 'A', + ]), + ], + 'description and directives' => [ + <<<'STRING' + """ + Description + """ + A + @a + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + + public function isIncludeDirectives(): bool { + return true; + } + }, + 0, + 0, + new EnumValueDefinition([ + 'name' => 'A', + 'value' => 'A', + 'astNode' => Parser::enumValueDefinition('A @a'), + 'description' => 'Description', + ]), + ], + ]; + } + // +} diff --git a/src/SchemaPrinter/Blocks/Nodes/UnionMembers.php b/src/SchemaPrinter/Blocks/Nodes/EnumValues.php similarity index 50% rename from src/SchemaPrinter/Blocks/Nodes/UnionMembers.php rename to src/SchemaPrinter/Blocks/Nodes/EnumValues.php index 8273483d..74baad3f 100644 --- a/src/SchemaPrinter/Blocks/Nodes/UnionMembers.php +++ b/src/SchemaPrinter/Blocks/Nodes/EnumValues.php @@ -2,53 +2,49 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Nodes; -use GraphQL\Type\Definition\ObjectType; +use GraphQL\Type\Definition\EnumValueDefinition; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\ObjectBlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use Traversable; /** * @internal - * @extends BlockList + * @extends ObjectBlockList */ -class UnionMembers extends BlockList { +class EnumValues extends ObjectBlockList { /** - * @param Traversable|array $types + * @param Traversable|array $values */ public function __construct( Dispatcher $dispatcher, Settings $settings, int $level, int $used, - Traversable|array $types, + Traversable|array $values, ) { parent::__construct($dispatcher, $settings, $level, $used); - foreach ($types as $type) { - $this[$type->name] = new TypeName( + foreach ($values as $value) { + $this[$value->name] = new EnumValue( $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, $this->getUsed(), - $type, + $value, ); } } - protected function getSeparator(): string { - return "{$this->space()}|{$this->space()}"; - } - - protected function getMultilineSeparator(): string { - return "|{$this->space()}"; + protected function isWrapped(): bool { + return true; } protected function isNormalized(): bool { - return $this->getSettings()->isNormalizeUnions(); + return $this->getSettings()->isNormalizeEnums(); } - protected function isBlock(): bool { - return false; + protected function isAlwaysMultiline(): bool { + return true; } } diff --git a/src/SchemaPrinter/Blocks/Nodes/TypeBlock.php b/src/SchemaPrinter/Blocks/Nodes/TypeBlock.php index fa4dafa4..9cc95caf 100644 --- a/src/SchemaPrinter/Blocks/Nodes/TypeBlock.php +++ b/src/SchemaPrinter/Blocks/Nodes/TypeBlock.php @@ -2,6 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Nodes; +use GraphQL\Type\Definition\EnumValueDefinition; use GraphQL\Type\Definition\Type; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; @@ -13,7 +14,7 @@ /** * @internal * - * @template TType of Type + * @template TType of Type|EnumValueDefinition */ abstract class TypeBlock extends Block implements Named { /** @@ -24,7 +25,7 @@ public function __construct( Settings $settings, int $level, int $used, - private Type $type, + private Type|EnumValueDefinition $type, ) { parent::__construct($dispatcher, $settings, $level, $used); } @@ -33,10 +34,14 @@ public function getName(): string { return $this->getType()->name; } + protected function isBlock(): bool { + return true; + } + /** * @return TType */ - protected function getType(): Type { + protected function getType(): Type|EnumValueDefinition { return $this->type; } @@ -47,7 +52,8 @@ protected function content(): string { $this->getSettings(), $this->getLevel(), $this->getUsed(), - $type->astNode?->directives, + $type->astNode->directives ?? null, + $type->deprecationReason ?? null, ); $description = new Description( $this->getDispatcher(), @@ -60,17 +66,23 @@ protected function content(): string { $eol = $this->eol(); $indent = $this->indent(); - $content = $this->body($this->getUsed() + mb_strlen($indent)); + $content = ''; + + if ($this->isBlock()) { + $content .= $indent; + } + + $body = $this->body($this->getUsed() + mb_strlen($content)); if ($description->getLength()) { - $content = "{$description}{$eol}{$indent}{$content}"; + $body = "{$description}{$eol}{$indent}{$body}"; } if ($directives->getLength() && $this->getSettings()->isIncludeDirectives()) { - $content = "{$content}{$eol}{$directives}"; + $body = "{$body}{$eol}{$directives}"; } - return "{$indent}{$content}"; + return "{$content}{$body}"; } abstract protected function body(int $used): string; diff --git a/src/SchemaPrinter/Blocks/ObjectBlockList.php b/src/SchemaPrinter/Blocks/ObjectBlockList.php index ab244437..4d36047e 100644 --- a/src/SchemaPrinter/Blocks/ObjectBlockList.php +++ b/src/SchemaPrinter/Blocks/ObjectBlockList.php @@ -2,6 +2,11 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; +/** + * @internal + * @template TBlock of Block + * @extends BlockList + */ class ObjectBlockList extends BlockList { protected function getPrefix(): string { return '{'; diff --git a/src/SchemaPrinter/Settings.php b/src/SchemaPrinter/Settings.php index a8d9e4cf..dac0cb39 100644 --- a/src/SchemaPrinter/Settings.php +++ b/src/SchemaPrinter/Settings.php @@ -39,6 +39,12 @@ public function isNormalizeTypes(): bool; */ public function isNormalizeUnions(): bool; + /** + * If `false` values will be printed in the original order if `true` they + * will be sorted by name. + */ + public function isNormalizeEnums(): bool; + /** * If `false` fields will be printed in the original order if `true` they * will be sorted by name. diff --git a/src/SchemaPrinter/Settings/DefaultSettings.php b/src/SchemaPrinter/Settings/DefaultSettings.php index 2c0d8dae..81310def 100644 --- a/src/SchemaPrinter/Settings/DefaultSettings.php +++ b/src/SchemaPrinter/Settings/DefaultSettings.php @@ -55,6 +55,10 @@ public function isNormalizeUnions(): bool { return false; } + public function isNormalizeEnums(): bool { + return false; + } + public function isNormalizeFields(): bool { return false; } From b9e18b53fd4bd423aaa51f9e10d892a60ba7382c Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 8 Jan 2022 10:50:28 +0400 Subject: [PATCH 30/90] Blocks split into AST and Types. --- src/SchemaPrinter/Blocks/{Nodes => Ast}/Arguments.php | 2 +- src/SchemaPrinter/Blocks/{Nodes => Ast}/Directive.php | 2 +- src/SchemaPrinter/Blocks/{Nodes => Ast}/DirectiveTest.php | 4 ++-- src/SchemaPrinter/Blocks/{Nodes => Ast}/Directives.php | 4 ++-- src/SchemaPrinter/Blocks/{Nodes => Ast}/DirectivesTest.php | 4 ++-- src/SchemaPrinter/Blocks/{Nodes => Ast}/Value.php | 3 ++- src/SchemaPrinter/Blocks/{Nodes => Ast}/ValueTest.php | 4 ++-- src/SchemaPrinter/Blocks/{Nodes => Types}/Description.php | 3 ++- .../Blocks/{Nodes => Types}/DescriptionTest.php | 5 +++-- src/SchemaPrinter/Blocks/{Nodes => Types}/Enum.php | 2 +- src/SchemaPrinter/Blocks/{Nodes => Types}/EnumTest.php | 6 +++--- src/SchemaPrinter/Blocks/{Nodes => Types}/EnumValue.php | 2 +- src/SchemaPrinter/Blocks/{Nodes => Types}/EnumValueTest.php | 4 ++-- src/SchemaPrinter/Blocks/{Nodes => Types}/EnumValues.php | 2 +- src/SchemaPrinter/Blocks/{Nodes => Types}/Scalar.php | 2 +- src/SchemaPrinter/Blocks/{Nodes => Types}/ScalarTest.php | 4 ++-- src/SchemaPrinter/Blocks/{Nodes => Types}/StringBlock.php | 2 +- .../Blocks/{Nodes => Types}/StringBlockTest.php | 4 ++-- src/SchemaPrinter/Blocks/{Nodes => Types}/TypeBlock.php | 3 ++- src/SchemaPrinter/Blocks/{Nodes => Types}/TypeName.php | 2 +- src/SchemaPrinter/Blocks/{Nodes => Types}/TypeNameTest.php | 4 ++-- src/SchemaPrinter/Blocks/{Nodes => Types}/Union.php | 2 +- src/SchemaPrinter/Blocks/{Nodes => Types}/UnionTest.php | 6 +++--- src/SchemaPrinter/Blocks/{Nodes => Types}/UnionTypes.php | 2 +- 24 files changed, 41 insertions(+), 37 deletions(-) rename src/SchemaPrinter/Blocks/{Nodes => Ast}/Arguments.php (95%) rename src/SchemaPrinter/Blocks/{Nodes => Ast}/Directive.php (95%) rename src/SchemaPrinter/Blocks/{Nodes => Ast}/DirectiveTest.php (97%) rename src/SchemaPrinter/Blocks/{Nodes => Ast}/Directives.php (93%) rename src/SchemaPrinter/Blocks/{Nodes => Ast}/DirectivesTest.php (98%) rename src/SchemaPrinter/Blocks/{Nodes => Ast}/Value.php (94%) rename src/SchemaPrinter/Blocks/{Nodes => Ast}/ValueTest.php (98%) rename src/SchemaPrinter/Blocks/{Nodes => Types}/Description.php (93%) rename src/SchemaPrinter/Blocks/{Nodes => Types}/DescriptionTest.php (98%) rename src/SchemaPrinter/Blocks/{Nodes => Types}/Enum.php (93%) rename src/SchemaPrinter/Blocks/{Nodes => Types}/EnumTest.php (97%) rename src/SchemaPrinter/Blocks/{Nodes => Types}/EnumValue.php (91%) rename src/SchemaPrinter/Blocks/{Nodes => Types}/EnumValueTest.php (97%) rename src/SchemaPrinter/Blocks/{Nodes => Types}/EnumValues.php (95%) rename src/SchemaPrinter/Blocks/{Nodes => Types}/Scalar.php (90%) rename src/SchemaPrinter/Blocks/{Nodes => Types}/ScalarTest.php (98%) rename src/SchemaPrinter/Blocks/{Nodes => Types}/StringBlock.php (97%) rename src/SchemaPrinter/Blocks/{Nodes => Types}/StringBlockTest.php (98%) rename src/SchemaPrinter/Blocks/{Nodes => Types}/TypeBlock.php (94%) rename src/SchemaPrinter/Blocks/{Nodes => Types}/TypeName.php (94%) rename src/SchemaPrinter/Blocks/{Nodes => Types}/TypeNameTest.php (96%) rename src/SchemaPrinter/Blocks/{Nodes => Types}/Union.php (95%) rename src/SchemaPrinter/Blocks/{Nodes => Types}/UnionTest.php (98%) rename src/SchemaPrinter/Blocks/{Nodes => Types}/UnionTypes.php (95%) diff --git a/src/SchemaPrinter/Blocks/Nodes/Arguments.php b/src/SchemaPrinter/Blocks/Ast/Arguments.php similarity index 95% rename from src/SchemaPrinter/Blocks/Nodes/Arguments.php rename to src/SchemaPrinter/Blocks/Ast/Arguments.php index 3076b478..85cdb59e 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Arguments.php +++ b/src/SchemaPrinter/Blocks/Ast/Arguments.php @@ -1,6 +1,6 @@ diff --git a/src/SchemaPrinter/Blocks/Nodes/Directives.php b/src/SchemaPrinter/Blocks/Ast/Directives.php similarity index 93% rename from src/SchemaPrinter/Blocks/Nodes/Directives.php rename to src/SchemaPrinter/Blocks/Ast/Directives.php index 37c0a70b..13674fd3 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Directives.php +++ b/src/SchemaPrinter/Blocks/Ast/Directives.php @@ -1,13 +1,13 @@ diff --git a/src/SchemaPrinter/Blocks/Nodes/Value.php b/src/SchemaPrinter/Blocks/Ast/Value.php similarity index 94% rename from src/SchemaPrinter/Blocks/Nodes/Value.php rename to src/SchemaPrinter/Blocks/Ast/Value.php index 54c80cc4..a83d23f8 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Value.php +++ b/src/SchemaPrinter/Blocks/Ast/Value.php @@ -1,6 +1,6 @@ diff --git a/src/SchemaPrinter/Blocks/Nodes/Description.php b/src/SchemaPrinter/Blocks/Types/Description.php similarity index 93% rename from src/SchemaPrinter/Blocks/Nodes/Description.php rename to src/SchemaPrinter/Blocks/Types/Description.php index df66b630..55c1add1 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Description.php +++ b/src/SchemaPrinter/Blocks/Types/Description.php @@ -1,8 +1,9 @@ diff --git a/src/SchemaPrinter/Blocks/Nodes/Enum.php b/src/SchemaPrinter/Blocks/Types/Enum.php similarity index 93% rename from src/SchemaPrinter/Blocks/Nodes/Enum.php rename to src/SchemaPrinter/Blocks/Types/Enum.php index fe4d8f83..a5fcefde 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Enum.php +++ b/src/SchemaPrinter/Blocks/Types/Enum.php @@ -1,6 +1,6 @@ // ========================================================================= /** * @covers ::__toString - * @covers \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Nodes\EnumValues::__toString + * @covers \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\EnumValues::__toString * * @dataProvider dataProviderToString */ diff --git a/src/SchemaPrinter/Blocks/Nodes/EnumValue.php b/src/SchemaPrinter/Blocks/Types/EnumValue.php similarity index 91% rename from src/SchemaPrinter/Blocks/Nodes/EnumValue.php rename to src/SchemaPrinter/Blocks/Types/EnumValue.php index c985cf26..945ad806 100644 --- a/src/SchemaPrinter/Blocks/Nodes/EnumValue.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValue.php @@ -1,6 +1,6 @@ diff --git a/src/SchemaPrinter/Blocks/Nodes/EnumValues.php b/src/SchemaPrinter/Blocks/Types/EnumValues.php similarity index 95% rename from src/SchemaPrinter/Blocks/Nodes/EnumValues.php rename to src/SchemaPrinter/Blocks/Types/EnumValues.php index 74baad3f..78896e8a 100644 --- a/src/SchemaPrinter/Blocks/Nodes/EnumValues.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValues.php @@ -1,6 +1,6 @@ diff --git a/src/SchemaPrinter/Blocks/Nodes/StringBlock.php b/src/SchemaPrinter/Blocks/Types/StringBlock.php similarity index 97% rename from src/SchemaPrinter/Blocks/Nodes/StringBlock.php rename to src/SchemaPrinter/Blocks/Types/StringBlock.php index b1d49c86..fb16dbe9 100644 --- a/src/SchemaPrinter/Blocks/Nodes/StringBlock.php +++ b/src/SchemaPrinter/Blocks/Types/StringBlock.php @@ -1,6 +1,6 @@ diff --git a/src/SchemaPrinter/Blocks/Nodes/TypeBlock.php b/src/SchemaPrinter/Blocks/Types/TypeBlock.php similarity index 94% rename from src/SchemaPrinter/Blocks/Nodes/TypeBlock.php rename to src/SchemaPrinter/Blocks/Types/TypeBlock.php index 9cc95caf..74b711a4 100644 --- a/src/SchemaPrinter/Blocks/Nodes/TypeBlock.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlock.php @@ -1,10 +1,11 @@ diff --git a/src/SchemaPrinter/Blocks/Nodes/Union.php b/src/SchemaPrinter/Blocks/Types/Union.php similarity index 95% rename from src/SchemaPrinter/Blocks/Nodes/Union.php rename to src/SchemaPrinter/Blocks/Types/Union.php index 8d44fbbf..79618eeb 100644 --- a/src/SchemaPrinter/Blocks/Nodes/Union.php +++ b/src/SchemaPrinter/Blocks/Types/Union.php @@ -1,6 +1,6 @@ // ========================================================================= /** * @covers ::__toString - * @covers \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Nodes\UnionTypes::__toString + * @covers \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\UnionTypes::__toString * * @dataProvider dataProviderToString */ diff --git a/src/SchemaPrinter/Blocks/Nodes/UnionTypes.php b/src/SchemaPrinter/Blocks/Types/UnionTypes.php similarity index 95% rename from src/SchemaPrinter/Blocks/Nodes/UnionTypes.php rename to src/SchemaPrinter/Blocks/Types/UnionTypes.php index 10977b9a..e58e3db2 100644 --- a/src/SchemaPrinter/Blocks/Nodes/UnionTypes.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypes.php @@ -1,6 +1,6 @@ Date: Sat, 8 Jan 2022 11:00:51 +0400 Subject: [PATCH 31/90] Better names for types and nodes. --- .../Blocks/Ast/{Arguments.php => ArgumentNodeList.php} | 6 +++--- .../Ast/{Directive.php => DirectiveNodeBlock.php} | 4 ++-- .../{DirectiveTest.php => DirectiveNodeBlockTest.php} | 8 ++++---- .../Ast/{Directives.php => DirectiveNodeList.php} | 6 +++--- .../{DirectivesTest.php => DirectiveNodeListTest.php} | 8 ++++---- .../Blocks/Ast/{Value.php => ValueNodeBlock.php} | 8 ++++---- .../Blocks/Ast/{ValueTest.php => ValueNodeTest.php} | 6 +++--- src/SchemaPrinter/Blocks/Types/Description.php | 6 +++--- src/SchemaPrinter/Blocks/Types/DescriptionTest.php | 4 ++-- .../Blocks/Types/{Enum.php => EnumTypeBlock.php} | 4 ++-- .../Types/{EnumTest.php => EnumTypeBlockTest.php} | 8 ++++---- .../{EnumValue.php => EnumValueDefinitionBlock.php} | 2 +- ...mValueTest.php => EnumValueDefinitionBlockTest.php} | 6 +++--- .../{EnumValues.php => EnumValueDefinitionList.php} | 6 +++--- .../Blocks/Types/{Scalar.php => ScalarTypeBlock.php} | 2 +- .../Types/{ScalarTest.php => ScalarTypeBlockTest.php} | 6 +++--- src/SchemaPrinter/Blocks/Types/TypeBlock.php | 4 ++-- .../Blocks/Types/{Union.php => UnionTypeBlock.php} | 4 ++-- .../Types/{UnionTest.php => UnionTypeBlockTest.php} | 10 +++++----- .../Types/{UnionTypes.php => UnionTypeTypeList.php} | 2 +- 20 files changed, 55 insertions(+), 55 deletions(-) rename src/SchemaPrinter/Blocks/Ast/{Arguments.php => ArgumentNodeList.php} (92%) rename src/SchemaPrinter/Blocks/Ast/{Directive.php => DirectiveNodeBlock.php} (93%) rename src/SchemaPrinter/Blocks/Ast/{DirectiveTest.php => DirectiveNodeBlockTest.php} (93%) rename src/SchemaPrinter/Blocks/Ast/{Directives.php => DirectiveNodeList.php} (95%) rename src/SchemaPrinter/Blocks/Ast/{DirectivesTest.php => DirectiveNodeListTest.php} (95%) rename src/SchemaPrinter/Blocks/Ast/{Value.php => ValueNodeBlock.php} (93%) rename src/SchemaPrinter/Blocks/Ast/{ValueTest.php => ValueNodeTest.php} (98%) rename src/SchemaPrinter/Blocks/Types/{Enum.php => EnumTypeBlock.php} (91%) rename src/SchemaPrinter/Blocks/Types/{EnumTest.php => EnumTypeBlockTest.php} (95%) rename src/SchemaPrinter/Blocks/Types/{EnumValue.php => EnumValueDefinitionBlock.php} (93%) rename src/SchemaPrinter/Blocks/Types/{EnumValueTest.php => EnumValueDefinitionBlockTest.php} (93%) rename src/SchemaPrinter/Blocks/Types/{EnumValues.php => EnumValueDefinitionList.php} (87%) rename src/SchemaPrinter/Blocks/Types/{Scalar.php => ScalarTypeBlock.php} (94%) rename src/SchemaPrinter/Blocks/Types/{ScalarTest.php => ScalarTypeBlockTest.php} (97%) rename src/SchemaPrinter/Blocks/Types/{Union.php => UnionTypeBlock.php} (93%) rename src/SchemaPrinter/Blocks/Types/{UnionTest.php => UnionTypeBlockTest.php} (96%) rename src/SchemaPrinter/Blocks/Types/{UnionTypes.php => UnionTypeTypeList.php} (96%) diff --git a/src/SchemaPrinter/Blocks/Ast/Arguments.php b/src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php similarity index 92% rename from src/SchemaPrinter/Blocks/Ast/Arguments.php rename to src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php index 85cdb59e..b51fde29 100644 --- a/src/SchemaPrinter/Blocks/Ast/Arguments.php +++ b/src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php @@ -11,9 +11,9 @@ /** * @internal - * @extends BlockList + * @extends BlockList */ -class Arguments extends BlockList { +class ArgumentNodeList extends BlockList { /** * @param Traversable|array $arguments */ @@ -32,7 +32,7 @@ public function __construct( $this->getDispatcher(), $this->getSettings(), $name, - new Value( + new ValueNodeBlock( $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, diff --git a/src/SchemaPrinter/Blocks/Ast/Directive.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php similarity index 93% rename from src/SchemaPrinter/Blocks/Ast/Directive.php rename to src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php index 8f67368c..fbf92cc9 100644 --- a/src/SchemaPrinter/Blocks/Ast/Directive.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php @@ -14,7 +14,7 @@ /** * @internal */ -class Directive extends Block implements Named { +class DirectiveNodeBlock extends Block implements Named { public function __construct( Dispatcher $dispatcher, Settings $settings, @@ -39,7 +39,7 @@ protected function content(): string { $node = $this->getNode(); $name = $this->getName(); $used = mb_strlen($name) + mb_strlen($at); - $args = new Arguments( + $args = new ArgumentNodeList( $this->getDispatcher(), $this->getSettings(), $this->getLevel(), diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php similarity index 93% rename from src/SchemaPrinter/Blocks/Ast/DirectiveTest.php rename to src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php index 11994b76..4c62f8b7 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php @@ -14,9 +14,9 @@ use PHPUnit\Framework\TestCase; /** - * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\Directive + * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeBlock */ -class DirectiveTest extends TestCase { +class DirectiveNodeBlockTest extends TestCase { // // ========================================================================= /** @@ -31,7 +31,7 @@ public function testToString( int $used, DirectiveNode $node, ): void { - $actual = (string) (new Directive(new Dispatcher(), $settings, $level, $used, $node)); + $actual = (string) (new DirectiveNodeBlock(new Dispatcher(), $settings, $level, $used, $node)); $parsed = Parser::directive($actual); self::assertEquals($expected, $actual); @@ -50,7 +50,7 @@ public function testToStringEvent(): void { $dispatcher->attach(Closure::fromCallable($spy)); self::assertNotNull( - (string) (new Directive($dispatcher, $settings, 0, 0, $node)), + (string) (new DirectiveNodeBlock($dispatcher, $settings, 0, 0, $node)), ); $spy diff --git a/src/SchemaPrinter/Blocks/Ast/Directives.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php similarity index 95% rename from src/SchemaPrinter/Blocks/Ast/Directives.php rename to src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php index 13674fd3..dd9cb1a3 100644 --- a/src/SchemaPrinter/Blocks/Ast/Directives.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php @@ -6,7 +6,7 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\Directive; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\Directive as DirectiveBlock; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeBlock as DirectiveBlock; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use Traversable; @@ -16,9 +16,9 @@ /** * @internal - * @extends BlockList + * @extends BlockList */ -class Directives extends BlockList { +class DirectiveNodeList extends BlockList { /** * @param Traversable|array $directives */ diff --git a/src/SchemaPrinter/Blocks/Ast/DirectivesTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php similarity index 95% rename from src/SchemaPrinter/Blocks/Ast/DirectivesTest.php rename to src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php index 38c1f984..9f56fe87 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectivesTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php @@ -16,9 +16,9 @@ use PHPUnit\Framework\TestCase; /** - * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\Directives + * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList */ -class DirectivesTest extends TestCase { +class DirectiveNodeListTest extends TestCase { // // ========================================================================= /** @@ -36,7 +36,7 @@ public function testToString( array|null $directives, string $reason = null, ): void { - $actual = (string) (new Directives(new Dispatcher(), $settings, $level, $used, $directives, $reason)); + $actual = (string) (new DirectiveNodeList(new Dispatcher(), $settings, $level, $used, $directives, $reason)); $parsed = Parser::directives($actual); self::assertEquals($expected, $actual); @@ -60,7 +60,7 @@ public function isIncludeDirectives(): bool { $dispatcher->attach(Closure::fromCallable($spy)); self::assertNotNull( - (string) (new Directives($dispatcher, $settings, 0, 0, [$a, $b])), + (string) (new DirectiveNodeList($dispatcher, $settings, 0, 0, [$a, $b])), ); $spy diff --git a/src/SchemaPrinter/Blocks/Ast/Value.php b/src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php similarity index 93% rename from src/SchemaPrinter/Blocks/Ast/Value.php rename to src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php index a83d23f8..7ed588a2 100644 --- a/src/SchemaPrinter/Blocks/Ast/Value.php +++ b/src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php @@ -11,12 +11,12 @@ use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\ListBlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\StringBlock; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\ObjectBlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Property; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\StringBlock; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -class Value extends Block { +class ValueNodeBlock extends Block { /** * @param ValueNode&Node $node */ @@ -41,7 +41,7 @@ protected function content(): string { $content = new ListBlockList($dispatcher, $settings, $level, $used); foreach ($this->node->values as $value) { - $content[] = new Value($dispatcher, $settings, $level + 1, $used, $value); + $content[] = new ValueNodeBlock($dispatcher, $settings, $level + 1, $used, $value); } } elseif ($this->node instanceof ObjectValueNode) { $content = new ObjectBlockList($dispatcher, $settings, $level, $used); @@ -52,7 +52,7 @@ protected function content(): string { $dispatcher, $settings, $name, - new Value( + new ValueNodeBlock( $dispatcher, $settings, $level + 1 + (int) ($field->value instanceof StringValueNode), diff --git a/src/SchemaPrinter/Blocks/Ast/ValueTest.php b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php similarity index 98% rename from src/SchemaPrinter/Blocks/Ast/ValueTest.php rename to src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php index 6b4455c9..1be10a18 100644 --- a/src/SchemaPrinter/Blocks/Ast/ValueTest.php +++ b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php @@ -20,9 +20,9 @@ use PHPUnit\Framework\TestCase; /** - * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\Value + * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\ValueNodeBlock */ -class ValueTest extends TestCase { +class ValueNodeTest extends TestCase { // // ========================================================================= /** @@ -39,7 +39,7 @@ public function testToString( int $used, ValueNode $node, ): void { - $actual = (string) (new Value(new Dispatcher(), $settings, $level, $used, $node)); + $actual = (string) (new ValueNodeBlock(new Dispatcher(), $settings, $level, $used, $node)); $parsed = Parser::valueLiteral($actual); self::assertEquals($expected, $actual); diff --git a/src/SchemaPrinter/Blocks/Types/Description.php b/src/SchemaPrinter/Blocks/Types/Description.php index 55c1add1..c530bb73 100644 --- a/src/SchemaPrinter/Blocks/Types/Description.php +++ b/src/SchemaPrinter/Blocks/Types/Description.php @@ -3,7 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\Directives; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use function preg_replace; @@ -21,12 +21,12 @@ public function __construct( int $level, int $used, ?string $string, - private ?Directives $directives = null, + private ?DirectiveNodeList $directives = null, ) { parent::__construct($dispatcher, $settings, $level, $used, (string) $string); } - protected function getDirectives(): ?Directives { + protected function getDirectives(): ?DirectiveNodeList { return $this->directives; } diff --git a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php index a5dff907..1e87bfb9 100644 --- a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php +++ b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php @@ -5,7 +5,7 @@ use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\Parser; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\Directives; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -34,7 +34,7 @@ public function testToString( ?array $directives, ): void { $dispatcher = new Dispatcher(); - $directives = new Directives($dispatcher, $settings, $level, $used, $directives); + $directives = new DirectiveNodeList($dispatcher, $settings, $level, $used, $directives); $actual = (string) (new Description($dispatcher, $settings, $level, $used, $description, $directives)); self::assertEquals($expected, $actual); diff --git a/src/SchemaPrinter/Blocks/Types/Enum.php b/src/SchemaPrinter/Blocks/Types/EnumTypeBlock.php similarity index 91% rename from src/SchemaPrinter/Blocks/Types/Enum.php rename to src/SchemaPrinter/Blocks/Types/EnumTypeBlock.php index a5fcefde..ac0fcb3c 100644 --- a/src/SchemaPrinter/Blocks/Types/Enum.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeBlock.php @@ -13,7 +13,7 @@ * * @extends TypeBlock */ -class Enum extends TypeBlock { +class EnumTypeBlock extends TypeBlock { public function __construct( Dispatcher $dispatcher, Settings $settings, @@ -27,7 +27,7 @@ public function __construct( protected function body(int $used): string { $space = $this->space(); $body = "enum{$space}{$this->getName()}{$space}"; - $values = new EnumValues( + $values = new EnumValueDefinitionList( $this->getDispatcher(), $this->getSettings(), $this->getLevel(), diff --git a/src/SchemaPrinter/Blocks/Types/EnumTest.php b/src/SchemaPrinter/Blocks/Types/EnumTypeBlockTest.php similarity index 95% rename from src/SchemaPrinter/Blocks/Types/EnumTest.php rename to src/SchemaPrinter/Blocks/Types/EnumTypeBlockTest.php index 48f78a94..be995b14 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeBlockTest.php @@ -14,14 +14,14 @@ use PHPUnit\Framework\TestCase; /** - * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\Enum + * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\EnumTypeBlock */ -class EnumTest extends TestCase { +class EnumTypeBlockTest extends TestCase { // // ========================================================================= /** * @covers ::__toString - * @covers \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\EnumValues::__toString + * @covers \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\EnumValueDefinitionList::__toString * * @dataProvider dataProviderToString */ @@ -36,7 +36,7 @@ public function testToString( $type = $type(); } - $actual = (string) (new Enum(new Dispatcher(), $settings, $level, $used, $type)); + $actual = (string) (new EnumTypeBlock(new Dispatcher(), $settings, $level, $used, $type)); $parsed = Parser::enumTypeDefinition($actual); self::assertEquals($expected, $actual); diff --git a/src/SchemaPrinter/Blocks/Types/EnumValue.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php similarity index 93% rename from src/SchemaPrinter/Blocks/Types/EnumValue.php rename to src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php index 945ad806..a7394a91 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValue.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php @@ -10,7 +10,7 @@ * @internal * @extends TypeBlock */ -class EnumValue extends TypeBlock { +class EnumValueDefinitionBlock extends TypeBlock { public function __construct( Dispatcher $dispatcher, Settings $settings, diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueTest.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php similarity index 93% rename from src/SchemaPrinter/Blocks/Types/EnumValueTest.php rename to src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php index df59a6e3..c3c88cd0 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php @@ -12,9 +12,9 @@ use PHPUnit\Framework\TestCase; /** - * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\EnumValue + * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\EnumValueDefinitionBlock */ -class EnumValueTest extends TestCase { +class EnumValueDefinitionBlockTest extends TestCase { // // ========================================================================= /** @@ -29,7 +29,7 @@ public function testToString( int $used, EnumValueDefinition $type, ): void { - $actual = (string) (new EnumValue(new Dispatcher(), $settings, $level, $used, $type)); + $actual = (string) (new EnumValueDefinitionBlock(new Dispatcher(), $settings, $level, $used, $type)); $parsed = Parser::enumValueDefinition($actual); self::assertEquals($expected, $actual); diff --git a/src/SchemaPrinter/Blocks/Types/EnumValues.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionList.php similarity index 87% rename from src/SchemaPrinter/Blocks/Types/EnumValues.php rename to src/SchemaPrinter/Blocks/Types/EnumValueDefinitionList.php index 78896e8a..61cd86db 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValues.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionList.php @@ -10,9 +10,9 @@ /** * @internal - * @extends ObjectBlockList + * @extends ObjectBlockList */ -class EnumValues extends ObjectBlockList { +class EnumValueDefinitionList extends ObjectBlockList { /** * @param Traversable|array $values */ @@ -26,7 +26,7 @@ public function __construct( parent::__construct($dispatcher, $settings, $level, $used); foreach ($values as $value) { - $this[$value->name] = new EnumValue( + $this[$value->name] = new EnumValueDefinitionBlock( $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, diff --git a/src/SchemaPrinter/Blocks/Types/Scalar.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeBlock.php similarity index 94% rename from src/SchemaPrinter/Blocks/Types/Scalar.php rename to src/SchemaPrinter/Blocks/Types/ScalarTypeBlock.php index b4d6bf20..709836b0 100644 --- a/src/SchemaPrinter/Blocks/Types/Scalar.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeBlock.php @@ -11,7 +11,7 @@ * * @extends TypeBlock */ -class Scalar extends TypeBlock { +class ScalarTypeBlock extends TypeBlock { public function __construct( Dispatcher $dispatcher, Settings $settings, diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTest.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeBlockTest.php similarity index 97% rename from src/SchemaPrinter/Blocks/Types/ScalarTest.php rename to src/SchemaPrinter/Blocks/Types/ScalarTypeBlockTest.php index eec6d922..e6cc9a57 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTest.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeBlockTest.php @@ -13,9 +13,9 @@ use PHPUnit\Framework\TestCase; /** - * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\Scalar + * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\ScalarTypeBlock */ -class ScalarTest extends TestCase { +class ScalarTypeBlockTest extends TestCase { // // ========================================================================= /** @@ -30,7 +30,7 @@ public function testToString( int $used, ScalarType $type, ): void { - $actual = (string) (new Scalar(new Dispatcher(), $settings, $level, $used, $type)); + $actual = (string) (new ScalarTypeBlock(new Dispatcher(), $settings, $level, $used, $type)); $parsed = Parser::scalarTypeDefinition($actual); self::assertEquals($expected, $actual); diff --git a/src/SchemaPrinter/Blocks/Types/TypeBlock.php b/src/SchemaPrinter/Blocks/Types/TypeBlock.php index 74b711a4..de62ed8e 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeBlock.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlock.php @@ -5,7 +5,7 @@ use GraphQL\Type\Definition\EnumValueDefinition; use GraphQL\Type\Definition\Type; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\Directives; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -48,7 +48,7 @@ protected function getType(): Type|EnumValueDefinition { protected function content(): string { $type = $this->getType(); - $directives = new Directives( + $directives = new DirectiveNodeList( $this->getDispatcher(), $this->getSettings(), $this->getLevel(), diff --git a/src/SchemaPrinter/Blocks/Types/Union.php b/src/SchemaPrinter/Blocks/Types/UnionTypeBlock.php similarity index 93% rename from src/SchemaPrinter/Blocks/Types/Union.php rename to src/SchemaPrinter/Blocks/Types/UnionTypeBlock.php index 79618eeb..2b66fc16 100644 --- a/src/SchemaPrinter/Blocks/Types/Union.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeBlock.php @@ -13,7 +13,7 @@ * * @extends TypeBlock */ -class Union extends TypeBlock { +class UnionTypeBlock extends TypeBlock { public function __construct( Dispatcher $dispatcher, Settings $settings, @@ -29,7 +29,7 @@ protected function body(int $used): string { $space = $this->space(); $equal = "{$space}={$space}"; $body = "union{$space}{$this->getName()}"; - $types = new UnionTypes( + $types = new UnionTypeTypeList( $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, diff --git a/src/SchemaPrinter/Blocks/Types/UnionTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeBlockTest.php similarity index 96% rename from src/SchemaPrinter/Blocks/Types/UnionTest.php rename to src/SchemaPrinter/Blocks/Types/UnionTypeBlockTest.php index f79870c4..72220e18 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeBlockTest.php @@ -17,14 +17,14 @@ use PHPUnit\Framework\TestCase; /** - * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\Union + * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\UnionTypeBlock */ -class UnionTest extends TestCase { +class UnionTypeBlockTest extends TestCase { // // ========================================================================= /** * @covers ::__toString - * @covers \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\UnionTypes::__toString + * @covers \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\UnionTypeTypeList::__toString * * @dataProvider dataProviderToString */ @@ -35,7 +35,7 @@ public function testToString( int $used, UnionType $type, ): void { - $actual = (string) (new Union(new Dispatcher(), $settings, $level, $used, $type)); + $actual = (string) (new UnionTypeBlock(new Dispatcher(), $settings, $level, $used, $type)); $parsed = Parser::unionTypeDefinition($actual); self::assertEquals($expected, $actual); @@ -64,7 +64,7 @@ public function testToStringEvent(): void { $dispatcher->attach(Closure::fromCallable($spy)); self::assertNotNull( - (string) (new Union($dispatcher, $settings, 0, 0, $union)), + (string) (new UnionTypeBlock($dispatcher, $settings, 0, 0, $union)), ); $spy diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypes.php b/src/SchemaPrinter/Blocks/Types/UnionTypeTypeList.php similarity index 96% rename from src/SchemaPrinter/Blocks/Types/UnionTypes.php rename to src/SchemaPrinter/Blocks/Types/UnionTypeTypeList.php index e58e3db2..0bce4b02 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypes.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeTypeList.php @@ -12,7 +12,7 @@ * @internal * @extends BlockList */ -class UnionTypes extends BlockList { +class UnionTypeTypeList extends BlockList { /** * @param Traversable|array $types */ From 0586189c3b8efd13fba3cdc44122048d423752d8 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 8 Jan 2022 11:47:13 +0400 Subject: [PATCH 32/90] Renames: `TypeBlock` => `TypeDefinitionBlock`, `TypeName` => `TypeBlock`. --- .../Blocks/Types/EnumTypeBlock.php | 4 +- .../Blocks/Types/EnumValueDefinitionBlock.php | 4 +- .../Blocks/Types/ScalarTypeBlock.php | 4 +- src/SchemaPrinter/Blocks/Types/TypeBlock.php | 65 ++----------- .../{TypeNameTest.php => TypeBlockTest.php} | 8 +- .../Blocks/Types/TypeDefinitionBlock.php | 91 +++++++++++++++++++ src/SchemaPrinter/Blocks/Types/TypeName.php | 41 --------- .../Blocks/Types/UnionTypeBlock.php | 4 +- .../Blocks/Types/UnionTypeTypeList.php | 4 +- 9 files changed, 113 insertions(+), 112 deletions(-) rename src/SchemaPrinter/Blocks/Types/{TypeNameTest.php => TypeBlockTest.php} (91%) create mode 100644 src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php delete mode 100644 src/SchemaPrinter/Blocks/Types/TypeName.php diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeBlock.php b/src/SchemaPrinter/Blocks/Types/EnumTypeBlock.php index ac0fcb3c..f78792fd 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeBlock.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeBlock.php @@ -11,9 +11,9 @@ /** * @internal * - * @extends TypeBlock + * @extends TypeDefinitionBlock */ -class EnumTypeBlock extends TypeBlock { +class EnumTypeBlock extends TypeDefinitionBlock { public function __construct( Dispatcher $dispatcher, Settings $settings, diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php index a7394a91..0b527229 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php @@ -8,9 +8,9 @@ /** * @internal - * @extends TypeBlock + * @extends TypeDefinitionBlock */ -class EnumValueDefinitionBlock extends TypeBlock { +class EnumValueDefinitionBlock extends TypeDefinitionBlock { public function __construct( Dispatcher $dispatcher, Settings $settings, diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeBlock.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeBlock.php index 709836b0..332a30e8 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeBlock.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeBlock.php @@ -9,9 +9,9 @@ /** * @internal * - * @extends TypeBlock + * @extends TypeDefinitionBlock */ -class ScalarTypeBlock extends TypeBlock { +class ScalarTypeBlock extends TypeDefinitionBlock { public function __construct( Dispatcher $dispatcher, Settings $settings, diff --git a/src/SchemaPrinter/Blocks/Types/TypeBlock.php b/src/SchemaPrinter/Blocks/Types/TypeBlock.php index de62ed8e..2c9368b4 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeBlock.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlock.php @@ -2,31 +2,23 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; -use GraphQL\Type\Definition\EnumValueDefinition; -use GraphQL\Type\Definition\Type; +use GraphQL\Type\Definition\ObjectType; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use function mb_strlen; - /** * @internal - * - * @template TType of Type|EnumValueDefinition */ -abstract class TypeBlock extends Block implements Named { - /** - * @param TType $type - */ +class TypeBlock extends Block implements Named { public function __construct( Dispatcher $dispatcher, Settings $settings, int $level, int $used, - private Type|EnumValueDefinition $type, + private ObjectType $type, ) { parent::__construct($dispatcher, $settings, $level, $used); } @@ -35,56 +27,15 @@ public function getName(): string { return $this->getType()->name; } - protected function isBlock(): bool { - return true; - } - - /** - * @return TType - */ - protected function getType(): Type|EnumValueDefinition { + protected function getType(): ObjectType { return $this->type; } protected function content(): string { - $type = $this->getType(); - $directives = new DirectiveNodeList( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel(), - $this->getUsed(), - $type->astNode->directives ?? null, - $type->deprecationReason ?? null, + $this->getDispatcher()->notify( + new TypeUsed($this->getName()), ); - $description = new Description( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel(), - $this->getUsed(), - $type->description, - $directives, - ); - - $eol = $this->eol(); - $indent = $this->indent(); - $content = ''; - if ($this->isBlock()) { - $content .= $indent; - } - - $body = $this->body($this->getUsed() + mb_strlen($content)); - - if ($description->getLength()) { - $body = "{$description}{$eol}{$indent}{$body}"; - } - - if ($directives->getLength() && $this->getSettings()->isIncludeDirectives()) { - $body = "{$body}{$eol}{$directives}"; - } - - return "{$content}{$body}"; + return $this->getName(); } - - abstract protected function body(int $used): string; } diff --git a/src/SchemaPrinter/Blocks/Types/TypeNameTest.php b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php similarity index 91% rename from src/SchemaPrinter/Blocks/Types/TypeNameTest.php rename to src/SchemaPrinter/Blocks/Types/TypeBlockTest.php index ed112867..44d13354 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeNameTest.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php @@ -14,9 +14,9 @@ use PHPUnit\Framework\TestCase; /** - * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\TypeName + * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\TypeBlock */ -class TypeNameTest extends TestCase { +class TypeBlockTest extends TestCase { // // ========================================================================= /** @@ -31,7 +31,7 @@ public function testToString( int $used, ObjectType $type, ): void { - $actual = (string) (new TypeName(new Dispatcher(), $settings, $level, $used, $type)); + $actual = (string) (new TypeBlock(new Dispatcher(), $settings, $level, $used, $type)); self::assertEquals($expected, $actual); } @@ -50,7 +50,7 @@ public function testToStringEvent(): void { $dispatcher->attach(Closure::fromCallable($spy)); self::assertNotNull( - (string) (new TypeName($dispatcher, $settings, 0, 0, $node)), + (string) (new TypeBlock($dispatcher, $settings, 0, 0, $node)), ); $spy diff --git a/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php new file mode 100644 index 00000000..abd3b38e --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php @@ -0,0 +1,91 @@ +getType()->name; + } + + protected function isBlock(): bool { + return true; + } + + /** + * @return TType + */ + protected function getType(): Type|FieldDefinition|EnumValueDefinition { + return $this->type; + } + + protected function content(): string { + $type = $this->getType(); + $directives = new DirectiveNodeList( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $type->astNode->directives ?? null, + $type->deprecationReason ?? null, + ); + $description = new Description( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $type->description, + $directives, + ); + + $eol = $this->eol(); + $indent = $this->indent(); + $content = ''; + + if ($this->isBlock()) { + $content .= $indent; + } + + $body = $this->body($this->getUsed() + mb_strlen($content)); + + if ($description->getLength()) { + $body = "{$description}{$eol}{$indent}{$body}"; + } + + if ($directives->getLength() && $this->getSettings()->isIncludeDirectives()) { + $body = "{$body}{$eol}{$directives}"; + } + + return "{$content}{$body}"; + } + + abstract protected function body(int $used): string; +} diff --git a/src/SchemaPrinter/Blocks/Types/TypeName.php b/src/SchemaPrinter/Blocks/Types/TypeName.php deleted file mode 100644 index 214f2e29..00000000 --- a/src/SchemaPrinter/Blocks/Types/TypeName.php +++ /dev/null @@ -1,41 +0,0 @@ -getType()->name; - } - - protected function getType(): ObjectType { - return $this->type; - } - - protected function content(): string { - $this->getDispatcher()->notify( - new TypeUsed($this->getName()), - ); - - return $this->getName(); - } -} diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeBlock.php b/src/SchemaPrinter/Blocks/Types/UnionTypeBlock.php index 2b66fc16..f1f7abab 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeBlock.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeBlock.php @@ -11,9 +11,9 @@ /** * @internal * - * @extends TypeBlock + * @extends TypeDefinitionBlock */ -class UnionTypeBlock extends TypeBlock { +class UnionTypeBlock extends TypeDefinitionBlock { public function __construct( Dispatcher $dispatcher, Settings $settings, diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeTypeList.php b/src/SchemaPrinter/Blocks/Types/UnionTypeTypeList.php index 0bce4b02..72c4eadb 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeTypeList.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeTypeList.php @@ -10,7 +10,7 @@ /** * @internal - * @extends BlockList + * @extends BlockList */ class UnionTypeTypeList extends BlockList { /** @@ -26,7 +26,7 @@ public function __construct( parent::__construct($dispatcher, $settings, $level, $used); foreach ($types as $type) { - $this[$type->name] = new TypeName( + $this[$type->name] = new TypeBlock( $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, From 4c82b22f31d96149a094d56b523ed9e11a5f2ac1 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 8 Jan 2022 11:52:14 +0400 Subject: [PATCH 33/90] `TypeBlock` will support any `Type`s. --- src/SchemaPrinter/Blocks/Types/TypeBlock.php | 17 +++++--- .../Blocks/Types/TypeBlockTest.php | 41 ++++++++++++++++--- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Types/TypeBlock.php b/src/SchemaPrinter/Blocks/Types/TypeBlock.php index 2c9368b4..ea67cf1b 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeBlock.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlock.php @@ -2,7 +2,8 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; -use GraphQL\Type\Definition\ObjectType; +use GraphQL\Type\Definition\Type; +use GraphQL\Type\Definition\WrappingType; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; @@ -18,16 +19,22 @@ public function __construct( Settings $settings, int $level, int $used, - private ObjectType $type, + private Type $type, ) { parent::__construct($dispatcher, $settings, $level, $used); } public function getName(): string { - return $this->getType()->name; + $type = $this->getType(); + + if ($type instanceof WrappingType) { + $type = $type->getWrappedType(true); + } + + return $type->name; } - protected function getType(): ObjectType { + protected function getType(): Type { return $this->type; } @@ -36,6 +43,6 @@ protected function content(): string { new TypeUsed($this->getName()), ); - return $this->getName(); + return (string) $this->getType(); } } diff --git a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php index 44d13354..78ba6519 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php @@ -4,7 +4,10 @@ use Closure; use GraphQL\Language\AST\DirectiveNode; +use GraphQL\Type\Definition\ListOfType; +use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ObjectType; +use GraphQL\Type\Definition\Type; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; @@ -29,7 +32,7 @@ public function testToString( Settings $settings, int $level, int $used, - ObjectType $type, + Type $type, ): void { $actual = (string) (new TypeBlock(new Dispatcher(), $settings, $level, $used, $type)); @@ -41,9 +44,11 @@ public function testToString( */ public function testToStringEvent(): void { $spy = Mockery::spy(static fn (Event $event) => null); - $node = new ObjectType([ - 'name' => 'Test', - ]); + $node = new NonNull( + new ObjectType([ + 'name' => 'Test', + ]) + ); $settings = new DefaultSettings(); $dispatcher = new Dispatcher(); @@ -57,7 +62,7 @@ public function testToStringEvent(): void { ->shouldHaveBeenCalled() ->withArgs(static function (Event $event) use ($node): bool { return $event instanceof TypeUsed - && $event->name === $node->name; + && $event->name === $node->getWrappedType(true)->name; }) ->once(); $spy @@ -73,7 +78,7 @@ public function testToStringEvent(): void { */ public function dataProviderToString(): array { return [ - 'object' => [ + 'object' => [ 'Test', new DefaultSettings(), 0, @@ -82,6 +87,30 @@ public function dataProviderToString(): array { 'name' => 'Test', ]), ], + 'non null' => [ + 'Test!', + new DefaultSettings(), + 0, + 0, + new NonNull( + new ObjectType([ + 'name' => 'Test', + ]), + ), + ], + 'non null list' => [ + '[Test]!', + new DefaultSettings(), + 0, + 0, + new NonNull( + new ListOfType( + new ObjectType([ + 'name' => 'Test', + ]), + ), + ), + ], ]; } // From e9d9698bdf201a256f4ce9471eb76495b6ca1c39 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 8 Jan 2022 12:06:51 +0400 Subject: [PATCH 34/90] Better names for Types blocks (as per GraphQL spec). --- .../{TypeDefinitionBlock.php => DefinitionBlock.php} | 2 +- .../{EnumTypeBlock.php => EnumTypeDefinitionBlock.php} | 6 +++--- ...peBlockTest.php => EnumTypeDefinitionBlockTest.php} | 8 ++++---- .../Blocks/Types/EnumValueDefinitionBlock.php | 4 ++-- ...DefinitionList.php => EnumValuesDefinitionList.php} | 2 +- ...alarTypeBlock.php => ScalarTypeDefinitionBlock.php} | 4 ++-- ...BlockTest.php => ScalarTypeDefinitionBlockTest.php} | 6 +++--- ...{UnionTypeTypeList.php => UnionMemberTypesList.php} | 2 +- ...UnionTypeBlock.php => UnionTypeDefinitionBlock.php} | 6 +++--- ...eBlockTest.php => UnionTypeDefinitionBlockTest.php} | 10 +++++----- 10 files changed, 25 insertions(+), 25 deletions(-) rename src/SchemaPrinter/Blocks/Types/{TypeDefinitionBlock.php => DefinitionBlock.php} (97%) rename src/SchemaPrinter/Blocks/Types/{EnumTypeBlock.php => EnumTypeDefinitionBlock.php} (86%) rename src/SchemaPrinter/Blocks/Types/{EnumTypeBlockTest.php => EnumTypeDefinitionBlockTest.php} (94%) rename src/SchemaPrinter/Blocks/Types/{EnumValueDefinitionList.php => EnumValuesDefinitionList.php} (95%) rename src/SchemaPrinter/Blocks/Types/{ScalarTypeBlock.php => ScalarTypeDefinitionBlock.php} (86%) rename src/SchemaPrinter/Blocks/Types/{ScalarTypeBlockTest.php => ScalarTypeDefinitionBlockTest.php} (96%) rename src/SchemaPrinter/Blocks/Types/{UnionTypeTypeList.php => UnionMemberTypesList.php} (96%) rename src/SchemaPrinter/Blocks/Types/{UnionTypeBlock.php => UnionTypeDefinitionBlock.php} (89%) rename src/SchemaPrinter/Blocks/Types/{UnionTypeBlockTest.php => UnionTypeDefinitionBlockTest.php} (95%) diff --git a/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php similarity index 97% rename from src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php rename to src/SchemaPrinter/Blocks/Types/DefinitionBlock.php index abd3b38e..e32564e8 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php @@ -18,7 +18,7 @@ * * @template TType of Type|FieldDefinition|EnumValueDefinition */ -abstract class TypeDefinitionBlock extends Block implements Named { +abstract class DefinitionBlock extends Block implements Named { /** * @param TType $type */ diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeBlock.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php similarity index 86% rename from src/SchemaPrinter/Blocks/Types/EnumTypeBlock.php rename to src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php index f78792fd..90e68990 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeBlock.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php @@ -11,9 +11,9 @@ /** * @internal * - * @extends TypeDefinitionBlock + * @extends DefinitionBlock */ -class EnumTypeBlock extends TypeDefinitionBlock { +class EnumTypeDefinitionBlock extends DefinitionBlock { public function __construct( Dispatcher $dispatcher, Settings $settings, @@ -27,7 +27,7 @@ public function __construct( protected function body(int $used): string { $space = $this->space(); $body = "enum{$space}{$this->getName()}{$space}"; - $values = new EnumValueDefinitionList( + $values = new EnumValuesDefinitionList( $this->getDispatcher(), $this->getSettings(), $this->getLevel(), diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php similarity index 94% rename from src/SchemaPrinter/Blocks/Types/EnumTypeBlockTest.php rename to src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php index be995b14..eed8c261 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php @@ -14,14 +14,14 @@ use PHPUnit\Framework\TestCase; /** - * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\EnumTypeBlock + * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\EnumTypeDefinitionBlock */ -class EnumTypeBlockTest extends TestCase { +class EnumTypeDefinitionBlockTest extends TestCase { // // ========================================================================= /** * @covers ::__toString - * @covers \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\EnumValueDefinitionList::__toString + * @covers \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\EnumValuesDefinitionList::__toString * * @dataProvider dataProviderToString */ @@ -36,7 +36,7 @@ public function testToString( $type = $type(); } - $actual = (string) (new EnumTypeBlock(new Dispatcher(), $settings, $level, $used, $type)); + $actual = (string) (new EnumTypeDefinitionBlock(new Dispatcher(), $settings, $level, $used, $type)); $parsed = Parser::enumTypeDefinition($actual); self::assertEquals($expected, $actual); diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php index 0b527229..5e3d9ee9 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php @@ -8,9 +8,9 @@ /** * @internal - * @extends TypeDefinitionBlock + * @extends DefinitionBlock */ -class EnumValueDefinitionBlock extends TypeDefinitionBlock { +class EnumValueDefinitionBlock extends DefinitionBlock { public function __construct( Dispatcher $dispatcher, Settings $settings, diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionList.php b/src/SchemaPrinter/Blocks/Types/EnumValuesDefinitionList.php similarity index 95% rename from src/SchemaPrinter/Blocks/Types/EnumValueDefinitionList.php rename to src/SchemaPrinter/Blocks/Types/EnumValuesDefinitionList.php index 61cd86db..a3b08e1a 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionList.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValuesDefinitionList.php @@ -12,7 +12,7 @@ * @internal * @extends ObjectBlockList */ -class EnumValueDefinitionList extends ObjectBlockList { +class EnumValuesDefinitionList extends ObjectBlockList { /** * @param Traversable|array $values */ diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeBlock.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php similarity index 86% rename from src/SchemaPrinter/Blocks/Types/ScalarTypeBlock.php rename to src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php index 332a30e8..f2045d00 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeBlock.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php @@ -9,9 +9,9 @@ /** * @internal * - * @extends TypeDefinitionBlock + * @extends DefinitionBlock */ -class ScalarTypeBlock extends TypeDefinitionBlock { +class ScalarTypeDefinitionBlock extends DefinitionBlock { public function __construct( Dispatcher $dispatcher, Settings $settings, diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeBlockTest.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php similarity index 96% rename from src/SchemaPrinter/Blocks/Types/ScalarTypeBlockTest.php rename to src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php index e6cc9a57..f35e21bd 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php @@ -13,9 +13,9 @@ use PHPUnit\Framework\TestCase; /** - * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\ScalarTypeBlock + * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\ScalarTypeDefinitionBlock */ -class ScalarTypeBlockTest extends TestCase { +class ScalarTypeDefinitionBlockTest extends TestCase { // // ========================================================================= /** @@ -30,7 +30,7 @@ public function testToString( int $used, ScalarType $type, ): void { - $actual = (string) (new ScalarTypeBlock(new Dispatcher(), $settings, $level, $used, $type)); + $actual = (string) (new ScalarTypeDefinitionBlock(new Dispatcher(), $settings, $level, $used, $type)); $parsed = Parser::scalarTypeDefinition($actual); self::assertEquals($expected, $actual); diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeTypeList.php b/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php similarity index 96% rename from src/SchemaPrinter/Blocks/Types/UnionTypeTypeList.php rename to src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php index 72c4eadb..bca6c11f 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeTypeList.php +++ b/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php @@ -12,7 +12,7 @@ * @internal * @extends BlockList */ -class UnionTypeTypeList extends BlockList { +class UnionMemberTypesList extends BlockList { /** * @param Traversable|array $types */ diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeBlock.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php similarity index 89% rename from src/SchemaPrinter/Blocks/Types/UnionTypeBlock.php rename to src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php index f1f7abab..74f04b42 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeBlock.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php @@ -11,9 +11,9 @@ /** * @internal * - * @extends TypeDefinitionBlock + * @extends DefinitionBlock */ -class UnionTypeBlock extends TypeDefinitionBlock { +class UnionTypeDefinitionBlock extends DefinitionBlock { public function __construct( Dispatcher $dispatcher, Settings $settings, @@ -29,7 +29,7 @@ protected function body(int $used): string { $space = $this->space(); $equal = "{$space}={$space}"; $body = "union{$space}{$this->getName()}"; - $types = new UnionTypeTypeList( + $types = new UnionMemberTypesList( $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeBlockTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php similarity index 95% rename from src/SchemaPrinter/Blocks/Types/UnionTypeBlockTest.php rename to src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php index 72220e18..0ac535c1 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php @@ -17,14 +17,14 @@ use PHPUnit\Framework\TestCase; /** - * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\UnionTypeBlock + * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\UnionTypeDefinitionBlock */ -class UnionTypeBlockTest extends TestCase { +class UnionTypeDefinitionBlockTest extends TestCase { // // ========================================================================= /** * @covers ::__toString - * @covers \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\UnionTypeTypeList::__toString + * @covers \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\UnionMemberTypesList::__toString * * @dataProvider dataProviderToString */ @@ -35,7 +35,7 @@ public function testToString( int $used, UnionType $type, ): void { - $actual = (string) (new UnionTypeBlock(new Dispatcher(), $settings, $level, $used, $type)); + $actual = (string) (new UnionTypeDefinitionBlock(new Dispatcher(), $settings, $level, $used, $type)); $parsed = Parser::unionTypeDefinition($actual); self::assertEquals($expected, $actual); @@ -64,7 +64,7 @@ public function testToStringEvent(): void { $dispatcher->attach(Closure::fromCallable($spy)); self::assertNotNull( - (string) (new UnionTypeBlock($dispatcher, $settings, 0, 0, $union)), + (string) (new UnionTypeDefinitionBlock($dispatcher, $settings, 0, 0, $union)), ); $spy From d9c374574d86c9ff6e8820679d1a2d88278bedc6 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 8 Jan 2022 12:19:20 +0400 Subject: [PATCH 35/90] `DefinitionBlock::getType()` renamed to `DefinitionBlock::getDefinition()`. --- .../Blocks/Types/DefinitionBlock.php | 15 ++++++++------- .../Blocks/Types/EnumTypeDefinitionBlock.php | 6 +++--- .../Blocks/Types/EnumValueDefinitionBlock.php | 4 ---- .../Blocks/Types/ScalarTypeDefinitionBlock.php | 4 ++-- src/SchemaPrinter/Blocks/Types/TypeBlock.php | 4 ++-- .../Blocks/Types/UnionTypeDefinitionBlock.php | 6 +++--- 6 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php index e32564e8..d5192fcb 100644 --- a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php @@ -3,6 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\EnumValueDefinition; +use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\FieldDefinition; use GraphQL\Type\Definition\Type; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; @@ -20,35 +21,35 @@ */ abstract class DefinitionBlock extends Block implements Named { /** - * @param TType $type + * @param TType $definition */ public function __construct( Dispatcher $dispatcher, Settings $settings, int $level, int $used, - private Type|FieldDefinition|EnumValueDefinition $type, + private Type|FieldDefinition|EnumValueDefinition|FieldArgument $definition, ) { parent::__construct($dispatcher, $settings, $level, $used); } public function getName(): string { - return $this->getType()->name; + return $this->getDefinition()->name; } protected function isBlock(): bool { - return true; + return $this->getDefinition() instanceof Type; } /** * @return TType */ - protected function getType(): Type|FieldDefinition|EnumValueDefinition { - return $this->type; + protected function getDefinition(): Type|FieldDefinition|EnumValueDefinition { + return $this->definition; } protected function content(): string { - $type = $this->getType(); + $type = $this->getDefinition(); $directives = new DirectiveNodeList( $this->getDispatcher(), $this->getSettings(), diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php index 90e68990..f8d80d4d 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php @@ -19,9 +19,9 @@ public function __construct( Settings $settings, int $level, int $used, - EnumType $type, + EnumType $definition, ) { - parent::__construct($dispatcher, $settings, $level, $used, $type); + parent::__construct($dispatcher, $settings, $level, $used, $definition); } protected function body(int $used): string { @@ -32,7 +32,7 @@ protected function body(int $used): string { $this->getSettings(), $this->getLevel(), $used + mb_strlen($body), - $this->getType()->getValues(), + $this->getDefinition()->getValues(), ); return "{$body}{$values}"; diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php index 5e3d9ee9..c4dd2c79 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php @@ -21,10 +21,6 @@ public function __construct( parent::__construct($dispatcher, $settings, $level, $used, $value); } - protected function isBlock(): bool { - return false; - } - protected function body(int $used): string { return $this->getName(); } diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php index f2045d00..474e0d34 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php @@ -17,9 +17,9 @@ public function __construct( Settings $settings, int $level, int $used, - ScalarType $type, + ScalarType $definition, ) { - parent::__construct($dispatcher, $settings, $level, $used, $type); + parent::__construct($dispatcher, $settings, $level, $used, $definition); } protected function body(int $used): string { diff --git a/src/SchemaPrinter/Blocks/Types/TypeBlock.php b/src/SchemaPrinter/Blocks/Types/TypeBlock.php index ea67cf1b..533cd296 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeBlock.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlock.php @@ -19,7 +19,7 @@ public function __construct( Settings $settings, int $level, int $used, - private Type $type, + private Type $definition, ) { parent::__construct($dispatcher, $settings, $level, $used); } @@ -35,7 +35,7 @@ public function getName(): string { } protected function getType(): Type { - return $this->type; + return $this->definition; } protected function content(): string { diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php index 74f04b42..6868c809 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php @@ -19,9 +19,9 @@ public function __construct( Settings $settings, int $level, int $used, - UnionType $type, + UnionType $definition, ) { - parent::__construct($dispatcher, $settings, $level, $used, $type); + parent::__construct($dispatcher, $settings, $level, $used, $definition); } protected function body(int $used): string { @@ -34,7 +34,7 @@ protected function body(int $used): string { $this->getSettings(), $this->getLevel() + 1, $used + mb_strlen($body) + mb_strlen($equal), - $this->getType()->getTypes(), + $this->getDefinition()->getTypes(), ); if ($types->isMultiline()) { From 486628248aa1c2349eef96fc7247b4fe4da43a7c Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 8 Jan 2022 14:39:52 +0400 Subject: [PATCH 36/90] `Block::isMultiline()` will not return `true` for long lines without EOL. --- src/SchemaPrinter/Blocks/Block.php | 3 +-- src/SchemaPrinter/Blocks/BlockTest.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Block.php b/src/SchemaPrinter/Blocks/Block.php index 19436a21..db119acb 100644 --- a/src/SchemaPrinter/Blocks/Block.php +++ b/src/SchemaPrinter/Blocks/Block.php @@ -67,8 +67,7 @@ protected function getContent(): string { if ($this->content === null) { $this->content = $this->content(); $this->length = mb_strlen($this->content); - $this->multiline = $this->isLineTooLong($this->length + $this->getUsed()) - || $this->isStringMultiline($this->content); + $this->multiline = $this->isStringMultiline($this->content); } return $this->content; diff --git a/src/SchemaPrinter/Blocks/BlockTest.php b/src/SchemaPrinter/Blocks/BlockTest.php index 5f8cecda..3d1ef7a8 100644 --- a/src/SchemaPrinter/Blocks/BlockTest.php +++ b/src/SchemaPrinter/Blocks/BlockTest.php @@ -83,7 +83,7 @@ public function dataProviderIsMultiline(): array { 'short line', ], 'single long line' => [ - true, + false, new class() extends DefaultSettings { public function getLineLength(): int { return 5; From c2e9e4d3863a31d039d0cce2f1c5830aeb2f6248 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 8 Jan 2022 14:42:01 +0400 Subject: [PATCH 37/90] `ArgumentsDefinition`, `FieldDefinition`, `FieldsDefinition`, `InputValueDefinition` support. --- .../Blocks/Types/ArgumentsDefinitionList.php | 54 ++++ .../Blocks/Types/DefinitionBlock.php | 4 +- .../Blocks/Types/FieldDefinitionBlock.php | 46 ++++ .../Blocks/Types/FieldDefinitionBlockTest.php | 253 ++++++++++++++++++ .../Blocks/Types/FieldsDefinitionList.php | 58 ++++ .../Types/InputValueDefinitionBlock.php | 53 ++++ .../Types/InputValueDefinitionBlockTest.php | 178 ++++++++++++ 7 files changed, 644 insertions(+), 2 deletions(-) create mode 100644 src/SchemaPrinter/Blocks/Types/ArgumentsDefinitionList.php create mode 100644 src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php create mode 100644 src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php create mode 100644 src/SchemaPrinter/Blocks/Types/FieldsDefinitionList.php create mode 100644 src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php create mode 100644 src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php diff --git a/src/SchemaPrinter/Blocks/Types/ArgumentsDefinitionList.php b/src/SchemaPrinter/Blocks/Types/ArgumentsDefinitionList.php new file mode 100644 index 00000000..25010c0b --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/ArgumentsDefinitionList.php @@ -0,0 +1,54 @@ + + */ +class ArgumentsDefinitionList extends BlockList { + /** + * @param Traversable|array $arguments + */ + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + Traversable|array $arguments, + ) { + parent::__construct($dispatcher, $settings, $level, $used); + + foreach ($arguments as $argument) { + $this[$argument->name] = new InputValueDefinitionBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel() + 1, + $this->getUsed(), + $argument, + ); + } + } + + protected function getPrefix(): string { + return '('; + } + + protected function getSuffix(): string { + return ')'; + } + + protected function isWrapped(): bool { + return true; + } + + protected function isNormalized(): bool { + return $this->getSettings()->isNormalizeArguments(); + } +} diff --git a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php index d5192fcb..144820a3 100644 --- a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php @@ -17,7 +17,7 @@ /** * @internal * - * @template TType of Type|FieldDefinition|EnumValueDefinition + * @template TType of Type|FieldDefinition|EnumValueDefinition|FieldArgument */ abstract class DefinitionBlock extends Block implements Named { /** @@ -44,7 +44,7 @@ protected function isBlock(): bool { /** * @return TType */ - protected function getDefinition(): Type|FieldDefinition|EnumValueDefinition { + protected function getDefinition(): Type|FieldDefinition|EnumValueDefinition|FieldArgument { return $this->definition; } diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php new file mode 100644 index 00000000..7e0ecadb --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php @@ -0,0 +1,46 @@ + + */ +class FieldDefinitionBlock extends DefinitionBlock { + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + FieldDefinition $definition, + ) { + parent::__construct($dispatcher, $settings, $level, $used, $definition); + } + + protected function body(int $used): string { + $definition = $this->getDefinition(); + $space = $this->space(); + $name = $this->getName(); + $type = new TypeBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $definition->getType(), + ); + $args = new ArgumentsDefinitionList( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $definition->args, + ); + + return "{$name}{$args}:{$space}{$type}"; + } +} diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php new file mode 100644 index 00000000..7c2c608c --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php @@ -0,0 +1,253 @@ + + // ========================================================================= + /** + * @covers ::__toString + * + * @dataProvider dataProviderToString + */ + public function testToString( + string $expected, + Settings $settings, + int $level, + int $used, + FieldDefinition $definition, + ): void { + $actual = (string) (new FieldDefinitionBlock(new Dispatcher(), $settings, $level, $used, $definition)); + $parsed = Parser::fieldDefinition($actual); + + self::assertEquals($expected, $actual); + self::assertInstanceOf(FieldDefinitionNode::class, $parsed); + } + + /** + * @covers ::__toString + */ + public function testToStringEvent(): void { + $spy = Mockery::spy(static fn (Event $event) => null); + $settings = new DefaultSettings(); + $dispatcher = new Dispatcher(); + $definition = FieldDefinition::create([ + 'name' => 'A', + 'type' => new NonNull( + new ObjectType([ + 'name' => 'A', + ]), + ), + ]); + + $dispatcher->attach(Closure::fromCallable($spy)); + + self::assertNotNull( + (string) (new FieldDefinitionBlock($dispatcher, $settings, 0, 0, $definition)), + ); + + $spy + ->shouldHaveBeenCalled() + ->withArgs(static function (Event $event): bool { + return $event instanceof TypeUsed + && $event->name === 'A'; + }) + ->once(); + $spy + ->shouldHaveBeenCalled() + ->once(); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderToString(): array { + return [ + 'without args' => [ + <<<'STRING' + """ + Description + """ + test: Test! + @a + STRING, + new DefaultSettings(), + 0, + 0, + FieldDefinition::create([ + 'name' => 'test', + 'type' => new NonNull( + new ObjectType([ + 'name' => 'Test', + ]), + ), + 'astNode' => Parser::fieldDefinition('test: Test! @a'), + 'description' => 'Description', + ]), + ], + 'with args (short)' => [ + <<<'STRING' + """ + Description + """ + test(a: [String!] = ["aaaaaaaaaaaaaaaaaaaaaaaaaa"], b: Int): Test! + STRING, + new DefaultSettings(), + 0, + 0, + FieldDefinition::create([ + 'name' => 'test', + 'type' => new NonNull( + new ObjectType([ + 'name' => 'Test', + ]), + ), + 'args' => [ + 'a' => [ + 'type' => new ListOfType(new NonNull(Type::string())), + 'defaultValue' => [ + "aaaaaaaaaaaaaaaaaaaaaaaaaa", + ], + ], + 'b' => [ + 'type' => Type::int(), + ], + ], + 'description' => 'Description', + ]), + ], + 'with args (long)' => [ + <<<'STRING' + test( + b: Int + + """ + Description + """ + a: String! = "aaaaaaaaaaaaaaaaaaaaaaaaaa" + ): Test! + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + + public function isNormalizeArguments(): bool { + return false; + } + }, + 0, + 0, + FieldDefinition::create([ + 'name' => 'test', + 'type' => new NonNull( + new ObjectType([ + 'name' => 'Test', + ]), + ), + 'args' => [ + 'b' => [ + 'type' => Type::int(), + ], + 'a' => [ + 'type' => new NonNull(Type::string()), + 'description' => 'Description', + 'defaultValue' => "aaaaaaaaaaaaaaaaaaaaaaaaaa", + ], + ], + ]), + ], + 'with args normalized' => [ + <<<'STRING' + test(a: String, b: Int): Test! + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + + public function isNormalizeArguments(): bool { + return true; + } + }, + 0, + 0, + FieldDefinition::create([ + 'name' => 'test', + 'type' => new NonNull( + new ObjectType([ + 'name' => 'Test', + ]), + ), + 'args' => [ + 'b' => [ + 'type' => Type::int(), + ], + 'a' => [ + 'type' => Type::string(), + ], + ], + ]), + ], + 'indent' => [ + <<<'STRING' + test( + a: String + b: Int + ): Test! + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + + public function isNormalizeArguments(): bool { + return true; + } + }, + 1, + 120, + FieldDefinition::create([ + 'name' => 'test', + 'type' => new NonNull( + new ObjectType([ + 'name' => 'Test', + ]), + ), + 'args' => [ + 'b' => [ + 'type' => Type::int(), + ], + 'a' => [ + 'type' => Type::string(), + ], + ], + ]), + ], + ]; + } + // +} diff --git a/src/SchemaPrinter/Blocks/Types/FieldsDefinitionList.php b/src/SchemaPrinter/Blocks/Types/FieldsDefinitionList.php new file mode 100644 index 00000000..2f0a0e3d --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/FieldsDefinitionList.php @@ -0,0 +1,58 @@ + + */ +class FieldsDefinitionList extends BlockList { + /** + * @param Traversable|array $fields + */ + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + Traversable|array $fields, + ) { + parent::__construct($dispatcher, $settings, $level, $used); + + foreach ($fields as $field) { + $this[$field->name] = new FieldDefinitionBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel() + 1, + $this->getUsed(), + $field, + ); + } + } + + protected function getPrefix(): string { + return '{'; + } + + protected function getSuffix(): string { + return '}'; + } + + protected function isWrapped(): bool { + return true; + } + + protected function isNormalized(): bool { + return $this->getSettings()->isNormalizeFields(); + } + + protected function isAlwaysMultiline(): bool { + return true; + } +} diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php new file mode 100644 index 00000000..857fee83 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php @@ -0,0 +1,53 @@ + + */ +class InputValueDefinitionBlock extends DefinitionBlock { + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + FieldArgument $definition, + ) { + parent::__construct($dispatcher, $settings, $level, $used, $definition); + } + + protected function body(int $used): string { + $definition = $this->getDefinition(); + $space = $this->space(); + $name = $this->getName(); + $type = new TypeBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $definition->getType(), + ); + $body = "{$name}:{$space}{$type}"; + + if ($definition->defaultValueExists()) { + $value = new ValueNodeBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + AST::astFromValue($definition->defaultValue, $definition->getType()), + ); + $body = "{$body}{$space}={$space}{$value}"; + } + + return $body; + } +} diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php new file mode 100644 index 00000000..c31df840 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php @@ -0,0 +1,178 @@ + + // ========================================================================= + /** + * @covers ::__toString + * + * @dataProvider dataProviderToString + */ + public function testToString( + string $expected, + Settings $settings, + int $level, + int $used, + FieldArgument $definition, + ): void { + $actual = (string) (new InputValueDefinitionBlock(new Dispatcher(), $settings, $level, $used, $definition)); + $parsed = Parser::inputValueDefinition($actual); + + self::assertEquals($expected, $actual); + self::assertInstanceOf(InputValueDefinitionNode::class, $parsed); + } + + /** + * @covers ::__toString + */ + public function testToStringEvent(): void { + $spy = Mockery::spy(static fn (Event $event) => null); + $settings = new DefaultSettings(); + $dispatcher = new Dispatcher(); + $definition = new FieldArgument([ + 'name' => 'A', + 'type' => new NonNull( + new ObjectType([ + 'name' => 'A', + ]), + ), + ]); + + $dispatcher->attach(Closure::fromCallable($spy)); + + self::assertNotNull( + (string) (new InputValueDefinitionBlock($dispatcher, $settings, 0, 0, $definition)), + ); + + $spy + ->shouldHaveBeenCalled() + ->withArgs(static function (Event $event): bool { + return $event instanceof TypeUsed + && $event->name === 'A'; + }) + ->once(); + $spy + ->shouldHaveBeenCalled() + ->once(); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderToString(): array { + return [ + 'without value' => [ + <<<'STRING' + """ + Description + """ + test: Test! + @a + STRING, + new DefaultSettings(), + 0, + 0, + new FieldArgument([ + 'name' => 'test', + 'type' => new NonNull( + new ObjectType([ + 'name' => 'Test', + ]), + ), + 'astNode' => Parser::inputValueDefinition('test: Test! @a'), + 'description' => 'Description', + ]), + ], + 'with value (short)' => [ + <<<'STRING' + """ + Description + """ + test: [String!] = ["aaaaaaaaaaaaaaaaaaaaaaaaaa"] + STRING, + new DefaultSettings(), + 0, + 0, + new FieldArgument([ + 'name' => 'test', + 'type' => new ListOfType(new NonNull(Type::string())), + 'defaultValue' => [ + "aaaaaaaaaaaaaaaaaaaaaaaaaa", + ], + 'description' => 'Description', + ]), + ], + 'with value (long)' => [ + <<<'STRING' + """ + Description + """ + test: [String!] = [ + "aaaaaaaaaaaaaaaaaaaaaaaaaa" + ] + STRING, + new DefaultSettings(), + 0, + 120, + new FieldArgument([ + 'name' => 'test', + 'type' => new ListOfType(new NonNull(Type::string())), + 'defaultValue' => [ + "aaaaaaaaaaaaaaaaaaaaaaaaaa", + ], + 'description' => 'Description', + ]), + ], + 'indent' => [ + <<<'STRING' + """ + Description + """ + test: [String!] = [ + "aaaaaaaaaaaaaaaaaaaaaaaaaa" + ] + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 1, + 70, + new FieldArgument([ + 'name' => 'test', + 'type' => new ListOfType(new NonNull(Type::string())), + 'defaultValue' => [ + "aaaaaaaaaaaaaaaaaaaaaaaaaa", + ], + 'description' => 'Description', + ]), + ], + ]; + } + // +} From 163cbf7e27a00bb67dcab49634e81a5ca27872e2 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 9 Jan 2022 11:05:23 +0400 Subject: [PATCH 38/90] First line of all blocks will be without indent, also improved formatting in `DefinitionBlock`. --- .../Blocks/Ast/DirectiveNodeBlockTest.php | 2 +- .../Blocks/Ast/DirectiveNodeListTest.php | 2 +- src/SchemaPrinter/Blocks/Block.php | 4 + src/SchemaPrinter/Blocks/BlockList.php | 11 ++- src/SchemaPrinter/Blocks/BlockListTest.php | 77 +++-------------- src/SchemaPrinter/Blocks/BlockTest.php | 27 ++++++ .../Blocks/Types/DefinitionBlock.php | 86 ++++++++++++------- .../Blocks/Types/EnumTypeDefinitionBlock.php | 16 +++- .../Types/EnumTypeDefinitionBlockTest.php | 2 +- .../Blocks/Types/EnumValueDefinitionBlock.php | 13 ++- .../Blocks/Types/FieldDefinitionBlock.php | 14 ++- .../Types/InputValueDefinitionBlock.php | 23 +++-- .../Types/ScalarTypeDefinitionBlock.php | 13 ++- .../Types/ScalarTypeDefinitionBlockTest.php | 4 +- .../Blocks/Types/UnionMemberTypesList.php | 4 - .../Blocks/Types/UnionTypeDefinitionBlock.php | 19 ++-- .../Types/UnionTypeDefinitionBlockTest.php | 4 +- 17 files changed, 185 insertions(+), 136 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php index 4c62f8b7..02ea5427 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php @@ -117,7 +117,7 @@ public function isNormalizeArguments(): bool { 0, Parser::directive('@directive(b: "b", a: "a")'), ], - 'with arguments (level)' => [ + 'with arguments (indent)' => [ <<<'STRING' @directive( b: "b" diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php index 9f56fe87..59756f21 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php @@ -175,7 +175,7 @@ public function dataProviderToString(): array { ], 'indent' => [ <<<'STRING' - @deprecated( + @deprecated( reason: "very very very long reason" ) @a(a: 123) diff --git a/src/SchemaPrinter/Blocks/Block.php b/src/SchemaPrinter/Blocks/Block.php index db119acb..b3224f4d 100644 --- a/src/SchemaPrinter/Blocks/Block.php +++ b/src/SchemaPrinter/Blocks/Block.php @@ -48,6 +48,10 @@ protected function getUsed(): int { // // ========================================================================= + public function isEmpty(): bool { + return $this->getLength() <= 0; + } + public function getLength(): int { return $this->length ?? mb_strlen($this->getContent()); } diff --git a/src/SchemaPrinter/Blocks/BlockList.php b/src/SchemaPrinter/Blocks/BlockList.php index 210b7f9a..21c50122 100644 --- a/src/SchemaPrinter/Blocks/BlockList.php +++ b/src/SchemaPrinter/Blocks/BlockList.php @@ -29,10 +29,6 @@ abstract class BlockList extends Block implements ArrayAccess { // // ========================================================================= - protected function isBlock(): bool { - return true; - } - protected function isWrapped(): bool { return false; } @@ -68,6 +64,10 @@ protected function getEmptyValue(): string { // // ========================================================================= + public function isEmpty(): bool { + return count($this->blocks) === 0 || parent::isEmpty(); + } + public function isMultiline(): bool { return count($this->multiline) > 0 || parent::isMultiline(); } @@ -121,7 +121,6 @@ protected function content(): string { $index = 0; $indent = $this->indent($this->getLevel() + (int) ($listPrefix || $listSuffix)); $wrapped = $this->isWrapped(); - $isBlock = $this->isBlock(); $previous = false; $separator = $this->getMultilineSeparator(); @@ -132,7 +131,7 @@ protected function content(): string { $content .= $eol; } - if ($isBlock || $index > 0) { + if ($index > 0 || ($listPrefix || $listSuffix)) { $content .= $indent; } diff --git a/src/SchemaPrinter/Blocks/BlockListTest.php b/src/SchemaPrinter/Blocks/BlockListTest.php index 3c70f054..0b30781e 100644 --- a/src/SchemaPrinter/Blocks/BlockListTest.php +++ b/src/SchemaPrinter/Blocks/BlockListTest.php @@ -29,7 +29,6 @@ public function testToString( Settings $settings, int $level, int $used, - bool $block, bool $normalized, bool $wrapped, string $prefix, @@ -43,7 +42,6 @@ public function testToString( $settings, $level, $used, - $block, $normalized, $wrapped, $prefix, @@ -75,7 +73,6 @@ public function dataProviderToString(): array { new DefaultSettings(), 0, 0, - true, false, true, '', @@ -93,7 +90,6 @@ public function dataProviderToString(): array { new DefaultSettings(), 0, 0, - true, false, true, '', @@ -111,7 +107,6 @@ public function dataProviderToString(): array { new DefaultSettings(), 0, 0, - true, false, true, '', @@ -135,7 +130,6 @@ public function getLineLength(): int { }, 0, 5, - true, false, true, '', @@ -156,7 +150,6 @@ public function getLineLength(): int { new DefaultSettings(), 0, 0, - true, false, true, '', @@ -185,7 +178,6 @@ public function getLineLength(): int { new DefaultSettings(), 0, 0, - true, false, true, '', @@ -211,7 +203,6 @@ public function getLineLength(): int { new DefaultSettings(), 0, 0, - true, false, false, '', @@ -233,7 +224,6 @@ public function getLineLength(): int { 0, true, true, - true, '', '', ', ', @@ -245,7 +235,8 @@ public function getLineLength(): int { ], 'multi-line with level' => [ <<<'STRING' - block a + block a + block b STRING, new class() extends DefaultSettings { public function getIndent(): string { @@ -254,7 +245,6 @@ public function getIndent(): string { }, 2, 0, - true, false, false, '', @@ -263,6 +253,7 @@ public function getIndent(): string { '', [ new BlockListTest__Block(true, 'block a'), + new BlockListTest__Block(true, 'block b'), ], ], ]), @@ -274,7 +265,6 @@ public function getIndent(): string { new DefaultSettings(), 0, 0, - true, false, true, '', @@ -292,7 +282,6 @@ public function getIndent(): string { new DefaultSettings(), 0, 0, - true, false, true, '', @@ -310,7 +299,6 @@ public function getIndent(): string { new DefaultSettings(), 0, 0, - true, false, true, '', @@ -334,7 +322,6 @@ public function getLineLength(): int { }, 0, 5, - true, false, true, '', @@ -355,7 +342,6 @@ public function getLineLength(): int { new DefaultSettings(), 0, 0, - true, false, true, '', @@ -384,7 +370,6 @@ public function getLineLength(): int { new DefaultSettings(), 0, 0, - true, false, true, '', @@ -410,7 +395,6 @@ public function getLineLength(): int { new DefaultSettings(), 0, 0, - true, false, false, '', @@ -432,7 +416,6 @@ public function getLineLength(): int { 0, true, true, - true, '', '', ', ', @@ -444,7 +427,8 @@ public function getLineLength(): int { ], 'multi-line with level' => [ <<<'STRING' - a: block a + a: block a + b: block b STRING, new class() extends DefaultSettings { public function getIndent(): string { @@ -453,7 +437,6 @@ public function getIndent(): string { }, 2, 0, - true, false, false, '', @@ -462,6 +445,7 @@ public function getIndent(): string { '', [ new BlockListTest__NamedBlock('a', true, 'block a'), + new BlockListTest__NamedBlock('b', true, 'block b'), ], ], ]), @@ -473,7 +457,6 @@ public function getIndent(): string { new DefaultSettings(), 0, 0, - true, false, true, '[', @@ -497,7 +480,6 @@ public function getIndent(): string { }, 0, 0, - true, false, true, '[', @@ -515,7 +497,6 @@ public function getIndent(): string { new DefaultSettings(), 0, 0, - true, false, true, '[', @@ -545,7 +526,6 @@ public function getIndent(): string { }, 0, 5, - true, false, true, '[', @@ -572,7 +552,6 @@ public function getIndent(): string { }, 0, 0, - true, false, true, '[', @@ -597,7 +576,6 @@ public function getIndent(): string { }, 2, 0, - true, false, false, '[', @@ -613,7 +591,6 @@ public function getIndent(): string { new DefaultSettings(), 0, 0, - true, false, false, '[', @@ -624,14 +601,13 @@ public function getIndent(): string { ], ]), 'separators' => new ArrayDataProvider([ - 'single-line' => [ + 'single-line' => [ <<<'STRING' block a | block b STRING, new DefaultSettings(), 0, 0, - true, false, true, '', @@ -643,7 +619,7 @@ public function getIndent(): string { new BlockListTest__Block(false, 'block b'), ], ], - 'multiline' => [ + 'multiline' => [ <<<'STRING' block a || block b @@ -651,7 +627,6 @@ public function getIndent(): string { new DefaultSettings(), 0, 120, - true, false, true, '', @@ -663,9 +638,9 @@ public function getIndent(): string { new BlockListTest__Block(false, 'block b'), ], ], - 'multiline and indent' => [ + 'multiline and indent' => [ <<<'STRING' - block a + block a || block b STRING, new class() extends DefaultSettings { @@ -675,7 +650,6 @@ public function getIndent(): string { }, 1, 120, - true, false, true, '', @@ -688,32 +662,6 @@ public function getIndent(): string { ], ], ]), - 'not a block' => new ArrayDataProvider([ - 'long block list' => [ - <<<'STRING' - block b - block a - STRING, - new class() extends DefaultSettings { - public function getLineLength(): int { - return 20; - } - }, - 1, - 5, - false, - false, - true, - '', - '', - ', ', - '', - [ - new BlockListTest__Block(false, 'block b'), - new BlockListTest__Block(false, 'block a'), - ], - ], - ]) ]))->getData(); } // @@ -732,7 +680,6 @@ public function __construct( Settings $settings, int $level, int $used, - private bool $block, private bool $normalized, private bool $wrapped, private string $prefix, @@ -743,10 +690,6 @@ public function __construct( parent::__construct($dispatcher, $settings, $level, $used); } - protected function isBlock(): bool { - return $this->block; - } - protected function isWrapped(): bool { return $this->wrapped; } diff --git a/src/SchemaPrinter/Blocks/BlockTest.php b/src/SchemaPrinter/Blocks/BlockTest.php index 3d1ef7a8..ee32ff8b 100644 --- a/src/SchemaPrinter/Blocks/BlockTest.php +++ b/src/SchemaPrinter/Blocks/BlockTest.php @@ -68,6 +68,23 @@ public function testIsMultiline(bool $expected, Settings $settings, string $cont self::assertEquals($expected, $block->isMultiline()); self::assertEquals($expected, $block->isMultiline()); } + + /** + * @covers ::isEmpty + * + * @dataProvider dataProviderIsEmpty + */ + public function testIsEmpty(bool $expected, string $content): void { + $block = Mockery::mock(BlockTest__Block::class, [new Dispatcher(), new DefaultSettings()]); + $block->shouldAllowMockingProtectedMethods(); + $block->makePartial(); + $block + ->shouldReceive('content') + ->once() + ->andReturn($content); + + self::assertEquals($expected, $block->isEmpty()); + } // // @@ -98,6 +115,16 @@ public function getLineLength(): int { ], ]; } + + /** + * @return array + */ + public function dataProviderIsEmpty(): array { + return [ + 'empty' => [true, ''], + 'non empty' => [false, 'content'], + ]; + } // } diff --git a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php index 144820a3..87808026 100644 --- a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php @@ -34,11 +34,15 @@ public function __construct( } public function getName(): string { - return $this->getDefinition()->name; - } + $name = $this->getDefinition()->name; + $type = $this->type(); + + if ($type) { + $space = $this->space(); + $name = "{$type}{$space}{$name}"; + } - protected function isBlock(): bool { - return $this->getDefinition() instanceof Type; + return $name; } /** @@ -49,44 +53,66 @@ protected function getDefinition(): Type|FieldDefinition|EnumValueDefinition|Fie } protected function content(): string { - $type = $this->getDefinition(); - $directives = new DirectiveNodeList( + $eol = $this->eol(); + $indent = $this->indent(); + $name = $this->getName(); + $body = (string) $this->body($this->getUsed() + mb_strlen($name)); + $fields = (string) $this->fields(); + $directives = $this->directives(); + $description = (string) $this->description($directives); + $directives = (string) $directives; + $content = ''; + + if ($description) { + $content .= "{$description}{$eol}{$indent}"; + } + + $content .= "{$name}{$body}"; + + if ($directives && $this->getSettings()->isIncludeDirectives()) { + $content .= "{$eol}{$indent}{$directives}"; + + if ($fields) { + $content .= "{$eol}{$indent}"; + } + } + + $content .= $fields; + + return $content; + } + + abstract protected function type(): string|null; + + abstract protected function body(int $used): Block|string|null; + + abstract protected function fields(): Block|string|null; + + protected function directives(): DirectiveNodeList { + $definition = $this->getDefinition(); + $directives = new DirectiveNodeList( $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $this->getUsed(), - $type->astNode->directives ?? null, - $type->deprecationReason ?? null, + $definition->astNode->directives ?? null, + $definition->deprecationReason ?? null, ); + + return $directives; + } + + protected function description(DirectiveNodeList $directives): Description { + $definition = $this->getDefinition(); $description = new Description( $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $this->getUsed(), - $type->description, + $definition->description, $directives, ); - $eol = $this->eol(); - $indent = $this->indent(); - $content = ''; - - if ($this->isBlock()) { - $content .= $indent; - } - - $body = $this->body($this->getUsed() + mb_strlen($content)); - - if ($description->getLength()) { - $body = "{$description}{$eol}{$indent}{$body}"; - } - - if ($directives->getLength() && $this->getSettings()->isIncludeDirectives()) { - $body = "{$body}{$eol}{$directives}"; - } - - return "{$content}{$body}"; + return $description; } - - abstract protected function body(int $used): string; } diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php index f8d80d4d..2b54be56 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php @@ -4,6 +4,7 @@ use GraphQL\Type\Definition\EnumType; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use function mb_strlen; @@ -24,17 +25,24 @@ public function __construct( parent::__construct($dispatcher, $settings, $level, $used, $definition); } - protected function body(int $used): string { + protected function type(): string { + return 'enum'; + } + + protected function body(int $used): Block|string|null { $space = $this->space(); - $body = "enum{$space}{$this->getName()}{$space}"; $values = new EnumValuesDefinitionList( $this->getDispatcher(), $this->getSettings(), $this->getLevel(), - $used + mb_strlen($body), + $used + mb_strlen($space), $this->getDefinition()->getValues(), ); - return "{$body}{$values}"; + return "{$space}{$values}"; + } + + protected function fields(): Block|string|null { + return null; } } diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php index eed8c261..c4192c2a 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php @@ -73,7 +73,7 @@ public function getIndent(): string { ], 'indent' => [ <<<'STRING' - enum Test { + enum Test { C B A diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php index c4dd2c79..c49ebd96 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php @@ -4,6 +4,7 @@ use GraphQL\Type\Definition\EnumValueDefinition; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; /** @@ -21,7 +22,15 @@ public function __construct( parent::__construct($dispatcher, $settings, $level, $used, $value); } - protected function body(int $used): string { - return $this->getName(); + protected function type(): string|null { + return null; + } + + protected function body(int $used): Block|string|null { + return null; + } + + protected function fields(): Block|string|null { + return null; } } diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php index 7e0ecadb..2dbb13dc 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php @@ -4,6 +4,7 @@ use GraphQL\Type\Definition\FieldDefinition; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; /** @@ -22,10 +23,13 @@ public function __construct( parent::__construct($dispatcher, $settings, $level, $used, $definition); } - protected function body(int $used): string { + protected function type(): string|null { + return null; + } + + protected function body(int $used): Block|string|null { $definition = $this->getDefinition(); $space = $this->space(); - $name = $this->getName(); $type = new TypeBlock( $this->getDispatcher(), $this->getSettings(), @@ -41,6 +45,10 @@ protected function body(int $used): string { $definition->args, ); - return "{$name}{$args}:{$space}{$type}"; + return "{$args}:{$space}{$type}"; + } + + protected function fields(): Block|string|null { + return null; } } diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php index 857fee83..d90f5226 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php @@ -6,8 +6,11 @@ use GraphQL\Utils\AST; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\ValueNodeBlock; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use function mb_strlen; + /** * @internal * @@ -24,10 +27,13 @@ public function __construct( parent::__construct($dispatcher, $settings, $level, $used, $definition); } - protected function body(int $used): string { + protected function type(): string|null { + return null; + } + + protected function body(int $used): Block|string|null { $definition = $this->getDefinition(); $space = $this->space(); - $name = $this->getName(); $type = new TypeBlock( $this->getDispatcher(), $this->getSettings(), @@ -35,19 +41,24 @@ protected function body(int $used): string { $this->getUsed(), $definition->getType(), ); - $body = "{$name}:{$space}{$type}"; + $body = ":{$space}{$type}"; if ($definition->defaultValueExists()) { - $value = new ValueNodeBlock( + $prefix = "{$body}{$space}={$space}"; + $value = new ValueNodeBlock( $this->getDispatcher(), $this->getSettings(), $this->getLevel(), - $this->getUsed(), + $this->getUsed() + mb_strlen($prefix), AST::astFromValue($definition->defaultValue, $definition->getType()), ); - $body = "{$body}{$space}={$space}{$value}"; + $body = "{$prefix}{$value}"; } return $body; } + + protected function fields(): Block|string|null { + return null; + } } diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php index 474e0d34..874cd4eb 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php @@ -4,6 +4,7 @@ use GraphQL\Type\Definition\ScalarType; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; /** @@ -22,7 +23,15 @@ public function __construct( parent::__construct($dispatcher, $settings, $level, $used, $definition); } - protected function body(int $used): string { - return "scalar{$this->space()}{$this->getName()}"; + protected function type(): string|null { + return 'scalar'; + } + + protected function body(int $used): Block|string|null { + return null; + } + + protected function fields(): Block|string|null { + return null; } } diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php index f35e21bd..04beb3f5 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php @@ -117,7 +117,7 @@ public function isIncludeDirectivesInDescription(): bool { ], 'indent' => [ <<<'STRING' - """ + """ Description """ scalar Test @@ -149,7 +149,7 @@ public function isIncludeDirectivesInDescription(): bool { ], 'indent + no description' => [ <<<'STRING' - scalar Test + scalar Test @a( value: "very very long value" ) diff --git a/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php b/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php index bca6c11f..dadcf23a 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php +++ b/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php @@ -47,8 +47,4 @@ protected function getMultilineSeparator(): string { protected function isNormalized(): bool { return $this->getSettings()->isNormalizeUnions(); } - - protected function isBlock(): bool { - return false; - } } diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php index 6868c809..0e6f126d 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php @@ -4,6 +4,8 @@ use GraphQL\Type\Definition\UnionType; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use function mb_strlen; @@ -24,26 +26,33 @@ public function __construct( parent::__construct($dispatcher, $settings, $level, $used, $definition); } - protected function body(int $used): string { + protected function type(): string|null { + return 'union'; + } + + protected function body(int $used): Block|string|null { $indent = $this->indent(); $space = $this->space(); $equal = "{$space}={$space}"; - $body = "union{$space}{$this->getName()}"; $types = new UnionMemberTypesList( $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, - $used + mb_strlen($body) + mb_strlen($equal), + $used + mb_strlen($equal), $this->getDefinition()->getTypes(), ); if ($types->isMultiline()) { $eol = $this->eol(); - $body = "{$body}{$eol}{$indent}{$this->indent(1)}={$space}{$types}"; + $body = "{$eol}{$indent}{$this->indent(1)}={$space}{$types}"; } else { - $body = "{$body}{$equal}{$types}"; + $body = "{$equal}{$types}"; } return $body; } + + protected function fields(): Block|string|null { + return null; + } } diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php index 0ac535c1..acfb0ab0 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php @@ -147,7 +147,7 @@ public function getIndent(): string { ], 'indent single-line' => [ <<<'STRING' - union Test = C | B | A + union Test = C | B | A STRING, new class() extends DefaultSettings { public function getIndent(): string { @@ -173,7 +173,7 @@ public function getIndent(): string { ], 'indent multiline' => [ <<<'STRING' - union Test + union Test = C | B | A From 1b2c7c2d431ab42527193d7f35314b029bb44340 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 9 Jan 2022 12:02:22 +0400 Subject: [PATCH 39/90] Each member of multiline union will have leading `|`. --- src/SchemaPrinter/Blocks/BlockList.php | 12 ++++-------- src/SchemaPrinter/Blocks/BlockListTest.php | 6 +++--- .../Blocks/Types/UnionMemberTypesList.php | 2 +- .../Blocks/Types/UnionTypeDefinitionBlock.php | 13 ++++++------- .../Blocks/Types/UnionTypeDefinitionBlockTest.php | 8 ++++---- 5 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/SchemaPrinter/Blocks/BlockList.php b/src/SchemaPrinter/Blocks/BlockList.php index 21c50122..c98884c0 100644 --- a/src/SchemaPrinter/Blocks/BlockList.php +++ b/src/SchemaPrinter/Blocks/BlockList.php @@ -53,7 +53,7 @@ protected function getSeparator(): string { return ",{$this->space()}"; } - protected function getMultilineSeparator(): string { + protected function getMultilineItemPrefix(): string { return ''; } @@ -104,7 +104,6 @@ protected function content(): string { } // Join - $eol = ''; $listPrefix = $this->getPrefix(); $listSuffix = $this->getSuffix(); $separator = $this->getSeparator(); @@ -122,7 +121,7 @@ protected function content(): string { $indent = $this->indent($this->getLevel() + (int) ($listPrefix || $listSuffix)); $wrapped = $this->isWrapped(); $previous = false; - $separator = $this->getMultilineSeparator(); + $separator = $this->getMultilineItemPrefix(); foreach ($blocks as $block) { $multiline = $wrapped && $block->isMultiline(); @@ -135,11 +134,7 @@ protected function content(): string { $content .= $indent; } - if ($index > 0) { - $content .= $separator; - } - - $content .= "{$block}"; + $content .= "{$separator}{$block}"; if ($index < $last) { $content .= $eol; @@ -154,6 +149,7 @@ protected function content(): string { // Prefix & Suffix if ($listPrefix || $listSuffix) { + $eol = $isMultiline ? $this->eol() : ''; $indent = $isMultiline ? $this->indent() : ''; $content = "{$listPrefix}{$eol}{$content}{$eol}{$indent}{$listSuffix}"; } diff --git a/src/SchemaPrinter/Blocks/BlockListTest.php b/src/SchemaPrinter/Blocks/BlockListTest.php index 0b30781e..0752b00c 100644 --- a/src/SchemaPrinter/Blocks/BlockListTest.php +++ b/src/SchemaPrinter/Blocks/BlockListTest.php @@ -621,7 +621,7 @@ public function getIndent(): string { ], 'multiline' => [ <<<'STRING' - block a + || block a || block b STRING, new DefaultSettings(), @@ -640,7 +640,7 @@ public function getIndent(): string { ], 'multiline and indent' => [ <<<'STRING' - block a + || block a || block b STRING, new class() extends DefaultSettings { @@ -710,7 +710,7 @@ protected function getSeparator(): string { return $this->separator; } - protected function getMultilineSeparator(): string { + protected function getMultilineItemPrefix(): string { return $this->multilineSeparator; } } diff --git a/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php b/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php index dadcf23a..51fdc51d 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php +++ b/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php @@ -40,7 +40,7 @@ protected function getSeparator(): string { return "{$this->space()}|{$this->space()}"; } - protected function getMultilineSeparator(): string { + protected function getMultilineItemPrefix(): string { return "|{$this->space()}"; } diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php index 0e6f126d..db442dc8 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php @@ -4,7 +4,6 @@ use GraphQL\Type\Definition\UnionType; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -31,10 +30,9 @@ protected function type(): string|null { } protected function body(int $used): Block|string|null { - $indent = $this->indent(); - $space = $this->space(); - $equal = "{$space}={$space}"; - $types = new UnionMemberTypesList( + $space = $this->space(); + $equal = "{$space}={$space}"; + $types = new UnionMemberTypesList( $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, @@ -43,8 +41,9 @@ protected function body(int $used): Block|string|null { ); if ($types->isMultiline()) { - $eol = $this->eol(); - $body = "{$eol}{$indent}{$this->indent(1)}={$space}{$types}"; + $eol = $this->eol(); + $indent = $this->indent($this->getLevel() + 1); + $body = "{$space}={$eol}{$indent}{$types}"; } else { $body = "{$equal}{$types}"; } diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php index acfb0ab0..ceb6b52b 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php @@ -118,8 +118,8 @@ public function dataProviderToString(): array { ], 'multiline' => [ <<<'STRING' - union Test - = C + union Test = + | C | B | A STRING, @@ -173,8 +173,8 @@ public function getIndent(): string { ], 'indent multiline' => [ <<<'STRING' - union Test - = C + union Test = + | C | B | A STRING, From 274b91a4e6aad2fefcc5e98d07110659e0c9b1a1 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 9 Jan 2022 12:10:57 +0400 Subject: [PATCH 40/90] Signature of `DefinitionBlock::fields()` changed to `DefinitionBlock::fields(int $used)`. --- src/SchemaPrinter/Blocks/Types/DefinitionBlock.php | 7 ++++--- src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php | 2 +- .../Blocks/Types/EnumValueDefinitionBlock.php | 2 +- src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php | 2 +- .../Blocks/Types/InputValueDefinitionBlock.php | 2 +- .../Blocks/Types/ScalarTypeDefinitionBlock.php | 2 +- .../Blocks/Types/UnionTypeDefinitionBlock.php | 2 +- 7 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php index 87808026..11e3339a 100644 --- a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php @@ -56,8 +56,9 @@ protected function content(): string { $eol = $this->eol(); $indent = $this->indent(); $name = $this->getName(); - $body = (string) $this->body($this->getUsed() + mb_strlen($name)); - $fields = (string) $this->fields(); + $used = $this->getUsed() + mb_strlen($name); + $body = (string) $this->body($used); + $fields = (string) $this->fields($used + mb_strlen($body)); $directives = $this->directives(); $description = (string) $this->description($directives); $directives = (string) $directives; @@ -86,7 +87,7 @@ abstract protected function type(): string|null; abstract protected function body(int $used): Block|string|null; - abstract protected function fields(): Block|string|null; + abstract protected function fields(int $used): Block|string|null; protected function directives(): DirectiveNodeList { $definition = $this->getDefinition(); diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php index 2b54be56..51365bb4 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php @@ -42,7 +42,7 @@ protected function body(int $used): Block|string|null { return "{$space}{$values}"; } - protected function fields(): Block|string|null { + protected function fields(int $used): Block|string|null { return null; } } diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php index c49ebd96..42a5d5a7 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php @@ -30,7 +30,7 @@ protected function body(int $used): Block|string|null { return null; } - protected function fields(): Block|string|null { + protected function fields(int $used): Block|string|null { return null; } } diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php index 2dbb13dc..281f10ea 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php @@ -48,7 +48,7 @@ protected function body(int $used): Block|string|null { return "{$args}:{$space}{$type}"; } - protected function fields(): Block|string|null { + protected function fields(int $used): Block|string|null { return null; } } diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php index d90f5226..bb9f92c0 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php @@ -58,7 +58,7 @@ protected function body(int $used): Block|string|null { return $body; } - protected function fields(): Block|string|null { + protected function fields(int $used): Block|string|null { return null; } } diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php index 874cd4eb..a4608377 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php @@ -31,7 +31,7 @@ protected function body(int $used): Block|string|null { return null; } - protected function fields(): Block|string|null { + protected function fields(int $used): Block|string|null { return null; } } diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php index db442dc8..65c023ae 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php @@ -51,7 +51,7 @@ protected function body(int $used): Block|string|null { return $body; } - protected function fields(): Block|string|null { + protected function fields(int $used): Block|string|null { return null; } } From 1efe1600609eed3608821e1ad62375c7bb23137e Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 9 Jan 2022 13:50:30 +0400 Subject: [PATCH 41/90] `InterfaceTypeDefinition` support. --- src/SchemaPrinter/Blocks/BlockList.php | 6 +- .../Blocks/Types/DefinitionBlock.php | 19 +- .../Blocks/Types/ImplementsInterfacesList.php | 74 ++++ .../Types/InterfaceTypeDefinitionBlock.php | 63 +++ .../InterfaceTypeDefinitionBlockTest.php | 396 ++++++++++++++++++ .../Blocks/Types/UnionTypeDefinitionBlock.php | 6 +- src/SchemaPrinter/Settings.php | 6 + .../Settings/DefaultSettings.php | 4 + 8 files changed, 563 insertions(+), 11 deletions(-) create mode 100644 src/SchemaPrinter/Blocks/Types/ImplementsInterfacesList.php create mode 100644 src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php create mode 100644 src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php diff --git a/src/SchemaPrinter/Blocks/BlockList.php b/src/SchemaPrinter/Blocks/BlockList.php index c98884c0..67525f23 100644 --- a/src/SchemaPrinter/Blocks/BlockList.php +++ b/src/SchemaPrinter/Blocks/BlockList.php @@ -151,7 +151,11 @@ protected function content(): string { if ($listPrefix || $listSuffix) { $eol = $isMultiline ? $this->eol() : ''; $indent = $isMultiline ? $this->indent() : ''; - $content = "{$listPrefix}{$eol}{$content}{$eol}{$indent}{$listSuffix}"; + $content = "{$listPrefix}{$eol}{$content}"; + + if ($listSuffix) { + $content .= "{$eol}{$indent}{$listSuffix}"; + } } // Return diff --git a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php index 11e3339a..2c712a09 100644 --- a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php @@ -54,14 +54,17 @@ protected function getDefinition(): Type|FieldDefinition|EnumValueDefinition|Fie protected function content(): string { $eol = $this->eol(); + $space = $this->space(); $indent = $this->indent(); $name = $this->getName(); - $used = $this->getUsed() + mb_strlen($name); + $used = $this->getUsed() + mb_strlen($name) + mb_strlen($space); $body = (string) $this->body($used); $fields = (string) $this->fields($used + mb_strlen($body)); $directives = $this->directives(); $description = (string) $this->description($directives); - $directives = (string) $directives; + $directives = $this->getSettings()->isIncludeDirectives() + ? (string) $directives + : ''; $content = ''; if ($description) { @@ -70,16 +73,18 @@ protected function content(): string { $content .= "{$name}{$body}"; - if ($directives && $this->getSettings()->isIncludeDirectives()) { + if ($directives) { $content .= "{$eol}{$indent}{$directives}"; + } - if ($fields) { - $content .= "{$eol}{$indent}"; + if ($fields) { + if ($directives || $this->isStringMultiline($content)) { + $content .= "{$eol}{$indent}{$fields}"; + } else { + $content .= "{$space}{$fields}"; } } - $content .= $fields; - return $content; } diff --git a/src/SchemaPrinter/Blocks/Types/ImplementsInterfacesList.php b/src/SchemaPrinter/Blocks/Types/ImplementsInterfacesList.php new file mode 100644 index 00000000..e47c3178 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/ImplementsInterfacesList.php @@ -0,0 +1,74 @@ + + */ +class ImplementsInterfacesList extends BlockList { + /** + * @param Traversable|array $fields + */ + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + Traversable|array $fields, + ) { + parent::__construct($dispatcher, $settings, $level, $used); + + foreach ($fields as $field) { + $this[$field->name] = new TypeBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel() + 1, + $this->getUsed(), + $field, + ); + } + } + + protected function getSeparator(): string { + return "{$this->space()}&{$this->space()}"; + } + + protected function getMultilineItemPrefix(): string { + return "&{$this->space()}"; + } + + protected function isNormalized(): bool { + return $this->getSettings()->isNormalizeInterfaces(); + } + + protected function content(): string { + $prefix = 'implements'; + $content = parent::content(); + + if ($content) { + if ($this->isStringMultiline($content)) { + $eol = $this->eol(); + $indent = $this->indent(); + $content = "{$prefix}{$eol}{$indent}{$content}"; + } else { + $space = $this->space(); + $content = "{$prefix}{$space}{$content}"; + } + } + + return $content; + } + + protected function getUsed(): int { + return parent::getUsed() + mb_strlen("implements{$this->space()}"); + } +} diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php new file mode 100644 index 00000000..f395554a --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php @@ -0,0 +1,63 @@ + + */ +class InterfaceTypeDefinitionBlock extends DefinitionBlock { + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + InterfaceType $definition, + ) { + parent::__construct($dispatcher, $settings, $level, $used, $definition); + } + + protected function type(): string|null { + return 'interface'; + } + + protected function body(int $used): Block|string|null { + $definition = $this->getDefinition(); + $space = $this->space(); + $interfaces = new ImplementsInterfacesList( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel() + 1, + $used + mb_strlen($space), + $definition->getInterfaces(), + ); + + if (!$interfaces->isEmpty()) { + $interfaces = "{$space}{$interfaces}"; + } + + return $interfaces; + } + + protected function fields(int $used): Block|string|null { + $definition = $this->getDefinition(); + $space = $this->space(); + $fields = new FieldsDefinitionList( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $used + mb_strlen($space), + $definition->getFields(), + ); + + return $fields; + } +} diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php new file mode 100644 index 00000000..203bfac2 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php @@ -0,0 +1,396 @@ + + // ========================================================================= + /** + * @covers ::__toString + * + * @dataProvider dataProviderToString + */ + public function testToString( + string $expected, + Settings $settings, + int $level, + int $used, + InterfaceType $definition, + ): void { + $actual = (string) (new InterfaceTypeDefinitionBlock(new Dispatcher(), $settings, $level, $used, $definition)); + $parsed = Parser::interfaceTypeDefinition($actual); + + self::assertEquals($expected, $actual); + self::assertInstanceOf(InterfaceTypeDefinitionNode::class, $parsed); + } + + /** + * @covers ::__toString + */ + public function testToStringEvent(): void { + $spy = Mockery::spy(static fn (Event $event) => null); + $settings = new DefaultSettings(); + $dispatcher = new Dispatcher(); + $definition = new InterfaceType([ + 'name' => 'A', + 'fields' => [ + 'b' => [ + 'name' => 'b', + 'type' => new ObjectType([ + 'name' => 'B', + ]), + 'args' => [ + 'c' => [ + 'type' => new ObjectType([ + 'name' => 'C', + ]), + ], + ], + ], + ], + ]); + + $dispatcher->attach(Closure::fromCallable($spy)); + + self::assertNotNull( + (string) (new InterfaceTypeDefinitionBlock($dispatcher, $settings, 0, 0, $definition)), + ); + + $spy + ->shouldHaveBeenCalled() + ->withArgs(static function (Event $event): bool { + return $event instanceof TypeUsed + && $event->name === 'B'; + }) + ->once(); + $spy + ->shouldHaveBeenCalled() + ->withArgs(static function (Event $event): bool { + return $event instanceof TypeUsed + && $event->name === 'C'; + }) + ->once(); + $spy + ->shouldHaveBeenCalled() + ->twice(3); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderToString(): array { + return [ + 'description + directives' => [ + <<<'STRING' + """ + Description + """ + interface Test + @a + STRING, + new DefaultSettings(), + 0, + 0, + new InterfaceType([ + 'name' => 'Test', + 'astNode' => Parser::interfaceTypeDefinition('interface Test @a'), + 'description' => 'Description', + ]), + ], + 'description + directives + fields' => [ + <<<'STRING' + """ + Description + """ + interface Test + @a + { + c: C + + """ + Description + """ + b(b: Int): B + + a(a: Int): A + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + new InterfaceType([ + 'name' => 'Test', + 'astNode' => Parser::interfaceTypeDefinition('interface Test @a'), + 'description' => 'Description', + 'fields' => [ + [ + 'name' => 'c', + 'type' => new ObjectType([ + 'name' => 'C', + ]), + ], + [ + 'name' => 'b', + 'type' => new ObjectType([ + 'name' => 'B', + ]), + 'args' => [ + 'b' => [ + 'type' => Type::int(), + ], + ], + 'description' => 'Description', + ], + [ + 'name' => 'a', + 'type' => new ObjectType([ + 'name' => 'A', + ]), + 'args' => [ + 'a' => [ + 'type' => Type::int(), + ], + ], + ], + ], + ]), + ], + 'fields' => [ + <<<'STRING' + interface Test { + a: String + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + new InterfaceType([ + 'name' => 'Test', + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::string(), + ], + ], + ]), + ], + 'implements + directives + fields' => [ + <<<'STRING' + interface Test implements B & A + @a + { + a: String + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + new InterfaceType([ + 'name' => 'Test', + 'astNode' => Parser::interfaceTypeDefinition('interface Test @a'), + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::string(), + ], + ], + 'interfaces' => [ + new InterfaceType(['name' => 'B']), + new InterfaceType(['name' => 'A']), + ], + ]), + ], + 'implements(multiline) + directives + fields' => [ + <<<'STRING' + interface Test implements + & B + & A + @a + { + a: String + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 120, + new InterfaceType([ + 'name' => 'Test', + 'astNode' => Parser::interfaceTypeDefinition('interface Test @a'), + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::string(), + ], + ], + 'interfaces' => [ + new InterfaceType(['name' => 'B']), + new InterfaceType(['name' => 'A']), + ], + ]), + ], + 'implements(multiline) + fields' => [ + <<<'STRING' + interface Test implements + & B + & A + { + a: String + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 120, + new InterfaceType([ + 'name' => 'Test', + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::string(), + ], + ], + 'interfaces' => [ + new InterfaceType(['name' => 'B']), + new InterfaceType(['name' => 'A']), + ], + ]), + ], + 'implements + fields' => [ + <<<'STRING' + interface Test implements B & A { + a: String + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + new InterfaceType([ + 'name' => 'Test', + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::string(), + ], + ], + 'interfaces' => [ + new InterfaceType(['name' => 'B']), + new InterfaceType(['name' => 'A']), + ], + ]), + ], + 'implements(normalized) + fields' => [ + <<<'STRING' + interface Test implements + & A + & B + { + a: String + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + + public function isNormalizeInterfaces(): bool { + return true; + } + }, + 0, + 120, + new InterfaceType([ + 'name' => 'Test', + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::string(), + ], + ], + 'interfaces' => [ + new InterfaceType(['name' => 'B']), + new InterfaceType(['name' => 'A']), + ], + ]), + ], + 'indent' => [ + <<<'STRING' + interface Test implements + & A + & B + { + a: String + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + + public function isNormalizeInterfaces(): bool { + return true; + } + }, + 1, + 120, + new InterfaceType([ + 'name' => 'Test', + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::string(), + ], + ], + 'interfaces' => [ + new InterfaceType(['name' => 'B']), + new InterfaceType(['name' => 'A']), + ], + ]), + ], + ]; + } + // +} diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php index 65c023ae..f58e93a9 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php @@ -43,12 +43,12 @@ protected function body(int $used): Block|string|null { if ($types->isMultiline()) { $eol = $this->eol(); $indent = $this->indent($this->getLevel() + 1); - $body = "{$space}={$eol}{$indent}{$types}"; + $types = "{$space}={$eol}{$indent}{$types}"; } else { - $body = "{$equal}{$types}"; + $types = "{$equal}{$types}"; } - return $body; + return $types; } protected function fields(int $used): Block|string|null { diff --git a/src/SchemaPrinter/Settings.php b/src/SchemaPrinter/Settings.php index dac0cb39..92b30b16 100644 --- a/src/SchemaPrinter/Settings.php +++ b/src/SchemaPrinter/Settings.php @@ -45,6 +45,12 @@ public function isNormalizeUnions(): bool; */ public function isNormalizeEnums(): bool; + /** + * If `false` implemented interface list will be printed in the original + * order if `true` they will be sorted by name. + */ + public function isNormalizeInterfaces(): bool; + /** * If `false` fields will be printed in the original order if `true` they * will be sorted by name. diff --git a/src/SchemaPrinter/Settings/DefaultSettings.php b/src/SchemaPrinter/Settings/DefaultSettings.php index 81310def..b91e9bc4 100644 --- a/src/SchemaPrinter/Settings/DefaultSettings.php +++ b/src/SchemaPrinter/Settings/DefaultSettings.php @@ -59,6 +59,10 @@ public function isNormalizeEnums(): bool { return false; } + public function isNormalizeInterfaces(): bool { + return false; + } + public function isNormalizeFields(): bool { return false; } From bb7d85c4c4e44d1c113c15308ea3904caf07939a Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 9 Jan 2022 13:58:36 +0400 Subject: [PATCH 42/90] `ObjectTypeDefinition` support. --- .../Types/InterfaceTypeDefinitionBlock.php | 37 +- .../Types/ObjectTypeDefinitionBlock.php | 28 ++ .../Types/ObjectTypeDefinitionBlockTest.php | 395 ++++++++++++++++++ .../Blocks/Types/TypeDefinitionBlock.php | 60 +++ 4 files changed, 484 insertions(+), 36 deletions(-) create mode 100644 src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php create mode 100644 src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php create mode 100644 src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php index f395554a..78386e77 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php @@ -4,17 +4,14 @@ use GraphQL\Type\Definition\InterfaceType; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use function mb_strlen; - /** * @internal * * @extends DefinitionBlock */ -class InterfaceTypeDefinitionBlock extends DefinitionBlock { +class InterfaceTypeDefinitionBlock extends TypeDefinitionBlock { public function __construct( Dispatcher $dispatcher, Settings $settings, @@ -28,36 +25,4 @@ public function __construct( protected function type(): string|null { return 'interface'; } - - protected function body(int $used): Block|string|null { - $definition = $this->getDefinition(); - $space = $this->space(); - $interfaces = new ImplementsInterfacesList( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel() + 1, - $used + mb_strlen($space), - $definition->getInterfaces(), - ); - - if (!$interfaces->isEmpty()) { - $interfaces = "{$space}{$interfaces}"; - } - - return $interfaces; - } - - protected function fields(int $used): Block|string|null { - $definition = $this->getDefinition(); - $space = $this->space(); - $fields = new FieldsDefinitionList( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel(), - $used + mb_strlen($space), - $definition->getFields(), - ); - - return $fields; - } } diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php new file mode 100644 index 00000000..c25cbf01 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php @@ -0,0 +1,28 @@ + + */ +class ObjectTypeDefinitionBlock extends TypeDefinitionBlock { + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + ObjectType $definition, + ) { + parent::__construct($dispatcher, $settings, $level, $used, $definition); + } + + protected function type(): string|null { + return 'type'; + } +} diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php new file mode 100644 index 00000000..a734425c --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php @@ -0,0 +1,395 @@ + + // ========================================================================= + /** + * @covers ::__toString + * + * @dataProvider dataProviderToString + */ + public function testToString( + string $expected, + Settings $settings, + int $level, + int $used, + ObjectType $definition, + ): void { + $actual = (string) (new ObjectTypeDefinitionBlock(new Dispatcher(), $settings, $level, $used, $definition)); + $parsed = Parser::objectTypeDefinition($actual); + + self::assertEquals($expected, $actual); + self::assertInstanceOf(ObjectTypeDefinitionNode::class, $parsed); + } + + /** + * @covers ::__toString + */ + public function testToStringEvent(): void { + $spy = Mockery::spy(static fn (Event $event) => null); + $settings = new DefaultSettings(); + $dispatcher = new Dispatcher(); + $definition = new ObjectType([ + 'name' => 'A', + 'fields' => [ + 'b' => [ + 'name' => 'b', + 'type' => new ObjectType([ + 'name' => 'B', + ]), + 'args' => [ + 'c' => [ + 'type' => new ObjectType([ + 'name' => 'C', + ]), + ], + ], + ], + ], + ]); + + $dispatcher->attach(Closure::fromCallable($spy)); + + self::assertNotNull( + (string) (new ObjectTypeDefinitionBlock($dispatcher, $settings, 0, 0, $definition)), + ); + + $spy + ->shouldHaveBeenCalled() + ->withArgs(static function (Event $event): bool { + return $event instanceof TypeUsed + && $event->name === 'B'; + }) + ->once(); + $spy + ->shouldHaveBeenCalled() + ->withArgs(static function (Event $event): bool { + return $event instanceof TypeUsed + && $event->name === 'C'; + }) + ->once(); + $spy + ->shouldHaveBeenCalled() + ->twice(3); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderToString(): array { + return [ + 'description + directives' => [ + <<<'STRING' + """ + Description + """ + type Test + @a + STRING, + new DefaultSettings(), + 0, + 0, + new ObjectType([ + 'name' => 'Test', + 'astNode' => Parser::objectTypeDefinition('type Test @a'), + 'description' => 'Description', + ]), + ], + 'description + directives + fields' => [ + <<<'STRING' + """ + Description + """ + type Test + @a + { + c: C + + """ + Description + """ + b(b: Int): B + + a(a: Int): A + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + new ObjectType([ + 'name' => 'Test', + 'astNode' => Parser::objectTypeDefinition('type Test @a'), + 'description' => 'Description', + 'fields' => [ + [ + 'name' => 'c', + 'type' => new ObjectType([ + 'name' => 'C', + ]), + ], + [ + 'name' => 'b', + 'type' => new ObjectType([ + 'name' => 'B', + ]), + 'args' => [ + 'b' => [ + 'type' => Type::int(), + ], + ], + 'description' => 'Description', + ], + [ + 'name' => 'a', + 'type' => new ObjectType([ + 'name' => 'A', + ]), + 'args' => [ + 'a' => [ + 'type' => Type::int(), + ], + ], + ], + ], + ]), + ], + 'fields' => [ + <<<'STRING' + type Test { + a: String + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + new ObjectType([ + 'name' => 'Test', + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::string(), + ], + ], + ]), + ], + 'implements + directives + fields' => [ + <<<'STRING' + type Test implements B & A + @a + { + a: String + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + new ObjectType([ + 'name' => 'Test', + 'astNode' => Parser::objectTypeDefinition('type Test @a'), + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::string(), + ], + ], + 'interfaces' => [ + new ObjectType(['name' => 'B']), + new ObjectType(['name' => 'A']), + ], + ]), + ], + 'implements(multiline) + directives + fields' => [ + <<<'STRING' + type Test implements + & B + & A + @a + { + a: String + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 120, + new ObjectType([ + 'name' => 'Test', + 'astNode' => Parser::objectTypeDefinition('type Test @a'), + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::string(), + ], + ], + 'interfaces' => [ + new ObjectType(['name' => 'B']), + new ObjectType(['name' => 'A']), + ], + ]), + ], + 'implements(multiline) + fields' => [ + <<<'STRING' + type Test implements + & B + & A + { + a: String + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 120, + new ObjectType([ + 'name' => 'Test', + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::string(), + ], + ], + 'interfaces' => [ + new ObjectType(['name' => 'B']), + new ObjectType(['name' => 'A']), + ], + ]), + ], + 'implements + fields' => [ + <<<'STRING' + type Test implements B & A { + a: String + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + new ObjectType([ + 'name' => 'Test', + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::string(), + ], + ], + 'interfaces' => [ + new ObjectType(['name' => 'B']), + new ObjectType(['name' => 'A']), + ], + ]), + ], + 'implements(normalized) + fields' => [ + <<<'STRING' + type Test implements + & A + & B + { + a: String + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + + public function isNormalizeInterfaces(): bool { + return true; + } + }, + 0, + 120, + new ObjectType([ + 'name' => 'Test', + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::string(), + ], + ], + 'interfaces' => [ + new ObjectType(['name' => 'B']), + new ObjectType(['name' => 'A']), + ], + ]), + ], + 'indent' => [ + <<<'STRING' + type Test implements + & A + & B + { + a: String + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + + public function isNormalizeInterfaces(): bool { + return true; + } + }, + 1, + 120, + new ObjectType([ + 'name' => 'Test', + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::string(), + ], + ], + 'interfaces' => [ + new ObjectType(['name' => 'B']), + new ObjectType(['name' => 'A']), + ], + ]), + ], + ]; + } + // +} diff --git a/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php new file mode 100644 index 00000000..bdb64d48 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php @@ -0,0 +1,60 @@ + + */ +abstract class TypeDefinitionBlock extends DefinitionBlock { + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + InterfaceType|ObjectType $definition, + ) { + parent::__construct($dispatcher, $settings, $level, $used, $definition); + } + + protected function body(int $used): Block|string|null { + $definition = $this->getDefinition(); + $space = $this->space(); + $interfaces = new ImplementsInterfacesList( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel() + 1, + $used + mb_strlen($space), + $definition->getInterfaces(), + ); + + if (!$interfaces->isEmpty()) { + $interfaces = "{$space}{$interfaces}"; + } + + return $interfaces; + } + + protected function fields(int $used): Block|string|null { + $definition = $this->getDefinition(); + $space = $this->space(); + $fields = new FieldsDefinitionList( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $used + mb_strlen($space), + $definition->getFields(), + ); + + return $fields; + } +} From 57dbba979b50e9d094854b87f4e5ee017247ea70 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 9 Jan 2022 15:02:22 +0400 Subject: [PATCH 43/90] `DirectiveDefinition` support. --- .../Blocks/Types/DefinitionBlock.php | 15 +- .../Blocks/Types/DirectiveDefinitionBlock.php | 83 +++++ .../Types/DirectiveDefinitionBlockTest.php | 283 ++++++++++++++++++ .../Blocks/Types/DirectiveLocationBlock.php | 35 +++ .../Blocks/Types/DirectiveLocationsList.php | 39 +++ .../Blocks/Types/ImplementsInterfacesList.php | 70 +---- .../InterfaceTypeDefinitionBlockTest.php | 2 +- .../Types/ObjectTypeDefinitionBlockTest.php | 2 +- .../Blocks/Types/UnionMemberTypesList.php | 46 +-- src/SchemaPrinter/Blocks/Types/UsageList.php | 81 +++++ src/SchemaPrinter/Settings.php | 4 +- .../Settings/DefaultSettings.php | 2 +- 12 files changed, 566 insertions(+), 96 deletions(-) create mode 100644 src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlock.php create mode 100644 src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php create mode 100644 src/SchemaPrinter/Blocks/Types/DirectiveLocationBlock.php create mode 100644 src/SchemaPrinter/Blocks/Types/DirectiveLocationsList.php create mode 100644 src/SchemaPrinter/Blocks/Types/UsageList.php diff --git a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php index 2c712a09..513b4f8e 100644 --- a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php @@ -2,6 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; +use GraphQL\Type\Definition\Directive; use GraphQL\Type\Definition\EnumValueDefinition; use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\FieldDefinition; @@ -17,7 +18,7 @@ /** * @internal * - * @template TType of Type|FieldDefinition|EnumValueDefinition|FieldArgument + * @template TType of Type|FieldDefinition|EnumValueDefinition|FieldArgument|Directive */ abstract class DefinitionBlock extends Block implements Named { /** @@ -28,13 +29,13 @@ public function __construct( Settings $settings, int $level, int $used, - private Type|FieldDefinition|EnumValueDefinition|FieldArgument $definition, + private Type|FieldDefinition|EnumValueDefinition|FieldArgument|Directive $definition, ) { parent::__construct($dispatcher, $settings, $level, $used); } public function getName(): string { - $name = $this->getDefinition()->name; + $name = $this->name(); $type = $this->type(); if ($type) { @@ -48,7 +49,7 @@ public function getName(): string { /** * @return TType */ - protected function getDefinition(): Type|FieldDefinition|EnumValueDefinition|FieldArgument { + protected function getDefinition(): Type|FieldDefinition|EnumValueDefinition|FieldArgument|Directive { return $this->definition; } @@ -78,7 +79,7 @@ protected function content(): string { } if ($fields) { - if ($directives || $this->isStringMultiline($content)) { + if ($directives || $this->isStringMultiline($body)) { $content .= "{$eol}{$indent}{$fields}"; } else { $content .= "{$space}{$fields}"; @@ -88,6 +89,10 @@ protected function content(): string { return $content; } + protected function name(): string { + return $this->getDefinition()->name; + } + abstract protected function type(): string|null; abstract protected function body(int $used): Block|string|null; diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlock.php new file mode 100644 index 00000000..354528e5 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlock.php @@ -0,0 +1,83 @@ + + */ +class DirectiveDefinitionBlock extends DefinitionBlock { + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + Directive $definition, + ) { + parent::__construct($dispatcher, $settings, $level, $used, $definition); + } + + protected function type(): string|null { + return 'directive'; + } + + protected function name(): string { + return '@'.parent::name(); + } + + protected function body(int $used): Block|string|null { + $definition = $this->getDefinition(); + $eol = $this->eol(); + $space = $this->space(); + $indent = $this->indent(); + $repeatable = 'repeatable'; + $used = $used + mb_strlen($repeatable) + 2 * mb_strlen($space); + $args = new ArgumentsDefinitionList( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $used, + $definition->args, + ); + $locations = new DirectiveLocationsList( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel() + 1, + $used + $args->getLength(), + $definition->locations, + ); + $content = "{$args}"; + + if ($args->isMultiline()) { + $content .= "{$eol}{$indent}"; + } + + if ($definition->isRepeatable) { + if (!$args->isMultiline()) { + $content .= "{$space}"; + } + + $content .= "{$repeatable}{$space}{$locations}"; + } else { + if (!$args->isMultiline()) { + $content .= "{$space}"; + } + + $content .= "{$locations}"; + } + + return $content; + } + + protected function fields(int $used): Block|string|null { + return null; + } +} diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php new file mode 100644 index 00000000..9debb5c8 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php @@ -0,0 +1,283 @@ + + // ========================================================================= + /** + * @covers ::__toString + * + * @dataProvider dataProviderToString + */ + public function testToString( + string $expected, + Settings $settings, + int $level, + int $used, + Directive $definition, + ): void { + $actual = (string) (new DirectiveDefinitionBlock(new Dispatcher(), $settings, $level, $used, $definition)); + $parsed = Parser::directiveDefinition($actual); + + self::assertEquals($expected, $actual); + self::assertInstanceOf(DirectiveDefinitionNode::class, $parsed); + } + + /** + * @covers ::__toString + */ + public function testToStringEvent(): void { + $spy = Mockery::spy(static fn (Event $event) => null); + $settings = new DefaultSettings(); + $dispatcher = new Dispatcher(); + $definition = new Directive([ + 'name' => 'A', + 'args' => [ + 'a' => [ + 'type' => new ObjectType(['name' => 'B']), + ], + ], + 'locations' => [ + DirectiveLocation::FIELD, + ], + ]); + + $dispatcher->attach(Closure::fromCallable($spy)); + + self::assertNotNull( + (string) (new DirectiveDefinitionBlock($dispatcher, $settings, 0, 0, $definition)), + ); + + $spy + ->shouldHaveBeenCalled() + ->withArgs(static function (Event $event): bool { + return $event instanceof TypeUsed + && $event->name === 'B'; + }) + ->once(); + $spy + ->shouldHaveBeenCalled() + ->once(); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderToString(): array { + return [ + 'description' => [ + <<<'STRING' + """ + Description + """ + directive @test on ARGUMENT_DEFINITION | ENUM + STRING, + new DefaultSettings(), + 0, + 0, + new Directive([ + 'name' => 'test', + 'description' => 'Description', + 'locations' => [ + DirectiveLocation::ARGUMENT_DEFINITION, + DirectiveLocation::ENUM, + ], + ]), + ], + 'repeatable' => [ + <<<'STRING' + directive @test repeatable on ARGUMENT_DEFINITION | ENUM + STRING, + new DefaultSettings(), + 0, + 0, + new Directive([ + 'name' => 'test', + 'locations' => [ + DirectiveLocation::ARGUMENT_DEFINITION, + DirectiveLocation::ENUM, + ], + 'isRepeatable' => true, + ]), + ], + 'args' => [ + <<<'STRING' + directive @test(a: String) repeatable on ARGUMENT_DEFINITION | ENUM + STRING, + new DefaultSettings(), + 0, + 0, + new Directive([ + 'name' => 'test', + 'args' => [ + 'a' => [ + 'type' => Type::string(), + ], + ], + 'locations' => [ + DirectiveLocation::ARGUMENT_DEFINITION, + DirectiveLocation::ENUM, + ], + 'isRepeatable' => true, + ]), + ], + 'multiline + repeatable' => [ + <<<'STRING' + directive @test( + a: String + ) + repeatable on + | ARGUMENT_DEFINITION + | ENUM + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 120, + new Directive([ + 'name' => 'test', + 'args' => [ + 'a' => [ + 'type' => Type::string(), + ], + ], + 'locations' => [ + DirectiveLocation::ARGUMENT_DEFINITION, + DirectiveLocation::ENUM, + ], + 'isRepeatable' => true, + ]), + ], + 'multiline' => [ + <<<'STRING' + directive @test( + a: String + ) + on + | ARGUMENT_DEFINITION + | ENUM + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 120, + new Directive([ + 'name' => 'test', + 'args' => [ + 'a' => [ + 'type' => Type::string(), + ], + ], + 'locations' => [ + DirectiveLocation::ARGUMENT_DEFINITION, + DirectiveLocation::ENUM, + ], + ]), + ], + 'multiline (no args)' => [ + <<<'STRING' + directive @test on + | ARGUMENT_DEFINITION + | ENUM + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 60, + new Directive([ + 'name' => 'test', + 'locations' => [ + DirectiveLocation::ARGUMENT_DEFINITION, + DirectiveLocation::ENUM, + ], + ]), + ], + 'indent' => [ + <<<'STRING' + directive @test( + a: String + ) + on + | ARGUMENT_DEFINITION + | ENUM + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 1, + 120, + new Directive([ + 'name' => 'test', + 'args' => [ + 'a' => [ + 'type' => Type::string(), + ], + ], + 'locations' => [ + DirectiveLocation::ARGUMENT_DEFINITION, + DirectiveLocation::ENUM, + ], + ]), + ], + 'normalized' => [ + <<<'STRING' + directive @test on ENUM | INPUT_FIELD_DEFINITION | OBJECT + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + + public function isNormalizeDirectiveLocations(): bool { + return true; + } + }, + 0, + 0, + new Directive([ + 'name' => 'test', + 'locations' => [ + DirectiveLocation::OBJECT, + DirectiveLocation::ENUM, + DirectiveLocation::INPUT_FIELD_DEFINITION, + ], + ]), + ], + ]; + } + // +} diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveLocationBlock.php b/src/SchemaPrinter/Blocks/Types/DirectiveLocationBlock.php new file mode 100644 index 00000000..6a168467 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/DirectiveLocationBlock.php @@ -0,0 +1,35 @@ +getLocation(); + } + + protected function getLocation(): string { + return $this->location; + } + + protected function content(): string { + return $this->getLocation(); + } +} diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveLocationsList.php b/src/SchemaPrinter/Blocks/Types/DirectiveLocationsList.php new file mode 100644 index 00000000..0b7bc4c0 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/DirectiveLocationsList.php @@ -0,0 +1,39 @@ + + */ +class DirectiveLocationsList extends UsageList { + protected function block(mixed $item): Block { + return new DirectiveLocationBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel() + 1, + $this->getUsed(), + $item, + ); + } + + protected function separator(): string { + return '|'; + } + + protected function prefix(): string { + return 'on'; + } + + protected function isNormalized(): bool { + return $this->getSettings()->isNormalizeDirectiveLocations(); + } +} diff --git a/src/SchemaPrinter/Blocks/Types/ImplementsInterfacesList.php b/src/SchemaPrinter/Blocks/Types/ImplementsInterfacesList.php index e47c3178..ba61b5bf 100644 --- a/src/SchemaPrinter/Blocks/Types/ImplementsInterfacesList.php +++ b/src/SchemaPrinter/Blocks/Types/ImplementsInterfacesList.php @@ -3,72 +3,32 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\InterfaceType; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use Traversable; - -use function mb_strlen; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; /** * @internal - * @extends BlockList + * @extends UsageList */ -class ImplementsInterfacesList extends BlockList { - /** - * @param Traversable|array $fields - */ - public function __construct( - Dispatcher $dispatcher, - Settings $settings, - int $level, - int $used, - Traversable|array $fields, - ) { - parent::__construct($dispatcher, $settings, $level, $used); - - foreach ($fields as $field) { - $this[$field->name] = new TypeBlock( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel() + 1, - $this->getUsed(), - $field, - ); - } +class ImplementsInterfacesList extends UsageList { + protected function block(mixed $item): Block { + return new TypeBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel() + 1, + $this->getUsed(), + $item, + ); } - protected function getSeparator(): string { - return "{$this->space()}&{$this->space()}"; + protected function separator(): string { + return '&'; } - protected function getMultilineItemPrefix(): string { - return "&{$this->space()}"; + protected function prefix(): string { + return 'implements'; } protected function isNormalized(): bool { return $this->getSettings()->isNormalizeInterfaces(); } - - protected function content(): string { - $prefix = 'implements'; - $content = parent::content(); - - if ($content) { - if ($this->isStringMultiline($content)) { - $eol = $this->eol(); - $indent = $this->indent(); - $content = "{$prefix}{$eol}{$indent}{$content}"; - } else { - $space = $this->space(); - $content = "{$prefix}{$space}{$content}"; - } - } - - return $content; - } - - protected function getUsed(): int { - return parent::getUsed() + mb_strlen("implements{$this->space()}"); - } } diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php index 203bfac2..e3f7867e 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php @@ -90,7 +90,7 @@ public function testToStringEvent(): void { ->once(); $spy ->shouldHaveBeenCalled() - ->twice(3); + ->twice(); } // diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php index a734425c..66561dad 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php @@ -89,7 +89,7 @@ public function testToStringEvent(): void { ->once(); $spy ->shouldHaveBeenCalled() - ->twice(3); + ->twice(); } // diff --git a/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php b/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php index 51fdc51d..73af7a47 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php +++ b/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php @@ -3,45 +3,29 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\ObjectType; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use Traversable; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; /** * @internal - * @extends BlockList + * @extends UsageList */ -class UnionMemberTypesList extends BlockList { - /** - * @param Traversable|array $types - */ - public function __construct( - Dispatcher $dispatcher, - Settings $settings, - int $level, - int $used, - Traversable|array $types, - ) { - parent::__construct($dispatcher, $settings, $level, $used); - - foreach ($types as $type) { - $this[$type->name] = new TypeBlock( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel() + 1, - $this->getUsed(), - $type, - ); - } +class UnionMemberTypesList extends UsageList { + protected function block(mixed $item): Block { + return new TypeBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel() + 1, + $this->getUsed(), + $item, + ); } - protected function getSeparator(): string { - return "{$this->space()}|{$this->space()}"; + protected function separator(): string { + return '|'; } - protected function getMultilineItemPrefix(): string { - return "|{$this->space()}"; + protected function prefix(): string { + return ''; } protected function isNormalized(): bool { diff --git a/src/SchemaPrinter/Blocks/Types/UsageList.php b/src/SchemaPrinter/Blocks/Types/UsageList.php new file mode 100644 index 00000000..c190ccf6 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/UsageList.php @@ -0,0 +1,81 @@ + + */ +abstract class UsageList extends BlockList { + /** + * @param Traversable|array $items + */ + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + Traversable|array $items, + ) { + parent::__construct($dispatcher, $settings, $level, $used); + + foreach ($items as $item) { + $this[] = $this->block($item); + } + } + + /** + * @param TType $item + */ + abstract protected function block(mixed $item): Block; + + abstract protected function separator(): string; + + abstract protected function prefix(): string; + + protected function getSeparator(): string { + return "{$this->space()}{$this->separator()}{$this->space()}"; + } + + protected function getMultilineItemPrefix(): string { + return "{$this->separator()}{$this->space()}"; + } + + protected function content(): string { + $prefix = $this->prefix(); + $content = parent::content(); + + if ($content) { + if ($this->isStringMultiline($content)) { + $eol = $this->eol(); + $indent = $this->indent(); + + if ($prefix) { + $content = "{$prefix}{$eol}{$indent}{$content}"; + } + } else { + $space = $this->space(); + + if ($prefix) { + $content = "{$prefix}{$space}{$content}"; + } + } + } + + return $content; + } + + protected function getUsed(): int { + return parent::getUsed() + mb_strlen("{$this->prefix()}{$this->space()}"); + } +} diff --git a/src/SchemaPrinter/Settings.php b/src/SchemaPrinter/Settings.php index 92b30b16..7bfb7ed2 100644 --- a/src/SchemaPrinter/Settings.php +++ b/src/SchemaPrinter/Settings.php @@ -66,10 +66,10 @@ public function isNormalizeArguments(): bool; public function isNormalizeDescription(): bool; /** - * If `false` directive definitions will be printed in the original order if + * If `false` directive locations will be printed in the original order if * `true` they will be sorted by name. */ - public function isNormalizeDirectiveDefinitions(): bool; + public function isNormalizeDirectiveLocations(): bool; /** * Used to determine should the directive included in output or not. diff --git a/src/SchemaPrinter/Settings/DefaultSettings.php b/src/SchemaPrinter/Settings/DefaultSettings.php index b91e9bc4..f007a712 100644 --- a/src/SchemaPrinter/Settings/DefaultSettings.php +++ b/src/SchemaPrinter/Settings/DefaultSettings.php @@ -75,7 +75,7 @@ public function isNormalizeDescription(): bool { return false; } - public function isNormalizeDirectiveDefinitions(): bool { + public function isNormalizeDirectiveLocations(): bool { return false; } From 0ab381e7a357be12847ae5195e23b66c84685f85 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 15 Jan 2022 12:55:21 +0400 Subject: [PATCH 44/90] `InputObjectTypeDefinition` support. --- .../Blocks/Types/DefinitionBlock.php | 7 +- .../Types/InputFieldsDefinitionList.php | 58 +++++ .../Types/InputObjectTypeDefinitionBlock.php | 47 ++++ .../InputObjectTypeDefinitionBlockTest.php | 213 ++++++++++++++++++ .../Types/InputValueDefinitionBlock.php | 5 +- 5 files changed, 325 insertions(+), 5 deletions(-) create mode 100644 src/SchemaPrinter/Blocks/Types/InputFieldsDefinitionList.php create mode 100644 src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php create mode 100644 src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php diff --git a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php index 513b4f8e..f5b9e48b 100644 --- a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php @@ -6,6 +6,7 @@ use GraphQL\Type\Definition\EnumValueDefinition; use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\FieldDefinition; +use GraphQL\Type\Definition\InputObjectField; use GraphQL\Type\Definition\Type; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; @@ -18,7 +19,7 @@ /** * @internal * - * @template TType of Type|FieldDefinition|EnumValueDefinition|FieldArgument|Directive + * @template TType of Type|FieldDefinition|EnumValueDefinition|FieldArgument|Directive|InputObjectField */ abstract class DefinitionBlock extends Block implements Named { /** @@ -29,7 +30,7 @@ public function __construct( Settings $settings, int $level, int $used, - private Type|FieldDefinition|EnumValueDefinition|FieldArgument|Directive $definition, + private Type|FieldDefinition|EnumValueDefinition|FieldArgument|Directive|InputObjectField $definition, ) { parent::__construct($dispatcher, $settings, $level, $used); } @@ -49,7 +50,7 @@ public function getName(): string { /** * @return TType */ - protected function getDefinition(): Type|FieldDefinition|EnumValueDefinition|FieldArgument|Directive { + protected function getDefinition(): Type|FieldDefinition|EnumValueDefinition|FieldArgument|Directive|InputObjectField { return $this->definition; } diff --git a/src/SchemaPrinter/Blocks/Types/InputFieldsDefinitionList.php b/src/SchemaPrinter/Blocks/Types/InputFieldsDefinitionList.php new file mode 100644 index 00000000..1d0f3f22 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/InputFieldsDefinitionList.php @@ -0,0 +1,58 @@ + + */ +class InputFieldsDefinitionList extends BlockList { + /** + * @param Traversable|array $fields + */ + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + Traversable|array $fields, + ) { + parent::__construct($dispatcher, $settings, $level, $used); + + foreach ($fields as $field) { + $this[$field->name] = new InputValueDefinitionBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel() + 1, + $this->getUsed(), + $field, + ); + } + } + + protected function getPrefix(): string { + return '{'; + } + + protected function getSuffix(): string { + return '}'; + } + + protected function isWrapped(): bool { + return true; + } + + protected function isNormalized(): bool { + return $this->getSettings()->isNormalizeFields(); + } + + protected function isAlwaysMultiline(): bool { + return true; + } +} diff --git a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php new file mode 100644 index 00000000..b1ef5c9b --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php @@ -0,0 +1,47 @@ + + */ +class InputObjectTypeDefinitionBlock extends DefinitionBlock { + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + InputObjectType $definition, + ) { + parent::__construct($dispatcher, $settings, $level, $used, $definition); + } + + protected function type(): string|null { + return 'input'; + } + + protected function body(int $used): Block|string|null { + return null; + } + + protected function fields(int $used): Block|string|null { + $definition = $this->getDefinition(); + $space = $this->space(); + $fields = new InputFieldsDefinitionList( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $used + mb_strlen($space), + $definition->getFields(), + ); + + return $fields; + } +} diff --git a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php new file mode 100644 index 00000000..bf9b2bf2 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php @@ -0,0 +1,213 @@ + + // ========================================================================= + /** + * @covers ::__toString + * + * @dataProvider dataProviderToString + */ + public function testToString( + string $expected, + Settings $settings, + int $level, + int $used, + InputObjectType $definition, + ): void { + $actual = (string) (new InputObjectTypeDefinitionBlock( + new Dispatcher(), $settings, $level, $used, $definition + )); + $parsed = Parser::InputObjectTypeDefinition($actual); + + self::assertEquals($expected, $actual); + self::assertInstanceOf(InputObjectTypeDefinitionNode::class, $parsed); + } + + /** + * @covers ::__toString + */ + public function testToStringEvent(): void { + $spy = Mockery::spy(static fn (Event $event) => null); + $settings = new DefaultSettings(); + $dispatcher = new Dispatcher(); + $definition = new InputObjectType([ + 'name' => 'A', + 'fields' => [ + 'b' => [ + 'name' => 'b', + 'type' => new InputObjectType([ + 'name' => 'B', + ]), + ], + ], + ]); + + $dispatcher->attach(Closure::fromCallable($spy)); + + self::assertNotNull( + (string) (new InputObjectTypeDefinitionBlock($dispatcher, $settings, 0, 0, $definition)), + ); + + $spy + ->shouldHaveBeenCalled() + ->withArgs(static function (Event $event): bool { + return $event instanceof TypeUsed + && $event->name === 'B'; + }) + ->once(); + $spy + ->shouldHaveBeenCalled() + ->once(); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderToString(): array { + return [ + 'description + directives' => [ + <<<'STRING' + """ + Description + """ + input Test + @a + STRING, + new DefaultSettings(), + 0, + 0, + new InputObjectType([ + 'name' => 'Test', + 'astNode' => Parser::inputObjectTypeDefinition('input Test @a'), + 'description' => 'Description', + ]), + ], + 'description + directives + fields' => [ + <<<'STRING' + """ + Description + """ + input Test + @a + { + c: C + + """ + Description + """ + b: B + + a: A + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + new InputObjectType([ + 'name' => 'Test', + 'astNode' => Parser::inputObjectTypeDefinition('input Test @a'), + 'description' => 'Description', + 'fields' => [ + [ + 'name' => 'c', + 'type' => new InputObjectType([ + 'name' => 'C', + ]), + ], + [ + 'name' => 'b', + 'type' => new InputObjectType([ + 'name' => 'B', + ]), + 'description' => 'Description', + ], + [ + 'name' => 'a', + 'type' => new InputObjectType([ + 'name' => 'A', + ]), + ], + ], + ]), + ], + 'fields' => [ + <<<'STRING' + input Test { + a: String + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + }, + 0, + 0, + new InputObjectType([ + 'name' => 'Test', + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::string(), + ], + ], + ]), + ], + 'indent' => [ + <<<'STRING' + input Test { + a: String + } + STRING, + new class() extends DefaultSettings { + public function getIndent(): string { + return ' '; + } + + public function isNormalizeInterfaces(): bool { + return true; + } + }, + 1, + 120, + new InputObjectType([ + 'name' => 'Test', + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::string(), + ], + ], + ]), + ], + ]; + } + // +} diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php index bb9f92c0..a6c97145 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php @@ -3,6 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\FieldArgument; +use GraphQL\Type\Definition\InputObjectField; use GraphQL\Utils\AST; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\ValueNodeBlock; @@ -14,7 +15,7 @@ /** * @internal * - * @extends DefinitionBlock + * @extends DefinitionBlock */ class InputValueDefinitionBlock extends DefinitionBlock { public function __construct( @@ -22,7 +23,7 @@ public function __construct( Settings $settings, int $level, int $used, - FieldArgument $definition, + FieldArgument|InputObjectField $definition, ) { parent::__construct($dispatcher, $settings, $level, $used, $definition); } From 49360eae63fac362cb165f2a69ca50ce088e77cf Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 15 Jan 2022 17:03:06 +0400 Subject: [PATCH 45/90] Typehints fixes. --- .../Blocks/Ast/ArgumentNodeList.php | 2 +- .../Blocks/Ast/DirectiveNodeBlock.php | 2 +- .../Blocks/Ast/DirectiveNodeBlockTest.php | 11 +++++-- .../Blocks/Ast/DirectiveNodeList.php | 2 +- .../Blocks/Ast/DirectiveNodeListTest.php | 32 ++++++++++++++++--- .../Blocks/Ast/ValueNodeTest.php | 6 +++- src/SchemaPrinter/Blocks/BlockList.php | 13 +++++--- src/SchemaPrinter/Blocks/BlockListTest.php | 2 +- src/SchemaPrinter/Blocks/Property.php | 8 +++++ .../Blocks/Types/DefinitionBlock.php | 4 +-- .../Blocks/Types/Description.php | 4 +-- .../Blocks/Types/DescriptionTest.php | 5 +-- .../Types/DirectiveDefinitionBlockTest.php | 10 +++--- .../Types/EnumTypeDefinitionBlockTest.php | 25 +++++++++------ .../Types/EnumValueDefinitionBlockTest.php | 7 ++-- .../Blocks/Types/FieldDefinitionBlockTest.php | 10 +++--- .../InputObjectTypeDefinitionBlockTest.php | 10 +++--- .../Types/InputValueDefinitionBlock.php | 9 +++--- .../Types/InputValueDefinitionBlockTest.php | 7 ++-- .../Types/InterfaceTypeDefinitionBlock.php | 2 +- .../InterfaceTypeDefinitionBlockTest.php | 10 +++--- .../Types/ObjectTypeDefinitionBlock.php | 2 +- .../Types/ObjectTypeDefinitionBlockTest.php | 10 +++--- .../Types/ScalarTypeDefinitionBlockTest.php | 7 ++-- .../Blocks/Types/StringBlockTest.php | 8 +++-- .../Blocks/Types/TypeBlockTest.php | 5 ++- .../Blocks/Types/TypeDefinitionBlock.php | 7 +++- .../Types/UnionTypeDefinitionBlockTest.php | 10 +++--- src/SchemaPrinter/Blocks/Types/UsageList.php | 2 ++ 29 files changed, 138 insertions(+), 94 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php b/src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php index b51fde29..343a1511 100644 --- a/src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php +++ b/src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php @@ -11,7 +11,7 @@ /** * @internal - * @extends BlockList + * @extends BlockList> */ class ArgumentNodeList extends BlockList { /** diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php index fbf92cc9..3b837d00 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php @@ -29,7 +29,7 @@ public function getName(): string { return $this->getNode()->name->value; } - protected function getNode(): DirectiveNode { + public function getNode(): DirectiveNode { return $this->node; } diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php index 02ea5427..6b566027 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php @@ -5,6 +5,7 @@ use Closure; use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\Parser; +use GraphQL\Language\Printer; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\DirectiveUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; @@ -35,7 +36,13 @@ public function testToString( $parsed = Parser::directive($actual); self::assertEquals($expected, $actual); - self::assertInstanceOf(DirectiveNode::class, $parsed); + + if (!$settings->isNormalizeArguments()) { + self::assertEquals( + Printer::doPrint($node), + Printer::doPrint($parsed), + ); + } } /** @@ -49,7 +56,7 @@ public function testToStringEvent(): void { $dispatcher->attach(Closure::fromCallable($spy)); - self::assertNotNull( + self::assertNotEmpty( (string) (new DirectiveNodeBlock($dispatcher, $settings, 0, 0, $node)), ); diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php index dd9cb1a3..a258d47f 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php @@ -66,7 +66,7 @@ protected function getBlocks(): array { $blocks = parent::getBlocks(); if ($filter !== null) { - $blocks = array_filter($blocks, static function (Directive $block) use ($filter): bool { + $blocks = array_filter($blocks, static function (DirectiveNodeBlock $block) use ($filter): bool { return $filter->isAllowedDirective($block->getNode()); }); } diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php index 59756f21..d551376a 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php @@ -4,12 +4,12 @@ use Closure; use GraphQL\Language\AST\DirectiveNode; -use GraphQL\Language\AST\NodeList; use GraphQL\Language\Parser; use GraphQL\Type\Definition\Directive; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\DirectiveUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\DirectiveFilter; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; use Mockery; @@ -37,10 +37,10 @@ public function testToString( string $reason = null, ): void { $actual = (string) (new DirectiveNodeList(new Dispatcher(), $settings, $level, $used, $directives, $reason)); - $parsed = Parser::directives($actual); + + Parser::directives($actual); self::assertEquals($expected, $actual); - self::assertInstanceOf(NodeList::class, $parsed); } /** @@ -59,7 +59,7 @@ public function isIncludeDirectives(): bool { $dispatcher->attach(Closure::fromCallable($spy)); - self::assertNotNull( + self::assertNotEmpty( (string) (new DirectiveNodeList($dispatcher, $settings, 0, 0, [$a, $b])), ); @@ -86,7 +86,7 @@ public function isIncludeDirectives(): bool { // // ========================================================================= /** - * @return array + * @return array, ?string}> */ public function dataProviderToString(): array { return [ @@ -192,6 +192,28 @@ public function dataProviderToString(): array { ], 'very very very long reason', ], + 'filter' => [ + <<<'STRING' + @a(a: 123) + STRING, + new class() extends DefaultSettings { + public function getDirectiveFilter(): ?DirectiveFilter { + return new class() implements DirectiveFilter { + public function isAllowedDirective(DirectiveNode $directive): bool { + return $directive->name->value === 'a'; + } + }; + } + }, + 0, + 0, + [ + Parser::directive('@a(a: 123)'), + Parser::directive('@b(b: 1234567890)'), + Parser::directive('@c'), + ], + null, + ], ]; } // diff --git a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php index 1be10a18..395b97e6 100644 --- a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php +++ b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php @@ -14,6 +14,7 @@ use GraphQL\Language\AST\ValueNode; use GraphQL\Language\AST\VariableNode; use GraphQL\Language\Parser; +use GraphQL\Language\Printer; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; @@ -43,7 +44,10 @@ public function testToString( $parsed = Parser::valueLiteral($actual); self::assertEquals($expected, $actual); - self::assertInstanceOf(ValueNode::class, $parsed); + self::assertEquals( + Printer::doPrint($node), + Printer::doPrint($parsed), + ); } // diff --git a/src/SchemaPrinter/Blocks/BlockList.php b/src/SchemaPrinter/Blocks/BlockList.php index 67525f23..dbe44d47 100644 --- a/src/SchemaPrinter/Blocks/BlockList.php +++ b/src/SchemaPrinter/Blocks/BlockList.php @@ -4,6 +4,7 @@ use ArrayAccess; +use function array_key_last; use function count; use function implode; use function mb_strlen; @@ -22,7 +23,7 @@ abstract class BlockList extends Block implements ArrayAccess { private array $blocks = []; /** - * @var array + * @var array */ private array $multiline = []; private int $length = 0; @@ -107,6 +108,7 @@ protected function content(): string { $listPrefix = $this->getPrefix(); $listSuffix = $this->getSuffix(); $separator = $this->getSeparator(); + $isWrapped = (bool) $listPrefix || (bool) $listSuffix; $isMultiline = $this->isMultilineContent( $blocks, $listSuffix, @@ -118,7 +120,7 @@ protected function content(): string { $eol = $this->eol(); $last = $count - 1; $index = 0; - $indent = $this->indent($this->getLevel() + (int) ($listPrefix || $listSuffix)); + $indent = $this->indent($this->getLevel() + (int) $isWrapped); $wrapped = $this->isWrapped(); $previous = false; $separator = $this->getMultilineItemPrefix(); @@ -130,7 +132,7 @@ protected function content(): string { $content .= $eol; } - if ($index > 0 || ($listPrefix || $listSuffix)) { + if ($index > 0 || $isWrapped) { $content .= $indent; } @@ -148,7 +150,7 @@ protected function content(): string { } // Prefix & Suffix - if ($listPrefix || $listSuffix) { + if ($isWrapped) { $eol = $isMultiline ? $this->eol() : ''; $indent = $isMultiline ? $this->indent() : ''; $content = "{$listPrefix}{$eol}{$content}"; @@ -172,7 +174,7 @@ private function isMultilineContent( string $separator, ): bool { // Always or Any multiline block? - if ($this->isAlwaysMultiline() || $this->multiline) { + if ($this->isAlwaysMultiline() || count($this->multiline) > 0) { return true; } @@ -215,6 +217,7 @@ public function offsetSet(mixed $offset, mixed $value): void { $this->blocks[$offset] = $value; } else { $this->blocks[] = $value; + $offset = array_key_last($this->blocks); } $this->length += $value->getLength(); diff --git a/src/SchemaPrinter/Blocks/BlockListTest.php b/src/SchemaPrinter/Blocks/BlockListTest.php index 0752b00c..7459e1cb 100644 --- a/src/SchemaPrinter/Blocks/BlockListTest.php +++ b/src/SchemaPrinter/Blocks/BlockListTest.php @@ -61,7 +61,7 @@ public function testToString( // // ========================================================================= /** - * @return array + * @return array> */ public function dataProviderToString(): array { return (new MergeDataProvider([ diff --git a/src/SchemaPrinter/Blocks/Property.php b/src/SchemaPrinter/Blocks/Property.php index 250686f1..b34b428d 100644 --- a/src/SchemaPrinter/Blocks/Property.php +++ b/src/SchemaPrinter/Blocks/Property.php @@ -7,8 +7,13 @@ /** * @internal + * + * @template TBlock of Block */ class Property extends Block implements Named { + /** + * @param TBlock $block + */ public function __construct( Dispatcher $dispatcher, Settings $settings, @@ -26,6 +31,9 @@ public function isMultiline(): bool { return $this->getBlock()->isMultiline() || parent::isMultiline(); } + /** + * @return TBlock + */ protected function getBlock(): Block { return $this->block; } diff --git a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php index f5b9e48b..6d3eb430 100644 --- a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php @@ -80,7 +80,7 @@ protected function content(): string { } if ($fields) { - if ($directives || $this->isStringMultiline($body)) { + if ((bool) $directives || $this->isStringMultiline($body)) { $content .= "{$eol}{$indent}{$fields}"; } else { $content .= "{$space}{$fields}"; @@ -107,7 +107,7 @@ protected function directives(): DirectiveNodeList { $this->getSettings(), $this->getLevel(), $this->getUsed(), - $definition->astNode->directives ?? null, + $definition->astNode?->directives ?? null, $definition->deprecationReason ?? null, ); diff --git a/src/SchemaPrinter/Blocks/Types/Description.php b/src/SchemaPrinter/Blocks/Types/Description.php index c530bb73..23db7f81 100644 --- a/src/SchemaPrinter/Blocks/Types/Description.php +++ b/src/SchemaPrinter/Blocks/Types/Description.php @@ -46,8 +46,8 @@ protected function getString(): string { $eol = $this->eol(); $string = str_replace(["\r\n", "\n\r", "\n", "\r"], $eol, $string); $string = rtrim(trim($string, $eol)); - $string = preg_replace('/\R{2,}/u', "{$eol}{$eol}", $string); - $string = preg_replace('/^(.*?)\h+$/mu', '$1', $string); + $string = (string) preg_replace('/\R{2,}/u', "{$eol}{$eol}", $string); + $string = (string) preg_replace('/^(.*?)\h+$/mu', '$1', $string); } // Directives diff --git a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php index 1e87bfb9..6adda2c3 100644 --- a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php +++ b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php @@ -3,6 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Language\AST\DirectiveNode; +use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\Parser; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; @@ -40,7 +41,7 @@ public function testToString( self::assertEquals($expected, $actual); if ($expected) { - self::assertNotNull(Parser::valueLiteral($actual)); + Parser::valueLiteral($actual); } } // @@ -48,7 +49,7 @@ public function testToString( // // ========================================================================= /** - * @return array + * @return array|null}> */ public function dataProviderToString(): array { return [ diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php index 9debb5c8..f4b196d3 100644 --- a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php @@ -3,11 +3,9 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use Closure; -use GraphQL\Language\AST\DirectiveDefinitionNode; use GraphQL\Language\DirectiveLocation; use GraphQL\Language\Parser; use GraphQL\Type\Definition\Directive; -use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; @@ -37,10 +35,10 @@ public function testToString( Directive $definition, ): void { $actual = (string) (new DirectiveDefinitionBlock(new Dispatcher(), $settings, $level, $used, $definition)); - $parsed = Parser::directiveDefinition($actual); + + Parser::directiveDefinition($actual); self::assertEquals($expected, $actual); - self::assertInstanceOf(DirectiveDefinitionNode::class, $parsed); } /** @@ -64,7 +62,7 @@ public function testToStringEvent(): void { $dispatcher->attach(Closure::fromCallable($spy)); - self::assertNotNull( + self::assertNotEmpty( (string) (new DirectiveDefinitionBlock($dispatcher, $settings, 0, 0, $definition)), ); @@ -84,7 +82,7 @@ public function testToStringEvent(): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { return [ diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php index c4192c2a..7a36c23d 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php @@ -3,11 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use Closure; -use GraphQL\Language\AST\DirectiveNode; -use GraphQL\Language\AST\EnumTypeDefinitionNode; use GraphQL\Language\Parser; use GraphQL\Type\Definition\Directive; use GraphQL\Type\Definition\EnumType; +use GraphQL\Type\Definition\EnumValueDefinition; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; @@ -37,17 +36,17 @@ public function testToString( } $actual = (string) (new EnumTypeDefinitionBlock(new Dispatcher(), $settings, $level, $used, $type)); - $parsed = Parser::enumTypeDefinition($actual); + + Parser::enumTypeDefinition($actual); self::assertEquals($expected, $actual); - self::assertInstanceOf(EnumTypeDefinitionNode::class, $parsed); } // // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { return [ @@ -144,12 +143,18 @@ static function (): EnumType { 'values' => ['C', 'B', 'A'], ]); - $a = $enum->getValue('A'); - $a->deprecationReason = Directive::DEFAULT_DEPRECATION_REASON; + $a = $enum->getValue('A'); + + if ($a instanceof EnumValueDefinition) { + $a->deprecationReason = Directive::DEFAULT_DEPRECATION_REASON; + } - $b = $enum->getValue('B'); - $b->astNode = Parser::enumValueDefinition('A @b @a'); - $b->description = 'Description'; + $b = $enum->getValue('B'); + + if ($b instanceof EnumValueDefinition) { + $b->astNode = Parser::enumValueDefinition('A @b @a'); + $b->description = 'Description'; + } return $enum; }, diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php index c3c88cd0..3dfba92b 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Language\AST\DirectiveNode; -use GraphQL\Language\AST\EnumValueDefinitionNode; use GraphQL\Language\Parser; use GraphQL\Type\Definition\EnumValueDefinition; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; @@ -30,17 +29,17 @@ public function testToString( EnumValueDefinition $type, ): void { $actual = (string) (new EnumValueDefinitionBlock(new Dispatcher(), $settings, $level, $used, $type)); - $parsed = Parser::enumValueDefinition($actual); + + Parser::enumValueDefinition($actual); self::assertEquals($expected, $actual); - self::assertInstanceOf(EnumValueDefinitionNode::class, $parsed); } // // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { return [ diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php index 7c2c608c..627abcc7 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php @@ -3,9 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use Closure; -use GraphQL\Language\AST\FieldDefinitionNode; use GraphQL\Language\Parser; -use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\FieldDefinition; use GraphQL\Type\Definition\ListOfType; use GraphQL\Type\Definition\NonNull; @@ -38,10 +36,10 @@ public function testToString( FieldDefinition $definition, ): void { $actual = (string) (new FieldDefinitionBlock(new Dispatcher(), $settings, $level, $used, $definition)); - $parsed = Parser::fieldDefinition($actual); + + Parser::fieldDefinition($actual); self::assertEquals($expected, $actual); - self::assertInstanceOf(FieldDefinitionNode::class, $parsed); } /** @@ -62,7 +60,7 @@ public function testToStringEvent(): void { $dispatcher->attach(Closure::fromCallable($spy)); - self::assertNotNull( + self::assertNotEmpty( (string) (new FieldDefinitionBlock($dispatcher, $settings, 0, 0, $definition)), ); @@ -82,7 +80,7 @@ public function testToStringEvent(): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { return [ diff --git a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php index bf9b2bf2..50946966 100644 --- a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php @@ -3,9 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use Closure; -use GraphQL\Language\AST\InputObjectTypeDefinitionNode; use GraphQL\Language\Parser; -use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\Type; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; @@ -37,10 +35,10 @@ public function testToString( $actual = (string) (new InputObjectTypeDefinitionBlock( new Dispatcher(), $settings, $level, $used, $definition )); - $parsed = Parser::InputObjectTypeDefinition($actual); + + Parser::inputObjectTypeDefinition($actual); self::assertEquals($expected, $actual); - self::assertInstanceOf(InputObjectTypeDefinitionNode::class, $parsed); } /** @@ -64,7 +62,7 @@ public function testToStringEvent(): void { $dispatcher->attach(Closure::fromCallable($spy)); - self::assertNotNull( + self::assertNotEmpty( (string) (new InputObjectTypeDefinitionBlock($dispatcher, $settings, 0, 0, $definition)), ); @@ -84,7 +82,7 @@ public function testToStringEvent(): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { return [ diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php index a6c97145..21c7e7e6 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php @@ -2,6 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; +use GraphQL\Language\AST\NullValueNode; use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\InputObjectField; use GraphQL\Utils\AST; @@ -45,15 +46,15 @@ protected function body(int $used): Block|string|null { $body = ":{$space}{$type}"; if ($definition->defaultValueExists()) { - $prefix = "{$body}{$space}={$space}"; - $value = new ValueNodeBlock( + $prefix = "{$body}{$space}={$space}"; + $value = new ValueNodeBlock( $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $this->getUsed() + mb_strlen($prefix), - AST::astFromValue($definition->defaultValue, $definition->getType()), + AST::astFromValue($definition->defaultValue, $definition->getType()) ?? new NullValueNode([]), ); - $body = "{$prefix}{$value}"; + $body = "{$prefix}{$value}"; } return $body; diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php index c31df840..f6b8e813 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use Closure; -use GraphQL\Language\AST\InputValueDefinitionNode; use GraphQL\Language\Parser; use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\ListOfType; @@ -37,10 +36,10 @@ public function testToString( FieldArgument $definition, ): void { $actual = (string) (new InputValueDefinitionBlock(new Dispatcher(), $settings, $level, $used, $definition)); - $parsed = Parser::inputValueDefinition($actual); + + Parser::inputValueDefinition($actual); self::assertEquals($expected, $actual); - self::assertInstanceOf(InputValueDefinitionNode::class, $parsed); } /** @@ -61,7 +60,7 @@ public function testToStringEvent(): void { $dispatcher->attach(Closure::fromCallable($spy)); - self::assertNotNull( + self::assertNotEmpty( (string) (new InputValueDefinitionBlock($dispatcher, $settings, 0, 0, $definition)), ); diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php index 78386e77..c8ec8f34 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php @@ -9,7 +9,7 @@ /** * @internal * - * @extends DefinitionBlock + * @extends TypeDefinitionBlock */ class InterfaceTypeDefinitionBlock extends TypeDefinitionBlock { public function __construct( diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php index e3f7867e..7814f92d 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php @@ -3,9 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use Closure; -use GraphQL\Language\AST\InterfaceTypeDefinitionNode; use GraphQL\Language\Parser; -use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; @@ -36,10 +34,10 @@ public function testToString( InterfaceType $definition, ): void { $actual = (string) (new InterfaceTypeDefinitionBlock(new Dispatcher(), $settings, $level, $used, $definition)); - $parsed = Parser::interfaceTypeDefinition($actual); + + Parser::interfaceTypeDefinition($actual); self::assertEquals($expected, $actual); - self::assertInstanceOf(InterfaceTypeDefinitionNode::class, $parsed); } /** @@ -70,7 +68,7 @@ public function testToStringEvent(): void { $dispatcher->attach(Closure::fromCallable($spy)); - self::assertNotNull( + self::assertNotEmpty( (string) (new InterfaceTypeDefinitionBlock($dispatcher, $settings, 0, 0, $definition)), ); @@ -97,7 +95,7 @@ public function testToStringEvent(): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { return [ diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php index c25cbf01..e3a701a8 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php @@ -9,7 +9,7 @@ /** * @internal * - * @extends DefinitionBlock + * @extends TypeDefinitionBlock */ class ObjectTypeDefinitionBlock extends TypeDefinitionBlock { public function __construct( diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php index 66561dad..8db1b17e 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php @@ -3,9 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use Closure; -use GraphQL\Language\AST\objectTypeDefinitionNode; use GraphQL\Language\Parser; -use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; @@ -35,10 +33,10 @@ public function testToString( ObjectType $definition, ): void { $actual = (string) (new ObjectTypeDefinitionBlock(new Dispatcher(), $settings, $level, $used, $definition)); - $parsed = Parser::objectTypeDefinition($actual); + + Parser::objectTypeDefinition($actual); self::assertEquals($expected, $actual); - self::assertInstanceOf(ObjectTypeDefinitionNode::class, $parsed); } /** @@ -69,7 +67,7 @@ public function testToStringEvent(): void { $dispatcher->attach(Closure::fromCallable($spy)); - self::assertNotNull( + self::assertNotEmpty( (string) (new ObjectTypeDefinitionBlock($dispatcher, $settings, 0, 0, $definition)), ); @@ -96,7 +94,7 @@ public function testToStringEvent(): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { return [ diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php index 04beb3f5..51795d04 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Language\AST\DirectiveNode; -use GraphQL\Language\AST\ScalarTypeDefinitionNode; use GraphQL\Language\Parser; use GraphQL\Type\Definition\CustomScalarType; use GraphQL\Type\Definition\ScalarType; @@ -31,17 +30,17 @@ public function testToString( ScalarType $type, ): void { $actual = (string) (new ScalarTypeDefinitionBlock(new Dispatcher(), $settings, $level, $used, $type)); - $parsed = Parser::scalarTypeDefinition($actual); + + Parser::scalarTypeDefinition($actual); self::assertEquals($expected, $actual); - self::assertInstanceOf(ScalarTypeDefinitionNode::class, $parsed); } // // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { return [ diff --git a/src/SchemaPrinter/Blocks/Types/StringBlockTest.php b/src/SchemaPrinter/Blocks/Types/StringBlockTest.php index c345a62f..df657152 100644 --- a/src/SchemaPrinter/Blocks/Types/StringBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/StringBlockTest.php @@ -2,6 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; +use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\Parser; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -29,17 +30,18 @@ public function testToString( string $string, ): void { $actual = (string) new StringBlock(new Dispatcher(), $settings, $level, $used, $string); - $parsed = Parser::valueLiteral($actual)->value; + $parsed = Parser::valueLiteral($actual); + self::assertInstanceOf(StringValueNode::class, $parsed); self::assertEquals($expected, $actual); - self::assertEquals($string, $parsed); + self::assertEquals($string, $parsed->value); } // // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { return [ diff --git a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php index 78ba6519..5a1d15fb 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use Closure; -use GraphQL\Language\AST\DirectiveNode; use GraphQL\Type\Definition\ListOfType; use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ObjectType; @@ -54,7 +53,7 @@ public function testToStringEvent(): void { $dispatcher->attach(Closure::fromCallable($spy)); - self::assertNotNull( + self::assertNotEmpty( (string) (new TypeBlock($dispatcher, $settings, 0, 0, $node)), ); @@ -74,7 +73,7 @@ public function testToStringEvent(): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { return [ diff --git a/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php index bdb64d48..178c74c7 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php @@ -13,9 +13,14 @@ /** * @internal * - * @extends DefinitionBlock + * @template TType of InterfaceType|ObjectType + * + * @extends DefinitionBlock */ abstract class TypeDefinitionBlock extends DefinitionBlock { + /** + * @param TType $definition + */ public function __construct( Dispatcher $dispatcher, Settings $settings, diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php index ceb6b52b..9d0d425c 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php @@ -3,8 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use Closure; -use GraphQL\Language\AST\DirectiveNode; -use GraphQL\Language\AST\UnionTypeDefinitionNode; use GraphQL\Language\Parser; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\UnionType; @@ -36,10 +34,10 @@ public function testToString( UnionType $type, ): void { $actual = (string) (new UnionTypeDefinitionBlock(new Dispatcher(), $settings, $level, $used, $type)); - $parsed = Parser::unionTypeDefinition($actual); + + Parser::unionTypeDefinition($actual); self::assertEquals($expected, $actual); - self::assertInstanceOf(UnionTypeDefinitionNode::class, $parsed); } /** @@ -63,7 +61,7 @@ public function testToStringEvent(): void { $dispatcher->attach(Closure::fromCallable($spy)); - self::assertNotNull( + self::assertNotEmpty( (string) (new UnionTypeDefinitionBlock($dispatcher, $settings, 0, 0, $union)), ); @@ -90,7 +88,7 @@ public function testToStringEvent(): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { return [ diff --git a/src/SchemaPrinter/Blocks/Types/UsageList.php b/src/SchemaPrinter/Blocks/Types/UsageList.php index c190ccf6..69176ac4 100644 --- a/src/SchemaPrinter/Blocks/Types/UsageList.php +++ b/src/SchemaPrinter/Blocks/Types/UsageList.php @@ -36,6 +36,8 @@ public function __construct( /** * @param TType $item + * + * @return TBlock */ abstract protected function block(mixed $item): Block; From 89b2794e53829e593900aad7a7141dddb6b8cbc0 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 15 Jan 2022 18:12:00 +0400 Subject: [PATCH 46/90] Removed `Settings::getDirectivesDefinitionsFilter()`. --- src/SchemaPrinter/Settings.php | 6 ------ src/SchemaPrinter/Settings/DefaultSettings.php | 4 ---- 2 files changed, 10 deletions(-) diff --git a/src/SchemaPrinter/Settings.php b/src/SchemaPrinter/Settings.php index 7bfb7ed2..ef951227 100644 --- a/src/SchemaPrinter/Settings.php +++ b/src/SchemaPrinter/Settings.php @@ -2,7 +2,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter; -use Closure; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\DirectiveFilter; // TODO: Directive resolver @@ -75,9 +74,4 @@ public function isNormalizeDirectiveLocations(): bool; * Used to determine should the directive included in output or not. */ public function getDirectiveFilter(): ?DirectiveFilter; - - /** - * @return Closure():bool|null - */ - public function getDirectivesDefinitionsFilter(): ?Closure; } diff --git a/src/SchemaPrinter/Settings/DefaultSettings.php b/src/SchemaPrinter/Settings/DefaultSettings.php index f007a712..3782caaf 100644 --- a/src/SchemaPrinter/Settings/DefaultSettings.php +++ b/src/SchemaPrinter/Settings/DefaultSettings.php @@ -82,8 +82,4 @@ public function isNormalizeDirectiveLocations(): bool { public function getDirectiveFilter(): ?DirectiveFilter { return null; } - - public function getDirectivesDefinitionsFilter(): ?Closure { - return null; - } } From 6d3c4485b16d2e1ced47b6dc28a7fe379ca0e1c2 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 16 Jan 2022 14:07:23 +0400 Subject: [PATCH 47/90] Added `TestSettings`. --- .../Blocks/Ast/DirectiveNodeBlockTest.php | 22 +- .../Blocks/Ast/DirectiveNodeListTest.php | 39 ++-- .../Blocks/Ast/ValueNodeTest.php | 46 ++-- src/SchemaPrinter/Blocks/BlockList.php | 2 +- src/SchemaPrinter/Blocks/BlockListTest.php | 104 +++------ src/SchemaPrinter/Blocks/BlockTest.php | 20 +- src/SchemaPrinter/Blocks/PropertyTest.php | 14 +- .../Blocks/Types/DescriptionTest.php | 129 +++-------- .../Types/DirectiveDefinitionBlockTest.php | 47 ++-- .../Types/EnumTypeDefinitionBlockTest.php | 32 +-- .../Types/EnumValueDefinitionBlockTest.php | 23 +- .../Blocks/Types/FieldDefinitionBlockTest.php | 40 +--- .../InputObjectTypeDefinitionBlockTest.php | 30 +-- .../Types/InputValueDefinitionBlockTest.php | 18 +- .../InterfaceTypeDefinitionBlockTest.php | 64 ++---- .../Types/ObjectTypeDefinitionBlockTest.php | 64 ++---- .../Types/ScalarTypeDefinitionBlockTest.php | 51 +---- .../Blocks/Types/StringBlockTest.php | 64 ++---- .../Blocks/Types/TypeBlockTest.php | 12 +- .../Types/UnionTypeDefinitionBlockTest.php | 34 +-- src/SchemaPrinter/Settings.php | 3 - .../Package/SchemaPrinter/TestSettings.php | 206 ++++++++++++++++++ 22 files changed, 449 insertions(+), 615 deletions(-) create mode 100644 src/Testing/Package/SchemaPrinter/TestSettings.php diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php index 6b566027..202b2a68 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php @@ -10,7 +10,7 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\DirectiveUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use Mockery; use PHPUnit\Framework\TestCase; @@ -51,7 +51,7 @@ public function testToString( public function testToStringEvent(): void { $spy = Mockery::spy(static fn(Event $event) => null); $node = Parser::directive('@test'); - $settings = new DefaultSettings(); + $settings = new TestSettings(); $dispatcher = new Dispatcher(); $dispatcher->attach(Closure::fromCallable($spy)); @@ -79,24 +79,26 @@ public function testToStringEvent(): void { * @return array */ public function dataProviderToString(): array { + $settings = new TestSettings(); + return [ 'without arguments' => [ '@directive', - new DefaultSettings(), + $settings, 0, 0, Parser::directive('@directive'), ], 'without arguments (level)' => [ '@directive', - new DefaultSettings(), + $settings, 0, 0, Parser::directive('@directive'), ], 'with arguments (short)' => [ '@directive(a: "a", b: "b")', - new DefaultSettings(), + $settings, 0, 0, Parser::directive('@directive(a: "a", b: "b")'), @@ -108,18 +110,14 @@ public function dataProviderToString(): array { a: "a" ) STRING, - new DefaultSettings(), + $settings, 0, 120, Parser::directive('@directive(b: "b", a: "a")'), ], 'with arguments (normalized)' => [ '@directive(a: "a", b: "b")', - new class() extends DefaultSettings { - public function isNormalizeArguments(): bool { - return true; - } - }, + $settings->setNormalizeArguments(true), 0, 0, Parser::directive('@directive(b: "b", a: "a")'), @@ -131,7 +129,7 @@ public function isNormalizeArguments(): bool { a: "a" ) STRING, - new DefaultSettings(), + $settings, 1, 120, Parser::directive('@directive(b: "b", a: "a")'), diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php index d551376a..a056ed5e 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php @@ -9,9 +9,8 @@ use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\DirectiveUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\DirectiveFilter; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use Mockery; use PHPUnit\Framework\TestCase; @@ -50,11 +49,7 @@ public function testToStringEvent(): void { $a = Parser::directive('@a'); $b = Parser::directive('@b'); $spy = Mockery::spy(static fn (Event $event) => null); - $settings = new class() extends DefaultSettings { - public function isIncludeDirectives(): bool { - return true; - } - }; + $settings = (new TestSettings())->setIncludeDirectives(true); $dispatcher = new Dispatcher(); $dispatcher->attach(Closure::fromCallable($spy)); @@ -89,10 +84,12 @@ public function isIncludeDirectives(): bool { * @return array, ?string}> */ public function dataProviderToString(): array { + $settings = new TestSettings(); + return [ 'null' => [ '', - new DefaultSettings(), + $settings, 0, 0, null, @@ -100,7 +97,7 @@ public function dataProviderToString(): array { ], 'empty' => [ '', - new DefaultSettings(), + $settings, 0, 0, [], @@ -111,7 +108,7 @@ public function dataProviderToString(): array { @b(b: 123) @a STRING, - new DefaultSettings(), + $settings, 0, 0, [ @@ -124,7 +121,7 @@ public function dataProviderToString(): array { <<<'STRING' @deprecated STRING, - new DefaultSettings(), + $settings, 0, 0, null, @@ -134,7 +131,7 @@ public function dataProviderToString(): array { <<<'STRING' @deprecated(reason: "reason") STRING, - new DefaultSettings(), + $settings, 0, 0, null, @@ -145,7 +142,7 @@ public function dataProviderToString(): array { @deprecated(reason: "reason") @b(b: 123) STRING, - new DefaultSettings(), + $settings, 0, 0, [ @@ -164,7 +161,7 @@ public function dataProviderToString(): array { b: 1234567890 ) STRING, - new DefaultSettings(), + $settings, 0, 70, [ @@ -183,7 +180,7 @@ public function dataProviderToString(): array { b: 1234567890 ) STRING, - new DefaultSettings(), + $settings, 1, 70, [ @@ -196,15 +193,9 @@ public function dataProviderToString(): array { <<<'STRING' @a(a: 123) STRING, - new class() extends DefaultSettings { - public function getDirectiveFilter(): ?DirectiveFilter { - return new class() implements DirectiveFilter { - public function isAllowedDirective(DirectiveNode $directive): bool { - return $directive->name->value === 'a'; - } - }; - } - }, + $settings->setDirectiveFilter(static function (DirectiveNode $directive): bool { + return $directive->name->value === 'a'; + }), 0, 0, [ diff --git a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php index 395b97e6..10b14bb1 100644 --- a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php +++ b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php @@ -17,7 +17,7 @@ use GraphQL\Language\Printer; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use PHPUnit\Framework\TestCase; /** @@ -57,59 +57,61 @@ public function testToString( * @return array */ public function dataProviderToString(): array { + $settings = new TestSettings(); + return [ NullValueNode::class => [ 'null', - new DefaultSettings(), + $settings, 0, 0, Parser::valueLiteral('null'), ], IntValueNode::class => [ '123', - new DefaultSettings(), + $settings, 0, 0, Parser::valueLiteral('123'), ], FloatValueNode::class => [ '123.45', - new DefaultSettings(), + $settings, 0, 0, Parser::valueLiteral('123.45'), ], BooleanValueNode::class => [ 'true', - new DefaultSettings(), + $settings, 0, 0, Parser::valueLiteral('true'), ], StringValueNode::class => [ '"true"', - new DefaultSettings(), + $settings, 0, 0, Parser::valueLiteral('"true"'), ], EnumValueNode::class => [ 'Value', - new DefaultSettings(), + $settings, 0, 0, Parser::valueLiteral('Value'), ], VariableNode::class => [ '$variable', - new DefaultSettings(), + $settings, 0, 0, Parser::valueLiteral('$variable'), ], ListValueNode::class.' (short)' => [ '["a", "b", "c"]', - new DefaultSettings(), + $settings, 0, 0, Parser::valueLiteral('["a", "b", "c"]'), @@ -124,11 +126,7 @@ public function dataProviderToString(): array { """ ] STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 0, 0, Parser::valueLiteral( @@ -153,11 +151,7 @@ public function getIndent(): string { """ ] STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 1, 0, Parser::valueLiteral( @@ -176,7 +170,7 @@ public function getIndent(): string { <<<'STRING' [] STRING, - new DefaultSettings(), + $settings, 1, 0, Parser::valueLiteral('[]'), @@ -190,11 +184,7 @@ public function getIndent(): string { } } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 0, 0, Parser::valueLiteral( @@ -231,11 +221,7 @@ public function getIndent(): string { } } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 0, 0, Parser::valueLiteral( diff --git a/src/SchemaPrinter/Blocks/BlockList.php b/src/SchemaPrinter/Blocks/BlockList.php index dbe44d47..c4edff46 100644 --- a/src/SchemaPrinter/Blocks/BlockList.php +++ b/src/SchemaPrinter/Blocks/BlockList.php @@ -82,7 +82,7 @@ public function isMultiline(): bool { protected function getBlocks(): array { $blocks = $this->blocks; - if ($this->isNormalized()) { + if (count($blocks) > 0 && $this->isNormalized()) { usort($blocks, static function (Block $a, Block $b): int { $aName = $a instanceof Named ? $a->getName() : ''; $bName = $b instanceof Named ? $b->getName() : ''; diff --git a/src/SchemaPrinter/Blocks/BlockListTest.php b/src/SchemaPrinter/Blocks/BlockListTest.php index 7459e1cb..acf9efb5 100644 --- a/src/SchemaPrinter/Blocks/BlockListTest.php +++ b/src/SchemaPrinter/Blocks/BlockListTest.php @@ -4,7 +4,7 @@ use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\MergeDataProvider; use PHPUnit\Framework\TestCase; @@ -64,13 +64,15 @@ public function testToString( * @return array> */ public function dataProviderToString(): array { + $settings = new TestSettings(); + return (new MergeDataProvider([ 'index' => new ArrayDataProvider([ 'one single-line block' => [ <<<'STRING' block a STRING, - new DefaultSettings(), + $settings, 0, 0, false, @@ -87,7 +89,7 @@ public function dataProviderToString(): array { <<<'STRING' block a STRING, - new DefaultSettings(), + $settings, 0, 0, false, @@ -104,7 +106,7 @@ public function dataProviderToString(): array { <<<'STRING' block a, block b STRING, - new DefaultSettings(), + $settings, 0, 0, false, @@ -123,11 +125,7 @@ public function dataProviderToString(): array { block b block a STRING, - new class() extends DefaultSettings { - public function getLineLength(): int { - return 20; - } - }, + $settings->setLineLength(20), 0, 5, false, @@ -147,7 +145,7 @@ public function getLineLength(): int { block b STRING, - new DefaultSettings(), + $settings, 0, 0, false, @@ -175,7 +173,7 @@ public function getLineLength(): int { block g STRING, - new DefaultSettings(), + $settings, 0, 0, false, @@ -200,7 +198,7 @@ public function getLineLength(): int { block b block a STRING, - new DefaultSettings(), + $settings, 0, 0, false, @@ -219,7 +217,7 @@ public function getLineLength(): int { <<<'STRING' block b, block a STRING, - new DefaultSettings(), + $settings, 0, 0, true, @@ -238,11 +236,7 @@ public function getLineLength(): int { block a block b STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings->setIndent(' '), 2, 0, false, @@ -262,7 +256,7 @@ public function getIndent(): string { <<<'STRING' a: block a STRING, - new DefaultSettings(), + $settings, 0, 0, false, @@ -279,7 +273,7 @@ public function getIndent(): string { <<<'STRING' a: block a STRING, - new DefaultSettings(), + $settings, 0, 0, false, @@ -296,7 +290,7 @@ public function getIndent(): string { <<<'STRING' a: block a, b: block b STRING, - new DefaultSettings(), + $settings, 0, 0, false, @@ -315,11 +309,7 @@ public function getIndent(): string { b: block b a: block a STRING, - new class() extends DefaultSettings { - public function getLineLength(): int { - return 20; - } - }, + $settings->setLineLength(20), 0, 5, false, @@ -339,7 +329,7 @@ public function getLineLength(): int { b: block b STRING, - new DefaultSettings(), + $settings, 0, 0, false, @@ -367,7 +357,7 @@ public function getLineLength(): int { g: block g STRING, - new DefaultSettings(), + $settings, 0, 0, false, @@ -392,7 +382,7 @@ public function getLineLength(): int { b: block b a: block a STRING, - new DefaultSettings(), + $settings, 0, 0, false, @@ -411,7 +401,7 @@ public function getLineLength(): int { <<<'STRING' a: block a, b: block b STRING, - new DefaultSettings(), + $settings, 0, 0, true, @@ -430,11 +420,7 @@ public function getLineLength(): int { a: block a b: block b STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings->setIndent(' '), 2, 0, false, @@ -454,7 +440,7 @@ public function getIndent(): string { <<<'STRING' [a: block a] STRING, - new DefaultSettings(), + $settings, 0, 0, false, @@ -473,11 +459,7 @@ public function getIndent(): string { a: block a ] STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 0, 0, false, @@ -494,7 +476,7 @@ public function getIndent(): string { <<<'STRING' [block a, b: block b] STRING, - new DefaultSettings(), + $settings, 0, 0, false, @@ -515,15 +497,7 @@ public function getIndent(): string { a: block a ] STRING, - new class() extends DefaultSettings { - public function getLineLength(): int { - return 20; - } - - public function getIndent(): string { - return ' '; - } - }, + $settings->setLineLength(20), 0, 5, false, @@ -545,11 +519,7 @@ public function getIndent(): string { block b ] STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 0, 0, false, @@ -569,11 +539,7 @@ public function getIndent(): string { block a ] STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 2, 0, false, @@ -588,7 +554,7 @@ public function getIndent(): string { ], 'empty' => [ '', - new DefaultSettings(), + $settings, 0, 0, false, @@ -605,7 +571,7 @@ public function getIndent(): string { <<<'STRING' block a | block b STRING, - new DefaultSettings(), + $settings, 0, 0, false, @@ -624,7 +590,7 @@ public function getIndent(): string { || block a || block b STRING, - new DefaultSettings(), + $settings, 0, 120, false, @@ -643,11 +609,7 @@ public function getIndent(): string { || block a || block b STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 1, 120, false, @@ -724,7 +686,7 @@ public function __construct( protected bool $multiline, protected string $content, ) { - parent::__construct(new Dispatcher(), new DefaultSettings()); + parent::__construct(new Dispatcher(), new TestSettings()); } protected function getContent(): string { @@ -756,7 +718,7 @@ public function __construct( ) { parent::__construct( new Dispatcher(), - new DefaultSettings(), + new TestSettings(), $name, new BlockListTest__Block($multiline, $content), ); diff --git a/src/SchemaPrinter/Blocks/BlockTest.php b/src/SchemaPrinter/Blocks/BlockTest.php index ee32ff8b..3c0a5d9e 100644 --- a/src/SchemaPrinter/Blocks/BlockTest.php +++ b/src/SchemaPrinter/Blocks/BlockTest.php @@ -4,7 +4,7 @@ use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use Mockery; use PHPUnit\Framework\TestCase; @@ -21,7 +21,7 @@ class BlockTest extends TestCase { */ public function testGetContent(): void { $content = 'content'; - $block = Mockery::mock(BlockTest__Block::class, [new Dispatcher(), new DefaultSettings()]); + $block = Mockery::mock(BlockTest__Block::class, [new Dispatcher(), new TestSettings()]); $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); $block @@ -39,7 +39,7 @@ public function testGetContent(): void { public function testGetLength(): void { $content = 'content'; $length = mb_strlen($content); - $block = Mockery::mock(BlockTest__Block::class, [new Dispatcher(), new DefaultSettings()]); + $block = Mockery::mock(BlockTest__Block::class, [new Dispatcher(), new TestSettings()]); $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); $block @@ -75,7 +75,7 @@ public function testIsMultiline(bool $expected, Settings $settings, string $cont * @dataProvider dataProviderIsEmpty */ public function testIsEmpty(bool $expected, string $content): void { - $block = Mockery::mock(BlockTest__Block::class, [new Dispatcher(), new DefaultSettings()]); + $block = Mockery::mock(BlockTest__Block::class, [new Dispatcher(), new TestSettings()]); $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); $block @@ -93,24 +93,22 @@ public function testIsEmpty(bool $expected, string $content): void { * @return array */ public function dataProviderIsMultiline(): array { + $settings = new TestSettings(); + return [ 'single short line' => [ false, - new DefaultSettings(), + $settings, 'short line', ], 'single long line' => [ false, - new class() extends DefaultSettings { - public function getLineLength(): int { - return 5; - } - }, + $settings->setLineLength(5), 'long line', ], 'multi line' => [ true, - new DefaultSettings(), + $settings, "multi\nline", ], ]; diff --git a/src/SchemaPrinter/Blocks/PropertyTest.php b/src/SchemaPrinter/Blocks/PropertyTest.php index 057a3153..c99677db 100644 --- a/src/SchemaPrinter/Blocks/PropertyTest.php +++ b/src/SchemaPrinter/Blocks/PropertyTest.php @@ -4,7 +4,7 @@ use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use PHPUnit\Framework\TestCase; use function mb_strlen; @@ -26,17 +26,7 @@ public function testToString(): void { $space = ' '; $separator = ':'; $content = 'abc abcabc abcabc abcabc abc'; - $settings = new class($space) extends DefaultSettings { - public function __construct( - protected string $space, - ) { - parent::__construct(); - } - - public function getSpace(): string { - return $this->space; - } - }; + $settings = (new TestSettings())->setSpace($space); $dispatcher = new Dispatcher(); $block = new class($dispatcher, $settings, $level, $used, $content) extends Block { public function __construct( diff --git a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php index 6adda2c3..d0594a0b 100644 --- a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php +++ b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php @@ -3,12 +3,11 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Language\AST\DirectiveNode; -use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\Parser; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use function implode; @@ -52,10 +51,12 @@ public function testToString( * @return array|null}> */ public function dataProviderToString(): array { + $settings = new TestSettings(); + return [ 'null' => [ '', - new DefaultSettings(), + $settings, 0, 0, null, @@ -63,11 +64,7 @@ public function dataProviderToString(): array { ], 'Prints an empty string' => [ '', - new class() extends DefaultSettings { - public function isNormalizeDescription(): bool { - return false; - } - }, + $settings, 0, 0, '', @@ -75,11 +72,7 @@ public function isNormalizeDescription(): bool { ], 'Prints an empty string (normalized)' => [ '', - new class() extends DefaultSettings { - public function isNormalizeDescription(): bool { - return true; - } - }, + $settings->setNormalizeDescription(true), 0, 0, '', @@ -87,11 +80,7 @@ public function isNormalizeDescription(): bool { ], 'Prints an empty string with only whitespace' => [ '" "', - new class() extends DefaultSettings { - public function isNormalizeDescription(): bool { - return false; - } - }, + $settings, 0, 0, ' ', @@ -99,11 +88,7 @@ public function isNormalizeDescription(): bool { ], 'Prints an empty string with only whitespace (normalized)' => [ '', - new class() extends DefaultSettings { - public function isNormalizeDescription(): bool { - return true; - } - }, + $settings->setNormalizeDescription(true), 0, 0, ' ', @@ -115,7 +100,7 @@ public function isNormalizeDescription(): bool { Short string """ STRING, - new DefaultSettings(), + $settings, 0, 0, 'Short string', @@ -127,11 +112,7 @@ public function isNormalizeDescription(): bool { Long string """ STRING, - new class() extends DefaultSettings { - public function getLineLength(): int { - return 4; - } - }, + $settings->setLineLength(4), 0, 0, 'Long string', @@ -143,15 +124,9 @@ public function getLineLength(): int { string """ STRING, - new class() extends DefaultSettings { - public function getLineLength(): int { - return 2; - } - - public function getIndent(): string { - return ' '; - } - }, + $settings + ->setIndent(' ') + ->setLineLength(2), 2, 0, 'string', @@ -163,15 +138,9 @@ public function getIndent(): string { string """ STRING, - new class() extends DefaultSettings { - public function getLineLength(): int { - return 22; - } - - public function getIndent(): string { - return ' '; - } - }, + $settings + ->setIndent(' ') + ->setLineLength(22), 2, 20, 'string', @@ -188,11 +157,7 @@ public function getIndent(): string { ccc """ STRING, - new class() extends DefaultSettings { - public function isNormalizeDescription(): bool { - return false; - } - }, + $settings, 0, 0, <<<'STRING' @@ -214,11 +179,7 @@ public function isNormalizeDescription(): bool { ccc """ STRING, - new class() extends DefaultSettings { - public function isNormalizeDescription(): bool { - return true; - } - }, + $settings->setNormalizeDescription(true), 0, 0, <<<'STRING' @@ -234,7 +195,7 @@ public function isNormalizeDescription(): bool { <<<'STRING' """ Leading space""" STRING, - new DefaultSettings(), + $settings, 0, 0, ' Leading space', @@ -242,7 +203,7 @@ public function isNormalizeDescription(): bool { ], 'Leading tab' => [ "\"\"\"\tLeading tab\"\"\"", - new DefaultSettings(), + $settings, 0, 0, "\tLeading tab", @@ -254,7 +215,7 @@ public function isNormalizeDescription(): bool { Trailing " """ STRING, - new DefaultSettings(), + $settings, 0, 0, 'Trailing "', @@ -267,7 +228,7 @@ public function isNormalizeDescription(): bool { abc """ STRING, - new DefaultSettings(), + $settings, 0, 0, <<<'STRING' @@ -282,7 +243,7 @@ public function isNormalizeDescription(): bool { Trailing \\ """ STRING, - new DefaultSettings(), + $settings, 0, 0, 'Trailing \\\\', @@ -294,7 +255,7 @@ public function isNormalizeDescription(): bool { String with \""" wrapper """ STRING, - new DefaultSettings(), + $settings, 0, 0, 'String with """ wrapper', @@ -314,11 +275,7 @@ public function isNormalizeDescription(): bool { ' """', ], ), - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings->setIndent(' '), 2, 0, implode( @@ -348,15 +305,9 @@ public function getIndent(): string { ' """', ], ), - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - - public function isNormalizeDescription(): bool { - return true; - } - }, + $settings + ->setIndent(' ') + ->setNormalizeDescription(true), 2, 0, implode( @@ -378,11 +329,7 @@ public function isNormalizeDescription(): bool { Description """ STRING, - new class() extends DefaultSettings { - public function isIncludeDirectivesInDescription(): bool { - return false; - } - }, + $settings, 0, 0, <<<'STRING' @@ -403,11 +350,7 @@ public function isIncludeDirectivesInDescription(): bool { @b(test: "abc") """ STRING, - new class() extends DefaultSettings { - public function isIncludeDirectivesInDescription(): bool { - return true; - } - }, + $settings->setIncludeDirectivesInDescription(true), 0, 0, <<<'STRING' @@ -429,15 +372,9 @@ public function isIncludeDirectivesInDescription(): bool { @b(test: "abc") """ STRING, - new class() extends DefaultSettings { - public function isNormalizeDescription(): bool { - return true; - } - - public function isIncludeDirectivesInDescription(): bool { - return true; - } - }, + $settings + ->setNormalizeDescription(true) + ->setIncludeDirectivesInDescription(true), 0, 0, <<<'STRING' diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php index f4b196d3..3959cff2 100644 --- a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php @@ -12,7 +12,7 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use Mockery; use PHPUnit\Framework\TestCase; @@ -46,7 +46,7 @@ public function testToString( */ public function testToStringEvent(): void { $spy = Mockery::spy(static fn (Event $event) => null); - $settings = new DefaultSettings(); + $settings = new TestSettings(); $dispatcher = new Dispatcher(); $definition = new Directive([ 'name' => 'A', @@ -85,6 +85,8 @@ public function testToStringEvent(): void { * @return array */ public function dataProviderToString(): array { + $settings = new TestSettings(); + return [ 'description' => [ <<<'STRING' @@ -93,7 +95,7 @@ public function dataProviderToString(): array { """ directive @test on ARGUMENT_DEFINITION | ENUM STRING, - new DefaultSettings(), + $settings, 0, 0, new Directive([ @@ -109,7 +111,7 @@ public function dataProviderToString(): array { <<<'STRING' directive @test repeatable on ARGUMENT_DEFINITION | ENUM STRING, - new DefaultSettings(), + $settings, 0, 0, new Directive([ @@ -125,7 +127,7 @@ public function dataProviderToString(): array { <<<'STRING' directive @test(a: String) repeatable on ARGUMENT_DEFINITION | ENUM STRING, - new DefaultSettings(), + $settings, 0, 0, new Directive([ @@ -151,11 +153,7 @@ public function dataProviderToString(): array { | ARGUMENT_DEFINITION | ENUM STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 0, 120, new Directive([ @@ -181,11 +179,7 @@ public function getIndent(): string { | ARGUMENT_DEFINITION | ENUM STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 0, 120, new Directive([ @@ -207,11 +201,7 @@ public function getIndent(): string { | ARGUMENT_DEFINITION | ENUM STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 0, 60, new Directive([ @@ -231,11 +221,7 @@ public function getIndent(): string { | ARGUMENT_DEFINITION | ENUM STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 1, 120, new Directive([ @@ -255,15 +241,8 @@ public function getIndent(): string { <<<'STRING' directive @test on ENUM | INPUT_FIELD_DEFINITION | OBJECT STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - - public function isNormalizeDirectiveLocations(): bool { - return true; - } - }, + $settings + ->setNormalizeDirectiveLocations(true), 0, 0, new Directive([ diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php index 7a36c23d..e462f395 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php @@ -9,7 +9,7 @@ use GraphQL\Type\Definition\EnumValueDefinition; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use PHPUnit\Framework\TestCase; /** @@ -49,6 +49,8 @@ public function testToString( * @return array */ public function dataProviderToString(): array { + $settings = new TestSettings(); + return [ 'enum' => [ <<<'STRING' @@ -58,11 +60,7 @@ enum Test { A } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 0, 0, new EnumType([ @@ -78,11 +76,7 @@ enum Test { A } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 1, 0, new EnumType([ @@ -98,15 +92,7 @@ enum Test { C } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - - public function isNormalizeEnums(): bool { - return true; - } - }, + $settings->setNormalizeEnums(true), 0, 0, new EnumType([ @@ -130,11 +116,7 @@ enum Test { @deprecated } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings->setIncludeDirectives(true), 0, 0, static function (): EnumType { diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php index 3dfba92b..7f39f446 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php @@ -2,12 +2,11 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; -use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\Parser; use GraphQL\Type\Definition\EnumValueDefinition; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use PHPUnit\Framework\TestCase; /** @@ -42,12 +41,14 @@ public function testToString( * @return array */ public function dataProviderToString(): array { + $settings = new TestSettings(); + return [ 'value' => [ <<<'STRING' A STRING, - new DefaultSettings(), + $settings, 0, 0, new EnumValueDefinition([ @@ -59,11 +60,7 @@ public function dataProviderToString(): array { <<<'STRING' A STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 1, 0, new EnumValueDefinition([ @@ -79,15 +76,7 @@ public function getIndent(): string { A @a STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - - public function isIncludeDirectives(): bool { - return true; - } - }, + $settings->setIncludeDirectives(true), 0, 0, new EnumValueDefinition([ diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php index 627abcc7..1e52f78d 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php @@ -13,7 +13,7 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use Mockery; use PHPUnit\Framework\TestCase; @@ -47,7 +47,7 @@ public function testToString( */ public function testToStringEvent(): void { $spy = Mockery::spy(static fn (Event $event) => null); - $settings = new DefaultSettings(); + $settings = new TestSettings(); $dispatcher = new Dispatcher(); $definition = FieldDefinition::create([ 'name' => 'A', @@ -83,6 +83,8 @@ public function testToStringEvent(): void { * @return array */ public function dataProviderToString(): array { + $settings = new TestSettings(); + return [ 'without args' => [ <<<'STRING' @@ -92,7 +94,7 @@ public function dataProviderToString(): array { test: Test! @a STRING, - new DefaultSettings(), + $settings->setIncludeDirectives(true), 0, 0, FieldDefinition::create([ @@ -113,7 +115,7 @@ public function dataProviderToString(): array { """ test(a: [String!] = ["aaaaaaaaaaaaaaaaaaaaaaaaaa"], b: Int): Test! STRING, - new DefaultSettings(), + $settings, 0, 0, FieldDefinition::create([ @@ -148,15 +150,7 @@ public function dataProviderToString(): array { a: String! = "aaaaaaaaaaaaaaaaaaaaaaaaaa" ): Test! STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - - public function isNormalizeArguments(): bool { - return false; - } - }, + $settings, 0, 0, FieldDefinition::create([ @@ -182,15 +176,7 @@ public function isNormalizeArguments(): bool { <<<'STRING' test(a: String, b: Int): Test! STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - - public function isNormalizeArguments(): bool { - return true; - } - }, + $settings->setNormalizeArguments(true), 0, 0, FieldDefinition::create([ @@ -217,15 +203,7 @@ public function isNormalizeArguments(): bool { b: Int ): Test! STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - - public function isNormalizeArguments(): bool { - return true; - } - }, + $settings->setNormalizeArguments(true), 1, 120, FieldDefinition::create([ diff --git a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php index 50946966..f5621d1b 100644 --- a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php @@ -10,7 +10,7 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use Mockery; use PHPUnit\Framework\TestCase; @@ -46,7 +46,7 @@ public function testToString( */ public function testToStringEvent(): void { $spy = Mockery::spy(static fn (Event $event) => null); - $settings = new DefaultSettings(); + $settings = new TestSettings(); $dispatcher = new Dispatcher(); $definition = new InputObjectType([ 'name' => 'A', @@ -85,6 +85,8 @@ public function testToStringEvent(): void { * @return array */ public function dataProviderToString(): array { + $settings = new TestSettings(); + return [ 'description + directives' => [ <<<'STRING' @@ -94,7 +96,7 @@ public function dataProviderToString(): array { input Test @a STRING, - new DefaultSettings(), + $settings->setIncludeDirectives(true), 0, 0, new InputObjectType([ @@ -121,11 +123,7 @@ public function dataProviderToString(): array { a: A } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings->setIncludeDirectives(true), 0, 0, new InputObjectType([ @@ -161,11 +159,7 @@ public function getIndent(): string { a: String } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 0, 0, new InputObjectType([ @@ -184,15 +178,7 @@ public function getIndent(): string { a: String } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - - public function isNormalizeInterfaces(): bool { - return true; - } - }, + $settings->setNormalizeInterfaces(true), 1, 120, new InputObjectType([ diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php index f6b8e813..630aea85 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php @@ -13,7 +13,7 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use Mockery; use PHPUnit\Framework\TestCase; @@ -47,7 +47,7 @@ public function testToString( */ public function testToStringEvent(): void { $spy = Mockery::spy(static fn (Event $event) => null); - $settings = new DefaultSettings(); + $settings = new TestSettings(); $dispatcher = new Dispatcher(); $definition = new FieldArgument([ 'name' => 'A', @@ -83,6 +83,8 @@ public function testToStringEvent(): void { * @return array */ public function dataProviderToString(): array { + $settings = new TestSettings(); + return [ 'without value' => [ <<<'STRING' @@ -92,7 +94,7 @@ public function dataProviderToString(): array { test: Test! @a STRING, - new DefaultSettings(), + $settings->setIncludeDirectives(true), 0, 0, new FieldArgument([ @@ -113,7 +115,7 @@ public function dataProviderToString(): array { """ test: [String!] = ["aaaaaaaaaaaaaaaaaaaaaaaaaa"] STRING, - new DefaultSettings(), + $settings, 0, 0, new FieldArgument([ @@ -134,7 +136,7 @@ public function dataProviderToString(): array { "aaaaaaaaaaaaaaaaaaaaaaaaaa" ] STRING, - new DefaultSettings(), + $settings, 0, 120, new FieldArgument([ @@ -155,11 +157,7 @@ public function dataProviderToString(): array { "aaaaaaaaaaaaaaaaaaaaaaaaaa" ] STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 1, 70, new FieldArgument([ diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php index 7814f92d..c0508d5d 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php @@ -11,7 +11,7 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use Mockery; use PHPUnit\Framework\TestCase; @@ -45,7 +45,7 @@ public function testToString( */ public function testToStringEvent(): void { $spy = Mockery::spy(static fn (Event $event) => null); - $settings = new DefaultSettings(); + $settings = new TestSettings(); $dispatcher = new Dispatcher(); $definition = new InterfaceType([ 'name' => 'A', @@ -98,6 +98,8 @@ public function testToStringEvent(): void { * @return array */ public function dataProviderToString(): array { + $settings = new TestSettings(); + return [ 'description + directives' => [ <<<'STRING' @@ -107,7 +109,7 @@ public function dataProviderToString(): array { interface Test @a STRING, - new DefaultSettings(), + $settings->setIncludeDirectives(true), 0, 0, new InterfaceType([ @@ -134,11 +136,7 @@ interface Test a(a: Int): A } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings->setIncludeDirectives(true), 0, 0, new InterfaceType([ @@ -184,11 +182,7 @@ interface Test { a: String } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 0, 0, new InterfaceType([ @@ -209,11 +203,7 @@ interface Test implements B & A a: String } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings->setIncludeDirectives(true), 0, 0, new InterfaceType([ @@ -241,11 +231,7 @@ interface Test implements a: String } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings->setIncludeDirectives(true), 0, 120, new InterfaceType([ @@ -272,11 +258,7 @@ interface Test implements a: String } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 0, 120, new InterfaceType([ @@ -299,11 +281,7 @@ interface Test implements B & A { a: String } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 0, 0, new InterfaceType([ @@ -329,15 +307,7 @@ interface Test implements a: String } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - - public function isNormalizeInterfaces(): bool { - return true; - } - }, + $settings->setNormalizeInterfaces(true), 0, 120, new InterfaceType([ @@ -363,15 +333,7 @@ interface Test implements a: String } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - - public function isNormalizeInterfaces(): bool { - return true; - } - }, + $settings->setNormalizeInterfaces(true), 1, 120, new InterfaceType([ diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php index 8db1b17e..c7ed3854 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php @@ -10,7 +10,7 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use Mockery; use PHPUnit\Framework\TestCase; @@ -44,7 +44,7 @@ public function testToString( */ public function testToStringEvent(): void { $spy = Mockery::spy(static fn (Event $event) => null); - $settings = new DefaultSettings(); + $settings = new TestSettings(); $dispatcher = new Dispatcher(); $definition = new ObjectType([ 'name' => 'A', @@ -97,6 +97,8 @@ public function testToStringEvent(): void { * @return array */ public function dataProviderToString(): array { + $settings = new TestSettings(); + return [ 'description + directives' => [ <<<'STRING' @@ -106,7 +108,7 @@ public function dataProviderToString(): array { type Test @a STRING, - new DefaultSettings(), + $settings->setIncludeDirectives(true), 0, 0, new ObjectType([ @@ -133,11 +135,7 @@ public function dataProviderToString(): array { a(a: Int): A } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings->setIncludeDirectives(true), 0, 0, new ObjectType([ @@ -183,11 +181,7 @@ public function getIndent(): string { a: String } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 0, 0, new ObjectType([ @@ -208,11 +202,7 @@ public function getIndent(): string { a: String } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings->setIncludeDirectives(true), 0, 0, new ObjectType([ @@ -240,11 +230,7 @@ public function getIndent(): string { a: String } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings->setIncludeDirectives(true), 0, 120, new ObjectType([ @@ -271,11 +257,7 @@ public function getIndent(): string { a: String } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 0, 120, new ObjectType([ @@ -298,11 +280,7 @@ public function getIndent(): string { a: String } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 0, 0, new ObjectType([ @@ -328,15 +306,7 @@ public function getIndent(): string { a: String } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - - public function isNormalizeInterfaces(): bool { - return true; - } - }, + $settings->setNormalizeInterfaces(true), 0, 120, new ObjectType([ @@ -362,15 +332,7 @@ public function isNormalizeInterfaces(): bool { a: String } STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - - public function isNormalizeInterfaces(): bool { - return true; - } - }, + $settings->setNormalizeInterfaces(true), 1, 120, new ObjectType([ diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php index 51795d04..be146c1b 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php @@ -2,13 +2,12 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; -use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\Parser; use GraphQL\Type\Definition\CustomScalarType; use GraphQL\Type\Definition\ScalarType; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use PHPUnit\Framework\TestCase; /** @@ -43,12 +42,14 @@ public function testToString( * @return array */ public function dataProviderToString(): array { + $settings = new TestSettings(); + return [ 'scalar' => [ <<<'STRING' scalar Test STRING, - new DefaultSettings(), + $settings, 0, 0, new CustomScalarType([ @@ -63,15 +64,7 @@ public function dataProviderToString(): array { scalar Test @a STRING, - new class() extends DefaultSettings { - public function isIncludeDirectives(): bool { - return true; - } - - public function isIncludeDirectivesInDescription(): bool { - return false; - } - }, + $settings->setIncludeDirectives(true), 0, 0, new CustomScalarType([ @@ -93,15 +86,7 @@ public function isIncludeDirectivesInDescription(): bool { """ scalar Test STRING, - new class() extends DefaultSettings { - public function isIncludeDirectives(): bool { - return false; - } - - public function isIncludeDirectivesInDescription(): bool { - return true; - } - }, + $settings->setIncludeDirectivesInDescription(true), 0, 0, new CustomScalarType([ @@ -125,15 +110,7 @@ public function isIncludeDirectivesInDescription(): bool { ) @b(value: "b") STRING, - new class() extends DefaultSettings { - public function isIncludeDirectives(): bool { - return true; - } - - public function isIncludeDirectivesInDescription(): bool { - return false; - } - }, + $settings->setIncludeDirectives(true), 1, 60, new CustomScalarType([ @@ -154,20 +131,12 @@ public function isIncludeDirectivesInDescription(): bool { ) @b(value: "b") STRING, - new class() extends DefaultSettings { - public function isIncludeDirectives(): bool { - return true; - } - - public function isIncludeDirectivesInDescription(): bool { - return false; - } - }, + $settings->setIncludeDirectives(true), 1, 60, new CustomScalarType([ - 'name' => 'Test', - 'astNode' => Parser::scalarTypeDefinition( + 'name' => 'Test', + 'astNode' => Parser::scalarTypeDefinition( <<<'STRING' scalar Test @a(value: "very very long value") @b(value: "b") STRING, diff --git a/src/SchemaPrinter/Blocks/Types/StringBlockTest.php b/src/SchemaPrinter/Blocks/Types/StringBlockTest.php index df657152..c0575cfa 100644 --- a/src/SchemaPrinter/Blocks/Types/StringBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/StringBlockTest.php @@ -6,7 +6,7 @@ use GraphQL\Language\Parser; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use function implode; @@ -44,24 +44,26 @@ public function testToString( * @return array */ public function dataProviderToString(): array { + $settings = new TestSettings(); + return [ 'Prints an empty string' => [ '""""""', - new DefaultSettings(), + $settings, 0, 0, '', ], 'Prints an string with only whitespace' => [ '" "', - new DefaultSettings(), + $settings, 0, 0, ' ', ], 'One-line prints a short string' => [ '"""Short string"""', - new DefaultSettings(), + $settings, 0, 0, 'Short string', @@ -72,11 +74,7 @@ public function dataProviderToString(): array { Long string """ STRING, - new class() extends DefaultSettings { - public function getLineLength(): int { - return 4; - } - }, + $settings->setLineLength(4), 0, 0, 'Long string', @@ -85,15 +83,7 @@ public function getLineLength(): int { <<<'STRING' """string""" STRING, - new class() extends DefaultSettings { - public function getLineLength(): int { - return 21; - } - - public function getIndent(): string { - return ' '; - } - }, + $settings->setLineLength(21), 2, 0, 'string', @@ -104,15 +94,9 @@ public function getIndent(): string { string """ STRING, - new class() extends DefaultSettings { - public function getLineLength(): int { - return 22; - } - - public function getIndent(): string { - return ' '; - } - }, + $settings + ->setIndent(' ') + ->setLineLength(22), 2, 20, 'string', @@ -126,7 +110,7 @@ public function getIndent(): string { ccc """ STRING, - new DefaultSettings(), + $settings, 0, 0, <<<'STRING' @@ -140,25 +124,21 @@ public function getIndent(): string { <<<'STRING' """ Leading space""" STRING, - new DefaultSettings(), + $settings, 0, 0, ' Leading space', ], 'Leading tab' => [ "\"\"\"\tLeading tab\"\"\"", - new DefaultSettings(), + $settings, 0, 0, "\tLeading tab", ], 'Leading whitespace (single line)' => [ "\"\"\"\tLeading tab\"\"\"", - new class() extends DefaultSettings { - public function getLineLength(): int { - return 1; - } - }, + $settings->setLineLength(1), 0, 0, "\tLeading tab", @@ -169,7 +149,7 @@ public function getLineLength(): int { Trailing " """ STRING, - new DefaultSettings(), + $settings, 0, 0, 'Trailing "', @@ -181,7 +161,7 @@ public function getLineLength(): int { abc """ STRING, - new DefaultSettings(), + $settings, 0, 0, <<<'STRING' @@ -195,7 +175,7 @@ public function getLineLength(): int { Trailing \\ """ STRING, - new DefaultSettings(), + $settings, 0, 0, 'Trailing \\\\', @@ -204,7 +184,7 @@ public function getLineLength(): int { <<<'STRING' """String with \""" wrapper""" STRING, - new DefaultSettings(), + $settings, 0, 0, 'String with """ wrapper', @@ -223,11 +203,7 @@ public function getLineLength(): int { ' """', ], ), - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings->setIndent(' '), 2, 0, implode( diff --git a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php index 5a1d15fb..bd6bbc96 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php @@ -11,7 +11,7 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use Mockery; use PHPUnit\Framework\TestCase; @@ -48,7 +48,7 @@ public function testToStringEvent(): void { 'name' => 'Test', ]) ); - $settings = new DefaultSettings(); + $settings = new TestSettings(); $dispatcher = new Dispatcher(); $dispatcher->attach(Closure::fromCallable($spy)); @@ -76,10 +76,12 @@ public function testToStringEvent(): void { * @return array */ public function dataProviderToString(): array { + $settings = new TestSettings(); + return [ 'object' => [ 'Test', - new DefaultSettings(), + $settings, 0, 0, new ObjectType([ @@ -88,7 +90,7 @@ public function dataProviderToString(): array { ], 'non null' => [ 'Test!', - new DefaultSettings(), + $settings, 0, 0, new NonNull( @@ -99,7 +101,7 @@ public function dataProviderToString(): array { ], 'non null list' => [ '[Test]!', - new DefaultSettings(), + $settings, 0, 0, new NonNull( diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php index 9d0d425c..e3a7210a 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php @@ -10,7 +10,7 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use Mockery; use PHPUnit\Framework\TestCase; @@ -22,7 +22,7 @@ class UnionTypeDefinitionBlockTest extends TestCase { // ========================================================================= /** * @covers ::__toString - * @covers \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\UnionMemberTypesList::__toString + * @covers \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\UnionMemberTypesList::__toString * * @dataProvider dataProviderToString */ @@ -56,7 +56,7 @@ public function testToStringEvent(): void { ]), ], ]); - $settings = new DefaultSettings(); + $settings = new TestSettings(); $dispatcher = new Dispatcher(); $dispatcher->attach(Closure::fromCallable($spy)); @@ -91,12 +91,14 @@ public function testToStringEvent(): void { * @return array */ public function dataProviderToString(): array { + $settings = new TestSettings(); + return [ 'single-line' => [ <<<'STRING' union Test = C | B | A STRING, - new DefaultSettings(), + $settings, 0, 0, new UnionType([ @@ -121,11 +123,7 @@ public function dataProviderToString(): array { | B | A STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 0, 120, new UnionType([ @@ -147,11 +145,7 @@ public function getIndent(): string { <<<'STRING' union Test = C | B | A STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 1, 0, new UnionType([ @@ -176,11 +170,7 @@ public function getIndent(): string { | B | A STRING, - new class() extends DefaultSettings { - public function getIndent(): string { - return ' '; - } - }, + $settings, 1, 120, new UnionType([ @@ -202,11 +192,7 @@ public function getIndent(): string { <<<'STRING' union Test = A | B | C STRING, - new class() extends DefaultSettings { - public function isNormalizeUnions(): bool { - return true; - } - }, + $settings->setNormalizeUnions(true), 0, 0, new UnionType([ diff --git a/src/SchemaPrinter/Settings.php b/src/SchemaPrinter/Settings.php index ef951227..f1953b77 100644 --- a/src/SchemaPrinter/Settings.php +++ b/src/SchemaPrinter/Settings.php @@ -4,9 +4,6 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\DirectiveFilter; -// TODO: Directive resolver -// TODO: Throw error if directive definition not found - interface Settings { public function getSpace(): string; diff --git a/src/Testing/Package/SchemaPrinter/TestSettings.php b/src/Testing/Package/SchemaPrinter/TestSettings.php new file mode 100644 index 00000000..95bcd5fe --- /dev/null +++ b/src/Testing/Package/SchemaPrinter/TestSettings.php @@ -0,0 +1,206 @@ + + */ + private array $settings = [ + 'Space' => ' ', + 'Indent' => ' ', + 'LineEnd' => "\n", + 'LineLength' => 80, + 'NormalizeEnums' => false, + 'NormalizeUnions' => false, + 'NormalizeFields' => false, + 'NormalizeArguments' => false, + 'NormalizeInterfaces' => false, + 'NormalizeDescription' => false, + 'NormalizeDirectiveLocations' => false, + 'IncludeDirectives' => false, + 'IncludeDirectivesInDescription' => false, + 'DirectiveFilter' => null, + ]; + + public function __construct() { + // empty + } + + public function getSpace(): string { + return $this->get('Space'); + } + + public function setSpace(string $value): static { + return $this->set('Space', $value); + } + + public function getIndent(): string { + return $this->get('Indent'); + } + + public function setIndent(string $value): static { + return $this->set('Indent', $value); + } + + public function getFileEnd(): string { + return $this->get('FileEnd'); + } + + public function setFileEnd(string $value): static { + return $this->set('FileEnd', $value); + } + + public function getLineEnd(): string { + return $this->get('LineEnd'); + } + + public function setLineEnd(string $value): static { + return $this->set('LineEnd', $value); + } + + public function getLineLength(): int { + return $this->get('LineLength'); + } + + public function setLineLength(int $value): static { + return $this->set('LineLength', $value); + } + + public function isIncludeUnusedTypeDefinitions(): bool { + return $this->get('IncludeUnusedTypeDefinitions'); + } + + public function setIncludeUnusedTypeDefinitions(bool $value): static { + return $this->set('IncludeUnusedTypeDefinitions', $value); + } + + public function isIncludeDirectives(): bool { + return $this->get('IncludeDirectives'); + } + + public function setIncludeDirectives(bool $value): static { + return $this->set('IncludeDirectives', $value); + } + + public function isIncludeDirectivesInDescription(): bool { + return $this->get('IncludeDirectivesInDescription'); + } + + public function setIncludeDirectivesInDescription(bool $value): static { + return $this->set('IncludeDirectivesInDescription', $value); + } + + public function isIncludeUnusedDirectiveDefinitions(): bool { + return $this->get('IncludeUnusedDirectiveDefinitions'); + } + + public function setIncludeUnusedDirectiveDefinitions(bool $value): static { + return $this->set('IncludeUnusedDirectiveDefinitions', $value); + } + + public function isNormalizeTypes(): bool { + return $this->get('NormalizeTypes'); + } + + public function setNormalizeTypes(bool $value): static { + return $this->set('NormalizeTypes', $value); + } + + public function isNormalizeUnions(): bool { + return $this->get('NormalizeUnions'); + } + + public function setNormalizeUnions(bool $value): static { + return $this->set('NormalizeUnions', $value); + } + + public function isNormalizeEnums(): bool { + return $this->get('NormalizeEnums'); + } + + public function setNormalizeEnums(bool $value): static { + return $this->set('NormalizeEnums', $value); + } + + public function isNormalizeInterfaces(): bool { + return $this->get('NormalizeInterfaces'); + } + + public function setNormalizeInterfaces(bool $value): static { + return $this->set('NormalizeInterfaces', $value); + } + + public function isNormalizeFields(): bool { + return $this->get('NormalizeFields'); + } + + public function setNormalizeFields(bool $value): static { + return $this->set('NormalizeFields', $value); + } + + public function isNormalizeArguments(): bool { + return $this->get('NormalizeArguments'); + } + + public function setNormalizeArguments(bool $value): static { + return $this->set('NormalizeArguments', $value); + } + + public function isNormalizeDescription(): bool { + return $this->get('NormalizeDescription'); + } + + public function setNormalizeDescription(bool $value): static { + return $this->set('NormalizeDescription', $value); + } + + public function isNormalizeDirectiveLocations(): bool { + return $this->get('NormalizeDirectiveLocations'); + } + + public function setNormalizeDirectiveLocations(bool $value): static { + return $this->set('NormalizeDirectiveLocations', $value); + } + + public function getDirectiveFilter(): ?DirectiveFilter { + return $this->get('DirectiveFilter'); + } + + /** + * @param DirectiveFilter|Closure(DirectiveNode):bool|null $value + */ + public function setDirectiveFilter(DirectiveFilter|Closure|null $value): static { + if ($value instanceof Closure) { + $value = new class($value) implements DirectiveFilter { + public function __construct( + protected Closure $filter, + ) { + // empty + } + + public function isAllowedDirective(DirectiveNode $directive): bool { + return ($this->filter)($directive); + } + }; + } + + return $this->set('DirectiveFilter', $value); + } + + protected function get(string $setting): mixed { + return $this->settings[$setting]; + } + + protected function set(string $setting, mixed $value): static { + $settings = clone $this; + $settings->settings[$setting] = $value; + + return $settings; + } +} From 03071a0f83ed877a5650ec58b51b58dad3fc561f Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 22 Jan 2022 11:28:12 +0400 Subject: [PATCH 48/90] Fixed formatting for "empty description + directives" case. --- .../Blocks/Types/Description.php | 2 +- .../Blocks/Types/DescriptionTest.php | 22 +++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Types/Description.php b/src/SchemaPrinter/Blocks/Types/Description.php index 23db7f81..58542cb7 100644 --- a/src/SchemaPrinter/Blocks/Types/Description.php +++ b/src/SchemaPrinter/Blocks/Types/Description.php @@ -55,7 +55,7 @@ protected function getString(): string { $directives = (string) $this->getDirectives(); if ($directives) { - $eol = $this->eol(); + $eol = $string ? $this->eol() : ''; $string = "{$string}{$eol}{$eol}{$directives}"; } } diff --git a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php index d0594a0b..e966ee6b 100644 --- a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php +++ b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php @@ -88,7 +88,7 @@ public function dataProviderToString(): array { ], 'Prints an empty string with only whitespace (normalized)' => [ '', - $settings->setNormalizeDescription(true), + $settings->setNormalizeDescription(true), 0, 0, ' ', @@ -179,7 +179,7 @@ public function dataProviderToString(): array { ccc """ STRING, - $settings->setNormalizeDescription(true), + $settings->setNormalizeDescription(true), 0, 0, <<<'STRING' @@ -387,6 +387,24 @@ public function dataProviderToString(): array { Parser::directive('@b(test: "abc")'), ], ], + 'empty description + directives (enabled) + normalized' => [ + <<<'STRING' + """ + @a + @b(test: "abc") + """ + STRING, + $settings + ->setNormalizeDescription(true) + ->setIncludeDirectivesInDescription(true), + 0, + 0, + '', + [ + Parser::directive('@a'), + Parser::directive('@b(test: "abc")'), + ], + ], ]; } // From e7b2d3647332424f72bdd611ed6a4b5ddd3adc49 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 22 Jan 2022 11:29:05 +0400 Subject: [PATCH 49/90] `SchemaDefinition` support. --- .../Blocks/Types/DefinitionBlock.php | 61 +++++-- .../Blocks/Types/OperationType.php | 23 +++ .../RootOperationTypeDefinitionBlock.php | 34 ++++ .../RootOperationTypesDefinitionList.php | 22 +++ .../Blocks/Types/SchemaDefinitionBlock.php | 104 ++++++++++++ .../Types/SchemaDefinitionBlockTest.php | 151 ++++++++++++++++++ 6 files changed, 385 insertions(+), 10 deletions(-) create mode 100644 src/SchemaPrinter/Blocks/Types/OperationType.php create mode 100644 src/SchemaPrinter/Blocks/Types/RootOperationTypeDefinitionBlock.php create mode 100644 src/SchemaPrinter/Blocks/Types/RootOperationTypesDefinitionList.php create mode 100644 src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php create mode 100644 src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php diff --git a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php index 6d3eb430..fba1f5ac 100644 --- a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php @@ -2,12 +2,15 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; +use GraphQL\Language\AST\DirectiveNode; +use GraphQL\Language\AST\NodeList; use GraphQL\Type\Definition\Directive; use GraphQL\Type\Definition\EnumValueDefinition; use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\FieldDefinition; use GraphQL\Type\Definition\InputObjectField; use GraphQL\Type\Definition\Type; +use GraphQL\Type\Schema; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; @@ -19,7 +22,7 @@ /** * @internal * - * @template TType of Type|FieldDefinition|EnumValueDefinition|FieldArgument|Directive|InputObjectField + * @template TType of Type|FieldDefinition|EnumValueDefinition|FieldArgument|Directive|InputObjectField|Schema */ abstract class DefinitionBlock extends Block implements Named { /** @@ -30,7 +33,7 @@ public function __construct( Settings $settings, int $level, int $used, - private Type|FieldDefinition|EnumValueDefinition|FieldArgument|Directive|InputObjectField $definition, + private Type|FieldDefinition|EnumValueDefinition|FieldArgument|Directive|InputObjectField|Schema $definition, ) { parent::__construct($dispatcher, $settings, $level, $used); } @@ -39,9 +42,13 @@ public function getName(): string { $name = $this->name(); $type = $this->type(); - if ($type) { + if ($type && $name) { $space = $this->space(); $name = "{$type}{$space}{$name}"; + } elseif ($type) { + $name = $type; + } else { + // empty } return $name; @@ -50,7 +57,9 @@ public function getName(): string { /** * @return TType */ - protected function getDefinition(): Type|FieldDefinition|EnumValueDefinition|FieldArgument|Directive|InputObjectField { + protected function getDefinition( + // empty + ): Type|FieldDefinition|EnumValueDefinition|FieldArgument|Directive|InputObjectField|Schema { return $this->definition; } @@ -91,7 +100,12 @@ protected function content(): string { } protected function name(): string { - return $this->getDefinition()->name; + $definition = $this->getDefinition(); + $name = !($definition instanceof Schema) + ? $definition->name + : ''; + + return $name; } abstract protected function type(): string|null; @@ -107,24 +121,51 @@ protected function directives(): DirectiveNodeList { $this->getSettings(), $this->getLevel(), $this->getUsed(), - $definition->astNode?->directives ?? null, + $this->getDefinitionDirectives(), $definition->deprecationReason ?? null, ); return $directives; } - protected function description(DirectiveNodeList $directives): Description { + protected function description(DirectiveNodeList $directives): ?Description { + // Supported? $definition = $this->getDefinition(); - $description = new Description( + $description = null; + + if ($definition instanceof Schema) { + // It is part of October2021 spec but not yet supported + // https://github.com/webonyx/graphql-php/issues/1027 + } else { + $description = $definition->description; + } + + return new Description( $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $this->getUsed(), - $definition->description, + $description, $directives, ); + } - return $description; + /** + * @return NodeList + */ + protected function getDefinitionDirectives(): NodeList { + $definition = $this->getDefinition(); + $astNode = $definition instanceof Schema + ? $definition->getAstNode() + : $definition->astNode; + $directives = $astNode?->directives ?? null; + + if ($directives === null) { + /** @var NodeList $empty */ + $empty = new NodeList([]); + $directives = $empty; + } + + return $directives; } } diff --git a/src/SchemaPrinter/Blocks/Types/OperationType.php b/src/SchemaPrinter/Blocks/Types/OperationType.php new file mode 100644 index 00000000..5e9f64c8 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/OperationType.php @@ -0,0 +1,23 @@ +operation; + } + + protected function content(): string { + $content = parent::content(); + $content = "{$this->getOperation()}:{$this->space()}{$content}"; + + return $content; + } +} diff --git a/src/SchemaPrinter/Blocks/Types/RootOperationTypesDefinitionList.php b/src/SchemaPrinter/Blocks/Types/RootOperationTypesDefinitionList.php new file mode 100644 index 00000000..30b29377 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/RootOperationTypesDefinitionList.php @@ -0,0 +1,22 @@ +getSettings()->isNormalizeFields(); + } + + protected function isAlwaysMultiline(): bool { + return true; + } +} diff --git a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php new file mode 100644 index 00000000..311e3b89 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php @@ -0,0 +1,104 @@ + + */ +class SchemaDefinitionBlock extends DefinitionBlock { + public function __construct( + Dispatcher $dispatcher, + Settings $settings, + int $level, + int $used, + Schema $definition, + ) { + parent::__construct($dispatcher, $settings, $level, $used, $definition); + } + + protected function type(): string|null { + return 'schema'; + } + + protected function content(): string { + $content = parent::content(); + + if ($this->isUseDefaultRootOperationTypeNames()) { + $content = ''; + } + + return $content; + } + + protected function body(int $used): Block|string|null { + return null; + } + + protected function fields(int $used): Block|string|null { + $definition = $this->getDefinition(); + $space = $this->space(); + $fields = new RootOperationTypesDefinitionList( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $used + mb_strlen($space), + ); + $types = [ + [OperationType::query(), $definition->getQueryType()], + [OperationType::mutation(), $definition->getMutationType()], + [OperationType::subscription(), $definition->getSubscriptionType()], + ]; + + foreach ($types as $config) { + [$operation, $type] = $config; + + if ($type) { + $fields[] = new RootOperationTypeDefinitionBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel() + 1, + $this->getUsed(), + $operation, + $type, + ); + } + } + + return $fields; + } + + public function isUseDefaultRootOperationTypeNames(): bool { + // Directives? + if (count($this->getDefinitionDirectives()) > 0) { + return false; + } + + // Names? + $definition = $this->getDefinition(); + $rootTypes = [ + 'Query' => $definition->getQueryType(), + 'Mutation' => $definition->getMutationType(), + 'Subscription' => $definition->getSubscriptionType(), + ]; + $nonStandard = array_filter( + $rootTypes, + static function (?ObjectType $type, string $name): bool { + return $type !== null && $type->name !== $name; + }, + ARRAY_FILTER_USE_BOTH, + ); + + return !$nonStandard; + } +} diff --git a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php new file mode 100644 index 00000000..af6109a8 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php @@ -0,0 +1,151 @@ + + // ========================================================================= + /** + * @covers ::__toString + * + * @dataProvider dataProviderToString + */ + public function testToString( + string $expected, + Settings $settings, + int $level, + int $used, + Schema $schema, + ): void { + $actual = (string) (new SchemaDefinitionBlock(new Dispatcher(), $settings, $level, $used, $schema)); + + if ($expected && !str_starts_with($actual, '"""')) { + // https://github.com/webonyx/graphql-php/issues/1027 + Parser::schemaDefinition($actual); + } + + self::assertEquals($expected, $actual); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderToString(): array { + $settings = new TestSettings(); + + return [ + 'standard names' => [ + '', + $settings, + 0, + 0, + new Schema([ + 'query' => new ObjectType(['name' => 'Query']), + 'mutation' => new ObjectType(['name' => 'Mutation']), + 'subscription' => new ObjectType(['name' => 'Subscription']), + ]), + ], + 'standard names with directives' => [ + <<<'STRING' + schema + @a + { + query: Query + mutation: Mutation + subscription: Subscription + } + STRING, + $settings->setIncludeDirectives(true), + 0, + 0, + new Schema([ + 'query' => new ObjectType(['name' => 'Query']), + 'mutation' => new ObjectType(['name' => 'Mutation']), + 'subscription' => new ObjectType(['name' => 'Subscription']), + 'astNode' => Parser::schemaDefinition( + <<<'STRING' + schema @a { query: Query } + STRING, + ), + ]), + ], + 'standard names with directives in description' => [ + <<<'STRING' + """ + @a + """ + schema { + query: Query + mutation: Mutation + subscription: Subscription + } + STRING, + $settings->setIncludeDirectivesInDescription(true), + 0, + 0, + new Schema([ + 'query' => new ObjectType(['name' => 'Query']), + 'mutation' => new ObjectType(['name' => 'Mutation']), + 'subscription' => new ObjectType(['name' => 'Subscription']), + 'astNode' => Parser::schemaDefinition( + <<<'STRING' + schema @a { query: Query } + STRING, + ), + ]), + ], + 'non standard names' => [ + <<<'STRING' + schema { + query: MyQuery + mutation: Mutation + subscription: Subscription + } + STRING, + $settings, + 0, + 0, + new Schema([ + 'query' => new ObjectType(['name' => 'MyQuery']), + 'mutation' => new ObjectType(['name' => 'Mutation']), + 'subscription' => new ObjectType(['name' => 'Subscription']), + ]), + ], + 'indent' => [ + <<<'STRING' + schema { + query: MyQuery + mutation: Mutation + subscription: Subscription + } + STRING, + $settings, + 1, + 0, + new Schema([ + 'query' => new ObjectType(['name' => 'MyQuery']), + 'mutation' => new ObjectType(['name' => 'Mutation']), + 'subscription' => new ObjectType(['name' => 'Subscription']), + ]), + ], + ]; + } + // +} From 2068def91310faca6a95ee6c630879955acdf880 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 22 Jan 2022 14:59:34 +0400 Subject: [PATCH 50/90] `SchemaPrinter` implementation (without tests). --- src/SchemaPrinter/Blocks/Block.php | 19 +- .../Blocks/Printer/DefinitionBlock.php | 180 +++++++++++++++++ .../Blocks/Printer/DefinitionList.php | 46 +++++ src/SchemaPrinter/Exceptions/Exception.php | 9 + .../Exceptions/TypeUnsupported.php | 27 +++ src/SchemaPrinter/PrintedSchema.php | 17 ++ src/SchemaPrinter/Printer.php | 189 ++++++++++++++++++ src/SchemaPrinter/Settings.php | 12 +- .../Settings/DefaultSettings.php | 7 +- .../Package/SchemaPrinter/TestSettings.php | 8 +- 10 files changed, 501 insertions(+), 13 deletions(-) create mode 100644 src/SchemaPrinter/Blocks/Printer/DefinitionBlock.php create mode 100644 src/SchemaPrinter/Blocks/Printer/DefinitionList.php create mode 100644 src/SchemaPrinter/Exceptions/Exception.php create mode 100644 src/SchemaPrinter/Exceptions/TypeUnsupported.php create mode 100644 src/SchemaPrinter/PrintedSchema.php create mode 100644 src/SchemaPrinter/Printer.php diff --git a/src/SchemaPrinter/Blocks/Block.php b/src/SchemaPrinter/Blocks/Block.php index b3224f4d..42bf9877 100644 --- a/src/SchemaPrinter/Blocks/Block.php +++ b/src/SchemaPrinter/Blocks/Block.php @@ -2,6 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; +use Closure; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use Stringable; @@ -53,16 +54,30 @@ public function isEmpty(): bool { } public function getLength(): int { - return $this->length ?? mb_strlen($this->getContent()); + return $this->resolve(fn(): int => (int) $this->length); } public function isMultiline(): bool { - return $this->getContent() && $this->multiline; + return $this->resolve(fn(): bool => (bool) $this->multiline); } public function __toString(): string { return $this->getContent(); } + + /** + * @template T + * + * @param Closure(string $content): T $callback + * + * @return T + */ + protected function resolve(Closure $callback): mixed { + $content = $this->getContent(); + $result = $callback($content); + + return $result; + } // // diff --git a/src/SchemaPrinter/Blocks/Printer/DefinitionBlock.php b/src/SchemaPrinter/Blocks/Printer/DefinitionBlock.php new file mode 100644 index 00000000..db3e1710 --- /dev/null +++ b/src/SchemaPrinter/Blocks/Printer/DefinitionBlock.php @@ -0,0 +1,180 @@ + + */ + private array $usedTypes = []; + + /** + * @var array + */ + private array $usedDirectives = []; + + public function __construct( + Settings $settings, + int $level, + Schema|Type|Directive $definition, + ) { + $this->block = $this->getDefinitionBlock($definition); + $dispatcher = new Dispatcher(); + $dispatcher->attach(function (Event $event): void { + if ($event instanceof DirectiveUsed) { + $this->usedDirectives[$event->name] = $event->name; + } + + if ($event instanceof TypeUsed) { + $this->usedTypes[$event->name] = $event->name; + } + }); + + parent::__construct($dispatcher, $settings, $level); + } + + public function getName(): string { + $name = ''; + $block = $this->getBlock(); + + if ($block instanceof Named) { + $name = $block->getName(); + } + + return $name; + } + + /** + * @return array + */ + public function getUsedTypes(): array { + return $this->resolve(fn (): array => $this->usedTypes); + } + + /** + * @return array + */ + public function getUsedDirectives(): array { + return $this->resolve(fn (): array => $this->usedDirectives); + } + + protected function getBlock(): Block { + return $this->block; + } + + protected function reset(): void { + $this->usedTypes = []; + $this->usedDirectives = []; + + parent::reset(); + } + + protected function content(): string { + return (string) $this->getBlock(); + } + + protected function getDefinitionBlock(Schema|Type|Directive $definition): Block { + $block = null; + + if ($definition instanceof ObjectType) { + $block = new ObjectTypeDefinitionBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $definition, + ); + } elseif ($definition instanceof InputObjectType) { + $block = new InputObjectTypeDefinitionBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $definition, + ); + } elseif ($definition instanceof ScalarType) { + $block = new ScalarTypeDefinitionBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $definition, + ); + } elseif ($definition instanceof InterfaceType) { + $block = new InterfaceTypeDefinitionBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $definition, + ); + } elseif ($definition instanceof UnionType) { + $block = new UnionTypeDefinitionBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $definition, + ); + } elseif ($definition instanceof EnumType) { + $block = new EnumTypeDefinitionBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $definition, + ); + } elseif ($definition instanceof Directive) { + $block = new DirectiveDefinitionBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $definition, + ); + } elseif ($definition instanceof Schema) { + $block = new SchemaDefinitionBlock( + $this->getDispatcher(), + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $definition, + ); + } else { + throw new TypeUnsupported($definition); + } + + return $block; + } +} diff --git a/src/SchemaPrinter/Blocks/Printer/DefinitionList.php b/src/SchemaPrinter/Blocks/Printer/DefinitionList.php new file mode 100644 index 00000000..741f323d --- /dev/null +++ b/src/SchemaPrinter/Blocks/Printer/DefinitionList.php @@ -0,0 +1,46 @@ +getSettings()->isNormalizeSchema(); + } + + protected function isAlwaysMultiline(): bool { + return true; + } + + protected function isIndented(): bool { + return $this->indented; + } + + protected function content(): string { + $content = parent::content(); + + if ($content && $this->isIndented()) { + $content = "{$this->indent()}{$content}"; + } + + return $content; + } +} diff --git a/src/SchemaPrinter/Exceptions/Exception.php b/src/SchemaPrinter/Exceptions/Exception.php new file mode 100644 index 00000000..d548e67a --- /dev/null +++ b/src/SchemaPrinter/Exceptions/Exception.php @@ -0,0 +1,9 @@ +type, + ), + $previous + ); + } + + public function getType(): Type { + return $this->type; + } +} diff --git a/src/SchemaPrinter/PrintedSchema.php b/src/SchemaPrinter/PrintedSchema.php new file mode 100644 index 00000000..76e8e64f --- /dev/null +++ b/src/SchemaPrinter/PrintedSchema.php @@ -0,0 +1,17 @@ +schema; + } +} diff --git a/src/SchemaPrinter/Printer.php b/src/SchemaPrinter/Printer.php new file mode 100644 index 00000000..34fecc28 --- /dev/null +++ b/src/SchemaPrinter/Printer.php @@ -0,0 +1,189 @@ +settings = $settings ?? new DefaultSettings(); + } + + public function getLevel(): int { + return $this->level; + } + + public function setLevel(int $level): static { + $this->level = $level; + + return $this; + } + + public function getSettings(): Settings { + return $this->settings; + } + + public function setSettings(Settings $settings): static { + $this->settings = $settings; + + return $this; + } + + public function print(Schema $schema): PrintedSchema { + // Collect + $usedTypes = []; + $usedDirectives = []; + $schemaBlock = $this->getSchema($schema, $usedTypes, $usedDirectives); + $typesBlocks = $this->getSchemaTypes($schema, $usedTypes, $usedDirectives); + $directivesBlocks = $this->getSchemaDirectives($schema, $usedTypes, $usedDirectives); + + // Print + $settings = $this->getSettings(); + $content = new DefinitionList($this->getSettings(), $this->getLevel(), true); + $content[] = $schemaBlock; + $content[] = $this->getDefinitionList( + $typesBlocks, + $usedTypes, + $settings->isIncludeUnusedTypeDefinitions(), + ); + $content[] = $this->getDefinitionList( + $directivesBlocks, + $usedDirectives, + $settings->isIncludeUnusedDirectiveDefinitions(), + ); + + // todo(graphql): directives in description + + // Return + return new PrintedSchema((string) $content); + } + + /** + * @param array $usedTypes + * @param array $usedDirectives + */ + protected function getSchema( + Schema $schema, + array &$usedTypes = [], + array &$usedDirectives = [], + ): Block { + return $this->getDefinitionBlock($schema, $usedTypes, $usedDirectives); + } + + /** + * @param array $usedTypes + * @param array $usedDirectives + * + * @return array + */ + protected function getSchemaTypes(Schema $schema, array &$usedTypes = [], array &$usedDirectives = []): array { + $blocks = []; + + foreach ($schema->getTypeMap() as $type) { + // Standard? + if (Type::isBuiltInType($type)) { + continue; + } + + // Nope + $blocks[] = $this->getDefinitionBlock($type, $usedTypes, $usedDirectives); + } + + return $blocks; + } + + /** + * @param array $usedTypes + * @param array $usedDirectives + * + * @return array + */ + protected function getSchemaDirectives(Schema $schema, array &$usedTypes = [], array &$usedDirectives = []): array { + // Included? + $blocks = []; + $settings = $this->getSettings(); + $included = $settings->isIncludeDirectives() || $settings->isIncludeDirectivesInDescription(); + + if (!$included) { + return $blocks; + } + + // Add + foreach ($schema->getDirectives() as $directive) { + // Standard? + if (Directive::isSpecifiedDirective($directive)) { + continue; + } + + // Nope + $blocks[] = $this->getDefinitionBlock($directive, $usedTypes, $usedDirectives); + } + + // Return + return $blocks; + } + + /** + * @param array $usedTypes + * @param array $usedDirectives + */ + protected function getDefinitionBlock( + Schema|Type|Directive $definition, + array &$usedTypes = [], + array &$usedDirectives = [], + ): Block { + $block = new DefinitionBlock($this->getSettings(), $this->getLevel(), $definition); + $usedTypes += $block->getUsedTypes(); + $usedDirectives += $block->getUsedDirectives(); + + return $block; + } + + /** + * @param array $blocks + * @param array $used + */ + protected function getDefinitionList( + array $blocks, + array $used, + bool $includeUnused, + ): BlockList { + $list = new DefinitionList($this->getSettings(), $this->getLevel()); + + foreach ($blocks as $block) { + if ($includeUnused || $this->getDefinitionListIsUsed($block, $used)) { + $list[] = $block; + } + } + + return $list; + } + + /** + * @param array $used + */ + private function getDefinitionListIsUsed(Block $block, array $used): bool { + // Block names may have a "type name" prefix eg "type A", "interface B", + // etc, meanwhile `$used` doesn't have it. So we should remove the + // prefix before checking. + $name = $block instanceof Named ? $block->getName() : ''; + $name = explode(' ', $name); + $name = end($name); + + return isset($used[$name]); + } +} diff --git a/src/SchemaPrinter/Settings.php b/src/SchemaPrinter/Settings.php index f1953b77..9efdb29a 100644 --- a/src/SchemaPrinter/Settings.php +++ b/src/SchemaPrinter/Settings.php @@ -19,15 +19,21 @@ public function isIncludeUnusedTypeDefinitions(): bool; public function isIncludeDirectives(): bool; + /** + * Temporary workaround to show directives when they are not supported out + * of the box. + * + * @see https://github.com/graphql/graphql-playground/issues/1207 + */ public function isIncludeDirectivesInDescription(): bool; public function isIncludeUnusedDirectiveDefinitions(): bool; /** - * If `false` types will be printed in the original order if `true` they - * will be sorted by name. + * If `false` types and directives in the schema will be printed in the + * original order if `true` they will be sorted by name. */ - public function isNormalizeTypes(): bool; + public function isNormalizeSchema(): bool; /** * If `false` members will be printed in the original order if `true` they diff --git a/src/SchemaPrinter/Settings/DefaultSettings.php b/src/SchemaPrinter/Settings/DefaultSettings.php index 3782caaf..7c46d3a9 100644 --- a/src/SchemaPrinter/Settings/DefaultSettings.php +++ b/src/SchemaPrinter/Settings/DefaultSettings.php @@ -2,7 +2,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use Closure; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\DirectiveFilter; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -16,7 +15,7 @@ public function getSpace(): string { } public function getIndent(): string { - return ' '; + return ' '; } public function getFileEnd(): string { @@ -36,7 +35,7 @@ public function isIncludeUnusedTypeDefinitions(): bool { } public function isIncludeDirectives(): bool { - return true; + return false; } public function isIncludeDirectivesInDescription(): bool { @@ -47,7 +46,7 @@ public function isIncludeUnusedDirectiveDefinitions(): bool { return false; } - public function isNormalizeTypes(): bool { + public function isNormalizeSchema(): bool { return false; } diff --git a/src/Testing/Package/SchemaPrinter/TestSettings.php b/src/Testing/Package/SchemaPrinter/TestSettings.php index 95bcd5fe..1ad37d30 100644 --- a/src/Testing/Package/SchemaPrinter/TestSettings.php +++ b/src/Testing/Package/SchemaPrinter/TestSettings.php @@ -104,12 +104,12 @@ public function setIncludeUnusedDirectiveDefinitions(bool $value): static { return $this->set('IncludeUnusedDirectiveDefinitions', $value); } - public function isNormalizeTypes(): bool { - return $this->get('NormalizeTypes'); + public function isNormalizeSchema(): bool { + return $this->get('NormalizeSchema'); } - public function setNormalizeTypes(bool $value): static { - return $this->set('NormalizeTypes', $value); + public function setNormalizeSchema(bool $value): static { + return $this->set('NormalizeSchema', $value); } public function isNormalizeUnions(): bool { From c966c3ebf5efa3d06fd0227794e7aa523a68371f Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 22 Jan 2022 15:34:03 +0400 Subject: [PATCH 51/90] Schema will end on EOF. --- src/SchemaPrinter/Blocks/Printer/DefinitionList.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Printer/DefinitionList.php b/src/SchemaPrinter/Blocks/Printer/DefinitionList.php index 741f323d..beb71101 100644 --- a/src/SchemaPrinter/Blocks/Printer/DefinitionList.php +++ b/src/SchemaPrinter/Blocks/Printer/DefinitionList.php @@ -13,7 +13,7 @@ class DefinitionList extends BlockList { public function __construct( Settings $settings, int $level, - protected bool $indented = false, + protected bool $schema = false, ) { parent::__construct(new Dispatcher(), $settings, $level); } @@ -30,15 +30,16 @@ protected function isAlwaysMultiline(): bool { return true; } - protected function isIndented(): bool { - return $this->indented; + protected function isSchema(): bool { + return $this->schema; } protected function content(): string { $content = parent::content(); - if ($content && $this->isIndented()) { - $content = "{$this->indent()}{$content}"; + if ($content && $this->isSchema()) { + $eof = $this->getSettings()->getFileEnd(); + $content = "{$this->indent()}{$content}{$eof}"; } return $content; From 80a68164e9b5b603dfa179be920344e74ba802f1 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 22 Jan 2022 15:47:17 +0400 Subject: [PATCH 52/90] Added `ImmutableSettings` and changed default values a bit. --- .../Blocks/Ast/DirectiveNodeBlockTest.php | 3 +- .../Blocks/Ast/DirectiveNodeList.php | 8 +- .../Blocks/Types/DescriptionTest.php | 3 +- .../Types/EnumTypeDefinitionBlockTest.php | 3 +- .../Blocks/Types/FieldDefinitionBlockTest.php | 3 +- .../InputObjectTypeDefinitionBlockTest.php | 3 +- .../InterfaceTypeDefinitionBlockTest.php | 4 +- .../Types/ObjectTypeDefinitionBlockTest.php | 4 +- .../Types/ScalarTypeDefinitionBlockTest.php | 3 +- .../Types/SchemaDefinitionBlockTest.php | 4 +- .../Types/UnionTypeDefinitionBlockTest.php | 3 +- .../Settings/DefaultSettings.php | 96 ++------ .../Settings/ImmutableSettings.php | 220 ++++++++++++++++++ .../Package/SchemaPrinter/TestSettings.php | 200 ++-------------- 14 files changed, 288 insertions(+), 269 deletions(-) create mode 100644 src/SchemaPrinter/Settings/ImmutableSettings.php diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php index 202b2a68..b64cf3f8 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php @@ -79,7 +79,8 @@ public function testToStringEvent(): void { * @return array */ public function dataProviderToString(): array { - $settings = new TestSettings(); + $settings = (new TestSettings()) + ->setNormalizeArguments(false); return [ 'without arguments' => [ diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php index a258d47f..48c51866 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php @@ -6,7 +6,6 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\Directive; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeBlock as DirectiveBlock; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use Traversable; @@ -32,7 +31,7 @@ public function __construct( ) { parent::__construct($dispatcher, $settings, $level, $used); - $deprecated = Directive::DEPRECATED_NAME; + $deprecated = Directive::DEPRECATED_NAME; $directives ??= []; if ($deprecationReason) { @@ -74,8 +73,9 @@ protected function getBlocks(): array { return $blocks; } - private function block(DirectiveNode $directive): DirectiveBlock { - return new DirectiveBlock( + private function block(DirectiveNode $directive + ): \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeBlock { + return new \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeBlock( $this->getDispatcher(), $this->getSettings(), $this->getLevel(), diff --git a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php index e966ee6b..1b2a1900 100644 --- a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php +++ b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php @@ -51,7 +51,8 @@ public function testToString( * @return array|null}> */ public function dataProviderToString(): array { - $settings = new TestSettings(); + $settings = (new TestSettings()) + ->setNormalizeDescription(false); return [ 'null' => [ diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php index e462f395..53d1491f 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php @@ -49,7 +49,8 @@ public function testToString( * @return array */ public function dataProviderToString(): array { - $settings = new TestSettings(); + $settings = (new TestSettings()) + ->setNormalizeEnums(false); return [ 'enum' => [ diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php index 1e52f78d..9831df2b 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php @@ -83,7 +83,8 @@ public function testToStringEvent(): void { * @return array */ public function dataProviderToString(): array { - $settings = new TestSettings(); + $settings = (new TestSettings()) + ->setNormalizeArguments(false); return [ 'without args' => [ diff --git a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php index f5621d1b..8d1ce5e3 100644 --- a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php @@ -85,7 +85,8 @@ public function testToStringEvent(): void { * @return array */ public function dataProviderToString(): array { - $settings = new TestSettings(); + $settings = (new TestSettings()) + ->setNormalizeFields(false); return [ 'description + directives' => [ diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php index c0508d5d..1988104f 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php @@ -98,7 +98,9 @@ public function testToStringEvent(): void { * @return array */ public function dataProviderToString(): array { - $settings = new TestSettings(); + $settings = (new TestSettings()) + ->setNormalizeFields(false) + ->setNormalizeInterfaces(false); return [ 'description + directives' => [ diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php index c7ed3854..ebb22386 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php @@ -97,7 +97,9 @@ public function testToStringEvent(): void { * @return array */ public function dataProviderToString(): array { - $settings = new TestSettings(); + $settings = (new TestSettings()) + ->setNormalizeFields(false) + ->setNormalizeInterfaces(false); return [ 'description + directives' => [ diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php index be146c1b..a2dd0366 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php @@ -42,7 +42,8 @@ public function testToString( * @return array */ public function dataProviderToString(): array { - $settings = new TestSettings(); + $settings = (new TestSettings()) + ->setIncludeDirectives(false); return [ 'scalar' => [ diff --git a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php index af6109a8..34b4c529 100644 --- a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php @@ -48,7 +48,9 @@ public function testToString( * @return array */ public function dataProviderToString(): array { - $settings = new TestSettings(); + $settings = (new TestSettings()) + ->setIncludeDirectives(false) + ->setNormalizeFields(false); return [ 'standard names' => [ diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php index e3a7210a..d38c48ac 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php @@ -91,7 +91,8 @@ public function testToStringEvent(): void { * @return array */ public function dataProviderToString(): array { - $settings = new TestSettings(); + $settings = (new TestSettings()) + ->setNormalizeUnions(false); return [ 'single-line' => [ diff --git a/src/SchemaPrinter/Settings/DefaultSettings.php b/src/SchemaPrinter/Settings/DefaultSettings.php index 7c46d3a9..1c93f146 100644 --- a/src/SchemaPrinter/Settings/DefaultSettings.php +++ b/src/SchemaPrinter/Settings/DefaultSettings.php @@ -3,82 +3,24 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\DirectiveFilter; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -class DefaultSettings implements Settings { - public function __construct() { - // empty - } - - public function getSpace(): string { - return ' '; - } - - public function getIndent(): string { - return ' '; - } - - public function getFileEnd(): string { - return "\n"; - } - - public function getLineEnd(): string { - return "\n"; - } - - public function getLineLength(): int { - return 80; - } - - public function isIncludeUnusedTypeDefinitions(): bool { - return false; - } - - public function isIncludeDirectives(): bool { - return false; - } - - public function isIncludeDirectivesInDescription(): bool { - return false; - } - - public function isIncludeUnusedDirectiveDefinitions(): bool { - return false; - } - - public function isNormalizeSchema(): bool { - return false; - } - - public function isNormalizeUnions(): bool { - return false; - } - - public function isNormalizeEnums(): bool { - return false; - } - - public function isNormalizeInterfaces(): bool { - return false; - } - - public function isNormalizeFields(): bool { - return false; - } - - public function isNormalizeArguments(): bool { - return false; - } - - public function isNormalizeDescription(): bool { - return false; - } - - public function isNormalizeDirectiveLocations(): bool { - return false; - } - - public function getDirectiveFilter(): ?DirectiveFilter { - return null; - } +class DefaultSettings extends ImmutableSettings { + protected string $space = ' '; + protected string $indent = ' '; + protected string $fileEnd = "\n"; + protected string $lineEnd = "\n"; + protected int $lineLength = 80; + protected bool $includeDirectives = false; + protected bool $includeDirectivesInDescription = false; + protected bool $includeUnusedTypeDefinitions = true; + protected bool $includeUnusedDirectiveDefinitions = true; + protected bool $normalizeSchema = true; + protected bool $normalizeUnions = false; + protected bool $normalizeEnums = false; + protected bool $normalizeInterfaces = false; + protected bool $normalizeFields = false; + protected bool $normalizeArguments = false; + protected bool $normalizeDescription = false; + protected bool $normalizeDirectiveLocations = false; + protected ?DirectiveFilter $directiveFilter = null; } diff --git a/src/SchemaPrinter/Settings/ImmutableSettings.php b/src/SchemaPrinter/Settings/ImmutableSettings.php new file mode 100644 index 00000000..350b65b9 --- /dev/null +++ b/src/SchemaPrinter/Settings/ImmutableSettings.php @@ -0,0 +1,220 @@ +space; + } + + public function setSpace(string $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->space = $value; + }); + } + + public function getIndent(): string { + return $this->indent; + } + + public function setIndent(string $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->indent = $value; + }); + } + + public function getFileEnd(): string { + return $this->fileEnd; + } + + public function setFileEnd(string $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->fileEnd = $value; + }); + } + + public function getLineEnd(): string { + return $this->lineEnd; + } + + public function setLineEnd(string $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->lineEnd = $value; + }); + } + + public function getLineLength(): int { + return $this->lineLength; + } + + public function setLineLength(int $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->lineLength = $value; + }); + } + + public function isIncludeUnusedTypeDefinitions(): bool { + return $this->includeUnusedTypeDefinitions; + } + + public function setIncludeUnusedTypeDefinitions(bool $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->includeUnusedTypeDefinitions = $value; + }); + } + + public function isIncludeDirectives(): bool { + return $this->includeDirectives; + } + + public function setIncludeDirectives(bool $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->includeDirectives = $value; + }); + } + + public function isIncludeDirectivesInDescription(): bool { + return $this->includeDirectivesInDescription; + } + + public function setIncludeDirectivesInDescription(bool $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->includeDirectivesInDescription = $value; + }); + } + + public function isIncludeUnusedDirectiveDefinitions(): bool { + return $this->includeUnusedDirectiveDefinitions; + } + + public function setIncludeUnusedDirectiveDefinitions(bool $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->includeUnusedDirectiveDefinitions = $value; + }); + } + + public function isNormalizeSchema(): bool { + return $this->normalizeSchema; + } + + public function setNormalizeSchema(bool $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->normalizeSchema = $value; + }); + } + + public function isNormalizeUnions(): bool { + return $this->normalizeUnions; + } + + public function setNormalizeUnions(bool $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->normalizeUnions = $value; + }); + } + + public function isNormalizeEnums(): bool { + return $this->normalizeEnums; + } + + public function setNormalizeEnums(bool $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->normalizeEnums = $value; + }); + } + + public function isNormalizeInterfaces(): bool { + return $this->normalizeInterfaces; + } + + public function setNormalizeInterfaces(bool $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->normalizeInterfaces = $value; + }); + } + + public function isNormalizeFields(): bool { + return $this->normalizeFields; + } + + public function setNormalizeFields(bool $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->normalizeFields = $value; + }); + } + + public function isNormalizeArguments(): bool { + return $this->normalizeArguments; + } + + public function setNormalizeArguments(bool $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->normalizeArguments = $value; + }); + } + + public function isNormalizeDescription(): bool { + return $this->normalizeDescription; + } + + public function setNormalizeDescription(bool $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->normalizeDescription = $value; + }); + } + + public function isNormalizeDirectiveLocations(): bool { + return $this->normalizeDirectiveLocations; + } + + public function setNormalizeDirectiveLocations(bool $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->normalizeDirectiveLocations = $value; + }); + } + + public function getDirectiveFilter(): ?DirectiveFilter { + return $this->directiveFilter; + } + + public function setDirectiveFilter(?DirectiveFilter $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->directiveFilter = $value; + }); + } + + protected function set(Closure $callback): static { + $settings = clone $this; + + $callback($settings); + + return $settings; + } +} diff --git a/src/Testing/Package/SchemaPrinter/TestSettings.php b/src/Testing/Package/SchemaPrinter/TestSettings.php index 1ad37d30..bcf4cfc4 100644 --- a/src/Testing/Package/SchemaPrinter/TestSettings.php +++ b/src/Testing/Package/SchemaPrinter/TestSettings.php @@ -5,172 +5,27 @@ use Closure; use GraphQL\Language\AST\DirectiveNode; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\DirectiveFilter; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings as SettingsContract; - -class TestSettings implements SettingsContract { - /** - * @var array - */ - private array $settings = [ - 'Space' => ' ', - 'Indent' => ' ', - 'LineEnd' => "\n", - 'LineLength' => 80, - 'NormalizeEnums' => false, - 'NormalizeUnions' => false, - 'NormalizeFields' => false, - 'NormalizeArguments' => false, - 'NormalizeInterfaces' => false, - 'NormalizeDescription' => false, - 'NormalizeDirectiveLocations' => false, - 'IncludeDirectives' => false, - 'IncludeDirectivesInDescription' => false, - 'DirectiveFilter' => null, - ]; - - public function __construct() { - // empty - } - - public function getSpace(): string { - return $this->get('Space'); - } - - public function setSpace(string $value): static { - return $this->set('Space', $value); - } - - public function getIndent(): string { - return $this->get('Indent'); - } - - public function setIndent(string $value): static { - return $this->set('Indent', $value); - } - - public function getFileEnd(): string { - return $this->get('FileEnd'); - } - - public function setFileEnd(string $value): static { - return $this->set('FileEnd', $value); - } - - public function getLineEnd(): string { - return $this->get('LineEnd'); - } - - public function setLineEnd(string $value): static { - return $this->set('LineEnd', $value); - } - - public function getLineLength(): int { - return $this->get('LineLength'); - } - - public function setLineLength(int $value): static { - return $this->set('LineLength', $value); - } - - public function isIncludeUnusedTypeDefinitions(): bool { - return $this->get('IncludeUnusedTypeDefinitions'); - } - - public function setIncludeUnusedTypeDefinitions(bool $value): static { - return $this->set('IncludeUnusedTypeDefinitions', $value); - } - - public function isIncludeDirectives(): bool { - return $this->get('IncludeDirectives'); - } - - public function setIncludeDirectives(bool $value): static { - return $this->set('IncludeDirectives', $value); - } - - public function isIncludeDirectivesInDescription(): bool { - return $this->get('IncludeDirectivesInDescription'); - } - - public function setIncludeDirectivesInDescription(bool $value): static { - return $this->set('IncludeDirectivesInDescription', $value); - } - - public function isIncludeUnusedDirectiveDefinitions(): bool { - return $this->get('IncludeUnusedDirectiveDefinitions'); - } - - public function setIncludeUnusedDirectiveDefinitions(bool $value): static { - return $this->set('IncludeUnusedDirectiveDefinitions', $value); - } - - public function isNormalizeSchema(): bool { - return $this->get('NormalizeSchema'); - } - - public function setNormalizeSchema(bool $value): static { - return $this->set('NormalizeSchema', $value); - } - - public function isNormalizeUnions(): bool { - return $this->get('NormalizeUnions'); - } - - public function setNormalizeUnions(bool $value): static { - return $this->set('NormalizeUnions', $value); - } - - public function isNormalizeEnums(): bool { - return $this->get('NormalizeEnums'); - } - - public function setNormalizeEnums(bool $value): static { - return $this->set('NormalizeEnums', $value); - } - - public function isNormalizeInterfaces(): bool { - return $this->get('NormalizeInterfaces'); - } - - public function setNormalizeInterfaces(bool $value): static { - return $this->set('NormalizeInterfaces', $value); - } - - public function isNormalizeFields(): bool { - return $this->get('NormalizeFields'); - } - - public function setNormalizeFields(bool $value): static { - return $this->set('NormalizeFields', $value); - } - - public function isNormalizeArguments(): bool { - return $this->get('NormalizeArguments'); - } - - public function setNormalizeArguments(bool $value): static { - return $this->set('NormalizeArguments', $value); - } - - public function isNormalizeDescription(): bool { - return $this->get('NormalizeDescription'); - } - - public function setNormalizeDescription(bool $value): static { - return $this->set('NormalizeDescription', $value); - } - - public function isNormalizeDirectiveLocations(): bool { - return $this->get('NormalizeDirectiveLocations'); - } - - public function setNormalizeDirectiveLocations(bool $value): static { - return $this->set('NormalizeDirectiveLocations', $value); - } - - public function getDirectiveFilter(): ?DirectiveFilter { - return $this->get('DirectiveFilter'); - } +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\ImmutableSettings; + +class TestSettings extends ImmutableSettings { + protected string $space = ' '; + protected string $indent = ' '; + protected string $fileEnd = "\n"; + protected string $lineEnd = "\n"; + protected int $lineLength = 80; + protected bool $includeDirectives = true; + protected bool $includeDirectivesInDescription = false; + protected bool $includeUnusedTypeDefinitions = false; + protected bool $includeUnusedDirectiveDefinitions = false; + protected bool $normalizeSchema = true; + protected bool $normalizeUnions = true; + protected bool $normalizeEnums = true; + protected bool $normalizeInterfaces = true; + protected bool $normalizeFields = true; + protected bool $normalizeArguments = true; + protected bool $normalizeDescription = true; + protected bool $normalizeDirectiveLocations = true; + protected ?DirectiveFilter $directiveFilter = null; /** * @param DirectiveFilter|Closure(DirectiveNode):bool|null $value @@ -190,17 +45,6 @@ public function isAllowedDirective(DirectiveNode $directive): bool { }; } - return $this->set('DirectiveFilter', $value); - } - - protected function get(string $setting): mixed { - return $this->settings[$setting]; - } - - protected function set(string $setting, mixed $value): static { - $settings = clone $this; - $settings->settings[$setting] = $value; - - return $settings; + return parent::setDirectiveFilter($value); } } From 14b3feab7cca1ef8b29881d0604af129aaaadc8a Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 22 Jan 2022 15:58:24 +0400 Subject: [PATCH 53/90] Settings rename: "include" => "print" --- .../Blocks/Ast/DirectiveNodeListTest.php | 2 +- .../Blocks/Types/DefinitionBlock.php | 2 +- .../Blocks/Types/Description.php | 2 +- .../Blocks/Types/DescriptionTest.php | 6 +-- .../Types/EnumTypeDefinitionBlockTest.php | 2 +- .../Types/EnumValueDefinitionBlockTest.php | 2 +- .../Blocks/Types/FieldDefinitionBlockTest.php | 2 +- .../InputObjectTypeDefinitionBlockTest.php | 4 +- .../Types/InputValueDefinitionBlockTest.php | 2 +- .../InterfaceTypeDefinitionBlockTest.php | 8 ++-- .../Types/ObjectTypeDefinitionBlockTest.php | 8 ++-- .../Types/ScalarTypeDefinitionBlockTest.php | 10 ++--- .../Types/SchemaDefinitionBlockTest.php | 6 +-- src/SchemaPrinter/Printer.php | 6 +-- src/SchemaPrinter/Settings.php | 8 ++-- .../Settings/DefaultSettings.php | 12 +++--- .../Settings/ImmutableSettings.php | 40 +++++++++---------- .../Package/SchemaPrinter/TestSettings.php | 12 +++--- 18 files changed, 67 insertions(+), 67 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php index a056ed5e..24498784 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php @@ -49,7 +49,7 @@ public function testToStringEvent(): void { $a = Parser::directive('@a'); $b = Parser::directive('@b'); $spy = Mockery::spy(static fn (Event $event) => null); - $settings = (new TestSettings())->setIncludeDirectives(true); + $settings = (new TestSettings())->setPrintDirectives(true); $dispatcher = new Dispatcher(); $dispatcher->attach(Closure::fromCallable($spy)); diff --git a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php index fba1f5ac..0c995566 100644 --- a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php @@ -73,7 +73,7 @@ protected function content(): string { $fields = (string) $this->fields($used + mb_strlen($body)); $directives = $this->directives(); $description = (string) $this->description($directives); - $directives = $this->getSettings()->isIncludeDirectives() + $directives = $this->getSettings()->isPrintDirectives() ? (string) $directives : ''; $content = ''; diff --git a/src/SchemaPrinter/Blocks/Types/Description.php b/src/SchemaPrinter/Blocks/Types/Description.php index 58542cb7..2d0d9e25 100644 --- a/src/SchemaPrinter/Blocks/Types/Description.php +++ b/src/SchemaPrinter/Blocks/Types/Description.php @@ -51,7 +51,7 @@ protected function getString(): string { } // Directives - if ($this->getSettings()->isIncludeDirectivesInDescription()) { + if ($this->getSettings()->isPrintDirectivesInDescription()) { $directives = (string) $this->getDirectives(); if ($directives) { diff --git a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php index 1b2a1900..6b1081b3 100644 --- a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php +++ b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php @@ -351,7 +351,7 @@ public function dataProviderToString(): array { @b(test: "abc") """ STRING, - $settings->setIncludeDirectivesInDescription(true), + $settings->setPrintDirectivesInDescription(true), 0, 0, <<<'STRING' @@ -375,7 +375,7 @@ public function dataProviderToString(): array { STRING, $settings ->setNormalizeDescription(true) - ->setIncludeDirectivesInDescription(true), + ->setPrintDirectivesInDescription(true), 0, 0, <<<'STRING' @@ -397,7 +397,7 @@ public function dataProviderToString(): array { STRING, $settings ->setNormalizeDescription(true) - ->setIncludeDirectivesInDescription(true), + ->setPrintDirectivesInDescription(true), 0, 0, '', diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php index 53d1491f..de8a0535 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php @@ -117,7 +117,7 @@ enum Test { @deprecated } STRING, - $settings->setIncludeDirectives(true), + $settings->setPrintDirectives(true), 0, 0, static function (): EnumType { diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php index 7f39f446..24b93b96 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php @@ -76,7 +76,7 @@ public function dataProviderToString(): array { A @a STRING, - $settings->setIncludeDirectives(true), + $settings->setPrintDirectives(true), 0, 0, new EnumValueDefinition([ diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php index 9831df2b..6faea464 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php @@ -95,7 +95,7 @@ public function dataProviderToString(): array { test: Test! @a STRING, - $settings->setIncludeDirectives(true), + $settings->setPrintDirectives(true), 0, 0, FieldDefinition::create([ diff --git a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php index 8d1ce5e3..46c8964b 100644 --- a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php @@ -97,7 +97,7 @@ public function dataProviderToString(): array { input Test @a STRING, - $settings->setIncludeDirectives(true), + $settings->setPrintDirectives(true), 0, 0, new InputObjectType([ @@ -124,7 +124,7 @@ public function dataProviderToString(): array { a: A } STRING, - $settings->setIncludeDirectives(true), + $settings->setPrintDirectives(true), 0, 0, new InputObjectType([ diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php index 630aea85..121ac7a7 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php @@ -94,7 +94,7 @@ public function dataProviderToString(): array { test: Test! @a STRING, - $settings->setIncludeDirectives(true), + $settings->setPrintDirectives(true), 0, 0, new FieldArgument([ diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php index 1988104f..6d544850 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php @@ -111,7 +111,7 @@ public function dataProviderToString(): array { interface Test @a STRING, - $settings->setIncludeDirectives(true), + $settings->setPrintDirectives(true), 0, 0, new InterfaceType([ @@ -138,7 +138,7 @@ interface Test a(a: Int): A } STRING, - $settings->setIncludeDirectives(true), + $settings->setPrintDirectives(true), 0, 0, new InterfaceType([ @@ -205,7 +205,7 @@ interface Test implements B & A a: String } STRING, - $settings->setIncludeDirectives(true), + $settings->setPrintDirectives(true), 0, 0, new InterfaceType([ @@ -233,7 +233,7 @@ interface Test implements a: String } STRING, - $settings->setIncludeDirectives(true), + $settings->setPrintDirectives(true), 0, 120, new InterfaceType([ diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php index ebb22386..11a5d6f6 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php @@ -110,7 +110,7 @@ public function dataProviderToString(): array { type Test @a STRING, - $settings->setIncludeDirectives(true), + $settings->setPrintDirectives(true), 0, 0, new ObjectType([ @@ -137,7 +137,7 @@ public function dataProviderToString(): array { a(a: Int): A } STRING, - $settings->setIncludeDirectives(true), + $settings->setPrintDirectives(true), 0, 0, new ObjectType([ @@ -204,7 +204,7 @@ public function dataProviderToString(): array { a: String } STRING, - $settings->setIncludeDirectives(true), + $settings->setPrintDirectives(true), 0, 0, new ObjectType([ @@ -232,7 +232,7 @@ public function dataProviderToString(): array { a: String } STRING, - $settings->setIncludeDirectives(true), + $settings->setPrintDirectives(true), 0, 120, new ObjectType([ diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php index a2dd0366..3f7cf80a 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php @@ -43,7 +43,7 @@ public function testToString( */ public function dataProviderToString(): array { $settings = (new TestSettings()) - ->setIncludeDirectives(false); + ->setPrintDirectives(false); return [ 'scalar' => [ @@ -65,7 +65,7 @@ public function dataProviderToString(): array { scalar Test @a STRING, - $settings->setIncludeDirectives(true), + $settings->setPrintDirectives(true), 0, 0, new CustomScalarType([ @@ -87,7 +87,7 @@ public function dataProviderToString(): array { """ scalar Test STRING, - $settings->setIncludeDirectivesInDescription(true), + $settings->setPrintDirectivesInDescription(true), 0, 0, new CustomScalarType([ @@ -111,7 +111,7 @@ public function dataProviderToString(): array { ) @b(value: "b") STRING, - $settings->setIncludeDirectives(true), + $settings->setPrintDirectives(true), 1, 60, new CustomScalarType([ @@ -132,7 +132,7 @@ public function dataProviderToString(): array { ) @b(value: "b") STRING, - $settings->setIncludeDirectives(true), + $settings->setPrintDirectives(true), 1, 60, new CustomScalarType([ diff --git a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php index 34b4c529..de467330 100644 --- a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php @@ -49,7 +49,7 @@ public function testToString( */ public function dataProviderToString(): array { $settings = (new TestSettings()) - ->setIncludeDirectives(false) + ->setPrintDirectives(false) ->setNormalizeFields(false); return [ @@ -74,7 +74,7 @@ public function dataProviderToString(): array { subscription: Subscription } STRING, - $settings->setIncludeDirectives(true), + $settings->setPrintDirectives(true), 0, 0, new Schema([ @@ -99,7 +99,7 @@ public function dataProviderToString(): array { subscription: Subscription } STRING, - $settings->setIncludeDirectivesInDescription(true), + $settings->setPrintDirectivesInDescription(true), 0, 0, new Schema([ diff --git a/src/SchemaPrinter/Printer.php b/src/SchemaPrinter/Printer.php index 34fecc28..9ad7fd0e 100644 --- a/src/SchemaPrinter/Printer.php +++ b/src/SchemaPrinter/Printer.php @@ -58,12 +58,12 @@ public function print(Schema $schema): PrintedSchema { $content[] = $this->getDefinitionList( $typesBlocks, $usedTypes, - $settings->isIncludeUnusedTypeDefinitions(), + $settings->isPrintUnusedTypeDefinitions(), ); $content[] = $this->getDefinitionList( $directivesBlocks, $usedDirectives, - $settings->isIncludeUnusedDirectiveDefinitions(), + $settings->isPrintUnusedDirectiveDefinitions(), ); // todo(graphql): directives in description @@ -116,7 +116,7 @@ protected function getSchemaDirectives(Schema $schema, array &$usedTypes = [], a // Included? $blocks = []; $settings = $this->getSettings(); - $included = $settings->isIncludeDirectives() || $settings->isIncludeDirectivesInDescription(); + $included = $settings->isPrintDirectives() || $settings->isPrintDirectivesInDescription(); if (!$included) { return $blocks; diff --git a/src/SchemaPrinter/Settings.php b/src/SchemaPrinter/Settings.php index 9efdb29a..b442d542 100644 --- a/src/SchemaPrinter/Settings.php +++ b/src/SchemaPrinter/Settings.php @@ -15,9 +15,9 @@ public function getLineEnd(): string; public function getLineLength(): int; - public function isIncludeUnusedTypeDefinitions(): bool; + public function isPrintUnusedTypeDefinitions(): bool; - public function isIncludeDirectives(): bool; + public function isPrintDirectives(): bool; /** * Temporary workaround to show directives when they are not supported out @@ -25,9 +25,9 @@ public function isIncludeDirectives(): bool; * * @see https://github.com/graphql/graphql-playground/issues/1207 */ - public function isIncludeDirectivesInDescription(): bool; + public function isPrintDirectivesInDescription(): bool; - public function isIncludeUnusedDirectiveDefinitions(): bool; + public function isPrintUnusedDirectiveDefinitions(): bool; /** * If `false` types and directives in the schema will be printed in the diff --git a/src/SchemaPrinter/Settings/DefaultSettings.php b/src/SchemaPrinter/Settings/DefaultSettings.php index 1c93f146..17314b2c 100644 --- a/src/SchemaPrinter/Settings/DefaultSettings.php +++ b/src/SchemaPrinter/Settings/DefaultSettings.php @@ -9,12 +9,12 @@ class DefaultSettings extends ImmutableSettings { protected string $indent = ' '; protected string $fileEnd = "\n"; protected string $lineEnd = "\n"; - protected int $lineLength = 80; - protected bool $includeDirectives = false; - protected bool $includeDirectivesInDescription = false; - protected bool $includeUnusedTypeDefinitions = true; - protected bool $includeUnusedDirectiveDefinitions = true; - protected bool $normalizeSchema = true; + protected int $lineLength = 80; + protected bool $printDirectives = false; + protected bool $printDirectivesInDescription = false; + protected bool $printUnusedTypeDefinitions = true; + protected bool $printUnusedDirectiveDefinitions = true; + protected bool $normalizeSchema = true; protected bool $normalizeUnions = false; protected bool $normalizeEnums = false; protected bool $normalizeInterfaces = false; diff --git a/src/SchemaPrinter/Settings/ImmutableSettings.php b/src/SchemaPrinter/Settings/ImmutableSettings.php index 350b65b9..4edd2a26 100644 --- a/src/SchemaPrinter/Settings/ImmutableSettings.php +++ b/src/SchemaPrinter/Settings/ImmutableSettings.php @@ -12,10 +12,10 @@ abstract class ImmutableSettings implements Settings { protected string $fileEnd; protected string $lineEnd; protected int $lineLength; - protected bool $includeUnusedTypeDefinitions; - protected bool $includeDirectives; - protected bool $includeDirectivesInDescription; - protected bool $includeUnusedDirectiveDefinitions; + protected bool $printDirectives; + protected bool $printDirectivesInDescription; + protected bool $printUnusedTypeDefinitions; + protected bool $printUnusedDirectiveDefinitions; protected bool $normalizeSchema; protected bool $normalizeUnions; protected bool $normalizeEnums; @@ -80,43 +80,43 @@ public function setLineLength(int $value): static { }); } - public function isIncludeUnusedTypeDefinitions(): bool { - return $this->includeUnusedTypeDefinitions; + public function isPrintUnusedTypeDefinitions(): bool { + return $this->printUnusedTypeDefinitions; } - public function setIncludeUnusedTypeDefinitions(bool $value): static { + public function setPrintUnusedTypeDefinitions(bool $value): static { return $this->set(static function (self $settings) use ($value): void { - $settings->includeUnusedTypeDefinitions = $value; + $settings->printUnusedTypeDefinitions = $value; }); } - public function isIncludeDirectives(): bool { - return $this->includeDirectives; + public function isPrintDirectives(): bool { + return $this->printDirectives; } - public function setIncludeDirectives(bool $value): static { + public function setPrintDirectives(bool $value): static { return $this->set(static function (self $settings) use ($value): void { - $settings->includeDirectives = $value; + $settings->printDirectives = $value; }); } - public function isIncludeDirectivesInDescription(): bool { - return $this->includeDirectivesInDescription; + public function isPrintDirectivesInDescription(): bool { + return $this->printDirectivesInDescription; } - public function setIncludeDirectivesInDescription(bool $value): static { + public function setPrintDirectivesInDescription(bool $value): static { return $this->set(static function (self $settings) use ($value): void { - $settings->includeDirectivesInDescription = $value; + $settings->printDirectivesInDescription = $value; }); } - public function isIncludeUnusedDirectiveDefinitions(): bool { - return $this->includeUnusedDirectiveDefinitions; + public function isPrintUnusedDirectiveDefinitions(): bool { + return $this->printUnusedDirectiveDefinitions; } - public function setIncludeUnusedDirectiveDefinitions(bool $value): static { + public function setPrintUnusedDirectiveDefinitions(bool $value): static { return $this->set(static function (self $settings) use ($value): void { - $settings->includeUnusedDirectiveDefinitions = $value; + $settings->printUnusedDirectiveDefinitions = $value; }); } diff --git a/src/Testing/Package/SchemaPrinter/TestSettings.php b/src/Testing/Package/SchemaPrinter/TestSettings.php index bcf4cfc4..9fe90c2f 100644 --- a/src/Testing/Package/SchemaPrinter/TestSettings.php +++ b/src/Testing/Package/SchemaPrinter/TestSettings.php @@ -12,12 +12,12 @@ class TestSettings extends ImmutableSettings { protected string $indent = ' '; protected string $fileEnd = "\n"; protected string $lineEnd = "\n"; - protected int $lineLength = 80; - protected bool $includeDirectives = true; - protected bool $includeDirectivesInDescription = false; - protected bool $includeUnusedTypeDefinitions = false; - protected bool $includeUnusedDirectiveDefinitions = false; - protected bool $normalizeSchema = true; + protected int $lineLength = 80; + protected bool $printDirectives = true; + protected bool $printDirectivesInDescription = false; + protected bool $printUnusedTypeDefinitions = false; + protected bool $printUnusedDirectiveDefinitions = false; + protected bool $normalizeSchema = true; protected bool $normalizeUnions = true; protected bool $normalizeEnums = true; protected bool $normalizeInterfaces = true; From 6dab912e1b4325da334bd122d5bec2201addba45 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 22 Jan 2022 16:15:04 +0400 Subject: [PATCH 54/90] New setting: `isAlwaysMultilineUnions()`. --- .../Blocks/Types/UnionMemberTypesList.php | 4 ++ .../Types/UnionTypeDefinitionBlockTest.php | 28 +++++++++++++- src/SchemaPrinter/Settings.php | 5 +++ .../Settings/DefaultSettings.php | 37 ++++++++++--------- .../Settings/ImmutableSettings.php | 11 ++++++ .../Package/SchemaPrinter/TestSettings.php | 37 ++++++++++--------- 6 files changed, 85 insertions(+), 37 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php b/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php index 73af7a47..b780ed86 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php +++ b/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php @@ -31,4 +31,8 @@ protected function prefix(): string { protected function isNormalized(): bool { return $this->getSettings()->isNormalizeUnions(); } + + protected function isAlwaysMultiline(): bool { + return $this->getSettings()->isAlwaysMultilineUnions(); + } } diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php index d38c48ac..9bb29c24 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php @@ -92,7 +92,8 @@ public function testToStringEvent(): void { */ public function dataProviderToString(): array { $settings = (new TestSettings()) - ->setNormalizeUnions(false); + ->setNormalizeUnions(false) + ->setAlwaysMultilineUnions(false); return [ 'single-line' => [ @@ -211,6 +212,31 @@ public function dataProviderToString(): array { ], ]), ], + 'multiline always' => [ + <<<'STRING' + union Test = + | C + | B + | A + STRING, + $settings->setAlwaysMultilineUnions(true), + 0, + 0, + new UnionType([ + 'name' => 'Test', + 'types' => [ + new ObjectType([ + 'name' => 'C', + ]), + new ObjectType([ + 'name' => 'B', + ]), + new ObjectType([ + 'name' => 'A', + ]), + ], + ]), + ], ]; } // diff --git a/src/SchemaPrinter/Settings.php b/src/SchemaPrinter/Settings.php index b442d542..eac05df9 100644 --- a/src/SchemaPrinter/Settings.php +++ b/src/SchemaPrinter/Settings.php @@ -73,6 +73,11 @@ public function isNormalizeDescription(): bool; */ public function isNormalizeDirectiveLocations(): bool; + /** + * If `true` members will always be printed multiline. + */ + public function isAlwaysMultilineUnions(): bool; + /** * Used to determine should the directive included in output or not. */ diff --git a/src/SchemaPrinter/Settings/DefaultSettings.php b/src/SchemaPrinter/Settings/DefaultSettings.php index 17314b2c..254a4a0a 100644 --- a/src/SchemaPrinter/Settings/DefaultSettings.php +++ b/src/SchemaPrinter/Settings/DefaultSettings.php @@ -5,22 +5,23 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\DirectiveFilter; class DefaultSettings extends ImmutableSettings { - protected string $space = ' '; - protected string $indent = ' '; - protected string $fileEnd = "\n"; - protected string $lineEnd = "\n"; - protected int $lineLength = 80; - protected bool $printDirectives = false; - protected bool $printDirectivesInDescription = false; - protected bool $printUnusedTypeDefinitions = true; - protected bool $printUnusedDirectiveDefinitions = true; - protected bool $normalizeSchema = true; - protected bool $normalizeUnions = false; - protected bool $normalizeEnums = false; - protected bool $normalizeInterfaces = false; - protected bool $normalizeFields = false; - protected bool $normalizeArguments = false; - protected bool $normalizeDescription = false; - protected bool $normalizeDirectiveLocations = false; - protected ?DirectiveFilter $directiveFilter = null; + protected string $space = ' '; + protected string $indent = ' '; + protected string $fileEnd = "\n"; + protected string $lineEnd = "\n"; + protected int $lineLength = 80; + protected bool $printDirectives = false; + protected bool $printDirectivesInDescription = false; + protected bool $printUnusedTypeDefinitions = true; + protected bool $printUnusedDirectiveDefinitions = true; + protected bool $normalizeSchema = true; + protected bool $normalizeUnions = false; + protected bool $normalizeEnums = false; + protected bool $normalizeInterfaces = false; + protected bool $normalizeFields = false; + protected bool $normalizeArguments = false; + protected bool $normalizeDescription = false; + protected bool $normalizeDirectiveLocations = false; + protected bool $alwaysMultilineUnions = false; + protected ?DirectiveFilter $directiveFilter = null; } diff --git a/src/SchemaPrinter/Settings/ImmutableSettings.php b/src/SchemaPrinter/Settings/ImmutableSettings.php index 4edd2a26..d7b031a6 100644 --- a/src/SchemaPrinter/Settings/ImmutableSettings.php +++ b/src/SchemaPrinter/Settings/ImmutableSettings.php @@ -24,6 +24,7 @@ abstract class ImmutableSettings implements Settings { protected bool $normalizeArguments; protected bool $normalizeDescription; protected bool $normalizeDirectiveLocations; + protected bool $alwaysMultilineUnions; protected ?DirectiveFilter $directiveFilter; public function __construct() { @@ -200,6 +201,16 @@ public function setNormalizeDirectiveLocations(bool $value): static { }); } + public function isAlwaysMultilineUnions(): bool { + return $this->alwaysMultilineUnions; + } + + public function setAlwaysMultilineUnions(bool $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->alwaysMultilineUnions = $value; + }); + } + public function getDirectiveFilter(): ?DirectiveFilter { return $this->directiveFilter; } diff --git a/src/Testing/Package/SchemaPrinter/TestSettings.php b/src/Testing/Package/SchemaPrinter/TestSettings.php index 9fe90c2f..50da11d5 100644 --- a/src/Testing/Package/SchemaPrinter/TestSettings.php +++ b/src/Testing/Package/SchemaPrinter/TestSettings.php @@ -8,24 +8,25 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\ImmutableSettings; class TestSettings extends ImmutableSettings { - protected string $space = ' '; - protected string $indent = ' '; - protected string $fileEnd = "\n"; - protected string $lineEnd = "\n"; - protected int $lineLength = 80; - protected bool $printDirectives = true; - protected bool $printDirectivesInDescription = false; - protected bool $printUnusedTypeDefinitions = false; - protected bool $printUnusedDirectiveDefinitions = false; - protected bool $normalizeSchema = true; - protected bool $normalizeUnions = true; - protected bool $normalizeEnums = true; - protected bool $normalizeInterfaces = true; - protected bool $normalizeFields = true; - protected bool $normalizeArguments = true; - protected bool $normalizeDescription = true; - protected bool $normalizeDirectiveLocations = true; - protected ?DirectiveFilter $directiveFilter = null; + protected string $space = ' '; + protected string $indent = ' '; + protected string $fileEnd = "\n"; + protected string $lineEnd = "\n"; + protected int $lineLength = 80; + protected bool $printDirectives = true; + protected bool $printDirectivesInDescription = false; + protected bool $printUnusedTypeDefinitions = false; + protected bool $printUnusedDirectiveDefinitions = false; + protected bool $normalizeSchema = true; + protected bool $normalizeUnions = true; + protected bool $normalizeEnums = true; + protected bool $normalizeInterfaces = true; + protected bool $normalizeFields = true; + protected bool $normalizeArguments = true; + protected bool $normalizeDescription = true; + protected bool $normalizeDirectiveLocations = true; + protected bool $alwaysMultilineUnions = true; + protected ?DirectiveFilter $directiveFilter = null; /** * @param DirectiveFilter|Closure(DirectiveNode):bool|null $value From 2e9cb10d7bffb342401523773e8fdf409071a465 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 22 Jan 2022 16:21:12 +0400 Subject: [PATCH 55/90] New setting: `isAlwaysMultilineDirectiveLocations()`. --- .../Types/DirectiveDefinitionBlockTest.php | 37 +++++++++++++----- .../Blocks/Types/DirectiveLocationsList.php | 10 ++--- src/SchemaPrinter/Settings.php | 5 +++ .../Settings/DefaultSettings.php | 39 ++++++++++--------- .../Settings/ImmutableSettings.php | 11 ++++++ .../Package/SchemaPrinter/TestSettings.php | 39 ++++++++++--------- 6 files changed, 88 insertions(+), 53 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php index 3959cff2..c31e7a00 100644 --- a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php @@ -85,10 +85,11 @@ public function testToStringEvent(): void { * @return array */ public function dataProviderToString(): array { - $settings = new TestSettings(); + $settings = (new TestSettings()) + ->setAlwaysMultilineDirectiveLocations(false); return [ - 'description' => [ + 'description' => [ <<<'STRING' """ Description @@ -107,7 +108,7 @@ public function dataProviderToString(): array { ], ]), ], - 'repeatable' => [ + 'repeatable' => [ <<<'STRING' directive @test repeatable on ARGUMENT_DEFINITION | ENUM STRING, @@ -123,7 +124,7 @@ public function dataProviderToString(): array { 'isRepeatable' => true, ]), ], - 'args' => [ + 'args' => [ <<<'STRING' directive @test(a: String) repeatable on ARGUMENT_DEFINITION | ENUM STRING, @@ -144,7 +145,7 @@ public function dataProviderToString(): array { 'isRepeatable' => true, ]), ], - 'multiline + repeatable' => [ + 'multiline + repeatable' => [ <<<'STRING' directive @test( a: String @@ -170,7 +171,7 @@ public function dataProviderToString(): array { 'isRepeatable' => true, ]), ], - 'multiline' => [ + 'multiline' => [ <<<'STRING' directive @test( a: String @@ -195,7 +196,7 @@ public function dataProviderToString(): array { ], ]), ], - 'multiline (no args)' => [ + 'multiline (no args)' => [ <<<'STRING' directive @test on | ARGUMENT_DEFINITION @@ -212,7 +213,7 @@ public function dataProviderToString(): array { ], ]), ], - 'indent' => [ + 'indent' => [ <<<'STRING' directive @test( a: String @@ -237,7 +238,7 @@ public function dataProviderToString(): array { ], ]), ], - 'normalized' => [ + 'normalized' => [ <<<'STRING' directive @test on ENUM | INPUT_FIELD_DEFINITION | OBJECT STRING, @@ -254,6 +255,24 @@ public function dataProviderToString(): array { ], ]), ], + 'locations always multiline' => [ + <<<'STRING' + directive @test on + | ARGUMENT_DEFINITION + | ENUM + STRING, + $settings + ->setAlwaysMultilineDirectiveLocations(true), + 0, + 0, + new Directive([ + 'name' => 'test', + 'locations' => [ + DirectiveLocation::ARGUMENT_DEFINITION, + DirectiveLocation::ENUM, + ], + ]), + ], ]; } // diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveLocationsList.php b/src/SchemaPrinter/Blocks/Types/DirectiveLocationsList.php index 0b7bc4c0..2133e009 100644 --- a/src/SchemaPrinter/Blocks/Types/DirectiveLocationsList.php +++ b/src/SchemaPrinter/Blocks/Types/DirectiveLocationsList.php @@ -2,13 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use Traversable; - -use function mb_strlen; /** * @internal @@ -36,4 +30,8 @@ protected function prefix(): string { protected function isNormalized(): bool { return $this->getSettings()->isNormalizeDirectiveLocations(); } + + protected function isAlwaysMultiline(): bool { + return $this->getSettings()->isAlwaysMultilineDirectiveLocations(); + } } diff --git a/src/SchemaPrinter/Settings.php b/src/SchemaPrinter/Settings.php index eac05df9..d093e1e5 100644 --- a/src/SchemaPrinter/Settings.php +++ b/src/SchemaPrinter/Settings.php @@ -78,6 +78,11 @@ public function isNormalizeDirectiveLocations(): bool; */ public function isAlwaysMultilineUnions(): bool; + /** + * If `true` directive locations will always be printed multiline. + */ + public function isAlwaysMultilineDirectiveLocations(): bool; + /** * Used to determine should the directive included in output or not. */ diff --git a/src/SchemaPrinter/Settings/DefaultSettings.php b/src/SchemaPrinter/Settings/DefaultSettings.php index 254a4a0a..1763b4e3 100644 --- a/src/SchemaPrinter/Settings/DefaultSettings.php +++ b/src/SchemaPrinter/Settings/DefaultSettings.php @@ -5,23 +5,24 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\DirectiveFilter; class DefaultSettings extends ImmutableSettings { - protected string $space = ' '; - protected string $indent = ' '; - protected string $fileEnd = "\n"; - protected string $lineEnd = "\n"; - protected int $lineLength = 80; - protected bool $printDirectives = false; - protected bool $printDirectivesInDescription = false; - protected bool $printUnusedTypeDefinitions = true; - protected bool $printUnusedDirectiveDefinitions = true; - protected bool $normalizeSchema = true; - protected bool $normalizeUnions = false; - protected bool $normalizeEnums = false; - protected bool $normalizeInterfaces = false; - protected bool $normalizeFields = false; - protected bool $normalizeArguments = false; - protected bool $normalizeDescription = false; - protected bool $normalizeDirectiveLocations = false; - protected bool $alwaysMultilineUnions = false; - protected ?DirectiveFilter $directiveFilter = null; + protected string $space = ' '; + protected string $indent = ' '; + protected string $fileEnd = "\n"; + protected string $lineEnd = "\n"; + protected int $lineLength = 80; + protected bool $printDirectives = false; + protected bool $printDirectivesInDescription = false; + protected bool $printUnusedTypeDefinitions = true; + protected bool $printUnusedDirectiveDefinitions = true; + protected bool $normalizeSchema = true; + protected bool $normalizeUnions = false; + protected bool $normalizeEnums = false; + protected bool $normalizeInterfaces = false; + protected bool $normalizeFields = false; + protected bool $normalizeArguments = false; + protected bool $normalizeDescription = false; + protected bool $normalizeDirectiveLocations = false; + protected bool $alwaysMultilineUnions = false; + protected bool $alwaysMultilineDirectiveLocations = false; + protected ?DirectiveFilter $directiveFilter = null; } diff --git a/src/SchemaPrinter/Settings/ImmutableSettings.php b/src/SchemaPrinter/Settings/ImmutableSettings.php index d7b031a6..66529380 100644 --- a/src/SchemaPrinter/Settings/ImmutableSettings.php +++ b/src/SchemaPrinter/Settings/ImmutableSettings.php @@ -25,6 +25,7 @@ abstract class ImmutableSettings implements Settings { protected bool $normalizeDescription; protected bool $normalizeDirectiveLocations; protected bool $alwaysMultilineUnions; + protected bool $alwaysMultilineDirectiveLocations; protected ?DirectiveFilter $directiveFilter; public function __construct() { @@ -211,6 +212,16 @@ public function setAlwaysMultilineUnions(bool $value): static { }); } + public function isAlwaysMultilineDirectiveLocations(): bool { + return $this->alwaysMultilineDirectiveLocations; + } + + public function setAlwaysMultilineDirectiveLocations(bool $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->alwaysMultilineDirectiveLocations = $value; + }); + } + public function getDirectiveFilter(): ?DirectiveFilter { return $this->directiveFilter; } diff --git a/src/Testing/Package/SchemaPrinter/TestSettings.php b/src/Testing/Package/SchemaPrinter/TestSettings.php index 50da11d5..f432a9e7 100644 --- a/src/Testing/Package/SchemaPrinter/TestSettings.php +++ b/src/Testing/Package/SchemaPrinter/TestSettings.php @@ -8,25 +8,26 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\ImmutableSettings; class TestSettings extends ImmutableSettings { - protected string $space = ' '; - protected string $indent = ' '; - protected string $fileEnd = "\n"; - protected string $lineEnd = "\n"; - protected int $lineLength = 80; - protected bool $printDirectives = true; - protected bool $printDirectivesInDescription = false; - protected bool $printUnusedTypeDefinitions = false; - protected bool $printUnusedDirectiveDefinitions = false; - protected bool $normalizeSchema = true; - protected bool $normalizeUnions = true; - protected bool $normalizeEnums = true; - protected bool $normalizeInterfaces = true; - protected bool $normalizeFields = true; - protected bool $normalizeArguments = true; - protected bool $normalizeDescription = true; - protected bool $normalizeDirectiveLocations = true; - protected bool $alwaysMultilineUnions = true; - protected ?DirectiveFilter $directiveFilter = null; + protected string $space = ' '; + protected string $indent = ' '; + protected string $fileEnd = "\n"; + protected string $lineEnd = "\n"; + protected int $lineLength = 80; + protected bool $printDirectives = true; + protected bool $printDirectivesInDescription = false; + protected bool $printUnusedTypeDefinitions = false; + protected bool $printUnusedDirectiveDefinitions = false; + protected bool $normalizeSchema = true; + protected bool $normalizeUnions = true; + protected bool $normalizeEnums = true; + protected bool $normalizeInterfaces = true; + protected bool $normalizeFields = true; + protected bool $normalizeArguments = true; + protected bool $normalizeDescription = true; + protected bool $normalizeDirectiveLocations = true; + protected bool $alwaysMultilineUnions = true; + protected bool $alwaysMultilineDirectiveLocations = true; + protected ?DirectiveFilter $directiveFilter = null; /** * @param DirectiveFilter|Closure(DirectiveNode):bool|null $value From 9321e61fdbc9d85d619da2924e67e9fa354b2e42 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 22 Jan 2022 16:30:56 +0400 Subject: [PATCH 56/90] New setting: `isAlwaysMultilineInterfaces()`. --- .../Blocks/Types/ImplementsInterfacesList.php | 4 +++ .../InterfaceTypeDefinitionBlockTest.php | 28 ++++++++++++++++- .../Types/ObjectTypeDefinitionBlockTest.php | 30 ++++++++++++++++++- src/SchemaPrinter/Blocks/Types/UsageList.php | 2 +- src/SchemaPrinter/Settings.php | 5 ++++ .../Settings/DefaultSettings.php | 1 + .../Settings/ImmutableSettings.php | 11 +++++++ .../Package/SchemaPrinter/TestSettings.php | 1 + 8 files changed, 79 insertions(+), 3 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Types/ImplementsInterfacesList.php b/src/SchemaPrinter/Blocks/Types/ImplementsInterfacesList.php index ba61b5bf..f58c40ea 100644 --- a/src/SchemaPrinter/Blocks/Types/ImplementsInterfacesList.php +++ b/src/SchemaPrinter/Blocks/Types/ImplementsInterfacesList.php @@ -31,4 +31,8 @@ protected function prefix(): string { protected function isNormalized(): bool { return $this->getSettings()->isNormalizeInterfaces(); } + + protected function isAlwaysMultiline(): bool { + return $this->getSettings()->isAlwaysMultilineInterfaces(); + } } diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php index 6d544850..aacc756f 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php @@ -100,7 +100,8 @@ public function testToStringEvent(): void { public function dataProviderToString(): array { $settings = (new TestSettings()) ->setNormalizeFields(false) - ->setNormalizeInterfaces(false); + ->setNormalizeInterfaces(false) + ->setAlwaysMultilineInterfaces(false); return [ 'description + directives' => [ @@ -352,6 +353,31 @@ interface Test implements ], ]), ], + 'implements always multiline' => [ + <<<'STRING' + interface Test implements + & A + { + a: String + } + STRING, + $settings + ->setAlwaysMultilineInterfaces(true), + 0, + 0, + new InterfaceType([ + 'name' => 'Test', + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::string(), + ], + ], + 'interfaces' => [ + new InterfaceType(['name' => 'A']), + ], + ]), + ], ]; } // diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php index 11a5d6f6..4c875087 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php @@ -99,7 +99,8 @@ public function testToStringEvent(): void { public function dataProviderToString(): array { $settings = (new TestSettings()) ->setNormalizeFields(false) - ->setNormalizeInterfaces(false); + ->setNormalizeInterfaces(false) + ->setAlwaysMultilineInterfaces(false); return [ 'description + directives' => [ @@ -351,6 +352,33 @@ public function dataProviderToString(): array { ], ]), ], + 'implements always multiline' => [ + <<<'STRING' + type Test implements + & B + & A + { + a: String + } + STRING, + $settings + ->setAlwaysMultilineInterfaces(true), + 0, + 0, + new ObjectType([ + 'name' => 'Test', + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::string(), + ], + ], + 'interfaces' => [ + new ObjectType(['name' => 'B']), + new ObjectType(['name' => 'A']), + ], + ]), + ], ]; } // diff --git a/src/SchemaPrinter/Blocks/Types/UsageList.php b/src/SchemaPrinter/Blocks/Types/UsageList.php index 69176ac4..3105f6d9 100644 --- a/src/SchemaPrinter/Blocks/Types/UsageList.php +++ b/src/SchemaPrinter/Blocks/Types/UsageList.php @@ -58,7 +58,7 @@ protected function content(): string { $content = parent::content(); if ($content) { - if ($this->isStringMultiline($content)) { + if ($this->isAlwaysMultiline() || $this->isStringMultiline($content)) { $eol = $this->eol(); $indent = $this->indent(); diff --git a/src/SchemaPrinter/Settings.php b/src/SchemaPrinter/Settings.php index d093e1e5..bfd0ea3d 100644 --- a/src/SchemaPrinter/Settings.php +++ b/src/SchemaPrinter/Settings.php @@ -78,6 +78,11 @@ public function isNormalizeDirectiveLocations(): bool; */ public function isAlwaysMultilineUnions(): bool; + /** + * If `true` implemented interfaces will always be printed multiline. + */ + public function isAlwaysMultilineInterfaces(): bool; + /** * If `true` directive locations will always be printed multiline. */ diff --git a/src/SchemaPrinter/Settings/DefaultSettings.php b/src/SchemaPrinter/Settings/DefaultSettings.php index 1763b4e3..92c8e9c4 100644 --- a/src/SchemaPrinter/Settings/DefaultSettings.php +++ b/src/SchemaPrinter/Settings/DefaultSettings.php @@ -23,6 +23,7 @@ class DefaultSettings extends ImmutableSettings { protected bool $normalizeDescription = false; protected bool $normalizeDirectiveLocations = false; protected bool $alwaysMultilineUnions = false; + protected bool $alwaysMultilineInterfaces = false; protected bool $alwaysMultilineDirectiveLocations = false; protected ?DirectiveFilter $directiveFilter = null; } diff --git a/src/SchemaPrinter/Settings/ImmutableSettings.php b/src/SchemaPrinter/Settings/ImmutableSettings.php index 66529380..16ea006e 100644 --- a/src/SchemaPrinter/Settings/ImmutableSettings.php +++ b/src/SchemaPrinter/Settings/ImmutableSettings.php @@ -25,6 +25,7 @@ abstract class ImmutableSettings implements Settings { protected bool $normalizeDescription; protected bool $normalizeDirectiveLocations; protected bool $alwaysMultilineUnions; + protected bool $alwaysMultilineInterfaces; protected bool $alwaysMultilineDirectiveLocations; protected ?DirectiveFilter $directiveFilter; @@ -212,6 +213,16 @@ public function setAlwaysMultilineUnions(bool $value): static { }); } + public function isAlwaysMultilineInterfaces(): bool { + return $this->alwaysMultilineInterfaces; + } + + public function setAlwaysMultilineInterfaces(bool $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->alwaysMultilineInterfaces = $value; + }); + } + public function isAlwaysMultilineDirectiveLocations(): bool { return $this->alwaysMultilineDirectiveLocations; } diff --git a/src/Testing/Package/SchemaPrinter/TestSettings.php b/src/Testing/Package/SchemaPrinter/TestSettings.php index f432a9e7..2f6288d2 100644 --- a/src/Testing/Package/SchemaPrinter/TestSettings.php +++ b/src/Testing/Package/SchemaPrinter/TestSettings.php @@ -26,6 +26,7 @@ class TestSettings extends ImmutableSettings { protected bool $normalizeDescription = true; protected bool $normalizeDirectiveLocations = true; protected bool $alwaysMultilineUnions = true; + protected bool $alwaysMultilineInterfaces = true; protected bool $alwaysMultilineDirectiveLocations = true; protected ?DirectiveFilter $directiveFilter = null; From 79213dc65ba9eb3e5b2f7fdfdef24bb7a368116d Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 22 Jan 2022 16:32:20 +0400 Subject: [PATCH 57/90] Code cleanup. --- .../Blocks/Ast/DirectiveNodeBlockTest.php | 2 +- .../Blocks/Ast/DirectiveNodeList.php | 8 +++----- .../Blocks/Types/FieldDefinitionBlockTest.php | 4 ++-- .../Types/InputObjectTypeDefinitionBlock.php | 1 + .../Types/InputObjectTypeDefinitionBlockTest.php | 6 +++++- .../Blocks/Types/InputValueDefinitionBlock.php | 6 +++--- .../Types/InputValueDefinitionBlockTest.php | 16 ++++++++-------- .../Blocks/Types/SchemaDefinitionBlock.php | 2 ++ .../Blocks/Types/SchemaDefinitionBlockTest.php | 1 - src/SchemaPrinter/Blocks/Types/TypeBlockTest.php | 2 +- .../Types/UnionTypeDefinitionBlockTest.php | 2 +- src/SchemaPrinter/Exceptions/TypeUnsupported.php | 2 +- src/SchemaPrinter/Printer.php | 2 +- 13 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php index b64cf3f8..bc48990e 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php @@ -123,7 +123,7 @@ public function dataProviderToString(): array { 0, Parser::directive('@directive(b: "b", a: "a")'), ], - 'with arguments (indent)' => [ + 'with arguments (indent)' => [ <<<'STRING' @directive( b: "b" diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php index 48c51866..d7ebf74e 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php @@ -9,7 +9,6 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use Traversable; - use function array_filter; use function json_encode; @@ -31,7 +30,7 @@ public function __construct( ) { parent::__construct($dispatcher, $settings, $level, $used); - $deprecated = Directive::DEPRECATED_NAME; + $deprecated = Directive::DEPRECATED_NAME; $directives ??= []; if ($deprecationReason) { @@ -73,9 +72,8 @@ protected function getBlocks(): array { return $blocks; } - private function block(DirectiveNode $directive - ): \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeBlock { - return new \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeBlock( + private function block(DirectiveNode $directive,): DirectiveNodeBlock { + return new DirectiveNodeBlock( $this->getDispatcher(), $this->getSettings(), $this->getLevel(), diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php index 6faea464..a85031fb 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php @@ -130,7 +130,7 @@ public function dataProviderToString(): array { 'a' => [ 'type' => new ListOfType(new NonNull(Type::string())), 'defaultValue' => [ - "aaaaaaaaaaaaaaaaaaaaaaaaaa", + 'aaaaaaaaaaaaaaaaaaaaaaaaaa', ], ], 'b' => [ @@ -168,7 +168,7 @@ public function dataProviderToString(): array { 'a' => [ 'type' => new NonNull(Type::string()), 'description' => 'Description', - 'defaultValue' => "aaaaaaaaaaaaaaaaaaaaaaaaaa", + 'defaultValue' => 'aaaaaaaaaaaaaaaaaaaaaaaaaa', ], ], ]), diff --git a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php index b1ef5c9b..8c6e5904 100644 --- a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php @@ -6,6 +6,7 @@ use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use function mb_strlen; /** * @internal diff --git a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php index 46c8964b..831dfc44 100644 --- a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php @@ -33,7 +33,11 @@ public function testToString( InputObjectType $definition, ): void { $actual = (string) (new InputObjectTypeDefinitionBlock( - new Dispatcher(), $settings, $level, $used, $definition + new Dispatcher(), + $settings, + $level, + $used, + $definition, )); Parser::inputObjectTypeDefinition($actual); diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php index 21c7e7e6..d96f9ccb 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php @@ -46,15 +46,15 @@ protected function body(int $used): Block|string|null { $body = ":{$space}{$type}"; if ($definition->defaultValueExists()) { - $prefix = "{$body}{$space}={$space}"; - $value = new ValueNodeBlock( + $prefix = "{$body}{$space}={$space}"; + $value = new ValueNodeBlock( $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $this->getUsed() + mb_strlen($prefix), AST::astFromValue($definition->defaultValue, $definition->getType()) ?? new NullValueNode([]), ); - $body = "{$prefix}{$value}"; + $body = "{$prefix}{$value}"; } return $body; diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php index 121ac7a7..3681ebe0 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php @@ -83,10 +83,10 @@ public function testToStringEvent(): void { * @return array */ public function dataProviderToString(): array { - $settings = new TestSettings(); + $settings = new TestSettings(); return [ - 'without value' => [ + 'without value' => [ <<<'STRING' """ Description @@ -108,7 +108,7 @@ public function dataProviderToString(): array { 'description' => 'Description', ]), ], - 'with value (short)' => [ + 'with value (short)' => [ <<<'STRING' """ Description @@ -122,12 +122,12 @@ public function dataProviderToString(): array { 'name' => 'test', 'type' => new ListOfType(new NonNull(Type::string())), 'defaultValue' => [ - "aaaaaaaaaaaaaaaaaaaaaaaaaa", + 'aaaaaaaaaaaaaaaaaaaaaaaaaa', ], 'description' => 'Description', ]), ], - 'with value (long)' => [ + 'with value (long)' => [ <<<'STRING' """ Description @@ -143,12 +143,12 @@ public function dataProviderToString(): array { 'name' => 'test', 'type' => new ListOfType(new NonNull(Type::string())), 'defaultValue' => [ - "aaaaaaaaaaaaaaaaaaaaaaaaaa", + 'aaaaaaaaaaaaaaaaaaaaaaaaaa', ], 'description' => 'Description', ]), ], - 'indent' => [ + 'indent' => [ <<<'STRING' """ Description @@ -164,7 +164,7 @@ public function dataProviderToString(): array { 'name' => 'test', 'type' => new ListOfType(new NonNull(Type::string())), 'defaultValue' => [ - "aaaaaaaaaaaaaaaaaaaaaaaaaa", + 'aaaaaaaaaaaaaaaaaaaaaaaaaa', ], 'description' => 'Description', ]), diff --git a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php index 311e3b89..d822ea8a 100644 --- a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php @@ -10,6 +10,8 @@ use function array_filter; use function count; +use function mb_strlen; +use const ARRAY_FILTER_USE_BOTH; /** * @internal diff --git a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php index de467330..bc5376d9 100644 --- a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php @@ -4,7 +4,6 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\ObjectType; -use GraphQL\Type\Definition\ScalarType; use GraphQL\Type\Schema; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; diff --git a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php index bd6bbc96..0a0b738f 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php @@ -46,7 +46,7 @@ public function testToStringEvent(): void { $node = new NonNull( new ObjectType([ 'name' => 'Test', - ]) + ]), ); $settings = new TestSettings(); $dispatcher = new Dispatcher(); diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php index 9bb29c24..22c8017c 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php @@ -212,7 +212,7 @@ public function dataProviderToString(): array { ], ]), ], - 'multiline always' => [ + 'multiline always' => [ <<<'STRING' union Test = | C diff --git a/src/SchemaPrinter/Exceptions/TypeUnsupported.php b/src/SchemaPrinter/Exceptions/TypeUnsupported.php index 5bb79f17..67037642 100644 --- a/src/SchemaPrinter/Exceptions/TypeUnsupported.php +++ b/src/SchemaPrinter/Exceptions/TypeUnsupported.php @@ -17,7 +17,7 @@ public function __construct( 'Type `%s` is not (yet) supported.', $this->type, ), - $previous + $previous, ); } diff --git a/src/SchemaPrinter/Printer.php b/src/SchemaPrinter/Printer.php index 9ad7fd0e..5e90ae42 100644 --- a/src/SchemaPrinter/Printer.php +++ b/src/SchemaPrinter/Printer.php @@ -146,7 +146,7 @@ protected function getDefinitionBlock( array &$usedTypes = [], array &$usedDirectives = [], ): Block { - $block = new DefinitionBlock($this->getSettings(), $this->getLevel(), $definition); + $block = new DefinitionBlock($this->getSettings(), $this->getLevel(), $definition); $usedTypes += $block->getUsedTypes(); $usedDirectives += $block->getUsedDirectives(); From 2b2ca3c8e4805133656328b967579aaed5bd7495 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 22 Jan 2022 16:46:44 +0400 Subject: [PATCH 58/90] Added missed `@internal` for tests. --- src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php | 1 + src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php | 1 + src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php | 1 + src/SchemaPrinter/Blocks/BlockListTest.php | 1 + src/SchemaPrinter/Blocks/BlockTest.php | 1 + src/SchemaPrinter/Blocks/PropertyTest.php | 1 + src/SchemaPrinter/Blocks/Types/DescriptionTest.php | 1 + src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php | 1 + src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php | 1 + src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php | 1 + src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php | 1 + .../Blocks/Types/InputObjectTypeDefinitionBlockTest.php | 1 + src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php | 1 + .../Blocks/Types/InterfaceTypeDefinitionBlockTest.php | 1 + src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php | 1 + src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php | 1 + src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php | 1 + src/SchemaPrinter/Blocks/Types/StringBlockTest.php | 1 + src/SchemaPrinter/Blocks/Types/TypeBlockTest.php | 1 + src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php | 1 + 20 files changed, 20 insertions(+) diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php index bc48990e..fc9e3803 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php @@ -15,6 +15,7 @@ use PHPUnit\Framework\TestCase; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeBlock */ class DirectiveNodeBlockTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php index 24498784..b0983c44 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php @@ -15,6 +15,7 @@ use PHPUnit\Framework\TestCase; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList */ class DirectiveNodeListTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php index 10b14bb1..b54c5dfd 100644 --- a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php +++ b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php @@ -21,6 +21,7 @@ use PHPUnit\Framework\TestCase; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\ValueNodeBlock */ class ValueNodeTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/BlockListTest.php b/src/SchemaPrinter/Blocks/BlockListTest.php index acf9efb5..9b25a6c6 100644 --- a/src/SchemaPrinter/Blocks/BlockListTest.php +++ b/src/SchemaPrinter/Blocks/BlockListTest.php @@ -12,6 +12,7 @@ use function mb_strlen; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList */ class BlockListTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/BlockTest.php b/src/SchemaPrinter/Blocks/BlockTest.php index 3c0a5d9e..44c79f7a 100644 --- a/src/SchemaPrinter/Blocks/BlockTest.php +++ b/src/SchemaPrinter/Blocks/BlockTest.php @@ -11,6 +11,7 @@ use function mb_strlen; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block */ class BlockTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/PropertyTest.php b/src/SchemaPrinter/Blocks/PropertyTest.php index c99677db..0aadb4be 100644 --- a/src/SchemaPrinter/Blocks/PropertyTest.php +++ b/src/SchemaPrinter/Blocks/PropertyTest.php @@ -10,6 +10,7 @@ use function mb_strlen; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Property */ class PropertyTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php index 6b1081b3..87e26a5a 100644 --- a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php +++ b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php @@ -13,6 +13,7 @@ use function implode; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\Description */ class DescriptionTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php index c31e7a00..ad6f586c 100644 --- a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php @@ -17,6 +17,7 @@ use PHPUnit\Framework\TestCase; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\DirectiveDefinitionBlock */ class DirectiveDefinitionBlockTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php index de8a0535..3efd37d8 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\TestCase; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\EnumTypeDefinitionBlock */ class EnumTypeDefinitionBlockTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php index 24b93b96..bd154da1 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php @@ -10,6 +10,7 @@ use PHPUnit\Framework\TestCase; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\EnumValueDefinitionBlock */ class EnumValueDefinitionBlockTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php index a85031fb..d0eba394 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php @@ -18,6 +18,7 @@ use PHPUnit\Framework\TestCase; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\FieldDefinitionBlock */ class FieldDefinitionBlockTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php index 831dfc44..8ca3979f 100644 --- a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php @@ -15,6 +15,7 @@ use PHPUnit\Framework\TestCase; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\InputObjectTypeDefinitionBlock */ class InputObjectTypeDefinitionBlockTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php index 3681ebe0..a248d7d4 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php @@ -18,6 +18,7 @@ use PHPUnit\Framework\TestCase; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\InputValueDefinitionBlock */ class InputValueDefinitionBlockTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php index aacc756f..d57c10bb 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php @@ -16,6 +16,7 @@ use PHPUnit\Framework\TestCase; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\InterfaceTypeDefinitionBlock */ class InterfaceTypeDefinitionBlockTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php index 4c875087..33946810 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php @@ -15,6 +15,7 @@ use PHPUnit\Framework\TestCase; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\ObjectTypeDefinitionBlock */ class ObjectTypeDefinitionBlockTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php index 3f7cf80a..0d985e73 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php @@ -11,6 +11,7 @@ use PHPUnit\Framework\TestCase; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\ScalarTypeDefinitionBlock */ class ScalarTypeDefinitionBlockTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php index bc5376d9..ca0b8ac5 100644 --- a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php @@ -13,6 +13,7 @@ use function str_starts_with; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\SchemaDefinitionBlock */ class SchemaDefinitionBlockTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/Types/StringBlockTest.php b/src/SchemaPrinter/Blocks/Types/StringBlockTest.php index c0575cfa..2ae01daa 100644 --- a/src/SchemaPrinter/Blocks/Types/StringBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/StringBlockTest.php @@ -12,6 +12,7 @@ use function implode; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\StringBlock */ class StringBlockTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php index 0a0b738f..1275e296 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php @@ -16,6 +16,7 @@ use PHPUnit\Framework\TestCase; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\TypeBlock */ class TypeBlockTest extends TestCase { diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php index 22c8017c..3acd2150 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php @@ -15,6 +15,7 @@ use PHPUnit\Framework\TestCase; /** + * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\UnionTypeDefinitionBlock */ class UnionTypeDefinitionBlockTest extends TestCase { From b745f070e6b9464881b4c590cabc3a014c3ac40f Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 22 Jan 2022 16:51:37 +0400 Subject: [PATCH 59/90] Fix base `TestCase` class. --- src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php | 4 ++-- src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php | 2 +- src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php | 2 +- src/SchemaPrinter/Blocks/BlockListTest.php | 2 +- src/SchemaPrinter/Blocks/BlockTest.php | 2 +- src/SchemaPrinter/Blocks/PropertyTest.php | 2 +- .../Blocks/Types/DirectiveDefinitionBlockTest.php | 2 +- .../Blocks/Types/EnumTypeDefinitionBlockTest.php | 2 +- .../Blocks/Types/EnumValueDefinitionBlockTest.php | 2 +- src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php | 2 +- .../Blocks/Types/InputObjectTypeDefinitionBlockTest.php | 2 +- .../Blocks/Types/InputValueDefinitionBlockTest.php | 2 +- .../Blocks/Types/InterfaceTypeDefinitionBlockTest.php | 2 +- .../Blocks/Types/ObjectTypeDefinitionBlockTest.php | 2 +- .../Blocks/Types/ScalarTypeDefinitionBlockTest.php | 2 +- src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php | 2 +- src/SchemaPrinter/Blocks/Types/TypeBlockTest.php | 2 +- .../Blocks/Types/UnionTypeDefinitionBlockTest.php | 2 +- src/SearchBy/Ast/UsageTest.php | 2 +- 19 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php index fc9e3803..809ac2f8 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php @@ -11,8 +11,8 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use Mockery; -use PHPUnit\Framework\TestCase; /** * @internal @@ -50,7 +50,7 @@ public function testToString( * @covers ::__toString */ public function testToStringEvent(): void { - $spy = Mockery::spy(static fn(Event $event) => null); + $spy = Mockery::spy(static fn (Event $event) => null); $node = Parser::directive('@test'); $settings = new TestSettings(); $dispatcher = new Dispatcher(); diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php index b0983c44..b58cc9e4 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php @@ -11,8 +11,8 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use Mockery; -use PHPUnit\Framework\TestCase; /** * @internal diff --git a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php index b54c5dfd..81d40fdc 100644 --- a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php +++ b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php @@ -18,7 +18,7 @@ use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; -use PHPUnit\Framework\TestCase; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; /** * @internal diff --git a/src/SchemaPrinter/Blocks/BlockListTest.php b/src/SchemaPrinter/Blocks/BlockListTest.php index 9b25a6c6..8aa96802 100644 --- a/src/SchemaPrinter/Blocks/BlockListTest.php +++ b/src/SchemaPrinter/Blocks/BlockListTest.php @@ -5,9 +5,9 @@ use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\MergeDataProvider; -use PHPUnit\Framework\TestCase; use function mb_strlen; diff --git a/src/SchemaPrinter/Blocks/BlockTest.php b/src/SchemaPrinter/Blocks/BlockTest.php index 44c79f7a..64d43273 100644 --- a/src/SchemaPrinter/Blocks/BlockTest.php +++ b/src/SchemaPrinter/Blocks/BlockTest.php @@ -5,8 +5,8 @@ use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use Mockery; -use PHPUnit\Framework\TestCase; use function mb_strlen; diff --git a/src/SchemaPrinter/Blocks/PropertyTest.php b/src/SchemaPrinter/Blocks/PropertyTest.php index 0aadb4be..61a41dee 100644 --- a/src/SchemaPrinter/Blocks/PropertyTest.php +++ b/src/SchemaPrinter/Blocks/PropertyTest.php @@ -5,7 +5,7 @@ use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; -use PHPUnit\Framework\TestCase; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use function mb_strlen; diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php index ad6f586c..4d3da112 100644 --- a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php @@ -13,8 +13,8 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use Mockery; -use PHPUnit\Framework\TestCase; /** * @internal diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php index 3efd37d8..fd459d5a 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php @@ -10,7 +10,7 @@ use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; -use PHPUnit\Framework\TestCase; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; /** * @internal diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php index bd154da1..675f7ae8 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php @@ -7,7 +7,7 @@ use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; -use PHPUnit\Framework\TestCase; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; /** * @internal diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php index d0eba394..015d598b 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php @@ -14,8 +14,8 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use Mockery; -use PHPUnit\Framework\TestCase; /** * @internal diff --git a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php index 8ca3979f..14aadb26 100644 --- a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php @@ -11,8 +11,8 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use Mockery; -use PHPUnit\Framework\TestCase; /** * @internal diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php index a248d7d4..7591bdc7 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php @@ -14,8 +14,8 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use Mockery; -use PHPUnit\Framework\TestCase; /** * @internal diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php index d57c10bb..54d6e38c 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php @@ -12,8 +12,8 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use Mockery; -use PHPUnit\Framework\TestCase; /** * @internal diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php index 33946810..2305f2cd 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php @@ -11,8 +11,8 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use Mockery; -use PHPUnit\Framework\TestCase; /** * @internal diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php index 0d985e73..a04c43f4 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php @@ -8,7 +8,7 @@ use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; -use PHPUnit\Framework\TestCase; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; /** * @internal diff --git a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php index ca0b8ac5..80f8eeff 100644 --- a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php @@ -8,7 +8,7 @@ use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; -use PHPUnit\Framework\TestCase; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use function str_starts_with; diff --git a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php index 1275e296..19ba54c3 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php @@ -12,8 +12,8 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use Mockery; -use PHPUnit\Framework\TestCase; /** * @internal diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php index 3acd2150..2e523880 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php @@ -11,8 +11,8 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use Mockery; -use PHPUnit\Framework\TestCase; /** * @internal diff --git a/src/SearchBy/Ast/UsageTest.php b/src/SearchBy/Ast/UsageTest.php index 07ca6485..0c8a6f7c 100644 --- a/src/SearchBy/Ast/UsageTest.php +++ b/src/SearchBy/Ast/UsageTest.php @@ -4,7 +4,7 @@ use LogicException; use OutOfBoundsException; -use PHPUnit\Framework\TestCase; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use stdClass; use function sprintf; From 8a149231c76fed4629eae940cddbd7e55be355b8 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 23 Jan 2022 10:26:30 +0400 Subject: [PATCH 60/90] New setting `isPrintDirectiveDefinitions()` and error fixes. --- .../Blocks/Printer/DefinitionBlock.php | 3 ++- .../Blocks/Printer/DefinitionList.php | 3 +++ src/SchemaPrinter/Printer.php | 14 +++++++++++--- src/SchemaPrinter/Settings.php | 2 ++ src/SchemaPrinter/Settings/DefaultSettings.php | 1 + src/SchemaPrinter/Settings/ImmutableSettings.php | 11 +++++++++++ src/Testing/Package/SchemaPrinter/TestSettings.php | 1 + 7 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Printer/DefinitionBlock.php b/src/SchemaPrinter/Blocks/Printer/DefinitionBlock.php index db3e1710..8ff18676 100644 --- a/src/SchemaPrinter/Blocks/Printer/DefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Printer/DefinitionBlock.php @@ -49,7 +49,6 @@ public function __construct( int $level, Schema|Type|Directive $definition, ) { - $this->block = $this->getDefinitionBlock($definition); $dispatcher = new Dispatcher(); $dispatcher->attach(function (Event $event): void { if ($event instanceof DirectiveUsed) { @@ -62,6 +61,8 @@ public function __construct( }); parent::__construct($dispatcher, $settings, $level); + + $this->block = $this->getDefinitionBlock($definition); } public function getName(): string { diff --git a/src/SchemaPrinter/Blocks/Printer/DefinitionList.php b/src/SchemaPrinter/Blocks/Printer/DefinitionList.php index beb71101..9f155a14 100644 --- a/src/SchemaPrinter/Blocks/Printer/DefinitionList.php +++ b/src/SchemaPrinter/Blocks/Printer/DefinitionList.php @@ -6,6 +6,8 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use function rtrim; + /** * @internal */ @@ -39,6 +41,7 @@ protected function content(): string { if ($content && $this->isSchema()) { $eof = $this->getSettings()->getFileEnd(); + $content = rtrim($content); $content = "{$this->indent()}{$content}{$eof}"; } diff --git a/src/SchemaPrinter/Printer.php b/src/SchemaPrinter/Printer.php index 5e90ae42..e1933b66 100644 --- a/src/SchemaPrinter/Printer.php +++ b/src/SchemaPrinter/Printer.php @@ -95,7 +95,7 @@ protected function getSchemaTypes(Schema $schema, array &$usedTypes = [], array foreach ($schema->getTypeMap() as $type) { // Standard? - if (Type::isBuiltInType($type)) { + if (!$this->isSchemaType($type)) { continue; } @@ -106,6 +106,10 @@ protected function getSchemaTypes(Schema $schema, array &$usedTypes = [], array return $blocks; } + protected function isSchemaType(Type $type): bool { + return !Type::isBuiltInType($type); + } + /** * @param array $usedTypes * @param array $usedDirectives @@ -116,7 +120,7 @@ protected function getSchemaDirectives(Schema $schema, array &$usedTypes = [], a // Included? $blocks = []; $settings = $this->getSettings(); - $included = $settings->isPrintDirectives() || $settings->isPrintDirectivesInDescription(); + $included = $settings->isPrintDirectiveDefinitions(); if (!$included) { return $blocks; @@ -125,7 +129,7 @@ protected function getSchemaDirectives(Schema $schema, array &$usedTypes = [], a // Add foreach ($schema->getDirectives() as $directive) { // Standard? - if (Directive::isSpecifiedDirective($directive)) { + if (!$this->isSchemaDirective($directive)) { continue; } @@ -137,6 +141,10 @@ protected function getSchemaDirectives(Schema $schema, array &$usedTypes = [], a return $blocks; } + protected function isSchemaDirective(Directive $directive): bool { + return !Directive::isSpecifiedDirective($directive); + } + /** * @param array $usedTypes * @param array $usedDirectives diff --git a/src/SchemaPrinter/Settings.php b/src/SchemaPrinter/Settings.php index bfd0ea3d..e046ef08 100644 --- a/src/SchemaPrinter/Settings.php +++ b/src/SchemaPrinter/Settings.php @@ -19,6 +19,8 @@ public function isPrintUnusedTypeDefinitions(): bool; public function isPrintDirectives(): bool; + public function isPrintDirectiveDefinitions(): bool; + /** * Temporary workaround to show directives when they are not supported out * of the box. diff --git a/src/SchemaPrinter/Settings/DefaultSettings.php b/src/SchemaPrinter/Settings/DefaultSettings.php index 92c8e9c4..44556f33 100644 --- a/src/SchemaPrinter/Settings/DefaultSettings.php +++ b/src/SchemaPrinter/Settings/DefaultSettings.php @@ -11,6 +11,7 @@ class DefaultSettings extends ImmutableSettings { protected string $lineEnd = "\n"; protected int $lineLength = 80; protected bool $printDirectives = false; + protected bool $printDirectiveDefinitions = true; protected bool $printDirectivesInDescription = false; protected bool $printUnusedTypeDefinitions = true; protected bool $printUnusedDirectiveDefinitions = true; diff --git a/src/SchemaPrinter/Settings/ImmutableSettings.php b/src/SchemaPrinter/Settings/ImmutableSettings.php index 16ea006e..3cdeffe1 100644 --- a/src/SchemaPrinter/Settings/ImmutableSettings.php +++ b/src/SchemaPrinter/Settings/ImmutableSettings.php @@ -13,6 +13,7 @@ abstract class ImmutableSettings implements Settings { protected string $lineEnd; protected int $lineLength; protected bool $printDirectives; + protected bool $printDirectiveDefinitions; protected bool $printDirectivesInDescription; protected bool $printUnusedTypeDefinitions; protected bool $printUnusedDirectiveDefinitions; @@ -103,6 +104,16 @@ public function setPrintDirectives(bool $value): static { }); } + public function isPrintDirectiveDefinitions(): bool { + return $this->printDirectiveDefinitions; + } + + public function setPrintDirectiveDefinitions(bool $value): static { + return $this->set(static function (self $settings) use ($value): void { + $settings->printDirectiveDefinitions = $value; + }); + } + public function isPrintDirectivesInDescription(): bool { return $this->printDirectivesInDescription; } diff --git a/src/Testing/Package/SchemaPrinter/TestSettings.php b/src/Testing/Package/SchemaPrinter/TestSettings.php index 2f6288d2..7f036109 100644 --- a/src/Testing/Package/SchemaPrinter/TestSettings.php +++ b/src/Testing/Package/SchemaPrinter/TestSettings.php @@ -14,6 +14,7 @@ class TestSettings extends ImmutableSettings { protected string $lineEnd = "\n"; protected int $lineLength = 80; protected bool $printDirectives = true; + protected bool $printDirectiveDefinitions = true; protected bool $printDirectivesInDescription = false; protected bool $printUnusedTypeDefinitions = false; protected bool $printUnusedDirectiveDefinitions = false; From 6f4de0082869f8c9b0e30f2bed4f765c52f17673 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 23 Jan 2022 10:49:47 +0400 Subject: [PATCH 61/90] Introspection Schema Printer implementation and tests. --- src/SchemaPrinter/IntrospectionPrinter.php | 35 +++ .../IntrospectionPrinterTest.php | 57 ++++ ...ectionPrinterTest~default-settings.graphql | 281 ++++++++++++++++++ ...ionPrinterTest~test-settings-level.graphql | 281 ++++++++++++++++++ ...ospectionPrinterTest~test-settings.graphql | 281 ++++++++++++++++++ .../Settings/ImmutableSettings.php | 28 ++ .../Settings/ImmutableSettingsTest.php | 31 ++ 7 files changed, 994 insertions(+) create mode 100644 src/SchemaPrinter/IntrospectionPrinter.php create mode 100644 src/SchemaPrinter/IntrospectionPrinterTest.php create mode 100644 src/SchemaPrinter/IntrospectionPrinterTest~default-settings.graphql create mode 100644 src/SchemaPrinter/IntrospectionPrinterTest~test-settings-level.graphql create mode 100644 src/SchemaPrinter/IntrospectionPrinterTest~test-settings.graphql create mode 100644 src/SchemaPrinter/Settings/ImmutableSettingsTest.php diff --git a/src/SchemaPrinter/IntrospectionPrinter.php b/src/SchemaPrinter/IntrospectionPrinter.php new file mode 100644 index 00000000..c32f7ca2 --- /dev/null +++ b/src/SchemaPrinter/IntrospectionPrinter.php @@ -0,0 +1,35 @@ +setPrintUnusedTypeDefinitions(true) + ->setPrintDirectiveDefinitions(true) + ->setPrintUnusedDirectiveDefinitions(true) + ); + } + + protected function isSchemaType(Type $type): bool { + return Introspection::isIntrospectionType($type); + } + + protected function isSchemaDirective(Directive $directive): bool { + return Directive::isSpecifiedDirective($directive); + } +} diff --git a/src/SchemaPrinter/IntrospectionPrinterTest.php b/src/SchemaPrinter/IntrospectionPrinterTest.php new file mode 100644 index 00000000..9e0fed07 --- /dev/null +++ b/src/SchemaPrinter/IntrospectionPrinterTest.php @@ -0,0 +1,57 @@ + + // ========================================================================= + /** + * @covers ::print + * + * @dataProvider dataProviderPrint + */ + public function testPrint(string $expected, Settings $settings, int $level): void { + $expected = $this->getTestData()->content($expected); + $printer = (new IntrospectionPrinter())->setSettings($settings)->setLevel($level); + $schema = new Schema([]); + $actual = $printer->print($schema); + + self::assertEquals($expected, (string) $actual); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderPrint(): array { + return [ + DefaultSettings::class => [ + '~default-settings.graphql', + new DefaultSettings(), + 0, + ], + TestSettings::class => [ + '~test-settings.graphql', + new TestSettings(), + 0, + ], + TestSettings::class.' (level)' => [ + '~test-settings-level.graphql', + new TestSettings(), + 1, + ], + ]; + } + // +} diff --git a/src/SchemaPrinter/IntrospectionPrinterTest~default-settings.graphql b/src/SchemaPrinter/IntrospectionPrinterTest~default-settings.graphql new file mode 100644 index 00000000..9885cf4e --- /dev/null +++ b/src/SchemaPrinter/IntrospectionPrinterTest~default-settings.graphql @@ -0,0 +1,281 @@ +""" +A Directive can be adjacent to many parts of the GraphQL language, a __DirectiveLocation describes one such possible adjacencies. +""" +enum __DirectiveLocation { + """ + Location adjacent to a query operation. + """ + QUERY + + """ + Location adjacent to a mutation operation. + """ + MUTATION + + """ + Location adjacent to a subscription operation. + """ + SUBSCRIPTION + + """ + Location adjacent to a field. + """ + FIELD + + """ + Location adjacent to a fragment definition. + """ + FRAGMENT_DEFINITION + + """ + Location adjacent to a fragment spread. + """ + FRAGMENT_SPREAD + + """ + Location adjacent to an inline fragment. + """ + INLINE_FRAGMENT + + """ + Location adjacent to a variable definition. + """ + VARIABLE_DEFINITION + + """ + Location adjacent to a schema definition. + """ + SCHEMA + + """ + Location adjacent to a scalar definition. + """ + SCALAR + + """ + Location adjacent to an object type definition. + """ + OBJECT + + """ + Location adjacent to a field definition. + """ + FIELD_DEFINITION + + """ + Location adjacent to an argument definition. + """ + ARGUMENT_DEFINITION + + """ + Location adjacent to an interface definition. + """ + INTERFACE + + """ + Location adjacent to a union definition. + """ + UNION + + """ + Location adjacent to an enum definition. + """ + ENUM + + """ + Location adjacent to an enum value definition. + """ + ENUM_VALUE + + """ + Location adjacent to an input object type definition. + """ + INPUT_OBJECT + + """ + Location adjacent to an input object field definition. + """ + INPUT_FIELD_DEFINITION +} + +""" +An enum describing what kind of type a given `__Type` is. +""" +enum __TypeKind { + """ + Indicates this type is a scalar. + """ + SCALAR + + """ + Indicates this type is an object. `fields` and `interfaces` are valid fields. + """ + OBJECT + + """ + Indicates this type is an interface. `fields`, `interfaces`, and `possibleTypes` are valid fields. + """ + INTERFACE + + """ + Indicates this type is a union. `possibleTypes` is a valid field. + """ + UNION + + """ + Indicates this type is an enum. `enumValues` is a valid field. + """ + ENUM + + """ + Indicates this type is an input object. `inputFields` is a valid field. + """ + INPUT_OBJECT + + """ + Indicates this type is a list. `ofType` is a valid field. + """ + LIST + + """ + Indicates this type is a non-null. `ofType` is a valid field. + """ + NON_NULL +} + +""" +A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document. + +In some cases, you need to provide options to alter GraphQL's execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to the executor. +""" +type __Directive { + name: String! + description: String + args: [__InputValue!]! + isRepeatable: Boolean! + locations: [__DirectiveLocation!]! +} + +""" +One possible value for a given Enum. Enum values are unique values, not a placeholder for a string or numeric value. However an Enum value is returned in a JSON response as a string. +""" +type __EnumValue { + name: String! + description: String + isDeprecated: Boolean! + deprecationReason: String +} + +""" +Object and Interface types are described by a list of Fields, each of which has a name, potentially a list of arguments, and a return type. +""" +type __Field { + name: String! + description: String + args: [__InputValue!]! + type: __Type! + isDeprecated: Boolean! + deprecationReason: String +} + +""" +Arguments provided to Fields or Directives and the input fields of an InputObject are represented as Input Values which describe their type and optionally a default value. +""" +type __InputValue { + name: String! + description: String + type: __Type! + + """ + A GraphQL-formatted string representing the default value for this input value. + """ + defaultValue: String +} + +""" +A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations. +""" +type __Schema { + """ + A list of all types supported by this server. + """ + types: [__Type!]! + + """ + The type that query operations will be rooted at. + """ + queryType: __Type! + + """ + If this server supports mutation, the type that mutation operations will be rooted at. + """ + mutationType: __Type + + """ + If this server support subscription, the type that subscription operations will be rooted at. + """ + subscriptionType: __Type + + """ + A list of all directives supported by this server. + """ + directives: [__Directive!]! +} + +""" +The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in GraphQL as represented by the `__TypeKind` enum. + +Depending on the kind of a type, certain fields describe information about that type. Scalar types provide no information beyond a name and description, while Enum types provide their values. Object and Interface types provide the fields they describe. Abstract types, Union and Interface, provide the Object types possible at runtime. List and NonNull types compose other types. +""" +type __Type { + kind: __TypeKind! + name: String + description: String + fields(includeDeprecated: Boolean = false): [__Field!] + interfaces: [__Type!] + possibleTypes: [__Type!] + enumValues(includeDeprecated: Boolean = false): [__EnumValue!] + inputFields: [__InputValue!] + ofType: __Type +} + +""" +Marks an element of a GraphQL schema as no longer supported. +""" +directive @deprecated( + """ + Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted using the Markdown syntax (as specified by [CommonMark](https://commonmark.org/). + """ + reason: String = "No longer supported" +) +on + | FIELD_DEFINITION + | ENUM_VALUE + +""" +Directs the executor to include this field or fragment only when the `if` argument is true. +""" +directive @include( + """ + Included when true. + """ + if: Boolean! +) +on + | FIELD + | FRAGMENT_SPREAD + | INLINE_FRAGMENT + +""" +Directs the executor to skip this field or fragment when the `if` argument is true. +""" +directive @skip( + """ + Skipped when true. + """ + if: Boolean! +) +on + | FIELD + | FRAGMENT_SPREAD + | INLINE_FRAGMENT diff --git a/src/SchemaPrinter/IntrospectionPrinterTest~test-settings-level.graphql b/src/SchemaPrinter/IntrospectionPrinterTest~test-settings-level.graphql new file mode 100644 index 00000000..552f4d93 --- /dev/null +++ b/src/SchemaPrinter/IntrospectionPrinterTest~test-settings-level.graphql @@ -0,0 +1,281 @@ + """ + A Directive can be adjacent to many parts of the GraphQL language, a __DirectiveLocation describes one such possible adjacencies. + """ + enum __DirectiveLocation { + """ + Location adjacent to an argument definition. + """ + ARGUMENT_DEFINITION + + """ + Location adjacent to an enum definition. + """ + ENUM + + """ + Location adjacent to an enum value definition. + """ + ENUM_VALUE + + """ + Location adjacent to a field. + """ + FIELD + + """ + Location adjacent to a field definition. + """ + FIELD_DEFINITION + + """ + Location adjacent to a fragment definition. + """ + FRAGMENT_DEFINITION + + """ + Location adjacent to a fragment spread. + """ + FRAGMENT_SPREAD + + """ + Location adjacent to an inline fragment. + """ + INLINE_FRAGMENT + + """ + Location adjacent to an input object field definition. + """ + INPUT_FIELD_DEFINITION + + """ + Location adjacent to an input object type definition. + """ + INPUT_OBJECT + + """ + Location adjacent to an interface definition. + """ + INTERFACE + + """ + Location adjacent to a mutation operation. + """ + MUTATION + + """ + Location adjacent to an object type definition. + """ + OBJECT + + """ + Location adjacent to a query operation. + """ + QUERY + + """ + Location adjacent to a scalar definition. + """ + SCALAR + + """ + Location adjacent to a schema definition. + """ + SCHEMA + + """ + Location adjacent to a subscription operation. + """ + SUBSCRIPTION + + """ + Location adjacent to a union definition. + """ + UNION + + """ + Location adjacent to a variable definition. + """ + VARIABLE_DEFINITION + } + + """ + An enum describing what kind of type a given `__Type` is. + """ + enum __TypeKind { + """ + Indicates this type is an enum. `enumValues` is a valid field. + """ + ENUM + + """ + Indicates this type is an input object. `inputFields` is a valid field. + """ + INPUT_OBJECT + + """ + Indicates this type is an interface. `fields`, `interfaces`, and `possibleTypes` are valid fields. + """ + INTERFACE + + """ + Indicates this type is a list. `ofType` is a valid field. + """ + LIST + + """ + Indicates this type is a non-null. `ofType` is a valid field. + """ + NON_NULL + + """ + Indicates this type is an object. `fields` and `interfaces` are valid fields. + """ + OBJECT + + """ + Indicates this type is a scalar. + """ + SCALAR + + """ + Indicates this type is a union. `possibleTypes` is a valid field. + """ + UNION + } + + """ + A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document. + + In some cases, you need to provide options to alter GraphQL's execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to the executor. + """ + type __Directive { + args: [__InputValue!]! + description: String + isRepeatable: Boolean! + locations: [__DirectiveLocation!]! + name: String! + } + + """ + One possible value for a given Enum. Enum values are unique values, not a placeholder for a string or numeric value. However an Enum value is returned in a JSON response as a string. + """ + type __EnumValue { + deprecationReason: String + description: String + isDeprecated: Boolean! + name: String! + } + + """ + Object and Interface types are described by a list of Fields, each of which has a name, potentially a list of arguments, and a return type. + """ + type __Field { + args: [__InputValue!]! + deprecationReason: String + description: String + isDeprecated: Boolean! + name: String! + type: __Type! + } + + """ + Arguments provided to Fields or Directives and the input fields of an InputObject are represented as Input Values which describe their type and optionally a default value. + """ + type __InputValue { + """ + A GraphQL-formatted string representing the default value for this input value. + """ + defaultValue: String + + description: String + name: String! + type: __Type! + } + + """ + A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations. + """ + type __Schema { + """ + A list of all directives supported by this server. + """ + directives: [__Directive!]! + + """ + If this server supports mutation, the type that mutation operations will be rooted at. + """ + mutationType: __Type + + """ + The type that query operations will be rooted at. + """ + queryType: __Type! + + """ + If this server support subscription, the type that subscription operations will be rooted at. + """ + subscriptionType: __Type + + """ + A list of all types supported by this server. + """ + types: [__Type!]! + } + + """ + The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in GraphQL as represented by the `__TypeKind` enum. + + Depending on the kind of a type, certain fields describe information about that type. Scalar types provide no information beyond a name and description, while Enum types provide their values. Object and Interface types provide the fields they describe. Abstract types, Union and Interface, provide the Object types possible at runtime. List and NonNull types compose other types. + """ + type __Type { + description: String + enumValues(includeDeprecated: Boolean = false): [__EnumValue!] + fields(includeDeprecated: Boolean = false): [__Field!] + inputFields: [__InputValue!] + interfaces: [__Type!] + kind: __TypeKind! + name: String + ofType: __Type + possibleTypes: [__Type!] + } + + """ + Marks an element of a GraphQL schema as no longer supported. + """ + directive @deprecated( + """ + Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted using the Markdown syntax (as specified by [CommonMark](https://commonmark.org/). + """ + reason: String = "No longer supported" + ) + on + | ENUM_VALUE + | FIELD_DEFINITION + + """ + Directs the executor to include this field or fragment only when the `if` argument is true. + """ + directive @include( + """ + Included when true. + """ + if: Boolean! + ) + on + | FIELD + | FRAGMENT_SPREAD + | INLINE_FRAGMENT + + """ + Directs the executor to skip this field or fragment when the `if` argument is true. + """ + directive @skip( + """ + Skipped when true. + """ + if: Boolean! + ) + on + | FIELD + | FRAGMENT_SPREAD + | INLINE_FRAGMENT diff --git a/src/SchemaPrinter/IntrospectionPrinterTest~test-settings.graphql b/src/SchemaPrinter/IntrospectionPrinterTest~test-settings.graphql new file mode 100644 index 00000000..ce5c8af0 --- /dev/null +++ b/src/SchemaPrinter/IntrospectionPrinterTest~test-settings.graphql @@ -0,0 +1,281 @@ +""" +A Directive can be adjacent to many parts of the GraphQL language, a __DirectiveLocation describes one such possible adjacencies. +""" +enum __DirectiveLocation { + """ + Location adjacent to an argument definition. + """ + ARGUMENT_DEFINITION + + """ + Location adjacent to an enum definition. + """ + ENUM + + """ + Location adjacent to an enum value definition. + """ + ENUM_VALUE + + """ + Location adjacent to a field. + """ + FIELD + + """ + Location adjacent to a field definition. + """ + FIELD_DEFINITION + + """ + Location adjacent to a fragment definition. + """ + FRAGMENT_DEFINITION + + """ + Location adjacent to a fragment spread. + """ + FRAGMENT_SPREAD + + """ + Location adjacent to an inline fragment. + """ + INLINE_FRAGMENT + + """ + Location adjacent to an input object field definition. + """ + INPUT_FIELD_DEFINITION + + """ + Location adjacent to an input object type definition. + """ + INPUT_OBJECT + + """ + Location adjacent to an interface definition. + """ + INTERFACE + + """ + Location adjacent to a mutation operation. + """ + MUTATION + + """ + Location adjacent to an object type definition. + """ + OBJECT + + """ + Location adjacent to a query operation. + """ + QUERY + + """ + Location adjacent to a scalar definition. + """ + SCALAR + + """ + Location adjacent to a schema definition. + """ + SCHEMA + + """ + Location adjacent to a subscription operation. + """ + SUBSCRIPTION + + """ + Location adjacent to a union definition. + """ + UNION + + """ + Location adjacent to a variable definition. + """ + VARIABLE_DEFINITION +} + +""" +An enum describing what kind of type a given `__Type` is. +""" +enum __TypeKind { + """ + Indicates this type is an enum. `enumValues` is a valid field. + """ + ENUM + + """ + Indicates this type is an input object. `inputFields` is a valid field. + """ + INPUT_OBJECT + + """ + Indicates this type is an interface. `fields`, `interfaces`, and `possibleTypes` are valid fields. + """ + INTERFACE + + """ + Indicates this type is a list. `ofType` is a valid field. + """ + LIST + + """ + Indicates this type is a non-null. `ofType` is a valid field. + """ + NON_NULL + + """ + Indicates this type is an object. `fields` and `interfaces` are valid fields. + """ + OBJECT + + """ + Indicates this type is a scalar. + """ + SCALAR + + """ + Indicates this type is a union. `possibleTypes` is a valid field. + """ + UNION +} + +""" +A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document. + +In some cases, you need to provide options to alter GraphQL's execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to the executor. +""" +type __Directive { + args: [__InputValue!]! + description: String + isRepeatable: Boolean! + locations: [__DirectiveLocation!]! + name: String! +} + +""" +One possible value for a given Enum. Enum values are unique values, not a placeholder for a string or numeric value. However an Enum value is returned in a JSON response as a string. +""" +type __EnumValue { + deprecationReason: String + description: String + isDeprecated: Boolean! + name: String! +} + +""" +Object and Interface types are described by a list of Fields, each of which has a name, potentially a list of arguments, and a return type. +""" +type __Field { + args: [__InputValue!]! + deprecationReason: String + description: String + isDeprecated: Boolean! + name: String! + type: __Type! +} + +""" +Arguments provided to Fields or Directives and the input fields of an InputObject are represented as Input Values which describe their type and optionally a default value. +""" +type __InputValue { + """ + A GraphQL-formatted string representing the default value for this input value. + """ + defaultValue: String + + description: String + name: String! + type: __Type! +} + +""" +A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations. +""" +type __Schema { + """ + A list of all directives supported by this server. + """ + directives: [__Directive!]! + + """ + If this server supports mutation, the type that mutation operations will be rooted at. + """ + mutationType: __Type + + """ + The type that query operations will be rooted at. + """ + queryType: __Type! + + """ + If this server support subscription, the type that subscription operations will be rooted at. + """ + subscriptionType: __Type + + """ + A list of all types supported by this server. + """ + types: [__Type!]! +} + +""" +The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in GraphQL as represented by the `__TypeKind` enum. + +Depending on the kind of a type, certain fields describe information about that type. Scalar types provide no information beyond a name and description, while Enum types provide their values. Object and Interface types provide the fields they describe. Abstract types, Union and Interface, provide the Object types possible at runtime. List and NonNull types compose other types. +""" +type __Type { + description: String + enumValues(includeDeprecated: Boolean = false): [__EnumValue!] + fields(includeDeprecated: Boolean = false): [__Field!] + inputFields: [__InputValue!] + interfaces: [__Type!] + kind: __TypeKind! + name: String + ofType: __Type + possibleTypes: [__Type!] +} + +""" +Marks an element of a GraphQL schema as no longer supported. +""" +directive @deprecated( + """ + Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted using the Markdown syntax (as specified by [CommonMark](https://commonmark.org/). + """ + reason: String = "No longer supported" +) +on + | ENUM_VALUE + | FIELD_DEFINITION + +""" +Directs the executor to include this field or fragment only when the `if` argument is true. +""" +directive @include( + """ + Included when true. + """ + if: Boolean! +) +on + | FIELD + | FRAGMENT_SPREAD + | INLINE_FRAGMENT + +""" +Directs the executor to skip this field or fragment when the `if` argument is true. +""" +directive @skip( + """ + Skipped when true. + """ + if: Boolean! +) +on + | FIELD + | FRAGMENT_SPREAD + | INLINE_FRAGMENT diff --git a/src/SchemaPrinter/Settings/ImmutableSettings.php b/src/SchemaPrinter/Settings/ImmutableSettings.php index 3cdeffe1..c1b8aa3a 100644 --- a/src/SchemaPrinter/Settings/ImmutableSettings.php +++ b/src/SchemaPrinter/Settings/ImmutableSettings.php @@ -261,4 +261,32 @@ protected function set(Closure $callback): static { return $settings; } + + public static function createFrom(Settings $settings): self { + return (new class() extends ImmutableSettings { + // empty + }) + ->setSpace($settings->getSpace()) + ->setIndent($settings->getIndent()) + ->setFileEnd($settings->getFileEnd()) + ->setLineEnd($settings->getLineEnd()) + ->setLineLength($settings->getLineLength()) + ->setPrintUnusedTypeDefinitions($settings->isPrintUnusedTypeDefinitions()) + ->setPrintDirectives($settings->isPrintDirectives()) + ->setPrintDirectiveDefinitions($settings->isPrintDirectiveDefinitions()) + ->setPrintDirectivesInDescription($settings->isPrintDirectivesInDescription()) + ->setPrintUnusedDirectiveDefinitions($settings->isPrintUnusedDirectiveDefinitions()) + ->setNormalizeSchema($settings->isNormalizeSchema()) + ->setNormalizeUnions($settings->isNormalizeUnions()) + ->setNormalizeEnums($settings->isNormalizeEnums()) + ->setNormalizeInterfaces($settings->isNormalizeInterfaces()) + ->setNormalizeFields($settings->isNormalizeFields()) + ->setNormalizeArguments($settings->isNormalizeArguments()) + ->setNormalizeDescription($settings->isNormalizeDescription()) + ->setNormalizeDirectiveLocations($settings->isNormalizeDirectiveLocations()) + ->setAlwaysMultilineUnions($settings->isAlwaysMultilineUnions()) + ->setAlwaysMultilineInterfaces($settings->isAlwaysMultilineInterfaces()) + ->setAlwaysMultilineDirectiveLocations($settings->isAlwaysMultilineDirectiveLocations()) + ->setDirectiveFilter($settings->getDirectiveFilter()); + } } diff --git a/src/SchemaPrinter/Settings/ImmutableSettingsTest.php b/src/SchemaPrinter/Settings/ImmutableSettingsTest.php new file mode 100644 index 00000000..1898af36 --- /dev/null +++ b/src/SchemaPrinter/Settings/ImmutableSettingsTest.php @@ -0,0 +1,31 @@ +getMethods(ReflectionMethod::IS_PUBLIC); + $settings = Mockery::mock(Settings::class); + + foreach ($methods as $method) { + $settings + ->shouldReceive($method->getName()) + ->once(); + } + + ImmutableSettings::createFrom($settings); + } +} From d89aabe621256135b08b198c98a86096fc01c29e Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 23 Jan 2022 10:59:22 +0400 Subject: [PATCH 62/90] `BlockList` will ignore empty blocks. --- src/SchemaPrinter/Blocks/BlockList.php | 4 ++++ src/SchemaPrinter/Blocks/BlockListTest.php | 24 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/SchemaPrinter/Blocks/BlockList.php b/src/SchemaPrinter/Blocks/BlockList.php index c4edff46..32aa3356 100644 --- a/src/SchemaPrinter/Blocks/BlockList.php +++ b/src/SchemaPrinter/Blocks/BlockList.php @@ -213,6 +213,10 @@ public function offsetGet(mixed $offset): Block { * @param TBlock $value */ public function offsetSet(mixed $offset, mixed $value): void { + if ($value->isEmpty()) { + return; + } + if ($offset !== null) { $this->blocks[$offset] = $value; } else { diff --git a/src/SchemaPrinter/Blocks/BlockListTest.php b/src/SchemaPrinter/Blocks/BlockListTest.php index 8aa96802..89c1f8ea 100644 --- a/src/SchemaPrinter/Blocks/BlockListTest.php +++ b/src/SchemaPrinter/Blocks/BlockListTest.php @@ -625,6 +625,30 @@ public function dataProviderToString(): array { ], ], ]), + 'empty blocks' => new ArrayDataProvider([ + 'should be ignored' => [ + <<<'STRING' + block a + block b + STRING, + $settings, + 0, + 120, + false, + true, + '', + '', + ', ', + '', + [ + new BlockListTest__Block(false, ''), + new BlockListTest__Block(false, 'block a'), + new BlockListTest__Block(true, ''), + new BlockListTest__Block(false, 'block b'), + new BlockListTest__Block(false, ''), + ], + ], + ]), ]))->getData(); } // From a01ef25da723fdfcb682bd6a1e89f37582764d03 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 29 Jan 2022 11:53:40 +0400 Subject: [PATCH 63/90] `Printer` tests (but expected schema is invalid). --- src/SchemaPrinter/PrinterTest.php | 138 ++++++++++++ .../PrinterTest~default-settings.graphql | 209 ++++++++++++++++++ src/SchemaPrinter/PrinterTest~schema.graphql | 135 +++++++++++ .../PrinterTest~test-settings.graphql | 173 +++++++++++++++ 4 files changed, 655 insertions(+) create mode 100644 src/SchemaPrinter/PrinterTest.php create mode 100644 src/SchemaPrinter/PrinterTest~default-settings.graphql create mode 100644 src/SchemaPrinter/PrinterTest~schema.graphql create mode 100644 src/SchemaPrinter/PrinterTest~test-settings.graphql diff --git a/src/SchemaPrinter/PrinterTest.php b/src/SchemaPrinter/PrinterTest.php new file mode 100644 index 00000000..f2dcf68e --- /dev/null +++ b/src/SchemaPrinter/PrinterTest.php @@ -0,0 +1,138 @@ + + // ========================================================================= + /** + * @covers ::print + * + * @dataProvider dataProviderPrint + */ + public function testPrint(string $expected, Settings $settings, int $level): void { + // Types + $directives = $this->app->make(DirectiveLocator::class); + $registry = $this->app->make(TypeRegistry::class); + $directive = (new class() extends BaseDirective { + public static function definition(): string { + throw new Exception("Should not be called."); + } + })::class; + + $codeScalar = new CustomScalarType([ + 'name' => 'CodeScalar', + ]); + $codeEnum = new EnumType([ + 'name' => 'CodeEnum', + 'values' => ['C', 'B', 'A'], + ]); + $codeInterface = new InterfaceType([ + 'name' => 'CodeInterface', + 'astNode' => Parser::interfaceTypeDefinition('interface CodeInterface @codeDirective'), + 'description' => 'Description', + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::boolean(), + ], + ], + ]); + $codeType = new ObjectType([ + 'name' => 'CodeType', + 'astNode' => Parser::objectTypeDefinition('type CodeType @schemaDirective'), + 'description' => 'Description', + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::boolean(), + ], + ], + ]); + $codeUnion = new UnionType([ + 'name' => 'CodeUnion', + 'types' => [ + $codeType, + ], + ]); + $codeInput = new InputObjectType([ + 'name' => 'CodeInput', + 'astNode' => Parser::inputObjectTypeDefinition('input InputObjectType @schemaDirective'), + 'description' => 'Description', + 'fields' => [ + [ + 'name' => 'a', + 'type' => Type::boolean(), + ], + ], + ]); + + $directives->setResolved('schemaDirective', $directive); + $directives->setResolved('schemaDirectiveUnused', $directive); + $directives->setResolved( + 'codeDirective', + (new class() extends BaseDirective { + public static function definition(): string { + return "directive @codeDirective repeatable on SCHEMA | SCALAR | INTERFACE"; + } + })::class + ); + $registry->register($codeScalar); + $registry->register($codeEnum); + $registry->register($codeInterface); + $registry->register($codeType); + $registry->register($codeUnion); + $registry->register($codeInput); + + // Test + $expected = $this->getTestData()->content($expected); + $schema = $this->getGraphQLSchema($this->getTestData()->file('~schema.graphql')); + $printer = (new Printer())->setSettings($settings)->setLevel($level); + $actual = $printer->print($schema); + + self::assertEquals($expected, (string) $actual); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderPrint(): array { + return [ + DefaultSettings::class => [ + '~default-settings.graphql', + new DefaultSettings(), + 0, + ], + TestSettings::class => [ + '~test-settings.graphql', + new TestSettings(), + 0, + ], + ]; + } + // +} diff --git a/src/SchemaPrinter/PrinterTest~default-settings.graphql b/src/SchemaPrinter/PrinterTest~default-settings.graphql new file mode 100644 index 00000000..3692e971 --- /dev/null +++ b/src/SchemaPrinter/PrinterTest~default-settings.graphql @@ -0,0 +1,209 @@ +enum CodeEnum { + C + B + A +} + +enum SchemaEnum { + A + + """ + Description + """ + B +} + +""" +This is unused enum. +""" +enum SchemaEnumUnused { + A +} + +""" +Description +""" +input CodeInput { + a: Boolean +} + +input SchemaInput { + f: [String!] + + """ + Recursion + """ + e: SchemaInput + + d: SchemaEnum + c: SchemaScalar + b: CodeEnum + a: CodeScalar +} + +""" +This is unused input. +""" +input SchemaInputUnused { + a: CodeScalar + b: CodeEnum + c: SchemaScalar + d: SchemaEnum + + """ + Recursion + """ + e: SchemaInput +} + +""" +Description +""" +interface CodeInterface { + a: Boolean +} + +interface SchemaInterfaceA { + a: Boolean! +} + +interface SchemaInterfaceB { + a: Boolean! + + """ + Deprecated field + """ + b: [String]! + + c( + """ + aaa + """ + a: String + + """ + bbb + """ + b: [SchemaScalar!]! + + c: SchemaEnum + ): CodeUnion + + d: CodeScalar + e: CodeEnum +} + +""" +This is unused interface. +""" +interface SchemaInterfaceUnused { + a: SchemaScalarUnused + b: SchemaEnumUnused +} + +scalar CodeScalar + +""" +The `String` scalar type represents textual data, represented as UTF-8 +character sequences. The String type is most often used by GraphQL to +represent free-form human-readable text. +""" +scalar SchemaScalar + +""" +This is unused scalar. +""" +scalar SchemaScalarUnused + +""" +Description +""" +type CodeType { + a: Boolean +} + +type Query { + a: SchemaType + b: SchemaEnum + c(a: SchemaInput = ): CodeScalar + d: CodeType +} + +type SchemaType implements SchemaInterfaceB { + a: Boolean! + + """ + Deprecated field + """ + b: [String]! + + c( + """ + aaa + """ + a: String + + """ + bbb + """ + b: [SchemaScalar!]! + + c: CodeInput + ): CodeUnion + + d: CodeScalar + e: CodeEnum + + f( + a: [String!] = [ + "very very very long line of text" + "very very very long line of text" + "very very very long line of text" + ] + ): SchemaUnion +} + +""" +This is unused type. +""" +type SchemaTypeUnused { + a: SchemaScalarUnused +} + +union CodeUnion = CodeType +union SchemaUnion = SchemaType | CodeType + +""" +This is unused union. +""" +union SchemaUnionUnused = SchemaTypeUnused + +""" +Directive +""" +directive @schemaDirective( + """ + Directive argument + """ + message: String +) +on + | SCHEMA + | FIELD + | ARGUMENT_DEFINITION + | INTERFACE + | OBJECT + | UNION + | INPUT_OBJECT + | SCALAR + +""" +This is unused directives. +""" +directive @schemaDirectiveUnused( + a: SchemaScalarUnused + b: SchemaEnumUnused +) +repeatable on + | SCALAR + | OBJECT diff --git a/src/SchemaPrinter/PrinterTest~schema.graphql b/src/SchemaPrinter/PrinterTest~schema.graphql new file mode 100644 index 00000000..12bdfd63 --- /dev/null +++ b/src/SchemaPrinter/PrinterTest~schema.graphql @@ -0,0 +1,135 @@ +"""Directive""" +directive @schemaDirective( + """ + + Directive argument + + """ + message: String +) on SCHEMA | FIELD | ARGUMENT_DEFINITION | INTERFACE | OBJECT | UNION | INPUT_OBJECT | SCALAR + +scalar SchemaScalar @scalar(class: "GraphQL\\Type\\Definition\\StringType") @codeDirective + +enum SchemaEnum { + A @deprecated + + "Description" + B +} + +interface SchemaInterfaceA { + a: Boolean! +} + +interface SchemaInterfaceB implements SchemaInterfaceA & CodeInterface @schemaDirective { + a: Boolean! + "Deprecated field" + b: [String]! @deprecated + c( + "aaa" + a: String + "bbb" + b: [SchemaScalar!]! + c: SchemaEnum + ): CodeUnion + d: CodeScalar + e: CodeEnum +} + +type Query { + a: SchemaType @deprecated(reason: "deprecated reason") @codeDirective @mock + b: SchemaEnum @deprecated(reason: "No longer supported") @mock + c (a: SchemaInput = {a: "aaa", b: "bbb", c: "ccc", d: "ddd", e: {a: "aaa", b: "bbb", c: "ccc", d: "ddd", e: ["aaa", "bbb", "ccc", "ddd"]}}): CodeScalar @mock + d: CodeType @mock +} + +type SchemaType implements SchemaInterfaceB @schemaDirective { + a: Boolean! + "Deprecated field" + b: [String]! @deprecated + c( + "aaa" + a: String + "bbb" + b: [SchemaScalar!]! + c: CodeInput + ): CodeUnion + d: CodeScalar + e: CodeEnum + f(a: [String!] = ["very very very long line of text", "very very very long line of text", "very very very long line of text"]): SchemaUnion +} + +union SchemaUnion @schemaDirective = SchemaType | CodeType + +input SchemaInput @schemaDirective { + f: [String!] + + "Recursion" + e: SchemaInput + d: SchemaEnum + + """ + + """ + c: SchemaScalar + + + b: CodeEnum + a: CodeScalar +} + +# Unused + +""" +This is unused directives. +""" +directive @schemaDirectiveUnused(a: SchemaScalarUnused, b: SchemaEnumUnused) repeatable on SCALAR | OBJECT + +""" +This is unused scalar. +""" +scalar SchemaScalarUnused @scalar(class: "GraphQL\\Type\\Definition\\StringType") + + +""" +This is unused enum. +""" +enum SchemaEnumUnused { + A +} + +""" +This is unused interface. +""" +interface SchemaInterfaceUnused { + a: SchemaScalarUnused + b: SchemaEnumUnused +} + +""" +This is unused type. +""" +type SchemaTypeUnused @schemaDirectiveUnused { + a: SchemaScalarUnused +} + +""" +This is unused union. +""" +union SchemaUnionUnused = SchemaTypeUnused + +""" +This is unused input. +""" +input SchemaInputUnused @schemaDirective { + a: CodeScalar + b: CodeEnum + """ + + """ + c: SchemaScalar + d: SchemaEnum + + "Recursion" + e: SchemaInput +} diff --git a/src/SchemaPrinter/PrinterTest~test-settings.graphql b/src/SchemaPrinter/PrinterTest~test-settings.graphql new file mode 100644 index 00000000..476e68d9 --- /dev/null +++ b/src/SchemaPrinter/PrinterTest~test-settings.graphql @@ -0,0 +1,173 @@ +enum CodeEnum { + A + B + C +} + +enum SchemaEnum { + A + @deprecated + + """ + Description + """ + B +} + +""" +This is unused enum. +""" +enum SchemaEnumUnused { + A +} + +""" +Description +""" +input CodeInput +@schemaDirective +{ + a: Boolean +} + +input SchemaInput +@schemaDirective +{ + a: CodeScalar + b: CodeEnum + c: SchemaScalar + d: SchemaEnum + + """ + Recursion + """ + e: SchemaInput + + f: [String!] +} + +interface SchemaInterfaceB +@schemaDirective +{ + a: Boolean! + + """ + Deprecated field + """ + b: [String]! + @deprecated + + c( + """ + aaa + """ + a: String + + """ + bbb + """ + b: [SchemaScalar!]! + + c: SchemaEnum + ): CodeUnion + + d: CodeScalar + e: CodeEnum +} + +scalar CodeScalar + +""" +The `String` scalar type represents textual data, represented as UTF-8 +character sequences. The String type is most often used by GraphQL to +represent free-form human-readable text. +""" +scalar SchemaScalar +@scalar(class: "GraphQL\\Type\\Definition\\StringType") +@codeDirective + +""" +This is unused scalar. +""" +scalar SchemaScalarUnused +@scalar(class: "GraphQL\\Type\\Definition\\StringType") + +""" +Description +""" +type CodeType +@schemaDirective +{ + a: Boolean +} + +type Query { + a: SchemaType + @deprecated(reason: "deprecated reason") + @codeDirective + @mock + + b: SchemaEnum + @deprecated + @mock + + c(a: SchemaInput = ): CodeScalar + @mock + + d: CodeType + @mock +} + +type SchemaType implements + & SchemaInterfaceB +@schemaDirective +{ + a: Boolean! + + """ + Deprecated field + """ + b: [String]! + @deprecated + + c( + """ + aaa + """ + a: String + + """ + bbb + """ + b: [SchemaScalar!]! + + c: CodeInput + ): CodeUnion + + d: CodeScalar + e: CodeEnum + + f( + a: [String!] = [ + "very very very long line of text" + "very very very long line of text" + "very very very long line of text" + ] + ): SchemaUnion +} + +""" +This is unused type. +""" +type SchemaTypeUnused +@schemaDirectiveUnused +{ + a: SchemaScalarUnused +} + +union CodeUnion = | CodeType + +union SchemaUnion = + | CodeType + | SchemaType +@schemaDirective From f8ad12abe7937ef465345cbfa95a5e39f0682567 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 29 Jan 2022 12:00:18 +0400 Subject: [PATCH 64/90] Union directives position fix. --- .../Blocks/Types/UnionTypeDefinitionBlock.php | 12 ++-- .../Types/UnionTypeDefinitionBlockTest.php | 61 +++++++++++++++++++ .../PrinterTest~test-settings.graphql | 5 +- 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php index f58e93a9..0601517f 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php @@ -30,8 +30,12 @@ protected function type(): string|null { } protected function body(int $used): Block|string|null { + return null; + } + + protected function fields(int $used): Block|string|null { $space = $this->space(); - $equal = "{$space}={$space}"; + $equal = "={$space}"; $types = new UnionMemberTypesList( $this->getDispatcher(), $this->getSettings(), @@ -43,15 +47,11 @@ protected function body(int $used): Block|string|null { if ($types->isMultiline()) { $eol = $this->eol(); $indent = $this->indent($this->getLevel() + 1); - $types = "{$space}={$eol}{$indent}{$types}"; + $types = "={$eol}{$indent}{$types}"; } else { $types = "{$equal}{$types}"; } return $types; } - - protected function fields(int $used): Block|string|null { - return null; - } } diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php index 2e523880..bd5660e9 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php @@ -238,6 +238,67 @@ public function dataProviderToString(): array { ], ]), ], + 'directives' => [ + <<<'STRING' + union Test + @a + = C | B | A + STRING, + $settings, + 0, + 0, + new UnionType([ + 'name' => 'Test', + 'types' => [ + new ObjectType([ + 'name' => 'C', + ]), + new ObjectType([ + 'name' => 'B', + ]), + new ObjectType([ + 'name' => 'A', + ]), + ], + 'astNode' => Parser::unionTypeDefinition( + <<<'STRING' + union Test @a = A | B | C + STRING, + ), + ]), + ], + 'directives + multiline' => [ + <<<'STRING' + union Test + @a + = + | C + | B + | A + STRING, + $settings, + 0, + 120, + new UnionType([ + 'name' => 'Test', + 'types' => [ + new ObjectType([ + 'name' => 'C', + ]), + new ObjectType([ + 'name' => 'B', + ]), + new ObjectType([ + 'name' => 'A', + ]), + ], + 'astNode' => Parser::unionTypeDefinition( + <<<'STRING' + union Test @a = A | B | C + STRING, + ), + ]), + ], ]; } // diff --git a/src/SchemaPrinter/PrinterTest~test-settings.graphql b/src/SchemaPrinter/PrinterTest~test-settings.graphql index 476e68d9..be3f438f 100644 --- a/src/SchemaPrinter/PrinterTest~test-settings.graphql +++ b/src/SchemaPrinter/PrinterTest~test-settings.graphql @@ -167,7 +167,8 @@ type SchemaTypeUnused union CodeUnion = | CodeType -union SchemaUnion = +union SchemaUnion +@schemaDirective += | CodeType | SchemaType -@schemaDirective From f790a3318ed54834888a804aabc01de19fa479f8 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 29 Jan 2022 12:18:24 +0400 Subject: [PATCH 65/90] Union "always multiline" fix. --- .../Types/DirectiveDefinitionBlockTest.php | 2 -- .../Types/ObjectTypeDefinitionBlockTest.php | 2 -- .../Types/UnionTypeDefinitionBlockTest.php | 17 +++++++++++++++++ src/SchemaPrinter/Blocks/Types/UsageList.php | 4 ++++ .../PrinterTest~test-settings.graphql | 3 ++- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php index 4d3da112..d6e12f77 100644 --- a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php @@ -260,7 +260,6 @@ public function dataProviderToString(): array { <<<'STRING' directive @test on | ARGUMENT_DEFINITION - | ENUM STRING, $settings ->setAlwaysMultilineDirectiveLocations(true), @@ -270,7 +269,6 @@ public function dataProviderToString(): array { 'name' => 'test', 'locations' => [ DirectiveLocation::ARGUMENT_DEFINITION, - DirectiveLocation::ENUM, ], ]), ], diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php index 2305f2cd..67d3f956 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php @@ -357,7 +357,6 @@ public function dataProviderToString(): array { <<<'STRING' type Test implements & B - & A { a: String } @@ -376,7 +375,6 @@ public function dataProviderToString(): array { ], 'interfaces' => [ new ObjectType(['name' => 'B']), - new ObjectType(['name' => 'A']), ], ]), ], diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php index bd5660e9..2d4e4d7b 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php @@ -299,6 +299,23 @@ public function dataProviderToString(): array { ), ]), ], + "one member + always multiline" => [ + <<<'STRING' + union Test = + | A + STRING, + $settings->setAlwaysMultilineUnions(true), + 0, + 0, + new UnionType([ + 'name' => 'Test', + 'types' => [ + new ObjectType([ + 'name' => 'A', + ]), + ], + ]), + ], ]; } // diff --git a/src/SchemaPrinter/Blocks/Types/UsageList.php b/src/SchemaPrinter/Blocks/Types/UsageList.php index 3105f6d9..8e37a38f 100644 --- a/src/SchemaPrinter/Blocks/Types/UsageList.php +++ b/src/SchemaPrinter/Blocks/Types/UsageList.php @@ -34,6 +34,10 @@ public function __construct( } } + public function isMultiline(): bool { + return parent::isMultiline() || $this->isAlwaysMultiline(); + } + /** * @param TType $item * diff --git a/src/SchemaPrinter/PrinterTest~test-settings.graphql b/src/SchemaPrinter/PrinterTest~test-settings.graphql index be3f438f..c367e162 100644 --- a/src/SchemaPrinter/PrinterTest~test-settings.graphql +++ b/src/SchemaPrinter/PrinterTest~test-settings.graphql @@ -165,7 +165,8 @@ type SchemaTypeUnused a: SchemaScalarUnused } -union CodeUnion = | CodeType +union CodeUnion = + | CodeType union SchemaUnion @schemaDirective From b838e35ab6f4633d8028d25c82dc564f8c90bd55 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 29 Jan 2022 14:44:57 +0400 Subject: [PATCH 66/90] Empty object `{}` print fix. --- .../ListValueList.php} | 6 ++-- .../Blocks/Ast/ObjectValueList.php | 28 +++++++++++++++++++ .../Blocks/Ast/ValueNodeBlock.php | 5 ++-- .../Blocks/Ast/ValueNodeTest.php | 9 ++++++ 4 files changed, 43 insertions(+), 5 deletions(-) rename src/SchemaPrinter/Blocks/{ListBlockList.php => Ast/ListValueList.php} (68%) create mode 100644 src/SchemaPrinter/Blocks/Ast/ObjectValueList.php diff --git a/src/SchemaPrinter/Blocks/ListBlockList.php b/src/SchemaPrinter/Blocks/Ast/ListValueList.php similarity index 68% rename from src/SchemaPrinter/Blocks/ListBlockList.php rename to src/SchemaPrinter/Blocks/Ast/ListValueList.php index 6f3bf336..bb5b6917 100644 --- a/src/SchemaPrinter/Blocks/ListBlockList.php +++ b/src/SchemaPrinter/Blocks/Ast/ListValueList.php @@ -1,13 +1,15 @@ */ -class ListBlockList extends BlockList { +class ListValueList extends BlockList { protected function getPrefix(): string { return '['; } diff --git a/src/SchemaPrinter/Blocks/Ast/ObjectValueList.php b/src/SchemaPrinter/Blocks/Ast/ObjectValueList.php new file mode 100644 index 00000000..b37760ba --- /dev/null +++ b/src/SchemaPrinter/Blocks/Ast/ObjectValueList.php @@ -0,0 +1,28 @@ + + */ +class ObjectValueList extends BlockList { + protected function getPrefix(): string { + return '{'; + } + + protected function getSuffix(): string { + return '}'; + } + + protected function getEmptyValue(): string { + return "{$this->getPrefix()}{$this->getSuffix()}"; + } + + protected function isAlwaysMultiline(): bool { + return true; + } +} diff --git a/src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php b/src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php index 7ed588a2..39494861 100644 --- a/src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php +++ b/src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php @@ -10,7 +10,6 @@ use GraphQL\Language\Printer; use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\ListBlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\ObjectBlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Property; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\StringBlock; @@ -38,13 +37,13 @@ protected function content(): string { $used = $this->getUsed(); if ($this->node instanceof ListValueNode) { - $content = new ListBlockList($dispatcher, $settings, $level, $used); + $content = new ListValueList($dispatcher, $settings, $level, $used); foreach ($this->node->values as $value) { $content[] = new ValueNodeBlock($dispatcher, $settings, $level + 1, $used, $value); } } elseif ($this->node instanceof ObjectValueNode) { - $content = new ObjectBlockList($dispatcher, $settings, $level, $used); + $content = new ObjectValueList($dispatcher, $settings, $level, $used); foreach ($this->node->fields as $field) { $name = $field->name->value; diff --git a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php index 81d40fdc..e20937b1 100644 --- a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php +++ b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php @@ -199,6 +199,15 @@ public function dataProviderToString(): array { STRING, ), ], + ObjectValueNode::class.' (empty)' => [ + <<<'STRING' + {} + STRING, + $settings, + 0, + 0, + Parser::valueLiteral('{}'), + ], 'all' => [ <<<'STRING' { From c53356cf328c3db6c5734607c1d00913083318f7 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 29 Jan 2022 14:45:52 +0400 Subject: [PATCH 67/90] Test schema fix. --- src/SchemaPrinter/PrinterTest.php | 13 +++++----- .../PrinterTest~default-settings.graphql | 25 +++++++++++++++++-- src/SchemaPrinter/PrinterTest~schema.graphql | 4 +-- .../PrinterTest~test-settings.graphql | 23 +++++++++++++++-- 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/SchemaPrinter/PrinterTest.php b/src/SchemaPrinter/PrinterTest.php index f2dcf68e..a0e348dd 100644 --- a/src/SchemaPrinter/PrinterTest.php +++ b/src/SchemaPrinter/PrinterTest.php @@ -4,14 +4,13 @@ use Exception; use GraphQL\Language\Parser; -use GraphQL\Type\Definition\CustomScalarType; use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\ObjectType; +use GraphQL\Type\Definition\StringType; use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\UnionType; -use GraphQL\Type\Schema; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -41,7 +40,7 @@ public static function definition(): string { } })::class; - $codeScalar = new CustomScalarType([ + $codeScalar = new StringType([ 'name' => 'CodeScalar', ]); $codeEnum = new EnumType([ @@ -107,9 +106,9 @@ public static function definition(): string { // Test $expected = $this->getTestData()->content($expected); - $schema = $this->getGraphQLSchema($this->getTestData()->file('~schema.graphql')); - $printer = (new Printer())->setSettings($settings)->setLevel($level); - $actual = $printer->print($schema); + $schema = $this->getGraphQLSchema($this->getTestData()->file('~schema.graphql')); + $printer = (new Printer())->setSettings($settings)->setLevel($level); + $actual = $printer->print($schema); self::assertEquals($expected, (string) $actual); } @@ -127,7 +126,7 @@ public function dataProviderPrint(): array { new DefaultSettings(), 0, ], - TestSettings::class => [ + TestSettings::class => [ '~test-settings.graphql', new TestSettings(), 0, diff --git a/src/SchemaPrinter/PrinterTest~default-settings.graphql b/src/SchemaPrinter/PrinterTest~default-settings.graphql index 3692e971..59bbd4e6 100644 --- a/src/SchemaPrinter/PrinterTest~default-settings.graphql +++ b/src/SchemaPrinter/PrinterTest~default-settings.graphql @@ -101,6 +101,11 @@ interface SchemaInterfaceUnused { b: SchemaEnumUnused } +""" +The `String` scalar type represents textual data, represented as UTF-8 +character sequences. The String type is most often used by GraphQL to +represent free-form human-readable text. +""" scalar CodeScalar """ @@ -125,8 +130,24 @@ type CodeType { type Query { a: SchemaType b: SchemaEnum - c(a: SchemaInput = ): CodeScalar - d: CodeType + + c( + a: SchemaInput = { + e: { + f: ["aaa", "bbb", "ccc", "ddd"] + d: A + c: "ccc" + b: A + a: "aaa" + } + d: A + c: "ccc" + b: A + a: "aaa" + } + ): CodeScalar + + d(a: SchemaInput = {}): CodeType } type SchemaType implements SchemaInterfaceB { diff --git a/src/SchemaPrinter/PrinterTest~schema.graphql b/src/SchemaPrinter/PrinterTest~schema.graphql index 12bdfd63..3565599e 100644 --- a/src/SchemaPrinter/PrinterTest~schema.graphql +++ b/src/SchemaPrinter/PrinterTest~schema.graphql @@ -39,8 +39,8 @@ interface SchemaInterfaceB implements SchemaInterfaceA & CodeInterface @schemaDi type Query { a: SchemaType @deprecated(reason: "deprecated reason") @codeDirective @mock b: SchemaEnum @deprecated(reason: "No longer supported") @mock - c (a: SchemaInput = {a: "aaa", b: "bbb", c: "ccc", d: "ddd", e: {a: "aaa", b: "bbb", c: "ccc", d: "ddd", e: ["aaa", "bbb", "ccc", "ddd"]}}): CodeScalar @mock - d: CodeType @mock + c (a: SchemaInput = {a: "aaa", b: A, c: "ccc", d: A, e: {a: "aaa", b: A, c: "ccc", d: A, f: ["aaa", "bbb", "ccc", "ddd"]}}): CodeScalar @mock + d (a: SchemaInput = {}): CodeType @mock } type SchemaType implements SchemaInterfaceB @schemaDirective { diff --git a/src/SchemaPrinter/PrinterTest~test-settings.graphql b/src/SchemaPrinter/PrinterTest~test-settings.graphql index c367e162..67d1c425 100644 --- a/src/SchemaPrinter/PrinterTest~test-settings.graphql +++ b/src/SchemaPrinter/PrinterTest~test-settings.graphql @@ -75,6 +75,11 @@ interface SchemaInterfaceB e: CodeEnum } +""" +The `String` scalar type represents textual data, represented as UTF-8 +character sequences. The String type is most often used by GraphQL to +represent free-form human-readable text. +""" scalar CodeScalar """ @@ -111,10 +116,24 @@ type Query { @deprecated @mock - c(a: SchemaInput = ): CodeScalar + c( + a: SchemaInput = { + e: { + f: ["aaa", "bbb", "ccc", "ddd"] + d: A + c: "ccc" + b: A + a: "aaa" + } + d: A + c: "ccc" + b: A + a: "aaa" + } + ): CodeScalar @mock - d: CodeType + d(a: SchemaInput = {}): CodeType @mock } From c0263807e9d194d506f91b331729ffa1e0963e6b Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 29 Jan 2022 14:52:27 +0400 Subject: [PATCH 68/90] `Printer::getDefinitionListIsUsed()` will correctly work for directives. --- src/SchemaPrinter/Printer.php | 6 ++-- .../PrinterTest~test-settings.graphql | 30 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/SchemaPrinter/Printer.php b/src/SchemaPrinter/Printer.php index e1933b66..68ba5d8f 100644 --- a/src/SchemaPrinter/Printer.php +++ b/src/SchemaPrinter/Printer.php @@ -14,6 +14,7 @@ use function end; use function explode; +use function ltrim; class Printer { protected Settings $settings; @@ -66,7 +67,8 @@ public function print(Schema $schema): PrintedSchema { $settings->isPrintUnusedDirectiveDefinitions(), ); - // todo(graphql): directives in description + // todo(graphql): directives in description for schema + // https://github.com/webonyx/graphql-php/issues/1027 // Return return new PrintedSchema((string) $content); @@ -190,7 +192,7 @@ private function getDefinitionListIsUsed(Block $block, array $used): bool { // prefix before checking. $name = $block instanceof Named ? $block->getName() : ''; $name = explode(' ', $name); - $name = end($name); + $name = ltrim((string) end($name), '@'); return isset($used[$name]); } diff --git a/src/SchemaPrinter/PrinterTest~test-settings.graphql b/src/SchemaPrinter/PrinterTest~test-settings.graphql index 67d1c425..bfc41406 100644 --- a/src/SchemaPrinter/PrinterTest~test-settings.graphql +++ b/src/SchemaPrinter/PrinterTest~test-settings.graphql @@ -192,3 +192,33 @@ union SchemaUnion = | CodeType | SchemaType + +""" +Directive +""" +directive @schemaDirective( + """ + Directive argument + """ + message: String +) +on + | ARGUMENT_DEFINITION + | FIELD + | INPUT_OBJECT + | INTERFACE + | OBJECT + | SCALAR + | SCHEMA + | UNION + +""" +This is unused directives. +""" +directive @schemaDirectiveUnused( + a: SchemaScalarUnused + b: SchemaEnumUnused +) +repeatable on + | OBJECT + | SCALAR From 16699c2606510fab00fb6bed6dae8d4a66cecf94 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 29 Jan 2022 15:00:04 +0400 Subject: [PATCH 69/90] Added normalization support for `ObjectValue`. --- .../Blocks/Ast/ObjectValueList.php | 4 +++ .../Blocks/Ast/ValueNodeTest.php | 34 ++++++++++++++++--- .../PrinterTest~test-settings.graphql | 16 ++++----- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Ast/ObjectValueList.php b/src/SchemaPrinter/Blocks/Ast/ObjectValueList.php index b37760ba..edc47cc3 100644 --- a/src/SchemaPrinter/Blocks/Ast/ObjectValueList.php +++ b/src/SchemaPrinter/Blocks/Ast/ObjectValueList.php @@ -25,4 +25,8 @@ protected function getEmptyValue(): string { protected function isAlwaysMultiline(): bool { return true; } + + protected function isNormalized(): bool { + return $this->getSettings()->isNormalizeArguments(); + } } diff --git a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php index e20937b1..c60bbc63 100644 --- a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php +++ b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php @@ -45,10 +45,13 @@ public function testToString( $parsed = Parser::valueLiteral($actual); self::assertEquals($expected, $actual); - self::assertEquals( - Printer::doPrint($node), - Printer::doPrint($parsed), - ); + + if (!$settings->isNormalizeArguments()) { + self::assertEquals( + Printer::doPrint($node), + Printer::doPrint($parsed), + ); + } } // @@ -58,7 +61,8 @@ public function testToString( * @return array */ public function dataProviderToString(): array { - $settings = new TestSettings(); + $settings = (new TestSettings()) + ->setNormalizeArguments(false); return [ NullValueNode::class => [ @@ -208,6 +212,26 @@ public function dataProviderToString(): array { 0, Parser::valueLiteral('{}'), ], + ObjectValueNode::class.' (normalized)' => [ + <<<'STRING' + { + a: "a" + b: "b" + } + STRING, + $settings + ->setNormalizeArguments(true), + 0, + 0, + Parser::valueLiteral( + <<<'STRING' + { + b: "b" + a: "a" + } + STRING, + ), + ], 'all' => [ <<<'STRING' { diff --git a/src/SchemaPrinter/PrinterTest~test-settings.graphql b/src/SchemaPrinter/PrinterTest~test-settings.graphql index bfc41406..8c846fbc 100644 --- a/src/SchemaPrinter/PrinterTest~test-settings.graphql +++ b/src/SchemaPrinter/PrinterTest~test-settings.graphql @@ -118,17 +118,17 @@ type Query { c( a: SchemaInput = { + a: "aaa" + b: A + c: "ccc" + d: A e: { - f: ["aaa", "bbb", "ccc", "ddd"] - d: A - c: "ccc" - b: A a: "aaa" + b: A + c: "ccc" + d: A + f: ["aaa", "bbb", "ccc", "ddd"] } - d: A - c: "ccc" - b: A - a: "aaa" } ): CodeScalar @mock From 2701c34120f9f0c5a483a3091fa2776f7abc2b8b Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 29 Jan 2022 19:26:08 +0400 Subject: [PATCH 70/90] Lighthouse directives support. --- src/SchemaPrinter/DirectiveResolver.php | 27 ++++++++++++ .../IntrospectionPrinterTest.php | 3 +- src/SchemaPrinter/Printer.php | 42 ++++++++++++++----- src/SchemaPrinter/PrinterTest.php | 3 +- .../PrinterTest~default-settings.graphql | 24 +++++++++++ .../PrinterTest~test-settings.graphql | 29 +++++++++++++ 6 files changed, 116 insertions(+), 12 deletions(-) create mode 100644 src/SchemaPrinter/DirectiveResolver.php diff --git a/src/SchemaPrinter/DirectiveResolver.php b/src/SchemaPrinter/DirectiveResolver.php new file mode 100644 index 00000000..270ca832 --- /dev/null +++ b/src/SchemaPrinter/DirectiveResolver.php @@ -0,0 +1,27 @@ +factory = new DirectiveFactory($this->converter); + } + + public function get(string $name): Directive { + $definition = $this->locator->resolve($name)::definition(); + $directive = $this->factory->handle(ASTHelper::extractDirectiveDefinition($definition)); + + return $directive; + } +} diff --git a/src/SchemaPrinter/IntrospectionPrinterTest.php b/src/SchemaPrinter/IntrospectionPrinterTest.php index 9e0fed07..431f85ab 100644 --- a/src/SchemaPrinter/IntrospectionPrinterTest.php +++ b/src/SchemaPrinter/IntrospectionPrinterTest.php @@ -21,7 +21,8 @@ class IntrospectionPrinterTest extends TestCase { */ public function testPrint(string $expected, Settings $settings, int $level): void { $expected = $this->getTestData()->content($expected); - $printer = (new IntrospectionPrinter())->setSettings($settings)->setLevel($level); + $resolver = $this->app->make(DirectiveResolver::class); + $printer = (new IntrospectionPrinter($resolver))->setSettings($settings)->setLevel($level); $schema = new Schema([]); $actual = $printer->print($schema); diff --git a/src/SchemaPrinter/Printer.php b/src/SchemaPrinter/Printer.php index 68ba5d8f..845cb217 100644 --- a/src/SchemaPrinter/Printer.php +++ b/src/SchemaPrinter/Printer.php @@ -20,7 +20,10 @@ class Printer { protected Settings $settings; protected int $level = 0; - public function __construct(Settings $settings = null) { + public function __construct( + protected DirectiveResolver $directives, + Settings $settings = null, + ) { $this->settings = $settings ?? new DefaultSettings(); } @@ -50,7 +53,7 @@ public function print(Schema $schema): PrintedSchema { $usedDirectives = []; $schemaBlock = $this->getSchema($schema, $usedTypes, $usedDirectives); $typesBlocks = $this->getSchemaTypes($schema, $usedTypes, $usedDirectives); - $directivesBlocks = $this->getSchemaDirectives($schema, $usedTypes, $usedDirectives); + $directivesBlocks = $this->getSchemaDirectives($schema, $usedDirectives, $usedTypes); // Print $settings = $this->getSettings(); @@ -82,7 +85,7 @@ protected function getSchema( Schema $schema, array &$usedTypes = [], array &$usedDirectives = [], - ): Block { + ): DefinitionBlock { return $this->getDefinitionBlock($schema, $usedTypes, $usedDirectives); } @@ -90,7 +93,7 @@ protected function getSchema( * @param array $usedTypes * @param array $usedDirectives * - * @return array + * @return array */ protected function getSchemaTypes(Schema $schema, array &$usedTypes = [], array &$usedDirectives = []): array { $blocks = []; @@ -114,11 +117,11 @@ protected function isSchemaType(Type $type): bool { /** * @param array $usedTypes - * @param array $usedDirectives + * @param array $directives * * @return array */ - protected function getSchemaDirectives(Schema $schema, array &$usedTypes = [], array &$usedDirectives = []): array { + protected function getSchemaDirectives(Schema $schema, array $directives, array &$usedTypes = []): array { // Included? $blocks = []; $settings = $this->getSettings(); @@ -128,15 +131,34 @@ protected function getSchemaDirectives(Schema $schema, array &$usedTypes = [], a return $blocks; } - // Add + // Add directives from Schema + $processed = []; + foreach ($schema->getDirectives() as $directive) { + // Mark + $processed[$directive->name] = true; + // Standard? if (!$this->isSchemaDirective($directive)) { continue; } // Nope - $blocks[] = $this->getDefinitionBlock($directive, $usedTypes, $usedDirectives); + $blocks[] = $this->getDefinitionBlock($directive, $usedTypes); + } + + // Add Lighthouse directives + foreach ($directives as $directive) { + // Processed? + if (isset($processed[$directive])) { + continue; + } + + // Nope + $blocks[] = $this->getDefinitionBlock( + $this->directives->get($directive), + $usedTypes, + ); } // Return @@ -155,8 +177,8 @@ protected function getDefinitionBlock( Schema|Type|Directive $definition, array &$usedTypes = [], array &$usedDirectives = [], - ): Block { - $block = new DefinitionBlock($this->getSettings(), $this->getLevel(), $definition); + ): DefinitionBlock { + $block = new DefinitionBlock($this->getSettings(), $this->getLevel(), $definition); $usedTypes += $block->getUsedTypes(); $usedDirectives += $block->getUsedDirectives(); diff --git a/src/SchemaPrinter/PrinterTest.php b/src/SchemaPrinter/PrinterTest.php index a0e348dd..ec1e1ba9 100644 --- a/src/SchemaPrinter/PrinterTest.php +++ b/src/SchemaPrinter/PrinterTest.php @@ -106,8 +106,9 @@ public static function definition(): string { // Test $expected = $this->getTestData()->content($expected); + $resolver = $this->app->make(DirectiveResolver::class); + $printer = (new Printer($resolver))->setSettings($settings)->setLevel($level); $schema = $this->getGraphQLSchema($this->getTestData()->file('~schema.graphql')); - $printer = (new Printer())->setSettings($settings)->setLevel($level); $actual = $printer->print($schema); self::assertEquals($expected, (string) $actual); diff --git a/src/SchemaPrinter/PrinterTest~default-settings.graphql b/src/SchemaPrinter/PrinterTest~default-settings.graphql index 59bbd4e6..3caccbcf 100644 --- a/src/SchemaPrinter/PrinterTest~default-settings.graphql +++ b/src/SchemaPrinter/PrinterTest~default-settings.graphql @@ -199,6 +199,30 @@ This is unused union. """ union SchemaUnionUnused = SchemaTypeUnused +directive @codeDirective repeatable on SCHEMA | SCALAR | INTERFACE + +""" +Allows you to easily hook up a resolver for an endpoint. +""" +directive @mock( + """ + Specify a unique key for the mock resolver. + """ + key: String = "default" +) +on | FIELD_DEFINITION + +""" +Reference a class implementing a scalar definition. +""" +directive @scalar( + """ + Reference to a class that extends `\GraphQL\Type\Definition\ScalarType`. + """ + class: String! +) +on | SCALAR + """ Directive """ diff --git a/src/SchemaPrinter/PrinterTest~test-settings.graphql b/src/SchemaPrinter/PrinterTest~test-settings.graphql index 8c846fbc..256ed7c0 100644 --- a/src/SchemaPrinter/PrinterTest~test-settings.graphql +++ b/src/SchemaPrinter/PrinterTest~test-settings.graphql @@ -193,6 +193,35 @@ union SchemaUnion | CodeType | SchemaType +directive @codeDirective repeatable on + | INTERFACE + | SCALAR + | SCHEMA + +""" +Allows you to easily hook up a resolver for an endpoint. +""" +directive @mock( + """ + Specify a unique key for the mock resolver. + """ + key: String = "default" +) +on + | FIELD_DEFINITION + +""" +Reference a class implementing a scalar definition. +""" +directive @scalar( + """ + Reference to a class that extends `\GraphQL\Type\Definition\ScalarType`. + """ + class: String! +) +on + | SCALAR + """ Directive """ From b20d364109e7a350c38dc64847cb593d47358a7c Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 29 Jan 2022 19:59:12 +0400 Subject: [PATCH 71/90] Multiline `implements` will be printed on new line. --- .../Types/InterfaceTypeDefinitionBlockTest.php | 15 ++++++++++----- .../Types/ObjectTypeDefinitionBlockTest.php | 15 ++++++++++----- .../Blocks/Types/TypeDefinitionBlock.php | 8 +++++++- .../PrinterTest~test-settings.graphql | 3 ++- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php index 54d6e38c..45bd16a9 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php @@ -227,7 +227,8 @@ interface Test implements B & A ], 'implements(multiline) + directives + fields' => [ <<<'STRING' - interface Test implements + interface Test + implements & B & A @a @@ -255,7 +256,8 @@ interface Test implements ], 'implements(multiline) + fields' => [ <<<'STRING' - interface Test implements + interface Test + implements & B & A { @@ -304,7 +306,8 @@ interface Test implements B & A { ], 'implements(normalized) + fields' => [ <<<'STRING' - interface Test implements + interface Test + implements & A & B { @@ -330,7 +333,8 @@ interface Test implements ], 'indent' => [ <<<'STRING' - interface Test implements + interface Test + implements & A & B { @@ -356,7 +360,8 @@ interface Test implements ], 'implements always multiline' => [ <<<'STRING' - interface Test implements + interface Test + implements & A { a: String diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php index 67d3f956..e5e5c742 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php @@ -226,7 +226,8 @@ public function dataProviderToString(): array { ], 'implements(multiline) + directives + fields' => [ <<<'STRING' - type Test implements + type Test + implements & B & A @a @@ -254,7 +255,8 @@ public function dataProviderToString(): array { ], 'implements(multiline) + fields' => [ <<<'STRING' - type Test implements + type Test + implements & B & A { @@ -303,7 +305,8 @@ public function dataProviderToString(): array { ], 'implements(normalized) + fields' => [ <<<'STRING' - type Test implements + type Test + implements & A & B { @@ -329,7 +332,8 @@ public function dataProviderToString(): array { ], 'indent' => [ <<<'STRING' - type Test implements + type Test + implements & A & B { @@ -355,7 +359,8 @@ public function dataProviderToString(): array { ], 'implements always multiline' => [ <<<'STRING' - type Test implements + type Test + implements & B { a: String diff --git a/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php index 178c74c7..4a947c3b 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php @@ -43,7 +43,13 @@ protected function body(int $used): Block|string|null { ); if (!$interfaces->isEmpty()) { - $interfaces = "{$space}{$interfaces}"; + if ($interfaces->isMultiline()) { + $eol = $this->eol(); + $indent = $this->indent($this->getLevel()); + $interfaces = "{$eol}{$indent}{$interfaces}"; + } else { + $interfaces = "{$space}{$interfaces}"; + } } return $interfaces; diff --git a/src/SchemaPrinter/PrinterTest~test-settings.graphql b/src/SchemaPrinter/PrinterTest~test-settings.graphql index 256ed7c0..9c93fde7 100644 --- a/src/SchemaPrinter/PrinterTest~test-settings.graphql +++ b/src/SchemaPrinter/PrinterTest~test-settings.graphql @@ -137,7 +137,8 @@ type Query { @mock } -type SchemaType implements +type SchemaType +implements & SchemaInterfaceB @schemaDirective { From 341715e63a914754814232b9a744a53be177a728 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 29 Jan 2022 20:17:42 +0400 Subject: [PATCH 72/90] `Settings::isPrintUnusedTypeDefinitions()` and `Settings::isPrintUnusedDirectiveDefinitions()` merged into `Settings::isPrintUnusedDefinitions()`. --- src/SchemaPrinter/IntrospectionPrinter.php | 8 +++---- src/SchemaPrinter/Printer.php | 4 ++-- src/SchemaPrinter/Settings.php | 7 +++--- .../Settings/DefaultSettings.php | 3 +-- .../Settings/ImmutableSettings.php | 24 +++++-------------- .../Package/SchemaPrinter/TestSettings.php | 3 +-- 6 files changed, 17 insertions(+), 32 deletions(-) diff --git a/src/SchemaPrinter/IntrospectionPrinter.php b/src/SchemaPrinter/IntrospectionPrinter.php index c32f7ca2..d3bc7be2 100644 --- a/src/SchemaPrinter/IntrospectionPrinter.php +++ b/src/SchemaPrinter/IntrospectionPrinter.php @@ -11,17 +11,15 @@ * Introspection schema printer. * * Following settings has no effects: - * - {@see Settings::isPrintUnusedTypeDefinitions()} + * - {@see Settings::isPrintUnusedDefinitions()} * - {@see Settings::isPrintDirectiveDefinitions()} - * - {@see Settings::isPrintUnusedDirectiveDefinitions()} */ class IntrospectionPrinter extends Printer { public function setSettings(Settings $settings): static { return parent::setSettings( ImmutableSettings::createFrom($settings) - ->setPrintUnusedTypeDefinitions(true) - ->setPrintDirectiveDefinitions(true) - ->setPrintUnusedDirectiveDefinitions(true) + ->setPrintUnusedDefinitions(true) + ->setPrintDirectiveDefinitions(true), ); } diff --git a/src/SchemaPrinter/Printer.php b/src/SchemaPrinter/Printer.php index 845cb217..51cddf1b 100644 --- a/src/SchemaPrinter/Printer.php +++ b/src/SchemaPrinter/Printer.php @@ -62,12 +62,12 @@ public function print(Schema $schema): PrintedSchema { $content[] = $this->getDefinitionList( $typesBlocks, $usedTypes, - $settings->isPrintUnusedTypeDefinitions(), + $settings->isPrintUnusedDefinitions(), ); $content[] = $this->getDefinitionList( $directivesBlocks, $usedDirectives, - $settings->isPrintUnusedDirectiveDefinitions(), + $settings->isPrintUnusedDefinitions(), ); // todo(graphql): directives in description for schema diff --git a/src/SchemaPrinter/Settings.php b/src/SchemaPrinter/Settings.php index e046ef08..a5b97776 100644 --- a/src/SchemaPrinter/Settings.php +++ b/src/SchemaPrinter/Settings.php @@ -15,8 +15,6 @@ public function getLineEnd(): string; public function getLineLength(): int; - public function isPrintUnusedTypeDefinitions(): bool; - public function isPrintDirectives(): bool; public function isPrintDirectiveDefinitions(): bool; @@ -29,7 +27,10 @@ public function isPrintDirectiveDefinitions(): bool; */ public function isPrintDirectivesInDescription(): bool; - public function isPrintUnusedDirectiveDefinitions(): bool; + /** + * If `false` unused Types and Directives definition will not be printed. + */ + public function isPrintUnusedDefinitions(): bool; /** * If `false` types and directives in the schema will be printed in the diff --git a/src/SchemaPrinter/Settings/DefaultSettings.php b/src/SchemaPrinter/Settings/DefaultSettings.php index 44556f33..2519ec62 100644 --- a/src/SchemaPrinter/Settings/DefaultSettings.php +++ b/src/SchemaPrinter/Settings/DefaultSettings.php @@ -13,8 +13,7 @@ class DefaultSettings extends ImmutableSettings { protected bool $printDirectives = false; protected bool $printDirectiveDefinitions = true; protected bool $printDirectivesInDescription = false; - protected bool $printUnusedTypeDefinitions = true; - protected bool $printUnusedDirectiveDefinitions = true; + protected bool $printUnusedDefinitions = true; protected bool $normalizeSchema = true; protected bool $normalizeUnions = false; protected bool $normalizeEnums = false; diff --git a/src/SchemaPrinter/Settings/ImmutableSettings.php b/src/SchemaPrinter/Settings/ImmutableSettings.php index c1b8aa3a..f52aeb35 100644 --- a/src/SchemaPrinter/Settings/ImmutableSettings.php +++ b/src/SchemaPrinter/Settings/ImmutableSettings.php @@ -15,8 +15,7 @@ abstract class ImmutableSettings implements Settings { protected bool $printDirectives; protected bool $printDirectiveDefinitions; protected bool $printDirectivesInDescription; - protected bool $printUnusedTypeDefinitions; - protected bool $printUnusedDirectiveDefinitions; + protected bool $printUnusedDefinitions; protected bool $normalizeSchema; protected bool $normalizeUnions; protected bool $normalizeEnums; @@ -84,13 +83,13 @@ public function setLineLength(int $value): static { }); } - public function isPrintUnusedTypeDefinitions(): bool { - return $this->printUnusedTypeDefinitions; + public function isPrintUnusedDefinitions(): bool { + return $this->printUnusedDefinitions; } - public function setPrintUnusedTypeDefinitions(bool $value): static { + public function setPrintUnusedDefinitions(bool $value): static { return $this->set(static function (self $settings) use ($value): void { - $settings->printUnusedTypeDefinitions = $value; + $settings->printUnusedDefinitions = $value; }); } @@ -124,16 +123,6 @@ public function setPrintDirectivesInDescription(bool $value): static { }); } - public function isPrintUnusedDirectiveDefinitions(): bool { - return $this->printUnusedDirectiveDefinitions; - } - - public function setPrintUnusedDirectiveDefinitions(bool $value): static { - return $this->set(static function (self $settings) use ($value): void { - $settings->printUnusedDirectiveDefinitions = $value; - }); - } - public function isNormalizeSchema(): bool { return $this->normalizeSchema; } @@ -271,11 +260,10 @@ public static function createFrom(Settings $settings): self { ->setFileEnd($settings->getFileEnd()) ->setLineEnd($settings->getLineEnd()) ->setLineLength($settings->getLineLength()) - ->setPrintUnusedTypeDefinitions($settings->isPrintUnusedTypeDefinitions()) ->setPrintDirectives($settings->isPrintDirectives()) ->setPrintDirectiveDefinitions($settings->isPrintDirectiveDefinitions()) ->setPrintDirectivesInDescription($settings->isPrintDirectivesInDescription()) - ->setPrintUnusedDirectiveDefinitions($settings->isPrintUnusedDirectiveDefinitions()) + ->setPrintUnusedDefinitions($settings->isPrintUnusedDefinitions()) ->setNormalizeSchema($settings->isNormalizeSchema()) ->setNormalizeUnions($settings->isNormalizeUnions()) ->setNormalizeEnums($settings->isNormalizeEnums()) diff --git a/src/Testing/Package/SchemaPrinter/TestSettings.php b/src/Testing/Package/SchemaPrinter/TestSettings.php index 7f036109..a537fc46 100644 --- a/src/Testing/Package/SchemaPrinter/TestSettings.php +++ b/src/Testing/Package/SchemaPrinter/TestSettings.php @@ -16,8 +16,7 @@ class TestSettings extends ImmutableSettings { protected bool $printDirectives = true; protected bool $printDirectiveDefinitions = true; protected bool $printDirectivesInDescription = false; - protected bool $printUnusedTypeDefinitions = false; - protected bool $printUnusedDirectiveDefinitions = false; + protected bool $printUnusedDefinitions = false; protected bool $normalizeSchema = true; protected bool $normalizeUnions = true; protected bool $normalizeEnums = true; From c07858d165c7eb6071a29d1ddb4e16289a55ac9f Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 29 Jan 2022 21:14:21 +0400 Subject: [PATCH 73/90] `BlockList` will implements `\Countable`. --- src/SchemaPrinter/Blocks/BlockList.php | 10 ++++++- src/SchemaPrinter/Blocks/BlockListTest.php | 31 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/SchemaPrinter/Blocks/BlockList.php b/src/SchemaPrinter/Blocks/BlockList.php index 32aa3356..7438c246 100644 --- a/src/SchemaPrinter/Blocks/BlockList.php +++ b/src/SchemaPrinter/Blocks/BlockList.php @@ -3,6 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; use ArrayAccess; +use Countable; use function array_key_last; use function count; @@ -16,7 +17,7 @@ * @template TBlock of Block * @implements ArrayAccess */ -abstract class BlockList extends Block implements ArrayAccess { +abstract class BlockList extends Block implements ArrayAccess, Countable { /** * @var array */ @@ -247,4 +248,11 @@ public function offsetUnset(mixed $offset): void { $this->reset(); } // + + // + // ========================================================================= + public function count() { + return count($this->blocks); + } + // } diff --git a/src/SchemaPrinter/Blocks/BlockListTest.php b/src/SchemaPrinter/Blocks/BlockListTest.php index 89c1f8ea..473d7d84 100644 --- a/src/SchemaPrinter/Blocks/BlockListTest.php +++ b/src/SchemaPrinter/Blocks/BlockListTest.php @@ -37,6 +37,7 @@ public function testToString( string $separator, string $multilineSeparator, array $blocks, + int $count, ): void { $list = new BlockListTest__BlockList( new Dispatcher(), @@ -56,6 +57,7 @@ public function testToString( } self::assertEquals($expected, (string) $list); + self::assertCount($count, $list); } // @@ -85,6 +87,7 @@ public function dataProviderToString(): array { [ new BlockListTest__Block(false, 'block a'), ], + 1, ], 'one multi-line block' => [ <<<'STRING' @@ -102,6 +105,7 @@ public function dataProviderToString(): array { [ new BlockListTest__Block(true, 'block a'), ], + 1, ], 'short block list' => [ <<<'STRING' @@ -120,6 +124,7 @@ public function dataProviderToString(): array { new BlockListTest__Block(false, 'block a'), new BlockListTest__Block(false, 'block b'), ], + 2, ], 'long block list' => [ <<<'STRING' @@ -139,6 +144,7 @@ public function dataProviderToString(): array { new BlockListTest__Block(false, 'block b'), new BlockListTest__Block(false, 'block a'), ], + 2, ], 'short block list with multiline block' => [ <<<'STRING' @@ -159,6 +165,7 @@ public function dataProviderToString(): array { new BlockListTest__Block(false, 'block a'), new BlockListTest__Block(true, 'block b'), ], + 2, ], 'block list with multiline blocks' => [ <<<'STRING' @@ -192,6 +199,7 @@ public function dataProviderToString(): array { new BlockListTest__Block(false, 'block f'), new BlockListTest__Block(true, 'block g'), ], + 7, ], 'block list with multiline blocks without wrap' => [ <<<'STRING' @@ -213,6 +221,7 @@ public function dataProviderToString(): array { new BlockListTest__Block(false, 'block b'), new BlockListTest__Block(true, 'block a'), ], + 3, ], 'normalized block list' => [ <<<'STRING' @@ -231,6 +240,7 @@ public function dataProviderToString(): array { new BlockListTest__Block(false, 'block b'), new BlockListTest__Block(false, 'block a'), ], + 2, ], 'multi-line with level' => [ <<<'STRING' @@ -250,6 +260,7 @@ public function dataProviderToString(): array { new BlockListTest__Block(true, 'block a'), new BlockListTest__Block(true, 'block b'), ], + 2, ], ]), 'named' => new ArrayDataProvider([ @@ -269,6 +280,7 @@ public function dataProviderToString(): array { [ new BlockListTest__NamedBlock('a', false, 'block a'), ], + 1, ], 'one multi-line block' => [ <<<'STRING' @@ -286,6 +298,7 @@ public function dataProviderToString(): array { [ new BlockListTest__NamedBlock('a', true, 'block a'), ], + 1, ], 'short block list' => [ <<<'STRING' @@ -304,6 +317,7 @@ public function dataProviderToString(): array { new BlockListTest__NamedBlock('a', false, 'block a'), new BlockListTest__NamedBlock('b', false, 'block b'), ], + 2, ], 'long block list' => [ <<<'STRING' @@ -323,6 +337,7 @@ public function dataProviderToString(): array { new BlockListTest__NamedBlock('b', false, 'block b'), new BlockListTest__NamedBlock('a', false, 'block a'), ], + 2, ], 'short block list with multiline block' => [ <<<'STRING' @@ -343,6 +358,7 @@ public function dataProviderToString(): array { new BlockListTest__NamedBlock('a', false, 'block a'), new BlockListTest__NamedBlock('b', true, 'block b'), ], + 2, ], 'block list with multiline blocks' => [ <<<'STRING' @@ -376,6 +392,7 @@ public function dataProviderToString(): array { new BlockListTest__NamedBlock('f', false, 'block f'), new BlockListTest__NamedBlock('g', true, 'block g'), ], + 7, ], 'block list with multiline blocks without wrap' => [ <<<'STRING' @@ -397,6 +414,7 @@ public function dataProviderToString(): array { new BlockListTest__NamedBlock('b', false, 'block b'), new BlockListTest__NamedBlock('a', true, 'block a'), ], + 3, ], 'normalized block list' => [ <<<'STRING' @@ -415,6 +433,7 @@ public function dataProviderToString(): array { new BlockListTest__NamedBlock('b', false, 'block b'), new BlockListTest__NamedBlock('a', false, 'block a'), ], + 2, ], 'multi-line with level' => [ <<<'STRING' @@ -434,6 +453,7 @@ public function dataProviderToString(): array { new BlockListTest__NamedBlock('a', true, 'block a'), new BlockListTest__NamedBlock('b', true, 'block b'), ], + 2, ], ]), 'prefix & suffix' => new ArrayDataProvider([ @@ -453,6 +473,7 @@ public function dataProviderToString(): array { [ new BlockListTest__NamedBlock('a', false, 'block a'), ], + 1, ], 'one multi-line block' => [ <<<'STRING' @@ -472,6 +493,7 @@ public function dataProviderToString(): array { [ new BlockListTest__NamedBlock('a', true, 'block a'), ], + 1, ], 'short block list' => [ <<<'STRING' @@ -490,6 +512,7 @@ public function dataProviderToString(): array { new BlockListTest__Block(false, 'block a'), new BlockListTest__NamedBlock('b', false, 'block b'), ], + 2, ], 'long block list' => [ <<<'STRING' @@ -511,6 +534,7 @@ public function dataProviderToString(): array { new BlockListTest__NamedBlock('b', false, 'block b'), new BlockListTest__NamedBlock('a', false, 'block a'), ], + 2, ], 'short block list with multiline block' => [ <<<'STRING' @@ -533,6 +557,7 @@ public function dataProviderToString(): array { new BlockListTest__Block(false, 'block a'), new BlockListTest__Block(true, 'block b'), ], + 2, ], 'multi-line with level' => [ <<<'STRING' @@ -552,6 +577,7 @@ public function dataProviderToString(): array { [ new BlockListTest__Block(true, 'block a'), ], + 1, ], 'empty' => [ '', @@ -565,6 +591,7 @@ public function dataProviderToString(): array { ', ', '', [], + 0, ], ]), 'separators' => new ArrayDataProvider([ @@ -585,6 +612,7 @@ public function dataProviderToString(): array { new BlockListTest__Block(false, 'block a'), new BlockListTest__Block(false, 'block b'), ], + 2, ], 'multiline' => [ <<<'STRING' @@ -604,6 +632,7 @@ public function dataProviderToString(): array { new BlockListTest__Block(false, 'block a'), new BlockListTest__Block(false, 'block b'), ], + 2, ], 'multiline and indent' => [ <<<'STRING' @@ -623,6 +652,7 @@ public function dataProviderToString(): array { new BlockListTest__Block(false, 'block a'), new BlockListTest__Block(false, 'block b'), ], + 2, ], ]), 'empty blocks' => new ArrayDataProvider([ @@ -647,6 +677,7 @@ public function dataProviderToString(): array { new BlockListTest__Block(false, 'block b'), new BlockListTest__Block(false, ''), ], + 2, ], ]), ]))->getData(); From 0cc757ec6b07b5aa65c923f42d3448ab0bd3fca8 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 30 Jan 2022 09:50:29 +0400 Subject: [PATCH 74/90] `Dispatcher` will not be used. --- .../Blocks/Ast/ArgumentNodeList.php | 6 +- .../Blocks/Ast/DirectiveNodeBlock.php | 24 +++--- .../Blocks/Ast/DirectiveNodeBlockTest.php | 35 ++------- .../Blocks/Ast/DirectiveNodeList.php | 6 +- .../Blocks/Ast/DirectiveNodeListTest.php | 44 +++-------- .../Blocks/Ast/ValueNodeBlock.php | 26 +++---- .../Blocks/Ast/ValueNodeTest.php | 3 +- src/SchemaPrinter/Blocks/Block.php | 76 +++++++++++++++--- src/SchemaPrinter/Blocks/BlockList.php | 24 +++++- src/SchemaPrinter/Blocks/BlockListTest.php | 53 +++++++++++-- src/SchemaPrinter/Blocks/BlockTest.php | 9 +-- .../Blocks/Events/DirectiveUsed.php | 14 ---- src/SchemaPrinter/Blocks/Events/Event.php | 10 --- src/SchemaPrinter/Blocks/Events/TypeUsed.php | 14 ---- .../Blocks/Printer/DefinitionBlock.php | 58 +------------- .../Blocks/Printer/DefinitionList.php | 3 +- src/SchemaPrinter/Blocks/Property.php | 9 ++- src/SchemaPrinter/Blocks/PropertyTest.php | 28 +++---- .../Blocks/Types/ArgumentsDefinitionList.php | 5 +- .../Blocks/Types/DefinitionBlock.php | 14 ++-- .../Blocks/Types/Description.php | 4 +- .../Blocks/Types/DescriptionTest.php | 6 +- .../Blocks/Types/DirectiveDefinitionBlock.php | 30 ++++---- .../Types/DirectiveDefinitionBlockTest.php | 31 ++------ .../Blocks/Types/DirectiveLocationBlock.php | 4 +- .../Blocks/Types/DirectiveLocationsList.php | 1 - .../Blocks/Types/EnumTypeDefinitionBlock.php | 17 ++-- .../Types/EnumTypeDefinitionBlockTest.php | 3 +- .../Blocks/Types/EnumValueDefinitionBlock.php | 4 +- .../Types/EnumValueDefinitionBlockTest.php | 3 +- .../Blocks/Types/EnumValuesDefinitionList.php | 5 +- .../Blocks/Types/FieldDefinitionBlock.php | 30 ++++---- .../Blocks/Types/FieldDefinitionBlockTest.php | 36 +++------ .../Blocks/Types/FieldsDefinitionList.php | 5 +- .../Blocks/Types/ImplementsInterfacesList.php | 1 - .../Types/InputFieldsDefinitionList.php | 5 +- .../Types/InputObjectTypeDefinitionBlock.php | 18 ++--- .../InputObjectTypeDefinitionBlockTest.php | 40 +++------- .../Types/InputValueDefinitionBlock.php | 30 ++++---- .../Types/InputValueDefinitionBlockTest.php | 35 +++------ .../Types/InterfaceTypeDefinitionBlock.php | 4 +- .../InterfaceTypeDefinitionBlockTest.php | 58 +++++--------- .../Types/ObjectTypeDefinitionBlock.php | 4 +- .../Types/ObjectTypeDefinitionBlockTest.php | 59 +++++--------- .../RootOperationTypeDefinitionBlock.php | 4 +- .../Types/ScalarTypeDefinitionBlock.php | 4 +- .../Types/ScalarTypeDefinitionBlockTest.php | 3 +- .../Blocks/Types/SchemaDefinitionBlock.php | 9 +-- .../Types/SchemaDefinitionBlockTest.php | 3 +- .../Blocks/Types/StringBlock.php | 4 +- .../Blocks/Types/StringBlockTest.php | 3 +- src/SchemaPrinter/Blocks/Types/TypeBlock.php | 9 +-- .../Blocks/Types/TypeBlockTest.php | 36 +++------ .../Blocks/Types/TypeDefinitionBlock.php | 20 +++-- .../Blocks/Types/UnionMemberTypesList.php | 1 - .../Blocks/Types/UnionTypeDefinitionBlock.php | 17 ++-- .../Types/UnionTypeDefinitionBlockTest.php | 77 +++++++------------ src/SchemaPrinter/Blocks/Types/UsageList.php | 4 +- src/SchemaPrinter/Printer.php | 2 +- src/SchemaPrinter/PrinterTest.php | 6 +- src/SchemaPrinter/Statistics.php | 15 ++++ src/SearchBy/Ast/UsageTest.php | 3 +- 62 files changed, 436 insertions(+), 678 deletions(-) delete mode 100644 src/SchemaPrinter/Blocks/Events/DirectiveUsed.php delete mode 100644 src/SchemaPrinter/Blocks/Events/Event.php delete mode 100644 src/SchemaPrinter/Blocks/Events/TypeUsed.php create mode 100644 src/SchemaPrinter/Statistics.php diff --git a/src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php b/src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php index 343a1511..b55876d8 100644 --- a/src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php +++ b/src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast; use GraphQL\Language\AST\ArgumentNode; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Property; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -18,22 +17,19 @@ class ArgumentNodeList extends BlockList { * @param Traversable|array $arguments */ public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, Traversable|array $arguments, ) { - parent::__construct($dispatcher, $settings, $level, $used); + parent::__construct($settings, $level, $used); foreach ($arguments as $argument) { $name = $argument->name->value; $this[$name] = new Property( - $this->getDispatcher(), $this->getSettings(), $name, new ValueNodeBlock( - $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, $this->getUsed(), diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php index 3b837d00..3d99cd95 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php @@ -3,9 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast; use GraphQL\Language\AST\DirectiveNode; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\DirectiveUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -16,13 +14,12 @@ */ class DirectiveNodeBlock extends Block implements Named { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, private DirectiveNode $node, ) { - parent::__construct($dispatcher, $settings, $level, $used); + parent::__construct($settings, $level, $used); } public function getName(): string { @@ -39,18 +36,17 @@ protected function content(): string { $node = $this->getNode(); $name = $this->getName(); $used = mb_strlen($name) + mb_strlen($at); - $args = new ArgumentNodeList( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel(), - $this->getUsed() + $used, - $node->arguments, + $args = $this->addUsed( + new ArgumentNodeList( + $this->getSettings(), + $this->getLevel(), + $this->getUsed() + $used, + $node->arguments, + ), ); - // Event - $this->getDispatcher()->notify( - new DirectiveUsed($name), - ); + // Statistics + $this->addUsedDirective($name); // Return return "{$at}{$name}{$args}"; diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php index 809ac2f8..76f20b0f 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php @@ -2,17 +2,12 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast; -use Closure; use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\Parser; use GraphQL\Language\Printer; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\DirectiveUsed; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use Mockery; /** * @internal @@ -33,7 +28,7 @@ public function testToString( int $used, DirectiveNode $node, ): void { - $actual = (string) (new DirectiveNodeBlock(new Dispatcher(), $settings, $level, $used, $node)); + $actual = (string) (new DirectiveNodeBlock($settings, $level, $used, $node)); $parsed = Parser::directive($actual); self::assertEquals($expected, $actual); @@ -49,28 +44,14 @@ public function testToString( /** * @covers ::__toString */ - public function testToStringEvent(): void { - $spy = Mockery::spy(static fn (Event $event) => null); - $node = Parser::directive('@test'); - $settings = new TestSettings(); - $dispatcher = new Dispatcher(); + public function testStatistics(): void { + $settings = new TestSettings(); + $node = Parser::directive('@test'); + $block = new DirectiveNodeBlock($settings, 0, 0, $node); - $dispatcher->attach(Closure::fromCallable($spy)); - - self::assertNotEmpty( - (string) (new DirectiveNodeBlock($dispatcher, $settings, 0, 0, $node)), - ); - - $spy - ->shouldHaveBeenCalled() - ->withArgs(static function (Event $event) use ($node): bool { - return $event instanceof DirectiveUsed - && $event->name === $node->name->value; - }) - ->once(); - $spy - ->shouldHaveBeenCalled() - ->once(); + self::assertNotEmpty((string) $block); + self::assertEquals([], $block->getUsedTypes()); + self::assertEquals(['test' => 'test'], $block->getUsedDirectives()); } // diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php index d7ebf74e..9af9d4c9 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php @@ -5,10 +5,10 @@ use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\Parser; use GraphQL\Type\Definition\Directive; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use Traversable; + use function array_filter; use function json_encode; @@ -21,14 +21,13 @@ class DirectiveNodeList extends BlockList { * @param Traversable|array $directives */ public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, Traversable|array|null $directives, string|null $deprecationReason = null, ) { - parent::__construct($dispatcher, $settings, $level, $used); + parent::__construct($settings, $level, $used); $deprecated = Directive::DEPRECATED_NAME; $directives ??= []; @@ -74,7 +73,6 @@ protected function getBlocks(): array { private function block(DirectiveNode $directive,): DirectiveNodeBlock { return new DirectiveNodeBlock( - $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $this->getUsed(), diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php index b58cc9e4..6bf88b88 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php @@ -2,17 +2,12 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast; -use Closure; use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\Parser; use GraphQL\Type\Definition\Directive; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\DirectiveUsed; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use Mockery; /** * @internal @@ -36,7 +31,7 @@ public function testToString( array|null $directives, string $reason = null, ): void { - $actual = (string) (new DirectiveNodeList(new Dispatcher(), $settings, $level, $used, $directives, $reason)); + $actual = (string) (new DirectiveNodeList($settings, $level, $used, $directives, $reason)); Parser::directives($actual); @@ -46,36 +41,15 @@ public function testToString( /** * @covers ::__toString */ - public function testToStringEvent(): void { - $a = Parser::directive('@a'); - $b = Parser::directive('@b'); - $spy = Mockery::spy(static fn (Event $event) => null); - $settings = (new TestSettings())->setPrintDirectives(true); - $dispatcher = new Dispatcher(); + public function testStatistics(): void { + $a = Parser::directive('@a'); + $b = Parser::directive('@b'); + $settings = (new TestSettings())->setPrintDirectives(true); + $block = new DirectiveNodeList($settings, 0, 0, [$a, $b]); - $dispatcher->attach(Closure::fromCallable($spy)); - - self::assertNotEmpty( - (string) (new DirectiveNodeList($dispatcher, $settings, 0, 0, [$a, $b])), - ); - - $spy - ->shouldHaveBeenCalled() - ->withArgs(static function (Event $event) use ($a): bool { - return $event instanceof DirectiveUsed - && $event->name === $a->name->value; - }) - ->once(); - $spy - ->shouldHaveBeenCalled() - ->withArgs(static function (Event $event) use ($b): bool { - return $event instanceof DirectiveUsed - && $event->name === $b->name->value; - }) - ->once(); - $spy - ->shouldHaveBeenCalled() - ->twice(); + self::assertNotEmpty((string) $block); + self::assertEquals([], $block->getUsedTypes()); + self::assertEquals(['a' => 'a', 'b' => 'b'], $block->getUsedDirectives()); } // diff --git a/src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php b/src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php index 39494861..a478f6de 100644 --- a/src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php +++ b/src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php @@ -8,9 +8,7 @@ use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\AST\ValueNode; use GraphQL\Language\Printer; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\ObjectBlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Property; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\StringBlock; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -20,39 +18,35 @@ class ValueNodeBlock extends Block { * @param ValueNode&Node $node */ public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, protected ValueNode $node, ) { - parent::__construct($dispatcher, $settings, $level, $used); + parent::__construct($settings, $level, $used); } protected function content(): string { - $content = ''; - $dispatcher = $this->getDispatcher(); - $settings = $this->getSettings(); - $level = $this->getLevel(); - $used = $this->getUsed(); + $content = ''; + $settings = $this->getSettings(); + $level = $this->getLevel(); + $used = $this->getUsed(); if ($this->node instanceof ListValueNode) { - $content = new ListValueList($dispatcher, $settings, $level, $used); + $content = new ListValueList($settings, $level, $used); foreach ($this->node->values as $value) { - $content[] = new ValueNodeBlock($dispatcher, $settings, $level + 1, $used, $value); + $content[] = new ValueNodeBlock($settings, $level + 1, $used, $value); } } elseif ($this->node instanceof ObjectValueNode) { - $content = new ObjectValueList($dispatcher, $settings, $level, $used); + $content = new ObjectValueList($settings, $level, $used); foreach ($this->node->fields as $field) { $name = $field->name->value; $content[$name] = new Property( - $dispatcher, $settings, $name, new ValueNodeBlock( - $dispatcher, $settings, $level + 1 + (int) ($field->value instanceof StringValueNode), $used, @@ -62,12 +56,12 @@ protected function content(): string { } } elseif ($this->node instanceof StringValueNode) { $content = $this->node->block - ? new StringBlock($dispatcher, $settings, $level, 0, $this->node->value) + ? new StringBlock($settings, $level, 0, $this->node->value) : Printer::doPrint($this->node); } else { $content = Printer::doPrint($this->node); } - return (string) $content; + return (string) $this->addUsed($content); } } diff --git a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php index c60bbc63..8e239c21 100644 --- a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php +++ b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php @@ -15,7 +15,6 @@ use GraphQL\Language\AST\VariableNode; use GraphQL\Language\Parser; use GraphQL\Language\Printer; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -41,7 +40,7 @@ public function testToString( int $used, ValueNode $node, ): void { - $actual = (string) (new ValueNodeBlock(new Dispatcher(), $settings, $level, $used, $node)); + $actual = (string) (new ValueNodeBlock($settings, $level, $used, $node)); $parsed = Parser::valueLiteral($actual); self::assertEquals($expected, $actual); diff --git a/src/SchemaPrinter/Blocks/Block.php b/src/SchemaPrinter/Blocks/Block.php index 42bf9877..3b43cfce 100644 --- a/src/SchemaPrinter/Blocks/Block.php +++ b/src/SchemaPrinter/Blocks/Block.php @@ -3,8 +3,8 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; use Closure; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Statistics; use Stringable; use function mb_strlen; @@ -14,13 +14,22 @@ /** * @internal */ -abstract class Block implements Stringable { +abstract class Block implements Statistics, Stringable { private ?string $content = null; private ?int $length = null; private ?bool $multiline = null; + /** + * @var array + */ + private array $usedTypes = []; + + /** + * @var array + */ + private array $usedDirectives = []; + public function __construct( - private Dispatcher $dispatcher, private Settings $settings, private int $level = 0, private int $used = 0, @@ -30,10 +39,6 @@ public function __construct( // // ========================================================================= - protected function getDispatcher(): Dispatcher { - return $this->dispatcher; - } - protected function getSettings(): Settings { return $this->settings; } @@ -54,11 +59,11 @@ public function isEmpty(): bool { } public function getLength(): int { - return $this->resolve(fn(): int => (int) $this->length); + return $this->resolve(fn (): int => (int) $this->length); } public function isMultiline(): bool { - return $this->resolve(fn(): bool => (bool) $this->multiline); + return $this->resolve(fn (): bool => (bool) $this->multiline); } public function __toString(): string { @@ -93,9 +98,11 @@ protected function getContent(): string { } protected function reset(): void { - $this->multiline = null; - $this->content = null; - $this->length = null; + $this->usedDirectives = []; + $this->usedTypes = []; + $this->multiline = null; + $this->content = null; + $this->length = null; } abstract protected function content(): string; @@ -124,4 +131,49 @@ protected function isStringMultiline(string $string): bool { || mb_strpos($string, "\r") !== false; } // + + // + // ========================================================================= + /** + * @return array + */ + public function getUsedTypes(): array { + return $this->resolve(fn (): array => $this->usedTypes); + } + + /** + * @return array + */ + public function getUsedDirectives(): array { + return $this->resolve(fn (): array => $this->usedDirectives); + } + + /** + * @template T + * + * @param T $block + * + * @return T + */ + protected function addUsed(mixed $block): mixed { + if ($block instanceof Statistics) { + $this->usedTypes += $block->getUsedTypes(); + $this->usedDirectives += $block->getUsedDirectives(); + } + + return $block; + } + + protected function addUsedType(string $type): static { + $this->usedTypes[$type] = $type; + + return $this; + } + + protected function addUsedDirective(string $directive): static { + $this->usedDirectives[$directive] = $directive; + + return $this; + } + // } diff --git a/src/SchemaPrinter/Blocks/BlockList.php b/src/SchemaPrinter/Blocks/BlockList.php index 7438c246..55054911 100644 --- a/src/SchemaPrinter/Blocks/BlockList.php +++ b/src/SchemaPrinter/Blocks/BlockList.php @@ -4,10 +4,10 @@ use ArrayAccess; use Countable; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Statistics; use function array_key_last; use function count; -use function implode; use function mb_strlen; use function strnatcmp; use function usort; @@ -17,7 +17,7 @@ * @template TBlock of Block * @implements ArrayAccess */ -abstract class BlockList extends Block implements ArrayAccess, Countable { +abstract class BlockList extends Block implements Statistics, ArrayAccess, Countable { /** * @var array */ @@ -127,6 +127,7 @@ protected function content(): string { $separator = $this->getMultilineItemPrefix(); foreach ($blocks as $block) { + $block = $this->analyze($block); $multiline = $wrapped && $block->isMultiline(); if (($multiline && $index > 0) || $previous) { @@ -147,7 +148,13 @@ protected function content(): string { $index = $index + 1; } } else { - $content = implode($separator, $blocks); + $last = $count - 1; + $index = 0; + + foreach ($blocks as $block) { + $content .= "{$this->analyze($block)}".($index !== $last ? $separator : ''); + $index = $index + 1; + } } // Prefix & Suffix @@ -189,6 +196,15 @@ private function isMultilineContent( return $this->isLineTooLong($length); } + + /** + * @param TBlock $block + * + * @return TBlock + */ + protected function analyze(Block $block): Block { + return $this->addUsed($block); + } // // @@ -251,7 +267,7 @@ public function offsetUnset(mixed $offset): void { // // ========================================================================= - public function count() { + public function count(): int { return count($this->blocks); } // diff --git a/src/SchemaPrinter/Blocks/BlockListTest.php b/src/SchemaPrinter/Blocks/BlockListTest.php index 473d7d84..4ae299ab 100644 --- a/src/SchemaPrinter/Blocks/BlockListTest.php +++ b/src/SchemaPrinter/Blocks/BlockListTest.php @@ -2,7 +2,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -40,7 +39,6 @@ public function testToString( int $count, ): void { $list = new BlockListTest__BlockList( - new Dispatcher(), $settings, $level, $used, @@ -59,6 +57,21 @@ public function testToString( self::assertEquals($expected, (string) $list); self::assertCount($count, $list); } + + /** + * @covers ::content + * @covers ::analyze + */ + public function testStatistics(): void { + $list = new class(new TestSettings()) extends BlockList { + // empty + }; + $list[] = new BlockListTest__StatisticsBlock(['ta'], ['da']); + $list[] = new BlockListTest__StatisticsBlock(['tb'], ['db']); + + self::assertEquals(['ta' => 'ta', 'tb' => 'tb'], $list->getUsedTypes()); + self::assertEquals(['da' => 'da', 'db' => 'db'], $list->getUsedDirectives()); + } // // @@ -694,7 +707,6 @@ public function dataProviderToString(): array { */ class BlockListTest__BlockList extends BlockList { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, @@ -705,7 +717,7 @@ public function __construct( private string $separator, private string $multilineSeparator, ) { - parent::__construct($dispatcher, $settings, $level, $used); + parent::__construct($settings, $level, $used); } protected function isWrapped(): bool { @@ -742,7 +754,7 @@ public function __construct( protected bool $multiline, protected string $content, ) { - parent::__construct(new Dispatcher(), new TestSettings()); + parent::__construct(new TestSettings()); } protected function getContent(): string { @@ -773,7 +785,6 @@ public function __construct( string $content, ) { parent::__construct( - new Dispatcher(), new TestSettings(), $name, new BlockListTest__Block($multiline, $content), @@ -784,3 +795,33 @@ public function getName(): string { return $this->name; } } + +/** + * @internal + * @noinspection PhpMultipleClassesDeclarationsInOneFile + */ +class BlockListTest__StatisticsBlock extends Block { + /** + * @param array $types + * @param array $directives + */ + public function __construct(array $types, array $directives) { + parent::__construct(new TestSettings()); + + foreach ($types as $type) { + $this->addUsedType($type); + } + + foreach ($directives as $directive) { + $this->addUsedDirective($directive); + } + } + + public function isEmpty(): bool { + return false; + } + + protected function content(): string { + return ''; + } +} diff --git a/src/SchemaPrinter/Blocks/BlockTest.php b/src/SchemaPrinter/Blocks/BlockTest.php index 64d43273..a8ea2647 100644 --- a/src/SchemaPrinter/Blocks/BlockTest.php +++ b/src/SchemaPrinter/Blocks/BlockTest.php @@ -2,7 +2,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -22,7 +21,7 @@ class BlockTest extends TestCase { */ public function testGetContent(): void { $content = 'content'; - $block = Mockery::mock(BlockTest__Block::class, [new Dispatcher(), new TestSettings()]); + $block = Mockery::mock(BlockTest__Block::class, [new TestSettings()]); $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); $block @@ -40,7 +39,7 @@ public function testGetContent(): void { public function testGetLength(): void { $content = 'content'; $length = mb_strlen($content); - $block = Mockery::mock(BlockTest__Block::class, [new Dispatcher(), new TestSettings()]); + $block = Mockery::mock(BlockTest__Block::class, [new TestSettings()]); $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); $block @@ -58,7 +57,7 @@ public function testGetLength(): void { * @dataProvider dataProviderIsMultiline */ public function testIsMultiline(bool $expected, Settings $settings, string $content): void { - $block = Mockery::mock(BlockTest__Block::class, [new Dispatcher(), $settings]); + $block = Mockery::mock(BlockTest__Block::class, [$settings]); $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); $block @@ -76,7 +75,7 @@ public function testIsMultiline(bool $expected, Settings $settings, string $cont * @dataProvider dataProviderIsEmpty */ public function testIsEmpty(bool $expected, string $content): void { - $block = Mockery::mock(BlockTest__Block::class, [new Dispatcher(), new TestSettings()]); + $block = Mockery::mock(BlockTest__Block::class, [new TestSettings()]); $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); $block diff --git a/src/SchemaPrinter/Blocks/Events/DirectiveUsed.php b/src/SchemaPrinter/Blocks/Events/DirectiveUsed.php deleted file mode 100644 index 18af398a..00000000 --- a/src/SchemaPrinter/Blocks/Events/DirectiveUsed.php +++ /dev/null @@ -1,14 +0,0 @@ - - */ - private array $usedTypes = []; - - /** - * @var array - */ - private array $usedDirectives = []; - public function __construct( Settings $settings, int $level, Schema|Type|Directive $definition, ) { - $dispatcher = new Dispatcher(); - $dispatcher->attach(function (Event $event): void { - if ($event instanceof DirectiveUsed) { - $this->usedDirectives[$event->name] = $event->name; - } - - if ($event instanceof TypeUsed) { - $this->usedTypes[$event->name] = $event->name; - } - }); - - parent::__construct($dispatcher, $settings, $level); + parent::__construct($settings, $level); $this->block = $this->getDefinitionBlock($definition); } @@ -76,33 +51,12 @@ public function getName(): string { return $name; } - /** - * @return array - */ - public function getUsedTypes(): array { - return $this->resolve(fn (): array => $this->usedTypes); - } - - /** - * @return array - */ - public function getUsedDirectives(): array { - return $this->resolve(fn (): array => $this->usedDirectives); - } - protected function getBlock(): Block { return $this->block; } - protected function reset(): void { - $this->usedTypes = []; - $this->usedDirectives = []; - - parent::reset(); - } - protected function content(): string { - return (string) $this->getBlock(); + return (string) $this->addUsed($this->getBlock()); } protected function getDefinitionBlock(Schema|Type|Directive $definition): Block { @@ -110,7 +64,6 @@ protected function getDefinitionBlock(Schema|Type|Directive $definition): Block if ($definition instanceof ObjectType) { $block = new ObjectTypeDefinitionBlock( - $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $this->getUsed(), @@ -118,7 +71,6 @@ protected function getDefinitionBlock(Schema|Type|Directive $definition): Block ); } elseif ($definition instanceof InputObjectType) { $block = new InputObjectTypeDefinitionBlock( - $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $this->getUsed(), @@ -126,7 +78,6 @@ protected function getDefinitionBlock(Schema|Type|Directive $definition): Block ); } elseif ($definition instanceof ScalarType) { $block = new ScalarTypeDefinitionBlock( - $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $this->getUsed(), @@ -134,7 +85,6 @@ protected function getDefinitionBlock(Schema|Type|Directive $definition): Block ); } elseif ($definition instanceof InterfaceType) { $block = new InterfaceTypeDefinitionBlock( - $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $this->getUsed(), @@ -142,7 +92,6 @@ protected function getDefinitionBlock(Schema|Type|Directive $definition): Block ); } elseif ($definition instanceof UnionType) { $block = new UnionTypeDefinitionBlock( - $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $this->getUsed(), @@ -150,7 +99,6 @@ protected function getDefinitionBlock(Schema|Type|Directive $definition): Block ); } elseif ($definition instanceof EnumType) { $block = new EnumTypeDefinitionBlock( - $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $this->getUsed(), @@ -158,7 +106,6 @@ protected function getDefinitionBlock(Schema|Type|Directive $definition): Block ); } elseif ($definition instanceof Directive) { $block = new DirectiveDefinitionBlock( - $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $this->getUsed(), @@ -166,7 +113,6 @@ protected function getDefinitionBlock(Schema|Type|Directive $definition): Block ); } elseif ($definition instanceof Schema) { $block = new SchemaDefinitionBlock( - $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $this->getUsed(), diff --git a/src/SchemaPrinter/Blocks/Printer/DefinitionList.php b/src/SchemaPrinter/Blocks/Printer/DefinitionList.php index 9f155a14..7237594e 100644 --- a/src/SchemaPrinter/Blocks/Printer/DefinitionList.php +++ b/src/SchemaPrinter/Blocks/Printer/DefinitionList.php @@ -2,7 +2,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Printer; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -17,7 +16,7 @@ public function __construct( int $level, protected bool $schema = false, ) { - parent::__construct(new Dispatcher(), $settings, $level); + parent::__construct($settings, $level); } protected function isWrapped(): bool { diff --git a/src/SchemaPrinter/Blocks/Property.php b/src/SchemaPrinter/Blocks/Property.php index b34b428d..9ee7cb40 100644 --- a/src/SchemaPrinter/Blocks/Property.php +++ b/src/SchemaPrinter/Blocks/Property.php @@ -2,7 +2,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; /** @@ -15,12 +14,11 @@ class Property extends Block implements Named { * @param TBlock $block */ public function __construct( - Dispatcher $dispatcher, Settings $settings, private string $name, private Block $block, ) { - parent::__construct($dispatcher, $settings, $block->getLevel(), $block->getUsed()); + parent::__construct($settings, $block->getLevel(), $block->getUsed()); } public function getName(): string { @@ -43,6 +41,9 @@ protected function getSeparator(): string { } protected function content(): string { - return "{$this->getName()}{$this->getSeparator()}{$this->space()}{$this->getBlock()}"; + $block = $this->addUsed($this->getBlock()); + $content = "{$this->getName()}{$this->getSeparator()}{$this->space()}{$block}"; + + return $content; } } diff --git a/src/SchemaPrinter/Blocks/PropertyTest.php b/src/SchemaPrinter/Blocks/PropertyTest.php index 61a41dee..1478cfdd 100644 --- a/src/SchemaPrinter/Blocks/PropertyTest.php +++ b/src/SchemaPrinter/Blocks/PropertyTest.php @@ -2,7 +2,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -21,38 +20,35 @@ class PropertyTest extends TestCase { * @covers ::getUsed */ public function testToString(): void { - $name = 'name'; - $used = 123; - $level = 2; - $space = ' '; - $separator = ':'; - $content = 'abc abcabc abcabc abcabc abc'; - $settings = (new TestSettings())->setSpace($space); - $dispatcher = new Dispatcher(); - $block = new class($dispatcher, $settings, $level, $used, $content) extends Block { + $name = 'name'; + $used = 123; + $level = 2; + $space = ' '; + $separator = ':'; + $content = 'abc abcabc abcabc abcabc abc'; + $settings = (new TestSettings())->setSpace($space); + $block = new class($settings, $level, $used, $content) extends Block { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, protected string $content, ) { - parent::__construct($dispatcher, $settings, $level, $used); + parent::__construct($settings, $level, $used); } protected function content(): string { return $this->content; } }; - $property = new class($dispatcher, $settings, $name, $block, $separator) extends Property { + $property = new class($settings, $name, $block, $separator) extends Property { public function __construct( - Dispatcher $dispatcher, Settings $settings, string $name, Block $block, private string $separator, ) { - parent::__construct($dispatcher, $settings, $name, $block); + parent::__construct($settings, $name, $block); } public function getUsed(): int { @@ -67,7 +63,7 @@ protected function getSeparator(): string { return $this->separator; } }; - $expected = "{$name}{$separator}{$space}{$content}"; + $expected = "{$name}{$separator}{$space}{$content}"; self::assertEquals($used, $property->getUsed()); self::assertEquals($level, $property->getLevel()); diff --git a/src/SchemaPrinter/Blocks/Types/ArgumentsDefinitionList.php b/src/SchemaPrinter/Blocks/Types/ArgumentsDefinitionList.php index 25010c0b..b24556a5 100644 --- a/src/SchemaPrinter/Blocks/Types/ArgumentsDefinitionList.php +++ b/src/SchemaPrinter/Blocks/Types/ArgumentsDefinitionList.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\FieldArgument; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use Traversable; @@ -17,17 +16,15 @@ class ArgumentsDefinitionList extends BlockList { * @param Traversable|array $arguments */ public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, Traversable|array $arguments, ) { - parent::__construct($dispatcher, $settings, $level, $used); + parent::__construct($settings, $level, $used); foreach ($arguments as $argument) { $this[$argument->name] = new InputValueDefinitionBlock( - $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, $this->getUsed(), diff --git a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php index 0c995566..03789554 100644 --- a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php @@ -11,7 +11,6 @@ use GraphQL\Type\Definition\InputObjectField; use GraphQL\Type\Definition\Type; use GraphQL\Type\Schema; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; @@ -29,13 +28,12 @@ abstract class DefinitionBlock extends Block implements Named { * @param TType $definition */ public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, private Type|FieldDefinition|EnumValueDefinition|FieldArgument|Directive|InputObjectField|Schema $definition, ) { - parent::__construct($dispatcher, $settings, $level, $used); + parent::__construct($settings, $level, $used); } public function getName(): string { @@ -69,12 +67,12 @@ protected function content(): string { $indent = $this->indent(); $name = $this->getName(); $used = $this->getUsed() + mb_strlen($name) + mb_strlen($space); - $body = (string) $this->body($used); - $fields = (string) $this->fields($used + mb_strlen($body)); + $body = (string) $this->addUsed($this->body($used)); + $fields = (string) $this->addUsed($this->fields($used + mb_strlen($body))); $directives = $this->directives(); - $description = (string) $this->description($directives); + $description = (string) $this->addUsed($this->description($directives)); $directives = $this->getSettings()->isPrintDirectives() - ? (string) $directives + ? (string) $this->addUsed($directives) : ''; $content = ''; @@ -117,7 +115,6 @@ abstract protected function fields(int $used): Block|string|null; protected function directives(): DirectiveNodeList { $definition = $this->getDefinition(); $directives = new DirectiveNodeList( - $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $this->getUsed(), @@ -141,7 +138,6 @@ protected function description(DirectiveNodeList $directives): ?Description { } return new Description( - $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $this->getUsed(), diff --git a/src/SchemaPrinter/Blocks/Types/Description.php b/src/SchemaPrinter/Blocks/Types/Description.php index 2d0d9e25..b6361e67 100644 --- a/src/SchemaPrinter/Blocks/Types/Description.php +++ b/src/SchemaPrinter/Blocks/Types/Description.php @@ -2,7 +2,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -16,14 +15,13 @@ */ class Description extends StringBlock { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, ?string $string, private ?DirectiveNodeList $directives = null, ) { - parent::__construct($dispatcher, $settings, $level, $used, (string) $string); + parent::__construct($settings, $level, $used, (string) $string); } protected function getDirectives(): ?DirectiveNodeList { diff --git a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php index 87e26a5a..b749625c 100644 --- a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php +++ b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php @@ -4,7 +4,6 @@ use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\Parser; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; @@ -34,9 +33,8 @@ public function testToString( ?string $description, ?array $directives, ): void { - $dispatcher = new Dispatcher(); - $directives = new DirectiveNodeList($dispatcher, $settings, $level, $used, $directives); - $actual = (string) (new Description($dispatcher, $settings, $level, $used, $description, $directives)); + $directives = new DirectiveNodeList($settings, $level, $used, $directives); + $actual = (string) (new Description($settings, $level, $used, $description, $directives)); self::assertEquals($expected, $actual); diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlock.php index 354528e5..596f72f5 100644 --- a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlock.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\Directive; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -16,13 +15,12 @@ */ class DirectiveDefinitionBlock extends DefinitionBlock { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, Directive $definition, ) { - parent::__construct($dispatcher, $settings, $level, $used, $definition); + parent::__construct($settings, $level, $used, $definition); } protected function type(): string|null { @@ -40,19 +38,21 @@ protected function body(int $used): Block|string|null { $indent = $this->indent(); $repeatable = 'repeatable'; $used = $used + mb_strlen($repeatable) + 2 * mb_strlen($space); - $args = new ArgumentsDefinitionList( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel(), - $used, - $definition->args, + $args = $this->addUsed( + new ArgumentsDefinitionList( + $this->getSettings(), + $this->getLevel(), + $used, + $definition->args, + ), ); - $locations = new DirectiveLocationsList( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel() + 1, - $used + $args->getLength(), - $definition->locations, + $locations = $this->addUsed( + new DirectiveLocationsList( + $this->getSettings(), + $this->getLevel() + 1, + $used + $args->getLength(), + $definition->locations, + ), ); $content = "{$args}"; diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php index d6e12f77..61b62d6f 100644 --- a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php @@ -2,19 +2,14 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; -use Closure; use GraphQL\Language\DirectiveLocation; use GraphQL\Language\Parser; use GraphQL\Type\Definition\Directive; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use Mockery; /** * @internal @@ -35,7 +30,7 @@ public function testToString( int $used, Directive $definition, ): void { - $actual = (string) (new DirectiveDefinitionBlock(new Dispatcher(), $settings, $level, $used, $definition)); + $actual = (string) (new DirectiveDefinitionBlock($settings, $level, $used, $definition)); Parser::directiveDefinition($actual); @@ -45,10 +40,8 @@ public function testToString( /** * @covers ::__toString */ - public function testToStringEvent(): void { - $spy = Mockery::spy(static fn (Event $event) => null); + public function testStatistics(): void { $settings = new TestSettings(); - $dispatcher = new Dispatcher(); $definition = new Directive([ 'name' => 'A', 'args' => [ @@ -60,23 +53,11 @@ public function testToStringEvent(): void { DirectiveLocation::FIELD, ], ]); + $block = new DirectiveDefinitionBlock($settings, 0, 0, $definition); - $dispatcher->attach(Closure::fromCallable($spy)); - - self::assertNotEmpty( - (string) (new DirectiveDefinitionBlock($dispatcher, $settings, 0, 0, $definition)), - ); - - $spy - ->shouldHaveBeenCalled() - ->withArgs(static function (Event $event): bool { - return $event instanceof TypeUsed - && $event->name === 'B'; - }) - ->once(); - $spy - ->shouldHaveBeenCalled() - ->once(); + self::assertNotEmpty((string) $block); + self::assertEquals(['B' => 'B'], $block->getUsedTypes()); + self::assertEquals([], $block->getUsedDirectives()); } // diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveLocationBlock.php b/src/SchemaPrinter/Blocks/Types/DirectiveLocationBlock.php index 6a168467..e0d47d03 100644 --- a/src/SchemaPrinter/Blocks/Types/DirectiveLocationBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DirectiveLocationBlock.php @@ -2,7 +2,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -12,13 +11,12 @@ */ class DirectiveLocationBlock extends Block implements Named { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, private string $location, ) { - parent::__construct($dispatcher, $settings, $level, $used); + parent::__construct($settings, $level, $used); } public function getName(): string { diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveLocationsList.php b/src/SchemaPrinter/Blocks/Types/DirectiveLocationsList.php index 2133e009..d0b4d5cf 100644 --- a/src/SchemaPrinter/Blocks/Types/DirectiveLocationsList.php +++ b/src/SchemaPrinter/Blocks/Types/DirectiveLocationsList.php @@ -11,7 +11,6 @@ class DirectiveLocationsList extends UsageList { protected function block(mixed $item): Block { return new DirectiveLocationBlock( - $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, $this->getUsed(), diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php index 51365bb4..2d61aac2 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\EnumType; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -16,13 +15,12 @@ */ class EnumTypeDefinitionBlock extends DefinitionBlock { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, EnumType $definition, ) { - parent::__construct($dispatcher, $settings, $level, $used, $definition); + parent::__construct($settings, $level, $used, $definition); } protected function type(): string { @@ -31,12 +29,13 @@ protected function type(): string { protected function body(int $used): Block|string|null { $space = $this->space(); - $values = new EnumValuesDefinitionList( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel(), - $used + mb_strlen($space), - $this->getDefinition()->getValues(), + $values = $this->addUsed( + new EnumValuesDefinitionList( + $this->getSettings(), + $this->getLevel(), + $used + mb_strlen($space), + $this->getDefinition()->getValues(), + ), ); return "{$space}{$values}"; diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php index fd459d5a..483a902b 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php @@ -7,7 +7,6 @@ use GraphQL\Type\Definition\Directive; use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\EnumValueDefinition; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -36,7 +35,7 @@ public function testToString( $type = $type(); } - $actual = (string) (new EnumTypeDefinitionBlock(new Dispatcher(), $settings, $level, $used, $type)); + $actual = (string) (new EnumTypeDefinitionBlock($settings, $level, $used, $type)); Parser::enumTypeDefinition($actual); diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php index 42a5d5a7..bc6f15fd 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\EnumValueDefinition; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -13,13 +12,12 @@ */ class EnumValueDefinitionBlock extends DefinitionBlock { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, EnumValueDefinition $value, ) { - parent::__construct($dispatcher, $settings, $level, $used, $value); + parent::__construct($settings, $level, $used, $value); } protected function type(): string|null { diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php index 675f7ae8..6577a9fa 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php @@ -4,7 +4,6 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\EnumValueDefinition; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -28,7 +27,7 @@ public function testToString( int $used, EnumValueDefinition $type, ): void { - $actual = (string) (new EnumValueDefinitionBlock(new Dispatcher(), $settings, $level, $used, $type)); + $actual = (string) (new EnumValueDefinitionBlock($settings, $level, $used, $type)); Parser::enumValueDefinition($actual); diff --git a/src/SchemaPrinter/Blocks/Types/EnumValuesDefinitionList.php b/src/SchemaPrinter/Blocks/Types/EnumValuesDefinitionList.php index a3b08e1a..3d39622b 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValuesDefinitionList.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValuesDefinitionList.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\EnumValueDefinition; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\ObjectBlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use Traversable; @@ -17,17 +16,15 @@ class EnumValuesDefinitionList extends ObjectBlockList { * @param Traversable|array $values */ public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, Traversable|array $values, ) { - parent::__construct($dispatcher, $settings, $level, $used); + parent::__construct($settings, $level, $used); foreach ($values as $value) { $this[$value->name] = new EnumValueDefinitionBlock( - $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, $this->getUsed(), diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php index 281f10ea..7feccf51 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\FieldDefinition; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -14,13 +13,12 @@ */ class FieldDefinitionBlock extends DefinitionBlock { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, FieldDefinition $definition, ) { - parent::__construct($dispatcher, $settings, $level, $used, $definition); + parent::__construct($settings, $level, $used, $definition); } protected function type(): string|null { @@ -30,19 +28,21 @@ protected function type(): string|null { protected function body(int $used): Block|string|null { $definition = $this->getDefinition(); $space = $this->space(); - $type = new TypeBlock( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel(), - $this->getUsed(), - $definition->getType(), + $type = $this->addUsed( + new TypeBlock( + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $definition->getType(), + ), ); - $args = new ArgumentsDefinitionList( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel(), - $this->getUsed(), - $definition->args, + $args = $this->addUsed( + new ArgumentsDefinitionList( + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $definition->args, + ), ); return "{$args}:{$space}{$type}"; diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php index 015d598b..f0922c44 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php @@ -2,20 +2,15 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; -use Closure; use GraphQL\Language\Parser; use GraphQL\Type\Definition\FieldDefinition; use GraphQL\Type\Definition\ListOfType; use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use Mockery; /** * @internal @@ -36,7 +31,7 @@ public function testToString( int $used, FieldDefinition $definition, ): void { - $actual = (string) (new FieldDefinitionBlock(new Dispatcher(), $settings, $level, $used, $definition)); + $actual = (string) (new FieldDefinitionBlock($settings, $level, $used, $definition)); Parser::fieldDefinition($actual); @@ -46,35 +41,22 @@ public function testToString( /** * @covers ::__toString */ - public function testToStringEvent(): void { - $spy = Mockery::spy(static fn (Event $event) => null); + public function testStatistics(): void { $settings = new TestSettings(); - $dispatcher = new Dispatcher(); $definition = FieldDefinition::create([ - 'name' => 'A', - 'type' => new NonNull( + 'name' => 'A', + 'type' => new NonNull( new ObjectType([ 'name' => 'A', ]), ), + 'astNode' => Parser::fieldDefinition('a: A @a'), ]); + $block = new FieldDefinitionBlock($settings, 0, 0, $definition); - $dispatcher->attach(Closure::fromCallable($spy)); - - self::assertNotEmpty( - (string) (new FieldDefinitionBlock($dispatcher, $settings, 0, 0, $definition)), - ); - - $spy - ->shouldHaveBeenCalled() - ->withArgs(static function (Event $event): bool { - return $event instanceof TypeUsed - && $event->name === 'A'; - }) - ->once(); - $spy - ->shouldHaveBeenCalled() - ->once(); + self::assertNotEmpty((string) $block); + self::assertEquals(['A' => 'A'], $block->getUsedTypes()); + self::assertEquals(['a' => 'a'], $block->getUsedDirectives()); } // diff --git a/src/SchemaPrinter/Blocks/Types/FieldsDefinitionList.php b/src/SchemaPrinter/Blocks/Types/FieldsDefinitionList.php index 2f0a0e3d..404bf651 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldsDefinitionList.php +++ b/src/SchemaPrinter/Blocks/Types/FieldsDefinitionList.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\FieldDefinition; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use Traversable; @@ -17,17 +16,15 @@ class FieldsDefinitionList extends BlockList { * @param Traversable|array $fields */ public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, Traversable|array $fields, ) { - parent::__construct($dispatcher, $settings, $level, $used); + parent::__construct($settings, $level, $used); foreach ($fields as $field) { $this[$field->name] = new FieldDefinitionBlock( - $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, $this->getUsed(), diff --git a/src/SchemaPrinter/Blocks/Types/ImplementsInterfacesList.php b/src/SchemaPrinter/Blocks/Types/ImplementsInterfacesList.php index f58c40ea..83ce6a4d 100644 --- a/src/SchemaPrinter/Blocks/Types/ImplementsInterfacesList.php +++ b/src/SchemaPrinter/Blocks/Types/ImplementsInterfacesList.php @@ -12,7 +12,6 @@ class ImplementsInterfacesList extends UsageList { protected function block(mixed $item): Block { return new TypeBlock( - $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, $this->getUsed(), diff --git a/src/SchemaPrinter/Blocks/Types/InputFieldsDefinitionList.php b/src/SchemaPrinter/Blocks/Types/InputFieldsDefinitionList.php index 1d0f3f22..be6df53a 100644 --- a/src/SchemaPrinter/Blocks/Types/InputFieldsDefinitionList.php +++ b/src/SchemaPrinter/Blocks/Types/InputFieldsDefinitionList.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\InputObjectField; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use Traversable; @@ -17,17 +16,15 @@ class InputFieldsDefinitionList extends BlockList { * @param Traversable|array $fields */ public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, Traversable|array $fields, ) { - parent::__construct($dispatcher, $settings, $level, $used); + parent::__construct($settings, $level, $used); foreach ($fields as $field) { $this[$field->name] = new InputValueDefinitionBlock( - $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, $this->getUsed(), diff --git a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php index 8c6e5904..aee21928 100644 --- a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php @@ -3,9 +3,9 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\InputObjectType; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; + use function mb_strlen; /** @@ -15,13 +15,12 @@ */ class InputObjectTypeDefinitionBlock extends DefinitionBlock { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, InputObjectType $definition, ) { - parent::__construct($dispatcher, $settings, $level, $used, $definition); + parent::__construct($settings, $level, $used, $definition); } protected function type(): string|null { @@ -35,12 +34,13 @@ protected function body(int $used): Block|string|null { protected function fields(int $used): Block|string|null { $definition = $this->getDefinition(); $space = $this->space(); - $fields = new InputFieldsDefinitionList( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel(), - $used + mb_strlen($space), - $definition->getFields(), + $fields = $this->addUsed( + new InputFieldsDefinitionList( + $this->getSettings(), + $this->getLevel(), + $used + mb_strlen($space), + $definition->getFields(), + ), ); return $fields; diff --git a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php index 14aadb26..25c20a05 100644 --- a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php @@ -2,17 +2,12 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; -use Closure; use GraphQL\Language\Parser; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\Type; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use Mockery; /** * @internal @@ -34,7 +29,6 @@ public function testToString( InputObjectType $definition, ): void { $actual = (string) (new InputObjectTypeDefinitionBlock( - new Dispatcher(), $settings, $level, $used, @@ -49,38 +43,26 @@ public function testToString( /** * @covers ::__toString */ - public function testToStringEvent(): void { - $spy = Mockery::spy(static fn (Event $event) => null); + public function testStatistics(): void { $settings = new TestSettings(); - $dispatcher = new Dispatcher(); $definition = new InputObjectType([ - 'name' => 'A', - 'fields' => [ + 'name' => 'A', + 'fields' => [ 'b' => [ - 'name' => 'b', - 'type' => new InputObjectType([ + 'name' => 'b', + 'type' => new InputObjectType([ 'name' => 'B', ]), + 'astNode' => Parser::fieldDefinition('b: B @a'), ], ], + 'astNode' => Parser::inputObjectTypeDefinition('input A @b'), ]); + $block = new InputObjectTypeDefinitionBlock($settings, 0, 0, $definition); - $dispatcher->attach(Closure::fromCallable($spy)); - - self::assertNotEmpty( - (string) (new InputObjectTypeDefinitionBlock($dispatcher, $settings, 0, 0, $definition)), - ); - - $spy - ->shouldHaveBeenCalled() - ->withArgs(static function (Event $event): bool { - return $event instanceof TypeUsed - && $event->name === 'B'; - }) - ->once(); - $spy - ->shouldHaveBeenCalled() - ->once(); + self::assertNotEmpty((string) $block); + self::assertEquals(['B' => 'B'], $block->getUsedTypes()); + self::assertEquals(['a' => 'a', 'b' => 'b'], $block->getUsedDirectives()); } // diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php index d96f9ccb..a8fa29e5 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php @@ -6,7 +6,6 @@ use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\InputObjectField; use GraphQL\Utils\AST; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\ValueNodeBlock; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -20,13 +19,12 @@ */ class InputValueDefinitionBlock extends DefinitionBlock { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, FieldArgument|InputObjectField $definition, ) { - parent::__construct($dispatcher, $settings, $level, $used, $definition); + parent::__construct($settings, $level, $used, $definition); } protected function type(): string|null { @@ -36,23 +34,25 @@ protected function type(): string|null { protected function body(int $used): Block|string|null { $definition = $this->getDefinition(); $space = $this->space(); - $type = new TypeBlock( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel(), - $this->getUsed(), - $definition->getType(), + $type = $this->addUsed( + new TypeBlock( + $this->getSettings(), + $this->getLevel(), + $this->getUsed(), + $definition->getType(), + ), ); $body = ":{$space}{$type}"; if ($definition->defaultValueExists()) { $prefix = "{$body}{$space}={$space}"; - $value = new ValueNodeBlock( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel(), - $this->getUsed() + mb_strlen($prefix), - AST::astFromValue($definition->defaultValue, $definition->getType()) ?? new NullValueNode([]), + $value = $this->addUsed( + new ValueNodeBlock( + $this->getSettings(), + $this->getLevel(), + $this->getUsed() + mb_strlen($prefix), + AST::astFromValue($definition->defaultValue, $definition->getType()) ?? new NullValueNode([]), + ), ); $body = "{$prefix}{$value}"; } diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php index 7591bdc7..2d49bea7 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php @@ -2,20 +2,15 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; -use Closure; use GraphQL\Language\Parser; use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\ListOfType; use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use Mockery; /** * @internal @@ -36,7 +31,7 @@ public function testToString( int $used, FieldArgument $definition, ): void { - $actual = (string) (new InputValueDefinitionBlock(new Dispatcher(), $settings, $level, $used, $definition)); + $actual = (string) (new InputValueDefinitionBlock($settings, $level, $used, $definition)); Parser::inputValueDefinition($actual); @@ -46,35 +41,23 @@ public function testToString( /** * @covers ::__toString */ - public function testToStringEvent(): void { - $spy = Mockery::spy(static fn (Event $event) => null); + public function testStatistics(): void { $settings = new TestSettings(); - $dispatcher = new Dispatcher(); $definition = new FieldArgument([ - 'name' => 'A', - 'type' => new NonNull( + 'name' => 'a', + 'type' => new NonNull( new ObjectType([ 'name' => 'A', ]), ), + 'astNode' => Parser::inputValueDefinition('test: Test! @a'), ]); - $dispatcher->attach(Closure::fromCallable($spy)); + $block = new InputValueDefinitionBlock($settings, 0, 0, $definition); - self::assertNotEmpty( - (string) (new InputValueDefinitionBlock($dispatcher, $settings, 0, 0, $definition)), - ); - - $spy - ->shouldHaveBeenCalled() - ->withArgs(static function (Event $event): bool { - return $event instanceof TypeUsed - && $event->name === 'A'; - }) - ->once(); - $spy - ->shouldHaveBeenCalled() - ->once(); + self::assertNotEmpty((string) $block); + self::assertEquals(['A' => 'A'], $block->getUsedTypes()); + self::assertEquals(['a' => 'a'], $block->getUsedDirectives()); } // diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php index c8ec8f34..d948be72 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\InterfaceType; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; /** @@ -13,13 +12,12 @@ */ class InterfaceTypeDefinitionBlock extends TypeDefinitionBlock { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, InterfaceType $definition, ) { - parent::__construct($dispatcher, $settings, $level, $used, $definition); + parent::__construct($settings, $level, $used, $definition); } protected function type(): string|null { diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php index 45bd16a9..6f2a86b0 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php @@ -2,18 +2,13 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; -use Closure; use GraphQL\Language\Parser; use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use Mockery; /** * @internal @@ -34,7 +29,7 @@ public function testToString( int $used, InterfaceType $definition, ): void { - $actual = (string) (new InterfaceTypeDefinitionBlock(new Dispatcher(), $settings, $level, $used, $definition)); + $actual = (string) (new InterfaceTypeDefinitionBlock($settings, $level, $used, $definition)); Parser::interfaceTypeDefinition($actual); @@ -44,52 +39,39 @@ public function testToString( /** * @covers ::__toString */ - public function testToStringEvent(): void { - $spy = Mockery::spy(static fn (Event $event) => null); + public function testStatistics(): void { $settings = new TestSettings(); - $dispatcher = new Dispatcher(); $definition = new InterfaceType([ - 'name' => 'A', - 'fields' => [ + 'name' => 'A', + 'fields' => [ 'b' => [ - 'name' => 'b', - 'type' => new ObjectType([ + 'name' => 'b', + 'type' => new ObjectType([ 'name' => 'B', ]), - 'args' => [ + 'args' => [ 'c' => [ - 'type' => new ObjectType([ + 'type' => new ObjectType([ 'name' => 'C', ]), + 'astNode' => Parser::inputValueDefinition('c: C @c'), ], ], + 'astNode' => Parser::fieldDefinition('b: B @b'), ], ], + 'interfaces' => [ + new InterfaceType([ + 'name' => 'D', + ]), + ], + 'astNode' => Parser::interfaceTypeDefinition('interface A @a'), ]); + $block = new InterfaceTypeDefinitionBlock($settings, 0, 0, $definition); - $dispatcher->attach(Closure::fromCallable($spy)); - - self::assertNotEmpty( - (string) (new InterfaceTypeDefinitionBlock($dispatcher, $settings, 0, 0, $definition)), - ); - - $spy - ->shouldHaveBeenCalled() - ->withArgs(static function (Event $event): bool { - return $event instanceof TypeUsed - && $event->name === 'B'; - }) - ->once(); - $spy - ->shouldHaveBeenCalled() - ->withArgs(static function (Event $event): bool { - return $event instanceof TypeUsed - && $event->name === 'C'; - }) - ->once(); - $spy - ->shouldHaveBeenCalled() - ->twice(); + self::assertNotEmpty((string) $block); + self::assertEquals(['B' => 'B', 'C' => 'C', 'D' => 'D'], $block->getUsedTypes()); + self::assertEquals(['a' => 'a', 'b' => 'b', 'c' => 'c'], $block->getUsedDirectives()); } // diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php index e3a701a8..5bfba18e 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\ObjectType; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; /** @@ -13,13 +12,12 @@ */ class ObjectTypeDefinitionBlock extends TypeDefinitionBlock { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, ObjectType $definition, ) { - parent::__construct($dispatcher, $settings, $level, $used, $definition); + parent::__construct($settings, $level, $used, $definition); } protected function type(): string|null { diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php index e5e5c742..a484185c 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php @@ -2,17 +2,13 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; -use Closure; use GraphQL\Language\Parser; +use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use Mockery; /** * @internal @@ -33,7 +29,7 @@ public function testToString( int $used, ObjectType $definition, ): void { - $actual = (string) (new ObjectTypeDefinitionBlock(new Dispatcher(), $settings, $level, $used, $definition)); + $actual = (string) (new ObjectTypeDefinitionBlock($settings, $level, $used, $definition)); Parser::objectTypeDefinition($actual); @@ -43,52 +39,39 @@ public function testToString( /** * @covers ::__toString */ - public function testToStringEvent(): void { - $spy = Mockery::spy(static fn (Event $event) => null); + public function testStatistics(): void { $settings = new TestSettings(); - $dispatcher = new Dispatcher(); $definition = new ObjectType([ - 'name' => 'A', - 'fields' => [ + 'name' => 'A', + 'fields' => [ 'b' => [ - 'name' => 'b', - 'type' => new ObjectType([ + 'name' => 'b', + 'type' => new ObjectType([ 'name' => 'B', ]), - 'args' => [ + 'args' => [ 'c' => [ - 'type' => new ObjectType([ + 'type' => new ObjectType([ 'name' => 'C', ]), + 'astNode' => Parser::inputValueDefinition('c: C @c'), ], ], + 'astNode' => Parser::fieldDefinition('b: B @b'), ], ], + 'interfaces' => [ + new InterfaceType([ + 'name' => 'D', + ]), + ], + 'astNode' => Parser::objectTypeDefinition('type A @a'), ]); + $block = new ObjectTypeDefinitionBlock($settings, 0, 0, $definition); - $dispatcher->attach(Closure::fromCallable($spy)); - - self::assertNotEmpty( - (string) (new ObjectTypeDefinitionBlock($dispatcher, $settings, 0, 0, $definition)), - ); - - $spy - ->shouldHaveBeenCalled() - ->withArgs(static function (Event $event): bool { - return $event instanceof TypeUsed - && $event->name === 'B'; - }) - ->once(); - $spy - ->shouldHaveBeenCalled() - ->withArgs(static function (Event $event): bool { - return $event instanceof TypeUsed - && $event->name === 'C'; - }) - ->once(); - $spy - ->shouldHaveBeenCalled() - ->twice(); + self::assertNotEmpty((string) $block); + self::assertEquals(['B' => 'B', 'C' => 'C', 'D' => 'D'], $block->getUsedTypes()); + self::assertEquals(['a' => 'a', 'b' => 'b', 'c' => 'c'], $block->getUsedDirectives()); } // diff --git a/src/SchemaPrinter/Blocks/Types/RootOperationTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/RootOperationTypeDefinitionBlock.php index 73483696..d7f369af 100644 --- a/src/SchemaPrinter/Blocks/Types/RootOperationTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/RootOperationTypeDefinitionBlock.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\ObjectType; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; /** @@ -11,14 +10,13 @@ */ class RootOperationTypeDefinitionBlock extends TypeBlock { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, private OperationType $operation, ObjectType $type, ) { - parent::__construct($dispatcher, $settings, $level, $used, $type); + parent::__construct($settings, $level, $used, $type); } public function getOperation(): OperationType { diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php index a4608377..4686e795 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\ScalarType; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -14,13 +13,12 @@ */ class ScalarTypeDefinitionBlock extends DefinitionBlock { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, ScalarType $definition, ) { - parent::__construct($dispatcher, $settings, $level, $used, $definition); + parent::__construct($settings, $level, $used, $definition); } protected function type(): string|null { diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php index a04c43f4..e4956603 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php @@ -5,7 +5,6 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\CustomScalarType; use GraphQL\Type\Definition\ScalarType; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -29,7 +28,7 @@ public function testToString( int $used, ScalarType $type, ): void { - $actual = (string) (new ScalarTypeDefinitionBlock(new Dispatcher(), $settings, $level, $used, $type)); + $actual = (string) (new ScalarTypeDefinitionBlock($settings, $level, $used, $type)); Parser::scalarTypeDefinition($actual); diff --git a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php index d822ea8a..6f1fd611 100644 --- a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php @@ -4,13 +4,13 @@ use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Schema; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use function array_filter; use function count; use function mb_strlen; + use const ARRAY_FILTER_USE_BOTH; /** @@ -20,13 +20,12 @@ */ class SchemaDefinitionBlock extends DefinitionBlock { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, Schema $definition, ) { - parent::__construct($dispatcher, $settings, $level, $used, $definition); + parent::__construct($settings, $level, $used, $definition); } protected function type(): string|null { @@ -51,7 +50,6 @@ protected function fields(int $used): Block|string|null { $definition = $this->getDefinition(); $space = $this->space(); $fields = new RootOperationTypesDefinitionList( - $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $used + mb_strlen($space), @@ -67,7 +65,6 @@ protected function fields(int $used): Block|string|null { if ($type) { $fields[] = new RootOperationTypeDefinitionBlock( - $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, $this->getUsed(), @@ -77,7 +74,7 @@ protected function fields(int $used): Block|string|null { } } - return $fields; + return $this->addUsed($fields); } public function isUseDefaultRootOperationTypeNames(): bool { diff --git a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php index 80f8eeff..e669f1ac 100644 --- a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php @@ -5,7 +5,6 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Schema; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -31,7 +30,7 @@ public function testToString( int $used, Schema $schema, ): void { - $actual = (string) (new SchemaDefinitionBlock(new Dispatcher(), $settings, $level, $used, $schema)); + $actual = (string) (new SchemaDefinitionBlock($settings, $level, $used, $schema)); if ($expected && !str_starts_with($actual, '"""')) { // https://github.com/webonyx/graphql-php/issues/1027 diff --git a/src/SchemaPrinter/Blocks/Types/StringBlock.php b/src/SchemaPrinter/Blocks/Types/StringBlock.php index fb16dbe9..b55c46ca 100644 --- a/src/SchemaPrinter/Blocks/Types/StringBlock.php +++ b/src/SchemaPrinter/Blocks/Types/StringBlock.php @@ -4,7 +4,6 @@ use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\Printer; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -19,13 +18,12 @@ */ class StringBlock extends Block { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, protected string $string, ) { - parent::__construct($dispatcher, $settings, $level, $used); + parent::__construct($settings, $level, $used); } protected function getString(): string { diff --git a/src/SchemaPrinter/Blocks/Types/StringBlockTest.php b/src/SchemaPrinter/Blocks/Types/StringBlockTest.php index 2ae01daa..31486630 100644 --- a/src/SchemaPrinter/Blocks/Types/StringBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/StringBlockTest.php @@ -4,7 +4,6 @@ use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\Parser; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -30,7 +29,7 @@ public function testToString( int $used, string $string, ): void { - $actual = (string) new StringBlock(new Dispatcher(), $settings, $level, $used, $string); + $actual = (string) new StringBlock($settings, $level, $used, $string); $parsed = Parser::valueLiteral($actual); self::assertInstanceOf(StringValueNode::class, $parsed); diff --git a/src/SchemaPrinter/Blocks/Types/TypeBlock.php b/src/SchemaPrinter/Blocks/Types/TypeBlock.php index 533cd296..18d66062 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeBlock.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlock.php @@ -4,9 +4,7 @@ use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\WrappingType; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -15,13 +13,12 @@ */ class TypeBlock extends Block implements Named { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, private Type $definition, ) { - parent::__construct($dispatcher, $settings, $level, $used); + parent::__construct($settings, $level, $used); } public function getName(): string { @@ -39,9 +36,7 @@ protected function getType(): Type { } protected function content(): string { - $this->getDispatcher()->notify( - new TypeUsed($this->getName()), - ); + $this->addUsedType($this->getName()); return (string) $this->getType(); } diff --git a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php index 19ba54c3..98ddde57 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php @@ -2,18 +2,13 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; -use Closure; use GraphQL\Type\Definition\ListOfType; use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use Mockery; /** * @internal @@ -34,7 +29,7 @@ public function testToString( int $used, Type $type, ): void { - $actual = (string) (new TypeBlock(new Dispatcher(), $settings, $level, $used, $type)); + $actual = (string) (new TypeBlock($settings, $level, $used, $type)); self::assertEquals($expected, $actual); } @@ -42,32 +37,19 @@ public function testToString( /** * @covers ::__toString */ - public function testToStringEvent(): void { - $spy = Mockery::spy(static fn (Event $event) => null); - $node = new NonNull( + public function testStatistics(): void { + $node = new NonNull( new ObjectType([ 'name' => 'Test', ]), ); - $settings = new TestSettings(); - $dispatcher = new Dispatcher(); - - $dispatcher->attach(Closure::fromCallable($spy)); - - self::assertNotEmpty( - (string) (new TypeBlock($dispatcher, $settings, 0, 0, $node)), - ); + $settings = new TestSettings(); + $block = new TypeBlock($settings, 0, 0, $node); + $type = $node->getWrappedType(true)->name; - $spy - ->shouldHaveBeenCalled() - ->withArgs(static function (Event $event) use ($node): bool { - return $event instanceof TypeUsed - && $event->name === $node->getWrappedType(true)->name; - }) - ->once(); - $spy - ->shouldHaveBeenCalled() - ->once(); + self::assertNotEmpty((string) $block); + self::assertEquals([$type => $type], $block->getUsedTypes()); + self::assertEquals([], $block->getUsedDirectives()); } // diff --git a/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php index 4a947c3b..5ec16081 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php @@ -4,7 +4,6 @@ use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\ObjectType; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -22,24 +21,24 @@ abstract class TypeDefinitionBlock extends DefinitionBlock { * @param TType $definition */ public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, InterfaceType|ObjectType $definition, ) { - parent::__construct($dispatcher, $settings, $level, $used, $definition); + parent::__construct($settings, $level, $used, $definition); } protected function body(int $used): Block|string|null { $definition = $this->getDefinition(); $space = $this->space(); - $interfaces = new ImplementsInterfacesList( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel() + 1, - $used + mb_strlen($space), - $definition->getInterfaces(), + $interfaces = $this->addUsed( + new ImplementsInterfacesList( + $this->getSettings(), + $this->getLevel() + 1, + $used + mb_strlen($space), + $definition->getInterfaces(), + ), ); if (!$interfaces->isEmpty()) { @@ -59,13 +58,12 @@ protected function fields(int $used): Block|string|null { $definition = $this->getDefinition(); $space = $this->space(); $fields = new FieldsDefinitionList( - $this->getDispatcher(), $this->getSettings(), $this->getLevel(), $used + mb_strlen($space), $definition->getFields(), ); - return $fields; + return $this->addUsed($fields); } } diff --git a/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php b/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php index b780ed86..9cf0f673 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php +++ b/src/SchemaPrinter/Blocks/Types/UnionMemberTypesList.php @@ -12,7 +12,6 @@ class UnionMemberTypesList extends UsageList { protected function block(mixed $item): Block { return new TypeBlock( - $this->getDispatcher(), $this->getSettings(), $this->getLevel() + 1, $this->getUsed(), diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php index 0601517f..238eeb34 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\UnionType; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -16,13 +15,12 @@ */ class UnionTypeDefinitionBlock extends DefinitionBlock { public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, UnionType $definition, ) { - parent::__construct($dispatcher, $settings, $level, $used, $definition); + parent::__construct($settings, $level, $used, $definition); } protected function type(): string|null { @@ -36,12 +34,13 @@ protected function body(int $used): Block|string|null { protected function fields(int $used): Block|string|null { $space = $this->space(); $equal = "={$space}"; - $types = new UnionMemberTypesList( - $this->getDispatcher(), - $this->getSettings(), - $this->getLevel() + 1, - $used + mb_strlen($equal), - $this->getDefinition()->getTypes(), + $types = $this->addUsed( + new UnionMemberTypesList( + $this->getSettings(), + $this->getLevel() + 1, + $used + mb_strlen($equal), + $this->getDefinition()->getTypes(), + ), ); if ($types->isMultiline()) { diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php index 2d4e4d7b..4b09ac8c 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php @@ -2,17 +2,12 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; -use Closure; use GraphQL\Language\Parser; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\UnionType; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\Event; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Events\TypeUsed; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use Mockery; /** * @internal @@ -34,7 +29,7 @@ public function testToString( int $used, UnionType $type, ): void { - $actual = (string) (new UnionTypeDefinitionBlock(new Dispatcher(), $settings, $level, $used, $type)); + $actual = (string) (new UnionTypeDefinitionBlock($settings, $level, $used, $type)); Parser::unionTypeDefinition($actual); @@ -44,11 +39,10 @@ public function testToString( /** * @covers ::__toString */ - public function testToStringEvent(): void { - $spy = Mockery::spy(static fn (Event $event) => null); - $union = new UnionType([ - 'name' => 'Test', - 'types' => [ + public function testStatistics(): void { + $union = new UnionType([ + 'name' => 'Test', + 'types' => [ new ObjectType([ 'name' => 'A', ]), @@ -56,33 +50,14 @@ public function testToStringEvent(): void { 'name' => 'B', ]), ], + 'astNode' => Parser::unionTypeDefinition('union Test @a = A | B'), ]); - $settings = new TestSettings(); - $dispatcher = new Dispatcher(); + $settings = new TestSettings(); + $block = new UnionTypeDefinitionBlock($settings, 0, 0, $union); - $dispatcher->attach(Closure::fromCallable($spy)); - - self::assertNotEmpty( - (string) (new UnionTypeDefinitionBlock($dispatcher, $settings, 0, 0, $union)), - ); - - $spy - ->shouldHaveBeenCalled() - ->withArgs(static function (Event $event): bool { - return $event instanceof TypeUsed - && $event->name === 'A'; - }) - ->once(); - $spy - ->shouldHaveBeenCalled() - ->withArgs(static function (Event $event): bool { - return $event instanceof TypeUsed - && $event->name === 'B'; - }) - ->once(); - $spy - ->shouldHaveBeenCalled() - ->twice(); + self::assertNotEmpty((string) $block); + self::assertEquals(['A' => 'A', 'B' => 'B'], $block->getUsedTypes()); + self::assertEquals(['a' => 'a'], $block->getUsedDirectives()); } // @@ -97,7 +72,7 @@ public function dataProviderToString(): array { ->setAlwaysMultilineUnions(false); return [ - 'single-line' => [ + 'single-line' => [ <<<'STRING' union Test = C | B | A STRING, @@ -119,7 +94,7 @@ public function dataProviderToString(): array { ], ]), ], - 'multiline' => [ + 'multiline' => [ <<<'STRING' union Test = | C @@ -144,7 +119,7 @@ public function dataProviderToString(): array { ], ]), ], - 'indent single-line' => [ + 'indent single-line' => [ <<<'STRING' union Test = C | B | A STRING, @@ -166,7 +141,7 @@ public function dataProviderToString(): array { ], ]), ], - 'indent multiline' => [ + 'indent multiline' => [ <<<'STRING' union Test = | C @@ -191,7 +166,7 @@ public function dataProviderToString(): array { ], ]), ], - 'multiline normalized' => [ + 'multiline normalized' => [ <<<'STRING' union Test = A | B | C STRING, @@ -213,7 +188,7 @@ public function dataProviderToString(): array { ], ]), ], - 'multiline always' => [ + 'multiline always' => [ <<<'STRING' union Test = | C @@ -238,7 +213,7 @@ public function dataProviderToString(): array { ], ]), ], - 'directives' => [ + 'directives' => [ <<<'STRING' union Test @a @@ -248,8 +223,8 @@ public function dataProviderToString(): array { 0, 0, new UnionType([ - 'name' => 'Test', - 'types' => [ + 'name' => 'Test', + 'types' => [ new ObjectType([ 'name' => 'C', ]), @@ -260,14 +235,14 @@ public function dataProviderToString(): array { 'name' => 'A', ]), ], - 'astNode' => Parser::unionTypeDefinition( + 'astNode' => Parser::unionTypeDefinition( <<<'STRING' union Test @a = A | B | C STRING, ), ]), ], - 'directives + multiline' => [ + 'directives + multiline' => [ <<<'STRING' union Test @a @@ -280,8 +255,8 @@ public function dataProviderToString(): array { 0, 120, new UnionType([ - 'name' => 'Test', - 'types' => [ + 'name' => 'Test', + 'types' => [ new ObjectType([ 'name' => 'C', ]), @@ -292,14 +267,14 @@ public function dataProviderToString(): array { 'name' => 'A', ]), ], - 'astNode' => Parser::unionTypeDefinition( + 'astNode' => Parser::unionTypeDefinition( <<<'STRING' union Test @a = A | B | C STRING, ), ]), ], - "one member + always multiline" => [ + 'one member + always multiline' => [ <<<'STRING' union Test = | A diff --git a/src/SchemaPrinter/Blocks/Types/UsageList.php b/src/SchemaPrinter/Blocks/Types/UsageList.php index 8e37a38f..ee684574 100644 --- a/src/SchemaPrinter/Blocks/Types/UsageList.php +++ b/src/SchemaPrinter/Blocks/Types/UsageList.php @@ -2,7 +2,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; -use LastDragon_ru\LaraASP\Core\Observer\Dispatcher; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -21,13 +20,12 @@ abstract class UsageList extends BlockList { * @param Traversable|array $items */ public function __construct( - Dispatcher $dispatcher, Settings $settings, int $level, int $used, Traversable|array $items, ) { - parent::__construct($dispatcher, $settings, $level, $used); + parent::__construct($settings, $level, $used); foreach ($items as $item) { $this[] = $this->block($item); diff --git a/src/SchemaPrinter/Printer.php b/src/SchemaPrinter/Printer.php index 51cddf1b..48a63dbf 100644 --- a/src/SchemaPrinter/Printer.php +++ b/src/SchemaPrinter/Printer.php @@ -178,7 +178,7 @@ protected function getDefinitionBlock( array &$usedTypes = [], array &$usedDirectives = [], ): DefinitionBlock { - $block = new DefinitionBlock($this->getSettings(), $this->getLevel(), $definition); + $block = new DefinitionBlock($this->getSettings(), $this->getLevel(), $definition); $usedTypes += $block->getUsedTypes(); $usedDirectives += $block->getUsedDirectives(); diff --git a/src/SchemaPrinter/PrinterTest.php b/src/SchemaPrinter/PrinterTest.php index ec1e1ba9..a819868a 100644 --- a/src/SchemaPrinter/PrinterTest.php +++ b/src/SchemaPrinter/PrinterTest.php @@ -36,7 +36,7 @@ public function testPrint(string $expected, Settings $settings, int $level): voi $registry = $this->app->make(TypeRegistry::class); $directive = (new class() extends BaseDirective { public static function definition(): string { - throw new Exception("Should not be called."); + throw new Exception('Should not be called.'); } })::class; @@ -93,9 +93,9 @@ public static function definition(): string { 'codeDirective', (new class() extends BaseDirective { public static function definition(): string { - return "directive @codeDirective repeatable on SCHEMA | SCALAR | INTERFACE"; + return 'directive @codeDirective repeatable on SCHEMA | SCALAR | INTERFACE'; } - })::class + })::class, ); $registry->register($codeScalar); $registry->register($codeEnum); diff --git a/src/SchemaPrinter/Statistics.php b/src/SchemaPrinter/Statistics.php new file mode 100644 index 00000000..331059af --- /dev/null +++ b/src/SchemaPrinter/Statistics.php @@ -0,0 +1,15 @@ + + */ + public function getUsedTypes(): array; + + /** + * @return array + */ + public function getUsedDirectives(): array; +} diff --git a/src/SearchBy/Ast/UsageTest.php b/src/SearchBy/Ast/UsageTest.php index 0c8a6f7c..8dcdcd20 100644 --- a/src/SearchBy/Ast/UsageTest.php +++ b/src/SearchBy/Ast/UsageTest.php @@ -2,11 +2,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Ast; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use LogicException; use OutOfBoundsException; -use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use stdClass; - use function sprintf; /** From 0050a50dc582110bbf2189fb132618cedfe3a654 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 30 Jan 2022 09:51:42 +0400 Subject: [PATCH 75/90] `DefaultSettings` will not print directive definitions. --- .../PrinterTest~default-settings.graphql | 54 ------------------- .../Settings/DefaultSettings.php | 2 +- 2 files changed, 1 insertion(+), 55 deletions(-) diff --git a/src/SchemaPrinter/PrinterTest~default-settings.graphql b/src/SchemaPrinter/PrinterTest~default-settings.graphql index 3caccbcf..1f0eeec1 100644 --- a/src/SchemaPrinter/PrinterTest~default-settings.graphql +++ b/src/SchemaPrinter/PrinterTest~default-settings.graphql @@ -198,57 +198,3 @@ union SchemaUnion = SchemaType | CodeType This is unused union. """ union SchemaUnionUnused = SchemaTypeUnused - -directive @codeDirective repeatable on SCHEMA | SCALAR | INTERFACE - -""" -Allows you to easily hook up a resolver for an endpoint. -""" -directive @mock( - """ - Specify a unique key for the mock resolver. - """ - key: String = "default" -) -on | FIELD_DEFINITION - -""" -Reference a class implementing a scalar definition. -""" -directive @scalar( - """ - Reference to a class that extends `\GraphQL\Type\Definition\ScalarType`. - """ - class: String! -) -on | SCALAR - -""" -Directive -""" -directive @schemaDirective( - """ - Directive argument - """ - message: String -) -on - | SCHEMA - | FIELD - | ARGUMENT_DEFINITION - | INTERFACE - | OBJECT - | UNION - | INPUT_OBJECT - | SCALAR - -""" -This is unused directives. -""" -directive @schemaDirectiveUnused( - a: SchemaScalarUnused - b: SchemaEnumUnused -) -repeatable on - | SCALAR - | OBJECT diff --git a/src/SchemaPrinter/Settings/DefaultSettings.php b/src/SchemaPrinter/Settings/DefaultSettings.php index 2519ec62..3a55dd64 100644 --- a/src/SchemaPrinter/Settings/DefaultSettings.php +++ b/src/SchemaPrinter/Settings/DefaultSettings.php @@ -11,7 +11,7 @@ class DefaultSettings extends ImmutableSettings { protected string $lineEnd = "\n"; protected int $lineLength = 80; protected bool $printDirectives = false; - protected bool $printDirectiveDefinitions = true; + protected bool $printDirectiveDefinitions = false; protected bool $printDirectivesInDescription = false; protected bool $printUnusedDefinitions = true; protected bool $normalizeSchema = true; From 649e43ed1cd387d743fc4e21df2e272bb4d3ebd5 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 30 Jan 2022 10:25:39 +0400 Subject: [PATCH 76/90] Directive filtering moved `offsetSet`. --- .../Blocks/Ast/DirectiveNodeList.php | 24 +++++++++---------- src/SchemaPrinter/Blocks/BlockList.php | 13 ++++++++-- .../Blocks/Printer/DefinitionList.php | 2 ++ 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php index 9af9d4c9..6d4880a0 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php @@ -5,11 +5,11 @@ use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\Parser; use GraphQL\Type\Definition\Directive; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use Traversable; -use function array_filter; use function json_encode; /** @@ -55,20 +55,18 @@ protected function isAlwaysMultiline(): bool { return true; } - /** - * @inheritDoc - */ - protected function getBlocks(): array { - $filter = $this->getSettings()->getDirectiveFilter(); - $blocks = parent::getBlocks(); - - if ($filter !== null) { - $blocks = array_filter($blocks, static function (DirectiveNodeBlock $block) use ($filter): bool { - return $filter->isAllowedDirective($block->getNode()); - }); + protected function isValidBlock(Block $value): bool { + // Parent? + if (!parent::isValidBlock($value)) { + return false; } - return $blocks; + // Allowed? + $filter = $this->getSettings()->getDirectiveFilter(); + $valid = $filter === null + || $filter->isAllowedDirective($value->getNode()); + + return $valid; } private function block(DirectiveNode $directive,): DirectiveNodeBlock { diff --git a/src/SchemaPrinter/Blocks/BlockList.php b/src/SchemaPrinter/Blocks/BlockList.php index 55054911..95103da5 100644 --- a/src/SchemaPrinter/Blocks/BlockList.php +++ b/src/SchemaPrinter/Blocks/BlockList.php @@ -15,7 +15,7 @@ /** * @internal * @template TBlock of Block - * @implements ArrayAccess + * @implements ArrayAccess */ abstract class BlockList extends Block implements Statistics, ArrayAccess, Countable { /** @@ -205,6 +205,15 @@ private function isMultilineContent( protected function analyze(Block $block): Block { return $this->addUsed($block); } + + /** + * @param TBlock $value + * + * @return mixed + */ + protected function isValidBlock(Block $value): bool { + return !$value->isEmpty(); + } // // @@ -230,7 +239,7 @@ public function offsetGet(mixed $offset): Block { * @param TBlock $value */ public function offsetSet(mixed $offset, mixed $value): void { - if ($value->isEmpty()) { + if (!$this->isValidBlock($value)) { return; } diff --git a/src/SchemaPrinter/Blocks/Printer/DefinitionList.php b/src/SchemaPrinter/Blocks/Printer/DefinitionList.php index 7237594e..71b6867d 100644 --- a/src/SchemaPrinter/Blocks/Printer/DefinitionList.php +++ b/src/SchemaPrinter/Blocks/Printer/DefinitionList.php @@ -2,6 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Printer; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; @@ -9,6 +10,7 @@ /** * @internal + * @extends BlockList */ class DefinitionList extends BlockList { public function __construct( From 5db2a6b7d694e69a5c21802a41afa527577aa978 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 30 Jan 2022 11:57:32 +0400 Subject: [PATCH 77/90] `DirectiveFilter` will accept instance of directive instead of `DirectiveNode`. --- .../Blocks/Ast/ArgumentNodeList.php | 4 +- .../Blocks/Ast/DirectiveNodeBlock.php | 4 +- .../Blocks/Ast/DirectiveNodeBlockTest.php | 8 +- .../Blocks/Ast/DirectiveNodeList.php | 11 +- .../Blocks/Ast/DirectiveNodeListTest.php | 31 ++++- .../Blocks/Ast/ValueNodeBlock.php | 4 +- .../Blocks/Ast/ValueNodeTest.php | 7 +- src/SchemaPrinter/Blocks/Block.php | 5 +- src/SchemaPrinter/Blocks/BlockListTest.php | 38 +++--- src/SchemaPrinter/Blocks/BlockSettings.php | 113 ++++++++++++++++++ src/SchemaPrinter/Blocks/BlockTest.php | 24 ++-- .../Blocks/Printer/DefinitionBlock.php | 4 +- .../Blocks/Printer/DefinitionList.php | 4 +- src/SchemaPrinter/Blocks/Property.php | 3 +- src/SchemaPrinter/Blocks/PropertyTest.php | 7 +- .../Blocks/Types/ArgumentsDefinitionList.php | 4 +- .../Blocks/Types/DefinitionBlock.php | 4 +- .../Blocks/Types/Description.php | 4 +- .../Blocks/Types/DescriptionTest.php | 3 + .../Blocks/Types/DirectiveDefinitionBlock.php | 4 +- .../Types/DirectiveDefinitionBlockTest.php | 6 +- .../Blocks/Types/DirectiveLocationBlock.php | 4 +- .../Blocks/Types/EnumTypeDefinitionBlock.php | 4 +- .../Types/EnumTypeDefinitionBlockTest.php | 5 +- .../Blocks/Types/EnumValueDefinitionBlock.php | 4 +- .../Types/EnumValueDefinitionBlockTest.php | 5 +- .../Blocks/Types/EnumValuesDefinitionList.php | 4 +- .../Blocks/Types/FieldDefinitionBlock.php | 4 +- .../Blocks/Types/FieldDefinitionBlockTest.php | 6 +- .../Blocks/Types/FieldsDefinitionList.php | 4 +- .../Types/InputFieldsDefinitionList.php | 4 +- .../Types/InputObjectTypeDefinitionBlock.php | 4 +- .../InputObjectTypeDefinitionBlockTest.php | 6 +- .../Types/InputValueDefinitionBlock.php | 4 +- .../Types/InputValueDefinitionBlockTest.php | 7 +- .../Types/InterfaceTypeDefinitionBlock.php | 4 +- .../InterfaceTypeDefinitionBlockTest.php | 6 +- .../Types/ObjectTypeDefinitionBlock.php | 4 +- .../Types/ObjectTypeDefinitionBlockTest.php | 6 +- .../RootOperationTypeDefinitionBlock.php | 4 +- .../Types/ScalarTypeDefinitionBlock.php | 4 +- .../Types/ScalarTypeDefinitionBlockTest.php | 5 +- .../Blocks/Types/SchemaDefinitionBlock.php | 4 +- .../Types/SchemaDefinitionBlockTest.php | 5 +- .../Blocks/Types/StringBlock.php | 4 +- .../Blocks/Types/StringBlockTest.php | 7 +- src/SchemaPrinter/Blocks/Types/TypeBlock.php | 4 +- .../Blocks/Types/TypeBlockTest.php | 6 +- .../Blocks/Types/TypeDefinitionBlock.php | 4 +- .../Blocks/Types/UnionTypeDefinitionBlock.php | 4 +- .../Types/UnionTypeDefinitionBlockTest.php | 8 +- src/SchemaPrinter/Blocks/Types/UsageList.php | 4 +- .../Contracts/DirectiveFilter.php | 5 +- src/SchemaPrinter/DirectiveResolver.php | 46 ++++++- .../Package/SchemaPrinter/TestSettings.php | 7 +- 55 files changed, 372 insertions(+), 126 deletions(-) create mode 100644 src/SchemaPrinter/Blocks/BlockSettings.php diff --git a/src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php b/src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php index b55876d8..98bfe559 100644 --- a/src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php +++ b/src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php @@ -4,8 +4,8 @@ use GraphQL\Language\AST\ArgumentNode; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Property; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use Traversable; /** @@ -17,7 +17,7 @@ class ArgumentNodeList extends BlockList { * @param Traversable|array $arguments */ public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, Traversable|array $arguments, diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php index 3d99cd95..b98784ab 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php @@ -4,8 +4,8 @@ use GraphQL\Language\AST\DirectiveNode; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use function mb_strlen; @@ -14,7 +14,7 @@ */ class DirectiveNodeBlock extends Block implements Named { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, private DirectiveNode $node, diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php index 76f20b0f..3b4f6a71 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php @@ -5,6 +5,8 @@ use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\Parser; use GraphQL\Language\Printer; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -28,8 +30,9 @@ public function testToString( int $used, DirectiveNode $node, ): void { - $actual = (string) (new DirectiveNodeBlock($settings, $level, $used, $node)); - $parsed = Parser::directive($actual); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $actual = (string) (new DirectiveNodeBlock($settings, $level, $used, $node)); + $parsed = Parser::directive($actual); self::assertEquals($expected, $actual); @@ -46,6 +49,7 @@ public function testToString( */ public function testStatistics(): void { $settings = new TestSettings(); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); $node = Parser::directive('@test'); $block = new DirectiveNodeBlock($settings, 0, 0, $node); diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php index 6d4880a0..32fe9bd7 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php @@ -7,7 +7,7 @@ use GraphQL\Type\Definition\Directive; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use Traversable; use function json_encode; @@ -21,7 +21,7 @@ class DirectiveNodeList extends BlockList { * @param Traversable|array $directives */ public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, Traversable|array|null $directives, @@ -62,9 +62,10 @@ protected function isValidBlock(Block $value): bool { } // Allowed? - $filter = $this->getSettings()->getDirectiveFilter(); - $valid = $filter === null - || $filter->isAllowedDirective($value->getNode()); + $settings = $this->getSettings(); + $filter = $settings->getDirectiveFilter(); + $valid = $filter === null + || $filter->isAllowedDirective($settings->getDirective($value->getNode())); return $valid; } diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php index 6bf88b88..6392f1c6 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php @@ -3,11 +3,17 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast; use GraphQL\Language\AST\DirectiveNode; +use GraphQL\Language\DirectiveLocation; use GraphQL\Language\Parser; -use GraphQL\Type\Definition\Directive; +use GraphQL\Type\Definition\Directive as GraphQLDirective; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; +use Nuwave\Lighthouse\Schema\DirectiveLocator; +use Nuwave\Lighthouse\Schema\ExecutableTypeNodeConverter; +use Nuwave\Lighthouse\Support\Contracts\Directive as LighthouseDirective; /** * @internal @@ -31,7 +37,20 @@ public function testToString( array|null $directives, string $reason = null, ): void { - $actual = (string) (new DirectiveNodeList($settings, $level, $used, $directives, $reason)); + $locator = $this->app->make(DirectiveLocator::class); + $convertor = $this->app->make(ExecutableTypeNodeConverter::class); + $instances = []; + + foreach ((array) $directives as $directive) { + $instances[] = new GraphQLDirective([ + 'name' => $directive->name->value, + 'locations' => [DirectiveLocation::OBJECT], + ]); + } + + $resolver = new DirectiveResolver($locator, $convertor, $instances); + $settings = new BlockSettings($resolver, $settings); + $actual = (string) (new DirectiveNodeList($settings, $level, $used, $directives, $reason)); Parser::directives($actual); @@ -45,6 +64,7 @@ public function testStatistics(): void { $a = Parser::directive('@a'); $b = Parser::directive('@b'); $settings = (new TestSettings())->setPrintDirectives(true); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); $block = new DirectiveNodeList($settings, 0, 0, [$a, $b]); self::assertNotEmpty((string) $block); @@ -100,7 +120,7 @@ public function dataProviderToString(): array { 0, 0, null, - Directive::DEFAULT_DEPRECATION_REASON, + GraphQLDirective::DEFAULT_DEPRECATION_REASON, ], 'deprecated (custom reason)' => [ <<<'STRING' @@ -168,8 +188,9 @@ public function dataProviderToString(): array { <<<'STRING' @a(a: 123) STRING, - $settings->setDirectiveFilter(static function (DirectiveNode $directive): bool { - return $directive->name->value === 'a'; + $settings->setDirectiveFilter(static function (GraphQLDirective|LighthouseDirective $directive): bool { + return $directive instanceof GraphQLDirective + && $directive->name === 'a'; }), 0, 0, diff --git a/src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php b/src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php index a478f6de..c61d2f9a 100644 --- a/src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php +++ b/src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php @@ -9,16 +9,16 @@ use GraphQL\Language\AST\ValueNode; use GraphQL\Language\Printer; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Property; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\StringBlock; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; class ValueNodeBlock extends Block { /** * @param ValueNode&Node $node */ public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, protected ValueNode $node, diff --git a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php index 8e239c21..5ecb9966 100644 --- a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php +++ b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php @@ -15,6 +15,8 @@ use GraphQL\Language\AST\VariableNode; use GraphQL\Language\Parser; use GraphQL\Language\Printer; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -40,8 +42,9 @@ public function testToString( int $used, ValueNode $node, ): void { - $actual = (string) (new ValueNodeBlock($settings, $level, $used, $node)); - $parsed = Parser::valueLiteral($actual); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $actual = (string) (new ValueNodeBlock($settings, $level, $used, $node)); + $parsed = Parser::valueLiteral($actual); self::assertEquals($expected, $actual); diff --git a/src/SchemaPrinter/Blocks/Block.php b/src/SchemaPrinter/Blocks/Block.php index 3b43cfce..bee0563b 100644 --- a/src/SchemaPrinter/Blocks/Block.php +++ b/src/SchemaPrinter/Blocks/Block.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; use Closure; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Statistics; use Stringable; @@ -30,7 +29,7 @@ abstract class Block implements Statistics, Stringable { private array $usedDirectives = []; public function __construct( - private Settings $settings, + private BlockSettings $settings, private int $level = 0, private int $used = 0, ) { @@ -39,7 +38,7 @@ public function __construct( // // ========================================================================= - protected function getSettings(): Settings { + protected function getSettings(): BlockSettings { return $this->settings; } diff --git a/src/SchemaPrinter/Blocks/BlockListTest.php b/src/SchemaPrinter/Blocks/BlockListTest.php index 4ae299ab..5a1dc214 100644 --- a/src/SchemaPrinter/Blocks/BlockListTest.php +++ b/src/SchemaPrinter/Blocks/BlockListTest.php @@ -2,6 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -38,7 +39,8 @@ public function testToString( array $blocks, int $count, ): void { - $list = new BlockListTest__BlockList( + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $list = new BlockListTest__BlockList( $settings, $level, $used, @@ -63,11 +65,13 @@ public function testToString( * @covers ::analyze */ public function testStatistics(): void { - $list = new class(new TestSettings()) extends BlockList { + $settings = new TestSettings(); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $list = new class($settings) extends BlockList { // empty }; - $list[] = new BlockListTest__StatisticsBlock(['ta'], ['da']); - $list[] = new BlockListTest__StatisticsBlock(['tb'], ['db']); + $list[] = new BlockListTest__StatisticsBlock(['ta'], ['da']); + $list[] = new BlockListTest__StatisticsBlock(['tb'], ['db']); self::assertEquals(['ta' => 'ta', 'tb' => 'tb'], $list->getUsedTypes()); self::assertEquals(['da' => 'da', 'db' => 'db'], $list->getUsedDirectives()); @@ -750,11 +754,12 @@ protected function getMultilineItemPrefix(): string { * @noinspection PhpMultipleClassesDeclarationsInOneFile */ class BlockListTest__Block extends Block { + /** @noinspection PhpMissingParentConstructorInspection */ public function __construct( protected bool $multiline, protected string $content, ) { - parent::__construct(new TestSettings()); + // empty } protected function getContent(): string { @@ -779,21 +784,26 @@ protected function content(): string { * @noinspection PhpMultipleClassesDeclarationsInOneFile */ class BlockListTest__NamedBlock extends Property { + /** @noinspection PhpMissingParentConstructorInspection */ public function __construct( protected string $name, - bool $multiline, - string $content, + protected bool $multiline, + protected string $content, ) { - parent::__construct( - new TestSettings(), - $name, - new BlockListTest__Block($multiline, $content), - ); + // empty } public function getName(): string { return $this->name; } + + protected function getBlock(): Block { + return new BlockListTest__Block($this->multiline, $this->content); + } + + protected function space(): string { + return ' '; + } } /** @@ -804,10 +814,10 @@ class BlockListTest__StatisticsBlock extends Block { /** * @param array $types * @param array $directives + * + * @noinspection PhpMissingParentConstructorInspection */ public function __construct(array $types, array $directives) { - parent::__construct(new TestSettings()); - foreach ($types as $type) { $this->addUsedType($type); } diff --git a/src/SchemaPrinter/Blocks/BlockSettings.php b/src/SchemaPrinter/Blocks/BlockSettings.php new file mode 100644 index 00000000..e65f8cf0 --- /dev/null +++ b/src/SchemaPrinter/Blocks/BlockSettings.php @@ -0,0 +1,113 @@ + + // ========================================================================= + public function getDirective(DirectiveNode $node): GraphQLDirective|LighthouseDirective { + return $this->resolver->getInstance($node->name->value); + } + // + + // + // ========================================================================= + public function getSpace(): string { + return $this->settings->getSpace(); + } + + public function getIndent(): string { + return $this->settings->getIndent(); + } + + public function getFileEnd(): string { + return $this->settings->getFileEnd(); + } + + public function getLineEnd(): string { + return $this->settings->getLineEnd(); + } + + public function getLineLength(): int { + return $this->settings->getLineLength(); + } + + public function isPrintDirectives(): bool { + return $this->settings->isPrintDirectives(); + } + + public function isPrintDirectiveDefinitions(): bool { + return $this->settings->isPrintDirectiveDefinitions(); + } + + public function isPrintDirectivesInDescription(): bool { + return $this->settings->isPrintDirectivesInDescription(); + } + + public function isPrintUnusedDefinitions(): bool { + return $this->settings->isPrintUnusedDefinitions(); + } + + public function isNormalizeSchema(): bool { + return $this->settings->isNormalizeSchema(); + } + + public function isNormalizeUnions(): bool { + return $this->settings->isNormalizeUnions(); + } + + public function isNormalizeEnums(): bool { + return $this->settings->isNormalizeEnums(); + } + + public function isNormalizeInterfaces(): bool { + return $this->settings->isNormalizeInterfaces(); + } + + public function isNormalizeFields(): bool { + return $this->settings->isNormalizeFields(); + } + + public function isNormalizeArguments(): bool { + return $this->settings->isNormalizeArguments(); + } + + public function isNormalizeDescription(): bool { + return $this->settings->isNormalizeDescription(); + } + + public function isNormalizeDirectiveLocations(): bool { + return $this->settings->isNormalizeDirectiveLocations(); + } + + public function isAlwaysMultilineUnions(): bool { + return $this->settings->isAlwaysMultilineUnions(); + } + + public function isAlwaysMultilineInterfaces(): bool { + return $this->settings->isAlwaysMultilineInterfaces(); + } + + public function isAlwaysMultilineDirectiveLocations(): bool { + return $this->settings->isAlwaysMultilineDirectiveLocations(); + } + + public function getDirectiveFilter(): ?DirectiveFilter { + return $this->settings->getDirectiveFilter(); + } + // +} diff --git a/src/SchemaPrinter/Blocks/BlockTest.php b/src/SchemaPrinter/Blocks/BlockTest.php index a8ea2647..f827a677 100644 --- a/src/SchemaPrinter/Blocks/BlockTest.php +++ b/src/SchemaPrinter/Blocks/BlockTest.php @@ -2,6 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -20,8 +21,10 @@ class BlockTest extends TestCase { * @covers ::getContent */ public function testGetContent(): void { - $content = 'content'; - $block = Mockery::mock(BlockTest__Block::class, [new TestSettings()]); + $settings = new TestSettings(); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $content = 'content'; + $block = Mockery::mock(BlockTest__Block::class, [$settings]); $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); $block @@ -37,9 +40,11 @@ public function testGetContent(): void { * @covers ::getLength */ public function testGetLength(): void { - $content = 'content'; - $length = mb_strlen($content); - $block = Mockery::mock(BlockTest__Block::class, [new TestSettings()]); + $settings = new TestSettings(); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $content = 'content'; + $length = mb_strlen($content); + $block = Mockery::mock(BlockTest__Block::class, [$settings]); $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); $block @@ -57,7 +62,8 @@ public function testGetLength(): void { * @dataProvider dataProviderIsMultiline */ public function testIsMultiline(bool $expected, Settings $settings, string $content): void { - $block = Mockery::mock(BlockTest__Block::class, [$settings]); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $block = Mockery::mock(BlockTest__Block::class, [$settings]); $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); $block @@ -75,7 +81,9 @@ public function testIsMultiline(bool $expected, Settings $settings, string $cont * @dataProvider dataProviderIsEmpty */ public function testIsEmpty(bool $expected, string $content): void { - $block = Mockery::mock(BlockTest__Block::class, [new TestSettings()]); + $settings = new TestSettings(); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $block = Mockery::mock(BlockTest__Block::class, [$settings]); $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); $block @@ -90,7 +98,7 @@ public function testIsEmpty(bool $expected, string $content): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderIsMultiline(): array { $settings = new TestSettings(); diff --git a/src/SchemaPrinter/Blocks/Printer/DefinitionBlock.php b/src/SchemaPrinter/Blocks/Printer/DefinitionBlock.php index a18638e8..8ab47fbf 100644 --- a/src/SchemaPrinter/Blocks/Printer/DefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Printer/DefinitionBlock.php @@ -12,6 +12,7 @@ use GraphQL\Type\Definition\UnionType; use GraphQL\Type\Schema; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\DirectiveDefinitionBlock; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\EnumTypeDefinitionBlock; @@ -22,7 +23,6 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\SchemaDefinitionBlock; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\UnionTypeDefinitionBlock; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Exceptions\TypeUnsupported; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; /** * @internal @@ -31,7 +31,7 @@ class DefinitionBlock extends Block implements Named { private Block $block; public function __construct( - Settings $settings, + BlockSettings $settings, int $level, Schema|Type|Directive $definition, ) { diff --git a/src/SchemaPrinter/Blocks/Printer/DefinitionList.php b/src/SchemaPrinter/Blocks/Printer/DefinitionList.php index 71b6867d..ab7fd001 100644 --- a/src/SchemaPrinter/Blocks/Printer/DefinitionList.php +++ b/src/SchemaPrinter/Blocks/Printer/DefinitionList.php @@ -4,7 +4,7 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use function rtrim; @@ -14,7 +14,7 @@ */ class DefinitionList extends BlockList { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, protected bool $schema = false, ) { diff --git a/src/SchemaPrinter/Blocks/Property.php b/src/SchemaPrinter/Blocks/Property.php index 9ee7cb40..f117b830 100644 --- a/src/SchemaPrinter/Blocks/Property.php +++ b/src/SchemaPrinter/Blocks/Property.php @@ -2,7 +2,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; /** * @internal @@ -14,7 +13,7 @@ class Property extends Block implements Named { * @param TBlock $block */ public function __construct( - Settings $settings, + BlockSettings $settings, private string $name, private Block $block, ) { diff --git a/src/SchemaPrinter/Blocks/PropertyTest.php b/src/SchemaPrinter/Blocks/PropertyTest.php index 1478cfdd..cd412aff 100644 --- a/src/SchemaPrinter/Blocks/PropertyTest.php +++ b/src/SchemaPrinter/Blocks/PropertyTest.php @@ -2,7 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -27,9 +27,10 @@ public function testToString(): void { $separator = ':'; $content = 'abc abcabc abcabc abcabc abc'; $settings = (new TestSettings())->setSpace($space); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); $block = new class($settings, $level, $used, $content) extends Block { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, protected string $content, @@ -43,7 +44,7 @@ protected function content(): string { }; $property = new class($settings, $name, $block, $separator) extends Property { public function __construct( - Settings $settings, + BlockSettings $settings, string $name, Block $block, private string $separator, diff --git a/src/SchemaPrinter/Blocks/Types/ArgumentsDefinitionList.php b/src/SchemaPrinter/Blocks/Types/ArgumentsDefinitionList.php index b24556a5..bcbdb1d4 100644 --- a/src/SchemaPrinter/Blocks/Types/ArgumentsDefinitionList.php +++ b/src/SchemaPrinter/Blocks/Types/ArgumentsDefinitionList.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\FieldArgument; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use Traversable; /** @@ -16,7 +16,7 @@ class ArgumentsDefinitionList extends BlockList { * @param Traversable|array $arguments */ public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, Traversable|array $arguments, diff --git a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php index 03789554..b1f1b83a 100644 --- a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php @@ -13,8 +13,8 @@ use GraphQL\Type\Schema; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use function mb_strlen; @@ -28,7 +28,7 @@ abstract class DefinitionBlock extends Block implements Named { * @param TType $definition */ public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, private Type|FieldDefinition|EnumValueDefinition|FieldArgument|Directive|InputObjectField|Schema $definition, diff --git a/src/SchemaPrinter/Blocks/Types/Description.php b/src/SchemaPrinter/Blocks/Types/Description.php index b6361e67..c29493ae 100644 --- a/src/SchemaPrinter/Blocks/Types/Description.php +++ b/src/SchemaPrinter/Blocks/Types/Description.php @@ -3,7 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use function preg_replace; use function rtrim; @@ -15,7 +15,7 @@ */ class Description extends StringBlock { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, ?string $string, diff --git a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php index b749625c..6c261602 100644 --- a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php +++ b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php @@ -5,6 +5,8 @@ use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\Parser; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -33,6 +35,7 @@ public function testToString( ?string $description, ?array $directives, ): void { + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); $directives = new DirectiveNodeList($settings, $level, $used, $directives); $actual = (string) (new Description($settings, $level, $used, $description, $directives)); diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlock.php index 596f72f5..8fe578b0 100644 --- a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlock.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\Directive; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use function mb_strlen; @@ -15,7 +15,7 @@ */ class DirectiveDefinitionBlock extends DefinitionBlock { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, Directive $definition, diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php index 61b62d6f..49032b9b 100644 --- a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php @@ -7,6 +7,8 @@ use GraphQL\Type\Definition\Directive; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -30,7 +32,8 @@ public function testToString( int $used, Directive $definition, ): void { - $actual = (string) (new DirectiveDefinitionBlock($settings, $level, $used, $definition)); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $actual = (string) (new DirectiveDefinitionBlock($settings, $level, $used, $definition)); Parser::directiveDefinition($actual); @@ -42,6 +45,7 @@ public function testToString( */ public function testStatistics(): void { $settings = new TestSettings(); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); $definition = new Directive([ 'name' => 'A', 'args' => [ diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveLocationBlock.php b/src/SchemaPrinter/Blocks/Types/DirectiveLocationBlock.php index e0d47d03..8ebd245d 100644 --- a/src/SchemaPrinter/Blocks/Types/DirectiveLocationBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DirectiveLocationBlock.php @@ -3,15 +3,15 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; /** * @internal */ class DirectiveLocationBlock extends Block implements Named { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, private string $location, diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php index 2d61aac2..9ffb5710 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\EnumType; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use function mb_strlen; @@ -15,7 +15,7 @@ */ class EnumTypeDefinitionBlock extends DefinitionBlock { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, EnumType $definition, diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php index 483a902b..e53a039b 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php @@ -7,6 +7,8 @@ use GraphQL\Type\Definition\Directive; use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\EnumValueDefinition; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -35,7 +37,8 @@ public function testToString( $type = $type(); } - $actual = (string) (new EnumTypeDefinitionBlock($settings, $level, $used, $type)); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $actual = (string) (new EnumTypeDefinitionBlock($settings, $level, $used, $type)); Parser::enumTypeDefinition($actual); diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php index bc6f15fd..59795c3a 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\EnumValueDefinition; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; /** * @internal @@ -12,7 +12,7 @@ */ class EnumValueDefinitionBlock extends DefinitionBlock { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, EnumValueDefinition $value, diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php index 6577a9fa..fc3320eb 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php @@ -4,6 +4,8 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\EnumValueDefinition; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -27,7 +29,8 @@ public function testToString( int $used, EnumValueDefinition $type, ): void { - $actual = (string) (new EnumValueDefinitionBlock($settings, $level, $used, $type)); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $actual = (string) (new EnumValueDefinitionBlock($settings, $level, $used, $type)); Parser::enumValueDefinition($actual); diff --git a/src/SchemaPrinter/Blocks/Types/EnumValuesDefinitionList.php b/src/SchemaPrinter/Blocks/Types/EnumValuesDefinitionList.php index 3d39622b..bc4ca274 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValuesDefinitionList.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValuesDefinitionList.php @@ -3,8 +3,8 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\EnumValueDefinition; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\ObjectBlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use Traversable; /** @@ -16,7 +16,7 @@ class EnumValuesDefinitionList extends ObjectBlockList { * @param Traversable|array $values */ public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, Traversable|array $values, diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php index 7feccf51..e0da0ae8 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\FieldDefinition; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; /** * @internal @@ -13,7 +13,7 @@ */ class FieldDefinitionBlock extends DefinitionBlock { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, FieldDefinition $definition, diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php index f0922c44..2773a573 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php @@ -8,6 +8,8 @@ use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -31,7 +33,8 @@ public function testToString( int $used, FieldDefinition $definition, ): void { - $actual = (string) (new FieldDefinitionBlock($settings, $level, $used, $definition)); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $actual = (string) (new FieldDefinitionBlock($settings, $level, $used, $definition)); Parser::fieldDefinition($actual); @@ -43,6 +46,7 @@ public function testToString( */ public function testStatistics(): void { $settings = new TestSettings(); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); $definition = FieldDefinition::create([ 'name' => 'A', 'type' => new NonNull( diff --git a/src/SchemaPrinter/Blocks/Types/FieldsDefinitionList.php b/src/SchemaPrinter/Blocks/Types/FieldsDefinitionList.php index 404bf651..72a8fe2e 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldsDefinitionList.php +++ b/src/SchemaPrinter/Blocks/Types/FieldsDefinitionList.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\FieldDefinition; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use Traversable; /** @@ -16,7 +16,7 @@ class FieldsDefinitionList extends BlockList { * @param Traversable|array $fields */ public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, Traversable|array $fields, diff --git a/src/SchemaPrinter/Blocks/Types/InputFieldsDefinitionList.php b/src/SchemaPrinter/Blocks/Types/InputFieldsDefinitionList.php index be6df53a..6e9b59d3 100644 --- a/src/SchemaPrinter/Blocks/Types/InputFieldsDefinitionList.php +++ b/src/SchemaPrinter/Blocks/Types/InputFieldsDefinitionList.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\InputObjectField; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use Traversable; /** @@ -16,7 +16,7 @@ class InputFieldsDefinitionList extends BlockList { * @param Traversable|array $fields */ public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, Traversable|array $fields, diff --git a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php index aee21928..47c7bc86 100644 --- a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\InputObjectType; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use function mb_strlen; @@ -15,7 +15,7 @@ */ class InputObjectTypeDefinitionBlock extends DefinitionBlock { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, InputObjectType $definition, diff --git a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php index 25c20a05..db60d037 100644 --- a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php @@ -5,6 +5,8 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\Type; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -28,7 +30,8 @@ public function testToString( int $used, InputObjectType $definition, ): void { - $actual = (string) (new InputObjectTypeDefinitionBlock( + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $actual = (string) (new InputObjectTypeDefinitionBlock( $settings, $level, $used, @@ -45,6 +48,7 @@ public function testToString( */ public function testStatistics(): void { $settings = new TestSettings(); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); $definition = new InputObjectType([ 'name' => 'A', 'fields' => [ diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php index a8fa29e5..6d817a68 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php @@ -8,7 +8,7 @@ use GraphQL\Utils\AST; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\ValueNodeBlock; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use function mb_strlen; @@ -19,7 +19,7 @@ */ class InputValueDefinitionBlock extends DefinitionBlock { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, FieldArgument|InputObjectField $definition, diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php index 2d49bea7..7337c769 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php @@ -8,6 +8,8 @@ use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -31,7 +33,8 @@ public function testToString( int $used, FieldArgument $definition, ): void { - $actual = (string) (new InputValueDefinitionBlock($settings, $level, $used, $definition)); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $actual = (string) (new InputValueDefinitionBlock($settings, $level, $used, $definition)); Parser::inputValueDefinition($actual); @@ -42,7 +45,7 @@ public function testToString( * @covers ::__toString */ public function testStatistics(): void { - $settings = new TestSettings(); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), new TestSettings()); $definition = new FieldArgument([ 'name' => 'a', 'type' => new NonNull( diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php index d948be72..deafc4ea 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php @@ -3,7 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\InterfaceType; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; /** * @internal @@ -12,7 +12,7 @@ */ class InterfaceTypeDefinitionBlock extends TypeDefinitionBlock { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, InterfaceType $definition, diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php index 6f2a86b0..6defab6c 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php @@ -6,6 +6,8 @@ use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -29,7 +31,8 @@ public function testToString( int $used, InterfaceType $definition, ): void { - $actual = (string) (new InterfaceTypeDefinitionBlock($settings, $level, $used, $definition)); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $actual = (string) (new InterfaceTypeDefinitionBlock($settings, $level, $used, $definition)); Parser::interfaceTypeDefinition($actual); @@ -41,6 +44,7 @@ public function testToString( */ public function testStatistics(): void { $settings = new TestSettings(); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); $definition = new InterfaceType([ 'name' => 'A', 'fields' => [ diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php index 5bfba18e..4273196b 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php @@ -3,7 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\ObjectType; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; /** * @internal @@ -12,7 +12,7 @@ */ class ObjectTypeDefinitionBlock extends TypeDefinitionBlock { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, ObjectType $definition, diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php index a484185c..51fae241 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php @@ -6,6 +6,8 @@ use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -29,7 +31,8 @@ public function testToString( int $used, ObjectType $definition, ): void { - $actual = (string) (new ObjectTypeDefinitionBlock($settings, $level, $used, $definition)); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $actual = (string) (new ObjectTypeDefinitionBlock($settings, $level, $used, $definition)); Parser::objectTypeDefinition($actual); @@ -41,6 +44,7 @@ public function testToString( */ public function testStatistics(): void { $settings = new TestSettings(); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); $definition = new ObjectType([ 'name' => 'A', 'fields' => [ diff --git a/src/SchemaPrinter/Blocks/Types/RootOperationTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/RootOperationTypeDefinitionBlock.php index d7f369af..c893b169 100644 --- a/src/SchemaPrinter/Blocks/Types/RootOperationTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/RootOperationTypeDefinitionBlock.php @@ -3,14 +3,14 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\ObjectType; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; /** * @internal */ class RootOperationTypeDefinitionBlock extends TypeBlock { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, private OperationType $operation, diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php index 4686e795..874d3074 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\ScalarType; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; /** * @internal @@ -13,7 +13,7 @@ */ class ScalarTypeDefinitionBlock extends DefinitionBlock { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, ScalarType $definition, diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php index e4956603..d213b4a4 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php @@ -5,6 +5,8 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\CustomScalarType; use GraphQL\Type\Definition\ScalarType; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -28,7 +30,8 @@ public function testToString( int $used, ScalarType $type, ): void { - $actual = (string) (new ScalarTypeDefinitionBlock($settings, $level, $used, $type)); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $actual = (string) (new ScalarTypeDefinitionBlock($settings, $level, $used, $type)); Parser::scalarTypeDefinition($actual); diff --git a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php index 6f1fd611..ffd55b11 100644 --- a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php @@ -5,7 +5,7 @@ use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Schema; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use function array_filter; use function count; @@ -20,7 +20,7 @@ */ class SchemaDefinitionBlock extends DefinitionBlock { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, Schema $definition, diff --git a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php index e669f1ac..798092df 100644 --- a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php @@ -5,6 +5,8 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Schema; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -30,7 +32,8 @@ public function testToString( int $used, Schema $schema, ): void { - $actual = (string) (new SchemaDefinitionBlock($settings, $level, $used, $schema)); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $actual = (string) (new SchemaDefinitionBlock($settings, $level, $used, $schema)); if ($expected && !str_starts_with($actual, '"""')) { // https://github.com/webonyx/graphql-php/issues/1027 diff --git a/src/SchemaPrinter/Blocks/Types/StringBlock.php b/src/SchemaPrinter/Blocks/Types/StringBlock.php index b55c46ca..3a3f5ba0 100644 --- a/src/SchemaPrinter/Blocks/Types/StringBlock.php +++ b/src/SchemaPrinter/Blocks/Types/StringBlock.php @@ -5,7 +5,7 @@ use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\Printer; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use function mb_strlen; use function preg_match; @@ -18,7 +18,7 @@ */ class StringBlock extends Block { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, protected string $string, diff --git a/src/SchemaPrinter/Blocks/Types/StringBlockTest.php b/src/SchemaPrinter/Blocks/Types/StringBlockTest.php index 31486630..7a7226b3 100644 --- a/src/SchemaPrinter/Blocks/Types/StringBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/StringBlockTest.php @@ -4,6 +4,8 @@ use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\Parser; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -29,8 +31,9 @@ public function testToString( int $used, string $string, ): void { - $actual = (string) new StringBlock($settings, $level, $used, $string); - $parsed = Parser::valueLiteral($actual); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $actual = (string) new StringBlock($settings, $level, $used, $string); + $parsed = Parser::valueLiteral($actual); self::assertInstanceOf(StringValueNode::class, $parsed); self::assertEquals($expected, $actual); diff --git a/src/SchemaPrinter/Blocks/Types/TypeBlock.php b/src/SchemaPrinter/Blocks/Types/TypeBlock.php index 18d66062..dbc054cc 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeBlock.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlock.php @@ -5,15 +5,15 @@ use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\WrappingType; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; /** * @internal */ class TypeBlock extends Block implements Named { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, private Type $definition, diff --git a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php index 98ddde57..d622eb84 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php @@ -6,6 +6,8 @@ use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -29,7 +31,8 @@ public function testToString( int $used, Type $type, ): void { - $actual = (string) (new TypeBlock($settings, $level, $used, $type)); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $actual = (string) (new TypeBlock($settings, $level, $used, $type)); self::assertEquals($expected, $actual); } @@ -44,6 +47,7 @@ public function testStatistics(): void { ]), ); $settings = new TestSettings(); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); $block = new TypeBlock($settings, 0, 0, $node); $type = $node->getWrappedType(true)->name; diff --git a/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php index 5ec16081..62864117 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php @@ -5,7 +5,7 @@ use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\ObjectType; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use function mb_strlen; @@ -21,7 +21,7 @@ abstract class TypeDefinitionBlock extends DefinitionBlock { * @param TType $definition */ public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, InterfaceType|ObjectType $definition, diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php index 238eeb34..47d69fc7 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\UnionType; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use function mb_strlen; @@ -15,7 +15,7 @@ */ class UnionTypeDefinitionBlock extends DefinitionBlock { public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, UnionType $definition, diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php index 4b09ac8c..dfeaa181 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php @@ -5,6 +5,8 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\UnionType; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -18,7 +20,7 @@ class UnionTypeDefinitionBlockTest extends TestCase { // ========================================================================= /** * @covers ::__toString - * @covers \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\UnionMemberTypesList::__toString + * @covers \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\UnionMemberTypesList::__toString * * @dataProvider dataProviderToString */ @@ -29,7 +31,8 @@ public function testToString( int $used, UnionType $type, ): void { - $actual = (string) (new UnionTypeDefinitionBlock($settings, $level, $used, $type)); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $actual = (string) (new UnionTypeDefinitionBlock($settings, $level, $used, $type)); Parser::unionTypeDefinition($actual); @@ -53,6 +56,7 @@ public function testStatistics(): void { 'astNode' => Parser::unionTypeDefinition('union Test @a = A | B'), ]); $settings = new TestSettings(); + $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); $block = new UnionTypeDefinitionBlock($settings, 0, 0, $union); self::assertNotEmpty((string) $block); diff --git a/src/SchemaPrinter/Blocks/Types/UsageList.php b/src/SchemaPrinter/Blocks/Types/UsageList.php index ee684574..0857ae2d 100644 --- a/src/SchemaPrinter/Blocks/Types/UsageList.php +++ b/src/SchemaPrinter/Blocks/Types/UsageList.php @@ -4,7 +4,7 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use Traversable; use function mb_strlen; @@ -20,7 +20,7 @@ abstract class UsageList extends BlockList { * @param Traversable|array $items */ public function __construct( - Settings $settings, + BlockSettings $settings, int $level, int $used, Traversable|array $items, diff --git a/src/SchemaPrinter/Contracts/DirectiveFilter.php b/src/SchemaPrinter/Contracts/DirectiveFilter.php index 176ea4e9..24116c3e 100644 --- a/src/SchemaPrinter/Contracts/DirectiveFilter.php +++ b/src/SchemaPrinter/Contracts/DirectiveFilter.php @@ -2,8 +2,9 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts; -use GraphQL\Language\AST\DirectiveNode; +use GraphQL\Type\Definition\Directive as GraphQLDirective; +use Nuwave\Lighthouse\Support\Contracts\Directive as LighthouseDirective; interface DirectiveFilter { - public function isAllowedDirective(DirectiveNode $directive): bool; + public function isAllowedDirective(GraphQLDirective|LighthouseDirective $directive): bool; } diff --git a/src/SchemaPrinter/DirectiveResolver.php b/src/SchemaPrinter/DirectiveResolver.php index 270ca832..10ad623b 100644 --- a/src/SchemaPrinter/DirectiveResolver.php +++ b/src/SchemaPrinter/DirectiveResolver.php @@ -2,26 +2,62 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter; -use GraphQL\Type\Definition\Directive; +use GraphQL\Type\Definition\Directive as GraphQLDirective; use Nuwave\Lighthouse\Schema\AST\ASTHelper; use Nuwave\Lighthouse\Schema\DirectiveLocator; use Nuwave\Lighthouse\Schema\ExecutableTypeNodeConverter; use Nuwave\Lighthouse\Schema\Factories\DirectiveFactory; +use Nuwave\Lighthouse\Support\Contracts\Directive as LighthouseDirective; +/** + * Class helps us to search defined directives and convert AST nodes into + * `Directive` instances. + * + * This is required because despite GraphQL-PHP supports custom directives it + * doesn't allow to add them into Types and after parsing the scheme they will + * be available only inside `astNode` as an array of `DirectiveDefinitionNode`. + * On another hand, Lighthouse uses its own Directive Locator to associate + * directives with classes. + * + * @see https://webonyx.github.io/graphql-php/type-definitions/directives/ + */ class DirectiveResolver { protected DirectiveFactory $factory; + /** + * @var array + */ + protected array $directives; + + /** + * @param array $directives + */ public function __construct( protected DirectiveLocator $locator, protected ExecutableTypeNodeConverter $converter, + array $directives = [], ) { - $this->factory = new DirectiveFactory($this->converter); + $this->factory = new DirectiveFactory($this->converter); + $this->directives = []; + + foreach ($directives as $directive) { + $this->directives[$directive->name] = $directive; + } } - public function get(string $name): Directive { - $definition = $this->locator->resolve($name)::definition(); - $directive = $this->factory->handle(ASTHelper::extractDirectiveDefinition($definition)); + public function getDefinition(string $name): GraphQLDirective { + $directive = $this->directives[$name] ?? null; + + if (!$directive) { + $definition = $this->locator->resolve($name)::definition(); + $definition = ASTHelper::extractDirectiveDefinition($definition); + $directive = $this->factory->handle($definition); + } return $directive; } + + public function getInstance(string $name): GraphQLDirective|LighthouseDirective { + return $this->directives[$name] ?? $this->locator->create($name); + } } diff --git a/src/Testing/Package/SchemaPrinter/TestSettings.php b/src/Testing/Package/SchemaPrinter/TestSettings.php index a537fc46..3bbeb235 100644 --- a/src/Testing/Package/SchemaPrinter/TestSettings.php +++ b/src/Testing/Package/SchemaPrinter/TestSettings.php @@ -3,9 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter; use Closure; -use GraphQL\Language\AST\DirectiveNode; +use GraphQL\Type\Definition\Directive as GraphQLDirective; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\DirectiveFilter; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\ImmutableSettings; +use Nuwave\Lighthouse\Support\Contracts\Directive as LighthouseDirective; class TestSettings extends ImmutableSettings { protected string $space = ' '; @@ -31,7 +32,7 @@ class TestSettings extends ImmutableSettings { protected ?DirectiveFilter $directiveFilter = null; /** - * @param DirectiveFilter|Closure(DirectiveNode):bool|null $value + * @param DirectiveFilter|Closure(GraphQLDirective|LighthouseDirective):bool|null $value */ public function setDirectiveFilter(DirectiveFilter|Closure|null $value): static { if ($value instanceof Closure) { @@ -42,7 +43,7 @@ public function __construct( // empty } - public function isAllowedDirective(DirectiveNode $directive): bool { + public function isAllowedDirective(GraphQLDirective|LighthouseDirective $directive): bool { return ($this->filter)($directive); } }; From a7c1f83ac4c9168f75dd0ca13ce8b24f95cbdd08 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 30 Jan 2022 12:15:54 +0400 Subject: [PATCH 78/90] `DirectiveResolver` and `PrinterSettings` (previous `BlockSettings`) moved to `Misc` namespace. --- .../Blocks/Ast/ArgumentNodeList.php | 4 ++-- .../Blocks/Ast/DirectiveNodeBlock.php | 4 ++-- .../Blocks/Ast/DirectiveNodeBlockTest.php | 8 ++++---- .../Blocks/Ast/DirectiveNodeList.php | 4 ++-- .../Blocks/Ast/DirectiveNodeListTest.php | 8 ++++---- src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php | 4 ++-- src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php | 6 +++--- src/SchemaPrinter/Blocks/Block.php | 5 +++-- src/SchemaPrinter/Blocks/BlockListTest.php | 7 ++++--- src/SchemaPrinter/Blocks/BlockTest.php | 13 +++++++------ .../Blocks/Printer/DefinitionBlock.php | 4 ++-- .../Blocks/Printer/DefinitionList.php | 4 ++-- src/SchemaPrinter/Blocks/Property.php | 4 +++- src/SchemaPrinter/Blocks/PropertyTest.php | 9 +++++---- .../Blocks/Types/ArgumentsDefinitionList.php | 4 ++-- .../Blocks/Types/DefinitionBlock.php | 4 ++-- src/SchemaPrinter/Blocks/Types/Description.php | 4 ++-- .../Blocks/Types/DescriptionTest.php | 6 +++--- .../Blocks/Types/DirectiveDefinitionBlock.php | 4 ++-- .../Types/DirectiveDefinitionBlockTest.php | 8 ++++---- .../Blocks/Types/DirectiveLocationBlock.php | 4 ++-- .../Blocks/Types/EnumTypeDefinitionBlock.php | 4 ++-- .../Blocks/Types/EnumTypeDefinitionBlockTest.php | 6 +++--- .../Blocks/Types/EnumValueDefinitionBlock.php | 4 ++-- .../Types/EnumValueDefinitionBlockTest.php | 6 +++--- .../Blocks/Types/EnumValuesDefinitionList.php | 4 ++-- .../Blocks/Types/FieldDefinitionBlock.php | 4 ++-- .../Blocks/Types/FieldDefinitionBlockTest.php | 8 ++++---- .../Blocks/Types/FieldsDefinitionList.php | 4 ++-- .../Blocks/Types/InputFieldsDefinitionList.php | 4 ++-- .../Types/InputObjectTypeDefinitionBlock.php | 4 ++-- .../Types/InputObjectTypeDefinitionBlockTest.php | 8 ++++---- .../Blocks/Types/InputValueDefinitionBlock.php | 4 ++-- .../Types/InputValueDefinitionBlockTest.php | 8 ++++---- .../Types/InterfaceTypeDefinitionBlock.php | 4 ++-- .../Types/InterfaceTypeDefinitionBlockTest.php | 8 ++++---- .../Blocks/Types/ObjectTypeDefinitionBlock.php | 4 ++-- .../Types/ObjectTypeDefinitionBlockTest.php | 8 ++++---- .../Types/RootOperationTypeDefinitionBlock.php | 4 ++-- .../Blocks/Types/ScalarTypeDefinitionBlock.php | 4 ++-- .../Types/ScalarTypeDefinitionBlockTest.php | 6 +++--- .../Blocks/Types/SchemaDefinitionBlock.php | 4 ++-- .../Blocks/Types/SchemaDefinitionBlockTest.php | 6 +++--- src/SchemaPrinter/Blocks/Types/StringBlock.php | 4 ++-- .../Blocks/Types/StringBlockTest.php | 6 +++--- src/SchemaPrinter/Blocks/Types/TypeBlock.php | 4 ++-- src/SchemaPrinter/Blocks/Types/TypeBlockTest.php | 8 ++++---- .../Blocks/Types/TypeDefinitionBlock.php | 4 ++-- .../Blocks/Types/UnionTypeDefinitionBlock.php | 4 ++-- .../Types/UnionTypeDefinitionBlockTest.php | 8 ++++---- src/SchemaPrinter/Blocks/Types/UsageList.php | 4 ++-- .../{ => Misc}/DirectiveResolver.php | 16 +++++++++++++++- .../PrinterSettings.php} | 12 +++++++++--- 53 files changed, 165 insertions(+), 139 deletions(-) rename src/SchemaPrinter/{ => Misc}/DirectiveResolver.php (82%) rename src/SchemaPrinter/{Blocks/BlockSettings.php => Misc/PrinterSettings.php} (90%) diff --git a/src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php b/src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php index 98bfe559..fc23a871 100644 --- a/src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php +++ b/src/SchemaPrinter/Blocks/Ast/ArgumentNodeList.php @@ -4,8 +4,8 @@ use GraphQL\Language\AST\ArgumentNode; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Property; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use Traversable; /** @@ -17,7 +17,7 @@ class ArgumentNodeList extends BlockList { * @param Traversable|array $arguments */ public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, Traversable|array $arguments, diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php index b98784ab..3e3844ed 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php @@ -4,8 +4,8 @@ use GraphQL\Language\AST\DirectiveNode; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use function mb_strlen; @@ -14,7 +14,7 @@ */ class DirectiveNodeBlock extends Block implements Named { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, private DirectiveNode $node, diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php index 3b4f6a71..b75e0494 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php @@ -5,8 +5,8 @@ use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\Parser; use GraphQL\Language\Printer; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -30,7 +30,7 @@ public function testToString( int $used, DirectiveNode $node, ): void { - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $actual = (string) (new DirectiveNodeBlock($settings, $level, $used, $node)); $parsed = Parser::directive($actual); @@ -49,7 +49,7 @@ public function testToString( */ public function testStatistics(): void { $settings = new TestSettings(); - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $node = Parser::directive('@test'); $block = new DirectiveNodeBlock($settings, 0, 0, $node); diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php index 32fe9bd7..d5b7c0d8 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeList.php @@ -7,7 +7,7 @@ use GraphQL\Type\Definition\Directive; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use Traversable; use function json_encode; @@ -21,7 +21,7 @@ class DirectiveNodeList extends BlockList { * @param Traversable|array $directives */ public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, Traversable|array|null $directives, diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php index 6392f1c6..fc598c4a 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php @@ -6,8 +6,8 @@ use GraphQL\Language\DirectiveLocation; use GraphQL\Language\Parser; use GraphQL\Type\Definition\Directive as GraphQLDirective; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -49,7 +49,7 @@ public function testToString( } $resolver = new DirectiveResolver($locator, $convertor, $instances); - $settings = new BlockSettings($resolver, $settings); + $settings = new PrinterSettings($resolver, $settings); $actual = (string) (new DirectiveNodeList($settings, $level, $used, $directives, $reason)); Parser::directives($actual); @@ -64,7 +64,7 @@ public function testStatistics(): void { $a = Parser::directive('@a'); $b = Parser::directive('@b'); $settings = (new TestSettings())->setPrintDirectives(true); - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $block = new DirectiveNodeList($settings, 0, 0, [$a, $b]); self::assertNotEmpty((string) $block); diff --git a/src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php b/src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php index c61d2f9a..84d7ca4d 100644 --- a/src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php +++ b/src/SchemaPrinter/Blocks/Ast/ValueNodeBlock.php @@ -9,16 +9,16 @@ use GraphQL\Language\AST\ValueNode; use GraphQL\Language\Printer; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Property; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\StringBlock; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; class ValueNodeBlock extends Block { /** * @param ValueNode&Node $node */ public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, protected ValueNode $node, diff --git a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php index 5ecb9966..dc9f032d 100644 --- a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php +++ b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php @@ -15,8 +15,8 @@ use GraphQL\Language\AST\VariableNode; use GraphQL\Language\Parser; use GraphQL\Language\Printer; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -42,7 +42,7 @@ public function testToString( int $used, ValueNode $node, ): void { - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $actual = (string) (new ValueNodeBlock($settings, $level, $used, $node)); $parsed = Parser::valueLiteral($actual); diff --git a/src/SchemaPrinter/Blocks/Block.php b/src/SchemaPrinter/Blocks/Block.php index bee0563b..0cfdb175 100644 --- a/src/SchemaPrinter/Blocks/Block.php +++ b/src/SchemaPrinter/Blocks/Block.php @@ -3,6 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; use Closure; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Statistics; use Stringable; @@ -29,7 +30,7 @@ abstract class Block implements Statistics, Stringable { private array $usedDirectives = []; public function __construct( - private BlockSettings $settings, + private PrinterSettings $settings, private int $level = 0, private int $used = 0, ) { @@ -38,7 +39,7 @@ public function __construct( // // ========================================================================= - protected function getSettings(): BlockSettings { + protected function getSettings(): PrinterSettings { return $this->settings; } diff --git a/src/SchemaPrinter/Blocks/BlockListTest.php b/src/SchemaPrinter/Blocks/BlockListTest.php index 5a1dc214..a3527693 100644 --- a/src/SchemaPrinter/Blocks/BlockListTest.php +++ b/src/SchemaPrinter/Blocks/BlockListTest.php @@ -2,7 +2,8 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -39,7 +40,7 @@ public function testToString( array $blocks, int $count, ): void { - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $list = new BlockListTest__BlockList( $settings, $level, @@ -66,7 +67,7 @@ public function testToString( */ public function testStatistics(): void { $settings = new TestSettings(); - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $list = new class($settings) extends BlockList { // empty }; diff --git a/src/SchemaPrinter/Blocks/BlockTest.php b/src/SchemaPrinter/Blocks/BlockTest.php index f827a677..1d7e23a0 100644 --- a/src/SchemaPrinter/Blocks/BlockTest.php +++ b/src/SchemaPrinter/Blocks/BlockTest.php @@ -2,7 +2,8 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -22,7 +23,7 @@ class BlockTest extends TestCase { */ public function testGetContent(): void { $settings = new TestSettings(); - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $content = 'content'; $block = Mockery::mock(BlockTest__Block::class, [$settings]); $block->shouldAllowMockingProtectedMethods(); @@ -41,7 +42,7 @@ public function testGetContent(): void { */ public function testGetLength(): void { $settings = new TestSettings(); - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $content = 'content'; $length = mb_strlen($content); $block = Mockery::mock(BlockTest__Block::class, [$settings]); @@ -62,7 +63,7 @@ public function testGetLength(): void { * @dataProvider dataProviderIsMultiline */ public function testIsMultiline(bool $expected, Settings $settings, string $content): void { - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $block = Mockery::mock(BlockTest__Block::class, [$settings]); $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); @@ -82,7 +83,7 @@ public function testIsMultiline(bool $expected, Settings $settings, string $cont */ public function testIsEmpty(bool $expected, string $content): void { $settings = new TestSettings(); - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $block = Mockery::mock(BlockTest__Block::class, [$settings]); $block->shouldAllowMockingProtectedMethods(); $block->makePartial(); @@ -98,7 +99,7 @@ public function testIsEmpty(bool $expected, string $content): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderIsMultiline(): array { $settings = new TestSettings(); diff --git a/src/SchemaPrinter/Blocks/Printer/DefinitionBlock.php b/src/SchemaPrinter/Blocks/Printer/DefinitionBlock.php index 8ab47fbf..07f479a8 100644 --- a/src/SchemaPrinter/Blocks/Printer/DefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Printer/DefinitionBlock.php @@ -12,7 +12,6 @@ use GraphQL\Type\Definition\UnionType; use GraphQL\Type\Schema; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\DirectiveDefinitionBlock; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\EnumTypeDefinitionBlock; @@ -23,6 +22,7 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\SchemaDefinitionBlock; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types\UnionTypeDefinitionBlock; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Exceptions\TypeUnsupported; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; /** * @internal @@ -31,7 +31,7 @@ class DefinitionBlock extends Block implements Named { private Block $block; public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, Schema|Type|Directive $definition, ) { diff --git a/src/SchemaPrinter/Blocks/Printer/DefinitionList.php b/src/SchemaPrinter/Blocks/Printer/DefinitionList.php index ab7fd001..ab5b697c 100644 --- a/src/SchemaPrinter/Blocks/Printer/DefinitionList.php +++ b/src/SchemaPrinter/Blocks/Printer/DefinitionList.php @@ -4,7 +4,7 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use function rtrim; @@ -14,7 +14,7 @@ */ class DefinitionList extends BlockList { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, protected bool $schema = false, ) { diff --git a/src/SchemaPrinter/Blocks/Property.php b/src/SchemaPrinter/Blocks/Property.php index f117b830..936b9931 100644 --- a/src/SchemaPrinter/Blocks/Property.php +++ b/src/SchemaPrinter/Blocks/Property.php @@ -3,6 +3,8 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; + /** * @internal * @@ -13,7 +15,7 @@ class Property extends Block implements Named { * @param TBlock $block */ public function __construct( - BlockSettings $settings, + PrinterSettings $settings, private string $name, private Block $block, ) { diff --git a/src/SchemaPrinter/Blocks/PropertyTest.php b/src/SchemaPrinter/Blocks/PropertyTest.php index cd412aff..8f734430 100644 --- a/src/SchemaPrinter/Blocks/PropertyTest.php +++ b/src/SchemaPrinter/Blocks/PropertyTest.php @@ -2,7 +2,8 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -27,10 +28,10 @@ public function testToString(): void { $separator = ':'; $content = 'abc abcabc abcabc abcabc abc'; $settings = (new TestSettings())->setSpace($space); - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $block = new class($settings, $level, $used, $content) extends Block { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, protected string $content, @@ -44,7 +45,7 @@ protected function content(): string { }; $property = new class($settings, $name, $block, $separator) extends Property { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, string $name, Block $block, private string $separator, diff --git a/src/SchemaPrinter/Blocks/Types/ArgumentsDefinitionList.php b/src/SchemaPrinter/Blocks/Types/ArgumentsDefinitionList.php index bcbdb1d4..9161684f 100644 --- a/src/SchemaPrinter/Blocks/Types/ArgumentsDefinitionList.php +++ b/src/SchemaPrinter/Blocks/Types/ArgumentsDefinitionList.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\FieldArgument; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use Traversable; /** @@ -16,7 +16,7 @@ class ArgumentsDefinitionList extends BlockList { * @param Traversable|array $arguments */ public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, Traversable|array $arguments, diff --git a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php index b1f1b83a..ca7e6f27 100644 --- a/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DefinitionBlock.php @@ -13,8 +13,8 @@ use GraphQL\Type\Schema; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use function mb_strlen; @@ -28,7 +28,7 @@ abstract class DefinitionBlock extends Block implements Named { * @param TType $definition */ public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, private Type|FieldDefinition|EnumValueDefinition|FieldArgument|Directive|InputObjectField|Schema $definition, diff --git a/src/SchemaPrinter/Blocks/Types/Description.php b/src/SchemaPrinter/Blocks/Types/Description.php index c29493ae..2382a15e 100644 --- a/src/SchemaPrinter/Blocks/Types/Description.php +++ b/src/SchemaPrinter/Blocks/Types/Description.php @@ -3,7 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use function preg_replace; use function rtrim; @@ -15,7 +15,7 @@ */ class Description extends StringBlock { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, ?string $string, diff --git a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php index 6c261602..c180ea33 100644 --- a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php +++ b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php @@ -5,8 +5,8 @@ use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\Parser; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -35,7 +35,7 @@ public function testToString( ?string $description, ?array $directives, ): void { - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $directives = new DirectiveNodeList($settings, $level, $used, $directives); $actual = (string) (new Description($settings, $level, $used, $description, $directives)); diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlock.php index 8fe578b0..6feb0d3b 100644 --- a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlock.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\Directive; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use function mb_strlen; @@ -15,7 +15,7 @@ */ class DirectiveDefinitionBlock extends DefinitionBlock { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, Directive $definition, diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php index 49032b9b..d17c9893 100644 --- a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php @@ -7,8 +7,8 @@ use GraphQL\Type\Definition\Directive; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -32,7 +32,7 @@ public function testToString( int $used, Directive $definition, ): void { - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $actual = (string) (new DirectiveDefinitionBlock($settings, $level, $used, $definition)); Parser::directiveDefinition($actual); @@ -45,7 +45,7 @@ public function testToString( */ public function testStatistics(): void { $settings = new TestSettings(); - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $definition = new Directive([ 'name' => 'A', 'args' => [ diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveLocationBlock.php b/src/SchemaPrinter/Blocks/Types/DirectiveLocationBlock.php index 8ebd245d..a1a4367f 100644 --- a/src/SchemaPrinter/Blocks/Types/DirectiveLocationBlock.php +++ b/src/SchemaPrinter/Blocks/Types/DirectiveLocationBlock.php @@ -3,15 +3,15 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; /** * @internal */ class DirectiveLocationBlock extends Block implements Named { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, private string $location, diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php index 9ffb5710..191b0552 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlock.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\EnumType; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use function mb_strlen; @@ -15,7 +15,7 @@ */ class EnumTypeDefinitionBlock extends DefinitionBlock { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, EnumType $definition, diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php index e53a039b..af5e4076 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php @@ -7,8 +7,8 @@ use GraphQL\Type\Definition\Directive; use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\EnumValueDefinition; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -37,7 +37,7 @@ public function testToString( $type = $type(); } - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $actual = (string) (new EnumTypeDefinitionBlock($settings, $level, $used, $type)); Parser::enumTypeDefinition($actual); diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php index 59795c3a..a84a0398 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlock.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\EnumValueDefinition; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; /** * @internal @@ -12,7 +12,7 @@ */ class EnumValueDefinitionBlock extends DefinitionBlock { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, EnumValueDefinition $value, diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php index fc3320eb..b34bf50b 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php @@ -4,8 +4,8 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\EnumValueDefinition; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -29,7 +29,7 @@ public function testToString( int $used, EnumValueDefinition $type, ): void { - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $actual = (string) (new EnumValueDefinitionBlock($settings, $level, $used, $type)); Parser::enumValueDefinition($actual); diff --git a/src/SchemaPrinter/Blocks/Types/EnumValuesDefinitionList.php b/src/SchemaPrinter/Blocks/Types/EnumValuesDefinitionList.php index bc4ca274..a620aaf1 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValuesDefinitionList.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValuesDefinitionList.php @@ -3,8 +3,8 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\EnumValueDefinition; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\ObjectBlockList; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use Traversable; /** @@ -16,7 +16,7 @@ class EnumValuesDefinitionList extends ObjectBlockList { * @param Traversable|array $values */ public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, Traversable|array $values, diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php index e0da0ae8..5e9f3653 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlock.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\FieldDefinition; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; /** * @internal @@ -13,7 +13,7 @@ */ class FieldDefinitionBlock extends DefinitionBlock { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, FieldDefinition $definition, diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php index 2773a573..c3f1ccdc 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php @@ -8,8 +8,8 @@ use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -33,7 +33,7 @@ public function testToString( int $used, FieldDefinition $definition, ): void { - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $actual = (string) (new FieldDefinitionBlock($settings, $level, $used, $definition)); Parser::fieldDefinition($actual); @@ -46,7 +46,7 @@ public function testToString( */ public function testStatistics(): void { $settings = new TestSettings(); - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $definition = FieldDefinition::create([ 'name' => 'A', 'type' => new NonNull( diff --git a/src/SchemaPrinter/Blocks/Types/FieldsDefinitionList.php b/src/SchemaPrinter/Blocks/Types/FieldsDefinitionList.php index 72a8fe2e..c7c1b66e 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldsDefinitionList.php +++ b/src/SchemaPrinter/Blocks/Types/FieldsDefinitionList.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\FieldDefinition; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use Traversable; /** @@ -16,7 +16,7 @@ class FieldsDefinitionList extends BlockList { * @param Traversable|array $fields */ public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, Traversable|array $fields, diff --git a/src/SchemaPrinter/Blocks/Types/InputFieldsDefinitionList.php b/src/SchemaPrinter/Blocks/Types/InputFieldsDefinitionList.php index 6e9b59d3..4e5a6d84 100644 --- a/src/SchemaPrinter/Blocks/Types/InputFieldsDefinitionList.php +++ b/src/SchemaPrinter/Blocks/Types/InputFieldsDefinitionList.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\InputObjectField; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use Traversable; /** @@ -16,7 +16,7 @@ class InputFieldsDefinitionList extends BlockList { * @param Traversable|array $fields */ public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, Traversable|array $fields, diff --git a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php index 47c7bc86..dc33a211 100644 --- a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlock.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\InputObjectType; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use function mb_strlen; @@ -15,7 +15,7 @@ */ class InputObjectTypeDefinitionBlock extends DefinitionBlock { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, InputObjectType $definition, diff --git a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php index db60d037..7885d731 100644 --- a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php @@ -5,8 +5,8 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\Type; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -30,7 +30,7 @@ public function testToString( int $used, InputObjectType $definition, ): void { - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $actual = (string) (new InputObjectTypeDefinitionBlock( $settings, $level, @@ -48,7 +48,7 @@ public function testToString( */ public function testStatistics(): void { $settings = new TestSettings(); - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $definition = new InputObjectType([ 'name' => 'A', 'fields' => [ diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php index 6d817a68..f6da2bae 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlock.php @@ -8,7 +8,7 @@ use GraphQL\Utils\AST; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\ValueNodeBlock; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use function mb_strlen; @@ -19,7 +19,7 @@ */ class InputValueDefinitionBlock extends DefinitionBlock { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, FieldArgument|InputObjectField $definition, diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php index 7337c769..d9b1fddc 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php @@ -8,8 +8,8 @@ use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -33,7 +33,7 @@ public function testToString( int $used, FieldArgument $definition, ): void { - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $actual = (string) (new InputValueDefinitionBlock($settings, $level, $used, $definition)); Parser::inputValueDefinition($actual); @@ -45,7 +45,7 @@ public function testToString( * @covers ::__toString */ public function testStatistics(): void { - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), new TestSettings()); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), new TestSettings()); $definition = new FieldArgument([ 'name' => 'a', 'type' => new NonNull( diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php index deafc4ea..36ed4429 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlock.php @@ -3,7 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\InterfaceType; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; /** * @internal @@ -12,7 +12,7 @@ */ class InterfaceTypeDefinitionBlock extends TypeDefinitionBlock { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, InterfaceType $definition, diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php index 6defab6c..3d268248 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php @@ -6,8 +6,8 @@ use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -31,7 +31,7 @@ public function testToString( int $used, InterfaceType $definition, ): void { - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $actual = (string) (new InterfaceTypeDefinitionBlock($settings, $level, $used, $definition)); Parser::interfaceTypeDefinition($actual); @@ -44,7 +44,7 @@ public function testToString( */ public function testStatistics(): void { $settings = new TestSettings(); - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $definition = new InterfaceType([ 'name' => 'A', 'fields' => [ diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php index 4273196b..ebe3419a 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlock.php @@ -3,7 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\ObjectType; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; /** * @internal @@ -12,7 +12,7 @@ */ class ObjectTypeDefinitionBlock extends TypeDefinitionBlock { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, ObjectType $definition, diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php index 51fae241..f1ced00f 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php @@ -6,8 +6,8 @@ use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -31,7 +31,7 @@ public function testToString( int $used, ObjectType $definition, ): void { - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $actual = (string) (new ObjectTypeDefinitionBlock($settings, $level, $used, $definition)); Parser::objectTypeDefinition($actual); @@ -44,7 +44,7 @@ public function testToString( */ public function testStatistics(): void { $settings = new TestSettings(); - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $definition = new ObjectType([ 'name' => 'A', 'fields' => [ diff --git a/src/SchemaPrinter/Blocks/Types/RootOperationTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/RootOperationTypeDefinitionBlock.php index c893b169..c383a7ba 100644 --- a/src/SchemaPrinter/Blocks/Types/RootOperationTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/RootOperationTypeDefinitionBlock.php @@ -3,14 +3,14 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Types; use GraphQL\Type\Definition\ObjectType; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; /** * @internal */ class RootOperationTypeDefinitionBlock extends TypeBlock { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, private OperationType $operation, diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php index 874d3074..545bddd5 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlock.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\ScalarType; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; /** * @internal @@ -13,7 +13,7 @@ */ class ScalarTypeDefinitionBlock extends DefinitionBlock { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, ScalarType $definition, diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php index d213b4a4..c1fde7f8 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php @@ -5,8 +5,8 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\CustomScalarType; use GraphQL\Type\Definition\ScalarType; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -30,7 +30,7 @@ public function testToString( int $used, ScalarType $type, ): void { - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $actual = (string) (new ScalarTypeDefinitionBlock($settings, $level, $used, $type)); Parser::scalarTypeDefinition($actual); diff --git a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php index ffd55b11..7c7e3fc9 100644 --- a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlock.php @@ -5,7 +5,7 @@ use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Schema; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use function array_filter; use function count; @@ -20,7 +20,7 @@ */ class SchemaDefinitionBlock extends DefinitionBlock { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, Schema $definition, diff --git a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php index 798092df..ddb15043 100644 --- a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php @@ -5,8 +5,8 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Schema; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -32,7 +32,7 @@ public function testToString( int $used, Schema $schema, ): void { - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $actual = (string) (new SchemaDefinitionBlock($settings, $level, $used, $schema)); if ($expected && !str_starts_with($actual, '"""')) { diff --git a/src/SchemaPrinter/Blocks/Types/StringBlock.php b/src/SchemaPrinter/Blocks/Types/StringBlock.php index 3a3f5ba0..08a98de6 100644 --- a/src/SchemaPrinter/Blocks/Types/StringBlock.php +++ b/src/SchemaPrinter/Blocks/Types/StringBlock.php @@ -5,7 +5,7 @@ use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\Printer; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use function mb_strlen; use function preg_match; @@ -18,7 +18,7 @@ */ class StringBlock extends Block { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, protected string $string, diff --git a/src/SchemaPrinter/Blocks/Types/StringBlockTest.php b/src/SchemaPrinter/Blocks/Types/StringBlockTest.php index 7a7226b3..9e0dc570 100644 --- a/src/SchemaPrinter/Blocks/Types/StringBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/StringBlockTest.php @@ -4,8 +4,8 @@ use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\Parser; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -31,7 +31,7 @@ public function testToString( int $used, string $string, ): void { - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $actual = (string) new StringBlock($settings, $level, $used, $string); $parsed = Parser::valueLiteral($actual); diff --git a/src/SchemaPrinter/Blocks/Types/TypeBlock.php b/src/SchemaPrinter/Blocks/Types/TypeBlock.php index dbc054cc..5ecf59c6 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeBlock.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlock.php @@ -5,15 +5,15 @@ use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\WrappingType; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; /** * @internal */ class TypeBlock extends Block implements Named { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, private Type $definition, diff --git a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php index d622eb84..81aad65c 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php @@ -6,8 +6,8 @@ use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -31,7 +31,7 @@ public function testToString( int $used, Type $type, ): void { - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $actual = (string) (new TypeBlock($settings, $level, $used, $type)); self::assertEquals($expected, $actual); @@ -47,7 +47,7 @@ public function testStatistics(): void { ]), ); $settings = new TestSettings(); - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $block = new TypeBlock($settings, 0, 0, $node); $type = $node->getWrappedType(true)->name; diff --git a/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php index 62864117..aa6b0a48 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/TypeDefinitionBlock.php @@ -5,7 +5,7 @@ use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\ObjectType; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use function mb_strlen; @@ -21,7 +21,7 @@ abstract class TypeDefinitionBlock extends DefinitionBlock { * @param TType $definition */ public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, InterfaceType|ObjectType $definition, diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php index 47d69fc7..9b0fbdb7 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlock.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\UnionType; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use function mb_strlen; @@ -15,7 +15,7 @@ */ class UnionTypeDefinitionBlock extends DefinitionBlock { public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, UnionType $definition, diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php index dfeaa181..98385f32 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php @@ -5,8 +5,8 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\UnionType; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -31,7 +31,7 @@ public function testToString( int $used, UnionType $type, ): void { - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $actual = (string) (new UnionTypeDefinitionBlock($settings, $level, $used, $type)); Parser::unionTypeDefinition($actual); @@ -56,7 +56,7 @@ public function testStatistics(): void { 'astNode' => Parser::unionTypeDefinition('union Test @a = A | B'), ]); $settings = new TestSettings(); - $settings = new BlockSettings($this->app->make(DirectiveResolver::class), $settings); + $settings = new PrinterSettings($this->app->make(DirectiveResolver::class), $settings); $block = new UnionTypeDefinitionBlock($settings, 0, 0, $union); self::assertNotEmpty((string) $block); diff --git a/src/SchemaPrinter/Blocks/Types/UsageList.php b/src/SchemaPrinter/Blocks/Types/UsageList.php index 0857ae2d..975a53de 100644 --- a/src/SchemaPrinter/Blocks/Types/UsageList.php +++ b/src/SchemaPrinter/Blocks/Types/UsageList.php @@ -4,7 +4,7 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use Traversable; use function mb_strlen; @@ -20,7 +20,7 @@ abstract class UsageList extends BlockList { * @param Traversable|array $items */ public function __construct( - BlockSettings $settings, + PrinterSettings $settings, int $level, int $used, Traversable|array $items, diff --git a/src/SchemaPrinter/DirectiveResolver.php b/src/SchemaPrinter/Misc/DirectiveResolver.php similarity index 82% rename from src/SchemaPrinter/DirectiveResolver.php rename to src/SchemaPrinter/Misc/DirectiveResolver.php index 10ad623b..b65f21a6 100644 --- a/src/SchemaPrinter/DirectiveResolver.php +++ b/src/SchemaPrinter/Misc/DirectiveResolver.php @@ -1,6 +1,6 @@ directives[$name] ?? $this->locator->create($name); } + + /** + * @return array + */ + public function getDefinitions(): array { + $directives = $this->directives; + + foreach ($this->locator->definitions() as $definition) { + $directive = $this->factory->handle($definition); + $directives[$directives->name] = $directive; + } + + return $directives; + } } diff --git a/src/SchemaPrinter/Blocks/BlockSettings.php b/src/SchemaPrinter/Misc/PrinterSettings.php similarity index 90% rename from src/SchemaPrinter/Blocks/BlockSettings.php rename to src/SchemaPrinter/Misc/PrinterSettings.php index e65f8cf0..d3a5bfc4 100644 --- a/src/SchemaPrinter/Blocks/BlockSettings.php +++ b/src/SchemaPrinter/Misc/PrinterSettings.php @@ -1,15 +1,14 @@ + // ========================================================================= + public function getResolver(): DirectiveResolver { + return $this->resolver; + } + // + // // ========================================================================= public function getDirective(DirectiveNode $node): GraphQLDirective|LighthouseDirective { From 73802706f880d92cc8a2bdaad68bc55372e56a2a Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 30 Jan 2022 12:30:20 +0400 Subject: [PATCH 79/90] `IntrospectionPrinter` speed up (will iterate only introspection types/directives) --- src/SchemaPrinter/IntrospectionPrinter.php | 27 +++++++++++++++---- .../IntrospectionPrinterTest.php | 3 +-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/SchemaPrinter/IntrospectionPrinter.php b/src/SchemaPrinter/IntrospectionPrinter.php index d3bc7be2..9d02f961 100644 --- a/src/SchemaPrinter/IntrospectionPrinter.php +++ b/src/SchemaPrinter/IntrospectionPrinter.php @@ -3,8 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter; use GraphQL\Type\Definition\Directive; -use GraphQL\Type\Definition\Type; use GraphQL\Type\Introspection; +use GraphQL\Type\Schema; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Printer\DefinitionList; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\ImmutableSettings; /** @@ -23,11 +25,26 @@ public function setSettings(Settings $settings): static { ); } - protected function isSchemaType(Type $type): bool { - return Introspection::isIntrospectionType($type); + protected function getSchemaTypes(PrinterSettings $settings, Schema $schema): DefinitionList { + $blocks = $this->getDefinitionList($settings); + + foreach (Introspection::getTypes() as $type) { + $blocks[] = $this->getDefinitionBlock($settings, $type); + } + + return $blocks; } - protected function isSchemaDirective(Directive $directive): bool { - return Directive::isSpecifiedDirective($directive); + protected function getSchemaDirectives(PrinterSettings $settings, Schema $schema): DefinitionList { + $blocks = $this->getDefinitionList($settings); + $directives = $schema->getDirectives(); + + foreach ($directives as $directive) { + if (Directive::isSpecifiedDirective($directive)) { + $blocks[] = $this->getDefinitionBlock($settings, $directive); + } + } + + return $blocks; } } diff --git a/src/SchemaPrinter/IntrospectionPrinterTest.php b/src/SchemaPrinter/IntrospectionPrinterTest.php index 431f85ab..e4741c4b 100644 --- a/src/SchemaPrinter/IntrospectionPrinterTest.php +++ b/src/SchemaPrinter/IntrospectionPrinterTest.php @@ -21,8 +21,7 @@ class IntrospectionPrinterTest extends TestCase { */ public function testPrint(string $expected, Settings $settings, int $level): void { $expected = $this->getTestData()->content($expected); - $resolver = $this->app->make(DirectiveResolver::class); - $printer = (new IntrospectionPrinter($resolver))->setSettings($settings)->setLevel($level); + $printer = $this->app->make(IntrospectionPrinter::class)->setSettings($settings)->setLevel($level); $schema = new Schema([]); $actual = $printer->print($schema); From f26574f292d1900fd320cc2016480ab881db6bff Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 30 Jan 2022 12:34:05 +0400 Subject: [PATCH 80/90] `Printer` improvements/fixes (without support for disabling `isPrintUnusedDefinitions()`) --- src/SchemaPrinter/Printer.php | 174 ++++++++---------------------- src/SchemaPrinter/PrinterTest.php | 13 ++- 2 files changed, 51 insertions(+), 136 deletions(-) diff --git a/src/SchemaPrinter/Printer.php b/src/SchemaPrinter/Printer.php index 48a63dbf..544d4b4c 100644 --- a/src/SchemaPrinter/Printer.php +++ b/src/SchemaPrinter/Printer.php @@ -5,23 +5,21 @@ use GraphQL\Type\Definition\Directive; use GraphQL\Type\Definition\Type; use GraphQL\Type\Schema; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Named; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Printer\DefinitionBlock; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Printer\DefinitionList; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; - -use function end; -use function explode; -use function ltrim; +use Nuwave\Lighthouse\Schema\DirectiveLocator; +use Nuwave\Lighthouse\Schema\ExecutableTypeNodeConverter; class Printer { protected Settings $settings; protected int $level = 0; public function __construct( - protected DirectiveResolver $directives, + protected DirectiveLocator $locator, + protected ExecutableTypeNodeConverter $converter, Settings $settings = null, ) { $this->settings = $settings ?? new DefaultSettings(); @@ -49,26 +47,17 @@ public function setSettings(Settings $settings): static { public function print(Schema $schema): PrintedSchema { // Collect - $usedTypes = []; - $usedDirectives = []; - $schemaBlock = $this->getSchema($schema, $usedTypes, $usedDirectives); - $typesBlocks = $this->getSchemaTypes($schema, $usedTypes, $usedDirectives); - $directivesBlocks = $this->getSchemaDirectives($schema, $usedDirectives, $usedTypes); + $resolver = new DirectiveResolver($this->locator, $this->converter, $schema->getDirectives()); + $settings = new PrinterSettings($resolver, $this->getSettings()); + $schemaBlock = $this->getSchema($settings, $schema); + $typesBlocks = $this->getSchemaTypes($settings, $schema); + $directivesBlocks = $this->getSchemaDirectives($settings, $schema); // Print - $settings = $this->getSettings(); - $content = new DefinitionList($this->getSettings(), $this->getLevel(), true); + $content = new DefinitionList($settings, $this->getLevel(), true); $content[] = $schemaBlock; - $content[] = $this->getDefinitionList( - $typesBlocks, - $usedTypes, - $settings->isPrintUnusedDefinitions(), - ); - $content[] = $this->getDefinitionList( - $directivesBlocks, - $usedDirectives, - $settings->isPrintUnusedDefinitions(), - ); + $content[] = $typesBlocks; + $content[] = $directivesBlocks; // todo(graphql): directives in description for schema // https://github.com/webonyx/graphql-php/issues/1027 @@ -77,145 +66,72 @@ public function print(Schema $schema): PrintedSchema { return new PrintedSchema((string) $content); } - /** - * @param array $usedTypes - * @param array $usedDirectives - */ - protected function getSchema( - Schema $schema, - array &$usedTypes = [], - array &$usedDirectives = [], - ): DefinitionBlock { - return $this->getDefinitionBlock($schema, $usedTypes, $usedDirectives); + protected function getSchema(PrinterSettings $settings, Schema $schema): DefinitionBlock { + return $this->getDefinitionBlock($settings, $schema); } /** - * @param array $usedTypes - * @param array $usedDirectives + * Returns all types defined in the schema. * - * @return array + * @return DefinitionList */ - protected function getSchemaTypes(Schema $schema, array &$usedTypes = [], array &$usedDirectives = []): array { - $blocks = []; + protected function getSchemaTypes(PrinterSettings $settings, Schema $schema): DefinitionList { + $blocks = $this->getDefinitionList($settings); foreach ($schema->getTypeMap() as $type) { // Standard? - if (!$this->isSchemaType($type)) { + if (Type::isBuiltInType($type)) { continue; } // Nope - $blocks[] = $this->getDefinitionBlock($type, $usedTypes, $usedDirectives); + $blocks[] = $this->getDefinitionBlock($settings, $type); } return $blocks; } - protected function isSchemaType(Type $type): bool { - return !Type::isBuiltInType($type); - } - /** - * @param array $usedTypes - * @param array $directives + * Returns all directives defined in the schema. * - * @return array + * @return DefinitionList */ - protected function getSchemaDirectives(Schema $schema, array $directives, array &$usedTypes = []): array { + protected function getSchemaDirectives(PrinterSettings $settings, Schema $schema): DefinitionList { // Included? - $blocks = []; - $settings = $this->getSettings(); - $included = $settings->isPrintDirectiveDefinitions(); + $blocks = $this->getDefinitionList($settings); - if (!$included) { - return $blocks; - } + if ($settings->isPrintDirectiveDefinitions()) { + $filter = $settings->getDirectiveFilter(); + $directives = $settings->getResolver()->getDefinitions(); - // Add directives from Schema - $processed = []; + foreach ($directives as $directive) { + // Introspection? + if (Directive::isSpecifiedDirective($directive)) { + continue; + } - foreach ($schema->getDirectives() as $directive) { - // Mark - $processed[$directive->name] = true; + // Not allowed? + if ($filter !== null && !$filter->isAllowedDirective($directive)) { + continue; + } - // Standard? - if (!$this->isSchemaDirective($directive)) { - continue; + // Nope + $blocks[] = $this->getDefinitionBlock($settings, $directive); } - - // Nope - $blocks[] = $this->getDefinitionBlock($directive, $usedTypes); - } - - // Add Lighthouse directives - foreach ($directives as $directive) { - // Processed? - if (isset($processed[$directive])) { - continue; - } - - // Nope - $blocks[] = $this->getDefinitionBlock( - $this->directives->get($directive), - $usedTypes, - ); } // Return return $blocks; } - protected function isSchemaDirective(Directive $directive): bool { - return !Directive::isSpecifiedDirective($directive); + protected function getDefinitionList(PrinterSettings $settings): DefinitionList { + return new DefinitionList($settings, $this->getLevel()); } - /** - * @param array $usedTypes - * @param array $usedDirectives - */ protected function getDefinitionBlock( - Schema|Type|Directive $definition, - array &$usedTypes = [], - array &$usedDirectives = [], + PrinterSettings $settings, + Schema|Type|Directive $definition ): DefinitionBlock { - $block = new DefinitionBlock($this->getSettings(), $this->getLevel(), $definition); - $usedTypes += $block->getUsedTypes(); - $usedDirectives += $block->getUsedDirectives(); - - return $block; - } - - /** - * @param array $blocks - * @param array $used - */ - protected function getDefinitionList( - array $blocks, - array $used, - bool $includeUnused, - ): BlockList { - $list = new DefinitionList($this->getSettings(), $this->getLevel()); - - foreach ($blocks as $block) { - if ($includeUnused || $this->getDefinitionListIsUsed($block, $used)) { - $list[] = $block; - } - } - - return $list; - } - - /** - * @param array $used - */ - private function getDefinitionListIsUsed(Block $block, array $used): bool { - // Block names may have a "type name" prefix eg "type A", "interface B", - // etc, meanwhile `$used` doesn't have it. So we should remove the - // prefix before checking. - $name = $block instanceof Named ? $block->getName() : ''; - $name = explode(' ', $name); - $name = ltrim((string) end($name), '@'); - - return isset($used[$name]); + return new DefinitionBlock($settings, $this->getLevel(), $definition); } } diff --git a/src/SchemaPrinter/PrinterTest.php b/src/SchemaPrinter/PrinterTest.php index a819868a..18e2187b 100644 --- a/src/SchemaPrinter/PrinterTest.php +++ b/src/SchemaPrinter/PrinterTest.php @@ -106,8 +106,7 @@ public static function definition(): string { // Test $expected = $this->getTestData()->content($expected); - $resolver = $this->app->make(DirectiveResolver::class); - $printer = (new Printer($resolver))->setSettings($settings)->setLevel($level); + $printer = $this->app->make(Printer::class)->setSettings($settings)->setLevel($level); $schema = $this->getGraphQLSchema($this->getTestData()->file('~schema.graphql')); $actual = $printer->print($schema); @@ -127,11 +126,11 @@ public function dataProviderPrint(): array { new DefaultSettings(), 0, ], - TestSettings::class => [ - '~test-settings.graphql', - new TestSettings(), - 0, - ], +// TestSettings::class => [ +// '~test-settings.graphql', +// new TestSettings(), +// 0, +// ], ]; } // From 15b50373b837bebf20c02bb9fc880e0de916681b Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 30 Jan 2022 14:20:58 +0400 Subject: [PATCH 81/90] `DirectiveNodeBlock::getName()` will starts with `@`. --- src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php | 7 +++---- src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php | 2 +- src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php | 2 +- .../Blocks/Types/FieldDefinitionBlockTest.php | 2 +- .../Blocks/Types/InputObjectTypeDefinitionBlockTest.php | 2 +- .../Blocks/Types/InputValueDefinitionBlockTest.php | 2 +- .../Blocks/Types/InterfaceTypeDefinitionBlockTest.php | 2 +- .../Blocks/Types/ObjectTypeDefinitionBlockTest.php | 2 +- .../Blocks/Types/UnionTypeDefinitionBlockTest.php | 2 +- src/SchemaPrinter/IntrospectionPrinter.php | 4 ++-- 10 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php index 3e3844ed..6a7e7266 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlock.php @@ -23,7 +23,7 @@ public function __construct( } public function getName(): string { - return $this->getNode()->name->value; + return "@{$this->getNode()->name->value}"; } public function getNode(): DirectiveNode { @@ -32,10 +32,9 @@ public function getNode(): DirectiveNode { protected function content(): string { // Convert - $at = '@'; $node = $this->getNode(); $name = $this->getName(); - $used = mb_strlen($name) + mb_strlen($at); + $used = mb_strlen($name); $args = $this->addUsed( new ArgumentNodeList( $this->getSettings(), @@ -49,6 +48,6 @@ protected function content(): string { $this->addUsedDirective($name); // Return - return "{$at}{$name}{$args}"; + return "{$name}{$args}"; } } diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php index b75e0494..d95f4a29 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php @@ -55,7 +55,7 @@ public function testStatistics(): void { self::assertNotEmpty((string) $block); self::assertEquals([], $block->getUsedTypes()); - self::assertEquals(['test' => 'test'], $block->getUsedDirectives()); + self::assertEquals(['@test' => '@test'], $block->getUsedDirectives()); } // diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php index fc598c4a..fb485d36 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php @@ -69,7 +69,7 @@ public function testStatistics(): void { self::assertNotEmpty((string) $block); self::assertEquals([], $block->getUsedTypes()); - self::assertEquals(['a' => 'a', 'b' => 'b'], $block->getUsedDirectives()); + self::assertEquals(['@a' => '@a', '@b' => '@b'], $block->getUsedDirectives()); } // diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php index c3f1ccdc..fe5e7385 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php @@ -60,7 +60,7 @@ public function testStatistics(): void { self::assertNotEmpty((string) $block); self::assertEquals(['A' => 'A'], $block->getUsedTypes()); - self::assertEquals(['a' => 'a'], $block->getUsedDirectives()); + self::assertEquals(['@a' => '@a'], $block->getUsedDirectives()); } // diff --git a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php index 7885d731..dba845a5 100644 --- a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php @@ -66,7 +66,7 @@ public function testStatistics(): void { self::assertNotEmpty((string) $block); self::assertEquals(['B' => 'B'], $block->getUsedTypes()); - self::assertEquals(['a' => 'a', 'b' => 'b'], $block->getUsedDirectives()); + self::assertEquals(['@a' => '@a', '@b' => '@b'], $block->getUsedDirectives()); } // diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php index d9b1fddc..56894f1d 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php @@ -60,7 +60,7 @@ public function testStatistics(): void { self::assertNotEmpty((string) $block); self::assertEquals(['A' => 'A'], $block->getUsedTypes()); - self::assertEquals(['a' => 'a'], $block->getUsedDirectives()); + self::assertEquals(['@a' => '@a'], $block->getUsedDirectives()); } // diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php index 3d268248..0d95a5b5 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php @@ -75,7 +75,7 @@ public function testStatistics(): void { self::assertNotEmpty((string) $block); self::assertEquals(['B' => 'B', 'C' => 'C', 'D' => 'D'], $block->getUsedTypes()); - self::assertEquals(['a' => 'a', 'b' => 'b', 'c' => 'c'], $block->getUsedDirectives()); + self::assertEquals(['@a' => '@a', '@b' => '@b', '@c' => '@c'], $block->getUsedDirectives()); } // diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php index f1ced00f..3a88d888 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php @@ -75,7 +75,7 @@ public function testStatistics(): void { self::assertNotEmpty((string) $block); self::assertEquals(['B' => 'B', 'C' => 'C', 'D' => 'D'], $block->getUsedTypes()); - self::assertEquals(['a' => 'a', 'b' => 'b', 'c' => 'c'], $block->getUsedDirectives()); + self::assertEquals(['@a' => '@a', '@b' => '@b', '@c' => '@c'], $block->getUsedDirectives()); } // diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php index 98385f32..577e6ed5 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php @@ -61,7 +61,7 @@ public function testStatistics(): void { self::assertNotEmpty((string) $block); self::assertEquals(['A' => 'A', 'B' => 'B'], $block->getUsedTypes()); - self::assertEquals(['a' => 'a'], $block->getUsedDirectives()); + self::assertEquals(['@a' => '@a'], $block->getUsedDirectives()); } // diff --git a/src/SchemaPrinter/IntrospectionPrinter.php b/src/SchemaPrinter/IntrospectionPrinter.php index 9d02f961..05a45589 100644 --- a/src/SchemaPrinter/IntrospectionPrinter.php +++ b/src/SchemaPrinter/IntrospectionPrinter.php @@ -25,7 +25,7 @@ public function setSettings(Settings $settings): static { ); } - protected function getSchemaTypes(PrinterSettings $settings, Schema $schema): DefinitionList { + protected function getTypeDefinitions(PrinterSettings $settings, Schema $schema): DefinitionList { $blocks = $this->getDefinitionList($settings); foreach (Introspection::getTypes() as $type) { @@ -35,7 +35,7 @@ protected function getSchemaTypes(PrinterSettings $settings, Schema $schema): De return $blocks; } - protected function getSchemaDirectives(PrinterSettings $settings, Schema $schema): DefinitionList { + protected function getDirectiveDefinitions(PrinterSettings $settings, Schema $schema): DefinitionList { $blocks = $this->getDefinitionList($settings); $directives = $schema->getDirectives(); From 6e92842941516f01bc1c2b395d70db66abf36bde Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 30 Jan 2022 14:51:44 +0400 Subject: [PATCH 82/90] `Printer` implemented `isPrintUnusedDefinitions()` support. --- src/SchemaPrinter/Printer.php | 113 ++++++++++++++---- src/SchemaPrinter/PrinterTest.php | 10 +- .../PrinterTest~test-settings.graphql | 33 ----- 3 files changed, 95 insertions(+), 61 deletions(-) diff --git a/src/SchemaPrinter/Printer.php b/src/SchemaPrinter/Printer.php index 544d4b4c..1a849d9f 100644 --- a/src/SchemaPrinter/Printer.php +++ b/src/SchemaPrinter/Printer.php @@ -5,6 +5,8 @@ use GraphQL\Type\Definition\Directive; use GraphQL\Type\Definition\Type; use GraphQL\Type\Schema; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Printer\DefinitionBlock; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Printer\DefinitionList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; @@ -13,6 +15,10 @@ use Nuwave\Lighthouse\Schema\DirectiveLocator; use Nuwave\Lighthouse\Schema\ExecutableTypeNodeConverter; +use function array_pop; +use function str_starts_with; +use function substr; + class Printer { protected Settings $settings; protected int $level = 0; @@ -46,41 +52,44 @@ public function setSettings(Settings $settings): static { } public function print(Schema $schema): PrintedSchema { - // Collect - $resolver = new DirectiveResolver($this->locator, $this->converter, $schema->getDirectives()); - $settings = new PrinterSettings($resolver, $this->getSettings()); - $schemaBlock = $this->getSchema($settings, $schema); - $typesBlocks = $this->getSchemaTypes($settings, $schema); - $directivesBlocks = $this->getSchemaDirectives($settings, $schema); - - // Print - $content = new DefinitionList($settings, $this->getLevel(), true); - $content[] = $schemaBlock; - $content[] = $typesBlocks; - $content[] = $directivesBlocks; - // todo(graphql): directives in description for schema // https://github.com/webonyx/graphql-php/issues/1027 + // Print + $resolver = new DirectiveResolver($this->locator, $this->converter, $schema->getDirectives()); + $settings = new PrinterSettings($resolver, $this->getSettings()); + $block = $this->getSchemaDefinition($settings, $schema); + $content = $this->getDefinitionList($settings, true); + $content[] = $block; + + if ($settings->isPrintUnusedDefinitions()) { + $content[] = $this->getTypeDefinitions($settings, $schema); + $content[] = $this->getDirectiveDefinitions($settings, $schema); + } else { + foreach ($this->getUsedDefinitions($settings, $schema, $block) as $definition) { + $content[] = $definition; + } + } + // Return return new PrintedSchema((string) $content); } - protected function getSchema(PrinterSettings $settings, Schema $schema): DefinitionBlock { + protected function getSchemaDefinition(PrinterSettings $settings, Schema $schema): Block { return $this->getDefinitionBlock($settings, $schema); } /** * Returns all types defined in the schema. * - * @return DefinitionList + * @return BlockList */ - protected function getSchemaTypes(PrinterSettings $settings, Schema $schema): DefinitionList { + protected function getTypeDefinitions(PrinterSettings $settings, Schema $schema): BlockList { $blocks = $this->getDefinitionList($settings); foreach ($schema->getTypeMap() as $type) { // Standard? - if (Type::isBuiltInType($type)) { + if (!$this->isType($type)) { continue; } @@ -94,9 +103,9 @@ protected function getSchemaTypes(PrinterSettings $settings, Schema $schema): De /** * Returns all directives defined in the schema. * - * @return DefinitionList + * @return BlockList */ - protected function getSchemaDirectives(PrinterSettings $settings, Schema $schema): DefinitionList { + protected function getDirectiveDefinitions(PrinterSettings $settings, Schema $schema): BlockList { // Included? $blocks = $this->getDefinitionList($settings); @@ -106,7 +115,7 @@ protected function getSchemaDirectives(PrinterSettings $settings, Schema $schema foreach ($directives as $directive) { // Introspection? - if (Directive::isSpecifiedDirective($directive)) { + if (!$this->isDirective($directive)) { continue; } @@ -124,14 +133,72 @@ protected function getSchemaDirectives(PrinterSettings $settings, Schema $schema return $blocks; } - protected function getDefinitionList(PrinterSettings $settings): DefinitionList { - return new DefinitionList($settings, $this->getLevel()); + /** + * @return array + */ + protected function getUsedDefinitions(PrinterSettings $settings, Schema $schema, Block $root): array { + $resolver = $settings->getResolver(); + $directives = $this->getDefinitionList($settings); + $types = $this->getDefinitionList($settings); + $stack = $root->getUsedDirectives() + $root->getUsedTypes(); + + while ($stack) { + // Added? + $name = array_pop($stack); + + if (isset($types[$name]) || isset($directives[$name])) { + continue; + } + + // Add + $block = null; + + if (str_starts_with($name, '@')) { + $directive = $resolver->getDefinition(substr($name, 1)); + + if ($this->isDirective($directive)) { + $block = $this->getDefinitionBlock($settings, $directive); + $directives[$name] = $block; + } + } else { + $type = $schema->getType($name); + + if ($this->isType($type)) { + $block = $this->getDefinitionBlock($settings, $type); + $types[$name] = $block; + } + } + + // Stack + if ($block) { + $stack = $stack + + $block->getUsedDirectives() + + $block->getUsedTypes(); + } + } + + return [ + $types, + $directives, + ]; + } + + protected function getDefinitionList(PrinterSettings $settings, bool $schema = false): BlockList { + return new DefinitionList($settings, $this->getLevel(), $schema); } protected function getDefinitionBlock( PrinterSettings $settings, Schema|Type|Directive $definition - ): DefinitionBlock { + ): Block { return new DefinitionBlock($settings, $this->getLevel(), $definition); } + + private function isType(Type $type): bool { + return !Type::isBuiltInType($type); + } + + private function isDirective(Directive $directive): bool { + return !Directive::isSpecifiedDirective($directive); + } } diff --git a/src/SchemaPrinter/PrinterTest.php b/src/SchemaPrinter/PrinterTest.php index 18e2187b..e06da26a 100644 --- a/src/SchemaPrinter/PrinterTest.php +++ b/src/SchemaPrinter/PrinterTest.php @@ -126,11 +126,11 @@ public function dataProviderPrint(): array { new DefaultSettings(), 0, ], -// TestSettings::class => [ -// '~test-settings.graphql', -// new TestSettings(), -// 0, -// ], + TestSettings::class => [ + '~test-settings.graphql', + new TestSettings(), + 0, + ], ]; } // diff --git a/src/SchemaPrinter/PrinterTest~test-settings.graphql b/src/SchemaPrinter/PrinterTest~test-settings.graphql index 9c93fde7..3fa49eee 100644 --- a/src/SchemaPrinter/PrinterTest~test-settings.graphql +++ b/src/SchemaPrinter/PrinterTest~test-settings.graphql @@ -14,13 +14,6 @@ enum SchemaEnum { B } -""" -This is unused enum. -""" -enum SchemaEnumUnused { - A -} - """ Description """ @@ -91,12 +84,6 @@ scalar SchemaScalar @scalar(class: "GraphQL\\Type\\Definition\\StringType") @codeDirective -""" -This is unused scalar. -""" -scalar SchemaScalarUnused -@scalar(class: "GraphQL\\Type\\Definition\\StringType") - """ Description """ @@ -176,15 +163,6 @@ implements ): SchemaUnion } -""" -This is unused type. -""" -type SchemaTypeUnused -@schemaDirectiveUnused -{ - a: SchemaScalarUnused -} - union CodeUnion = | CodeType @@ -241,14 +219,3 @@ on | SCALAR | SCHEMA | UNION - -""" -This is unused directives. -""" -directive @schemaDirectiveUnused( - a: SchemaScalarUnused - b: SchemaEnumUnused -) -repeatable on - | OBJECT - | SCALAR From 55d6df5dd854b43acf66d88406c47231bdb3ad7d Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 30 Jan 2022 14:52:56 +0400 Subject: [PATCH 83/90] `Settings` moved to `Contracts`. --- src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php | 2 +- src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php | 2 +- src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php | 2 +- src/SchemaPrinter/Blocks/BlockListTest.php | 2 +- src/SchemaPrinter/Blocks/BlockTest.php | 2 +- src/SchemaPrinter/Blocks/Types/DescriptionTest.php | 4 ++-- .../Blocks/Types/DirectiveDefinitionBlockTest.php | 2 +- .../Blocks/Types/EnumTypeDefinitionBlockTest.php | 4 ++-- .../Blocks/Types/EnumValueDefinitionBlockTest.php | 2 +- src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php | 4 ++-- .../Blocks/Types/InputObjectTypeDefinitionBlockTest.php | 2 +- .../Blocks/Types/InputValueDefinitionBlockTest.php | 4 ++-- .../Blocks/Types/InterfaceTypeDefinitionBlockTest.php | 4 ++-- .../Blocks/Types/ObjectTypeDefinitionBlockTest.php | 2 +- .../Blocks/Types/ScalarTypeDefinitionBlockTest.php | 2 +- src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php | 4 ++-- src/SchemaPrinter/Blocks/Types/StringBlockTest.php | 4 ++-- src/SchemaPrinter/Blocks/Types/TypeBlockTest.php | 4 ++-- .../Blocks/Types/UnionTypeDefinitionBlockTest.php | 4 ++-- src/SchemaPrinter/{ => Contracts}/Settings.php | 4 +--- src/SchemaPrinter/IntrospectionPrinter.php | 1 + src/SchemaPrinter/IntrospectionPrinterTest.php | 1 + src/SchemaPrinter/Misc/PrinterSettings.php | 2 +- src/SchemaPrinter/Printer.php | 3 ++- src/SchemaPrinter/PrinterTest.php | 1 + src/SchemaPrinter/Settings/ImmutableSettings.php | 2 +- src/SchemaPrinter/Settings/ImmutableSettingsTest.php | 2 +- 27 files changed, 37 insertions(+), 35 deletions(-) rename src/SchemaPrinter/{ => Contracts}/Settings.php (95%) diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php index d95f4a29..faf4653c 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeBlockTest.php @@ -5,9 +5,9 @@ use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\Parser; use GraphQL\Language\Printer; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; diff --git a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php index fb485d36..0d85ca22 100644 --- a/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php +++ b/src/SchemaPrinter/Blocks/Ast/DirectiveNodeListTest.php @@ -6,9 +6,9 @@ use GraphQL\Language\DirectiveLocation; use GraphQL\Language\Parser; use GraphQL\Type\Definition\Directive as GraphQLDirective; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use Nuwave\Lighthouse\Schema\DirectiveLocator; diff --git a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php index dc9f032d..2553adea 100644 --- a/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php +++ b/src/SchemaPrinter/Blocks/Ast/ValueNodeTest.php @@ -15,9 +15,9 @@ use GraphQL\Language\AST\VariableNode; use GraphQL\Language\Parser; use GraphQL\Language\Printer; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; diff --git a/src/SchemaPrinter/Blocks/BlockListTest.php b/src/SchemaPrinter/Blocks/BlockListTest.php index a3527693..dfe6ac06 100644 --- a/src/SchemaPrinter/Blocks/BlockListTest.php +++ b/src/SchemaPrinter/Blocks/BlockListTest.php @@ -2,9 +2,9 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; diff --git a/src/SchemaPrinter/Blocks/BlockTest.php b/src/SchemaPrinter/Blocks/BlockTest.php index 1d7e23a0..a6fa6ba4 100644 --- a/src/SchemaPrinter/Blocks/BlockTest.php +++ b/src/SchemaPrinter/Blocks/BlockTest.php @@ -2,9 +2,9 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use Mockery; diff --git a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php index c180ea33..6c07194f 100644 --- a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php +++ b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php @@ -5,9 +5,9 @@ use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\Parser; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast\DirectiveNodeList; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -50,7 +50,7 @@ public function testToString( // // ========================================================================= /** - * @return array|null}> + * @return array|null}> */ public function dataProviderToString(): array { $settings = (new TestSettings()) diff --git a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php index d17c9893..a9abbff0 100644 --- a/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/DirectiveDefinitionBlockTest.php @@ -7,9 +7,9 @@ use GraphQL\Type\Definition\Directive; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php index af5e4076..a1e7ec90 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php @@ -7,9 +7,9 @@ use GraphQL\Type\Definition\Directive; use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\EnumValueDefinition; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -49,7 +49,7 @@ public function testToString( // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { $settings = (new TestSettings()) diff --git a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php index b34bf50b..83d0d8b6 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumValueDefinitionBlockTest.php @@ -4,9 +4,9 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\EnumValueDefinition; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php index fe5e7385..126dc8ee 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php @@ -8,9 +8,9 @@ use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -67,7 +67,7 @@ public function testStatistics(): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { $settings = (new TestSettings()) diff --git a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php index dba845a5..f93306b1 100644 --- a/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputObjectTypeDefinitionBlockTest.php @@ -5,9 +5,9 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\Type; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php index 56894f1d..df07d181 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php @@ -8,9 +8,9 @@ use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -67,7 +67,7 @@ public function testStatistics(): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { $settings = new TestSettings(); diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php index 0d95a5b5..50833d81 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php @@ -6,9 +6,9 @@ use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -82,7 +82,7 @@ public function testStatistics(): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { $settings = (new TestSettings()) diff --git a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php index 3a88d888..dd3315da 100644 --- a/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ObjectTypeDefinitionBlockTest.php @@ -6,9 +6,9 @@ use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; diff --git a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php index c1fde7f8..fdc351de 100644 --- a/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/ScalarTypeDefinitionBlockTest.php @@ -5,9 +5,9 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\CustomScalarType; use GraphQL\Type\Definition\ScalarType; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; diff --git a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php index ddb15043..a8736dcd 100644 --- a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php @@ -5,9 +5,9 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Schema; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -47,7 +47,7 @@ public function testToString( // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { $settings = (new TestSettings()) diff --git a/src/SchemaPrinter/Blocks/Types/StringBlockTest.php b/src/SchemaPrinter/Blocks/Types/StringBlockTest.php index 9e0dc570..f6714e54 100644 --- a/src/SchemaPrinter/Blocks/Types/StringBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/StringBlockTest.php @@ -4,9 +4,9 @@ use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\Parser; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -44,7 +44,7 @@ public function testToString( // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { $settings = new TestSettings(); diff --git a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php index 81aad65c..d8ace899 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php @@ -6,9 +6,9 @@ use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -60,7 +60,7 @@ public function testStatistics(): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { $settings = new TestSettings(); diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php index 577e6ed5..08dbf40b 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php @@ -5,9 +5,9 @@ use GraphQL\Language\Parser; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\UnionType; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -68,7 +68,7 @@ public function testStatistics(): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { $settings = (new TestSettings()) diff --git a/src/SchemaPrinter/Settings.php b/src/SchemaPrinter/Contracts/Settings.php similarity index 95% rename from src/SchemaPrinter/Settings.php rename to src/SchemaPrinter/Contracts/Settings.php index a5b97776..132c5e31 100644 --- a/src/SchemaPrinter/Settings.php +++ b/src/SchemaPrinter/Contracts/Settings.php @@ -1,8 +1,6 @@ getLevel(), $definition); } diff --git a/src/SchemaPrinter/PrinterTest.php b/src/SchemaPrinter/PrinterTest.php index e06da26a..3555a97d 100644 --- a/src/SchemaPrinter/PrinterTest.php +++ b/src/SchemaPrinter/PrinterTest.php @@ -11,6 +11,7 @@ use GraphQL\Type\Definition\StringType; use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\UnionType; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; diff --git a/src/SchemaPrinter/Settings/ImmutableSettings.php b/src/SchemaPrinter/Settings/ImmutableSettings.php index f52aeb35..d1681c8a 100644 --- a/src/SchemaPrinter/Settings/ImmutableSettings.php +++ b/src/SchemaPrinter/Settings/ImmutableSettings.php @@ -4,7 +4,7 @@ use Closure; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\DirectiveFilter; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; abstract class ImmutableSettings implements Settings { protected string $space; diff --git a/src/SchemaPrinter/Settings/ImmutableSettingsTest.php b/src/SchemaPrinter/Settings/ImmutableSettingsTest.php index 1898af36..8e1b27f2 100644 --- a/src/SchemaPrinter/Settings/ImmutableSettingsTest.php +++ b/src/SchemaPrinter/Settings/ImmutableSettingsTest.php @@ -2,7 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use Mockery; use ReflectionClass; From dc169ee3e473f7b94764ec5ed119c9d614ae7cc8 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 30 Jan 2022 14:54:01 +0400 Subject: [PATCH 84/90] `Statistics` moved to `Contracts`. --- src/SchemaPrinter/Blocks/Block.php | 2 +- src/SchemaPrinter/Blocks/BlockList.php | 2 +- src/SchemaPrinter/{ => Contracts}/Statistics.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename src/SchemaPrinter/{ => Contracts}/Statistics.php (79%) diff --git a/src/SchemaPrinter/Blocks/Block.php b/src/SchemaPrinter/Blocks/Block.php index 0cfdb175..bc274e39 100644 --- a/src/SchemaPrinter/Blocks/Block.php +++ b/src/SchemaPrinter/Blocks/Block.php @@ -3,8 +3,8 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks; use Closure; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Statistics; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Statistics; use Stringable; use function mb_strlen; diff --git a/src/SchemaPrinter/Blocks/BlockList.php b/src/SchemaPrinter/Blocks/BlockList.php index 95103da5..2babdc58 100644 --- a/src/SchemaPrinter/Blocks/BlockList.php +++ b/src/SchemaPrinter/Blocks/BlockList.php @@ -4,7 +4,7 @@ use ArrayAccess; use Countable; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Statistics; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Statistics; use function array_key_last; use function count; diff --git a/src/SchemaPrinter/Statistics.php b/src/SchemaPrinter/Contracts/Statistics.php similarity index 79% rename from src/SchemaPrinter/Statistics.php rename to src/SchemaPrinter/Contracts/Statistics.php index 331059af..94a98692 100644 --- a/src/SchemaPrinter/Statistics.php +++ b/src/SchemaPrinter/Contracts/Statistics.php @@ -1,6 +1,6 @@ Date: Sun, 30 Jan 2022 15:54:40 +0400 Subject: [PATCH 85/90] `PrintedSchema` will contains information about printed types and directives. --- src/SchemaPrinter/PrintedSchema.php | 24 ++++++- src/SchemaPrinter/Printer.php | 2 +- src/SchemaPrinter/PrinterTest.php | 69 ++++++++++++++++--- .../PrinterTest~default-settings.graphql | 7 +- src/SchemaPrinter/PrinterTest~schema.graphql | 5 ++ .../PrinterTest~test-settings.graphql | 5 ++ 6 files changed, 98 insertions(+), 14 deletions(-) diff --git a/src/SchemaPrinter/PrintedSchema.php b/src/SchemaPrinter/PrintedSchema.php index 76e8e64f..438c3f8d 100644 --- a/src/SchemaPrinter/PrintedSchema.php +++ b/src/SchemaPrinter/PrintedSchema.php @@ -2,16 +2,34 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Statistics; use Stringable; -class PrintedSchema implements Stringable { +use function array_values; + +class PrintedSchema implements Statistics, Stringable { public function __construct( - protected string $schema, + protected Block $schema, ) { // empty } public function __toString(): string { - return $this->schema; + return (string) $this->schema; + } + + /** + * @return array + */ + public function getUsedTypes(): array { + return array_values($this->schema->getUsedTypes()); + } + + /** + * @return array + */ + public function getUsedDirectives(): array { + return array_values($this->schema->getUsedDirectives()); } } diff --git a/src/SchemaPrinter/Printer.php b/src/SchemaPrinter/Printer.php index a3a470b0..5b341719 100644 --- a/src/SchemaPrinter/Printer.php +++ b/src/SchemaPrinter/Printer.php @@ -73,7 +73,7 @@ public function print(Schema $schema): PrintedSchema { } // Return - return new PrintedSchema((string) $content); + return new PrintedSchema($content); } protected function getSchemaDefinition(PrinterSettings $settings, Schema $schema): Block { diff --git a/src/SchemaPrinter/PrinterTest.php b/src/SchemaPrinter/PrinterTest.php index 3555a97d..0776fc78 100644 --- a/src/SchemaPrinter/PrinterTest.php +++ b/src/SchemaPrinter/PrinterTest.php @@ -30,8 +30,10 @@ class PrinterTest extends TestCase { * @covers ::print * * @dataProvider dataProviderPrint + * + * @param array{schema: string, types: array, directives: array} */ - public function testPrint(string $expected, Settings $settings, int $level): void { + public function testPrint(array $expected, Settings $settings, int $level): void { // Types $directives = $this->app->make(DirectiveLocator::class); $registry = $this->app->make(TypeRegistry::class); @@ -55,7 +57,7 @@ public static function definition(): string { 'fields' => [ [ 'name' => 'a', - 'type' => Type::boolean(), + 'type' => Type::nonNull(Type::boolean()), ], ], ]); @@ -106,12 +108,14 @@ public static function definition(): string { $registry->register($codeInput); // Test - $expected = $this->getTestData()->content($expected); - $printer = $this->app->make(Printer::class)->setSettings($settings)->setLevel($level); - $schema = $this->getGraphQLSchema($this->getTestData()->file('~schema.graphql')); - $actual = $printer->print($schema); + $output = $this->getTestData()->content($expected['schema']); + $printer = $this->app->make(Printer::class)->setSettings($settings)->setLevel($level); + $schema = $this->getGraphQLSchema($this->getTestData()->file('~schema.graphql')); + $actual = $printer->print($schema); - self::assertEquals($expected, (string) $actual); + self::assertEquals($output, (string) $actual); + self::assertEqualsCanonicalizing($expected['types'], $actual->getUsedTypes()); + self::assertEqualsCanonicalizing($expected['directives'], $actual->getUsedDirectives()); } // @@ -123,12 +127,59 @@ public static function definition(): string { public function dataProviderPrint(): array { return [ DefaultSettings::class => [ - '~default-settings.graphql', + [ + 'schema' => '~default-settings.graphql', + 'types' => [ + 'String', + 'Boolean', + 'SchemaType', + 'SchemaEnum', + 'SchemaInput', + 'SchemaUnion', + 'SchemaScalar', + 'SchemaInterfaceB', + 'CodeScalar', + 'CodeInput', + 'CodeUnion', + 'CodeEnum', + 'CodeType', + 'SchemaTypeUnused', + 'SchemaEnumUnused', + 'SchemaScalarUnused', + ], + 'directives' => [ + // empty + ], + ], new DefaultSettings(), 0, ], TestSettings::class => [ - '~test-settings.graphql', + [ + 'schema' => '~test-settings.graphql', + 'types' => [ + 'String', + 'Boolean', + 'SchemaType', + 'SchemaEnum', + 'SchemaInput', + 'SchemaUnion', + 'SchemaScalar', + 'SchemaInterfaceB', + 'CodeScalar', + 'CodeInput', + 'CodeUnion', + 'CodeEnum', + 'CodeType', + ], + 'directives' => [ + '@schemaDirective', + '@codeDirective', + '@deprecated', + '@scalar', + '@mock', + ], + ], new TestSettings(), 0, ], diff --git a/src/SchemaPrinter/PrinterTest~default-settings.graphql b/src/SchemaPrinter/PrinterTest~default-settings.graphql index 1f0eeec1..61451717 100644 --- a/src/SchemaPrinter/PrinterTest~default-settings.graphql +++ b/src/SchemaPrinter/PrinterTest~default-settings.graphql @@ -60,13 +60,18 @@ input SchemaInputUnused { Description """ interface CodeInterface { - a: Boolean + a: Boolean! } interface SchemaInterfaceA { a: Boolean! } +""" +Lighthouse not yet support "implements" for interface... + +@see https://github.com/webonyx/graphql-php/issues/728 +""" interface SchemaInterfaceB { a: Boolean! diff --git a/src/SchemaPrinter/PrinterTest~schema.graphql b/src/SchemaPrinter/PrinterTest~schema.graphql index 3565599e..dc44e0e5 100644 --- a/src/SchemaPrinter/PrinterTest~schema.graphql +++ b/src/SchemaPrinter/PrinterTest~schema.graphql @@ -21,6 +21,11 @@ interface SchemaInterfaceA { a: Boolean! } +""" +Lighthouse not yet support "implements" for interface... + +@see https://github.com/webonyx/graphql-php/issues/728 +""" interface SchemaInterfaceB implements SchemaInterfaceA & CodeInterface @schemaDirective { a: Boolean! "Deprecated field" diff --git a/src/SchemaPrinter/PrinterTest~test-settings.graphql b/src/SchemaPrinter/PrinterTest~test-settings.graphql index 3fa49eee..271ee6aa 100644 --- a/src/SchemaPrinter/PrinterTest~test-settings.graphql +++ b/src/SchemaPrinter/PrinterTest~test-settings.graphql @@ -39,6 +39,11 @@ input SchemaInput f: [String!] } +""" +Lighthouse not yet support "implements" for interface... + +@see https://github.com/webonyx/graphql-php/issues/728 +""" interface SchemaInterfaceB @schemaDirective { From 798dc3f9140ba6cd3f8daf46bd8dddc0b8359913 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 30 Jan 2022 15:58:09 +0400 Subject: [PATCH 86/90] `DefaultSettings` renamed to `GraphQLSettings`. --- src/SchemaPrinter/IntrospectionPrinterTest.php | 8 ++++---- ... => IntrospectionPrinterTest~graphql-settings.graphql} | 0 src/SchemaPrinter/Printer.php | 4 ++-- src/SchemaPrinter/PrinterTest.php | 8 ++++---- ...tings.graphql => PrinterTest~graphql-settings.graphql} | 0 .../Settings/{DefaultSettings.php => GraphQLSettings.php} | 5 ++++- 6 files changed, 14 insertions(+), 11 deletions(-) rename src/SchemaPrinter/{IntrospectionPrinterTest~default-settings.graphql => IntrospectionPrinterTest~graphql-settings.graphql} (100%) rename src/SchemaPrinter/{PrinterTest~default-settings.graphql => PrinterTest~graphql-settings.graphql} (100%) rename src/SchemaPrinter/Settings/{DefaultSettings.php => GraphQLSettings.php} (92%) diff --git a/src/SchemaPrinter/IntrospectionPrinterTest.php b/src/SchemaPrinter/IntrospectionPrinterTest.php index da9e7954..e20eb59d 100644 --- a/src/SchemaPrinter/IntrospectionPrinterTest.php +++ b/src/SchemaPrinter/IntrospectionPrinterTest.php @@ -4,7 +4,7 @@ use GraphQL\Type\Schema; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\GraphQLSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -37,9 +37,9 @@ public function testPrint(string $expected, Settings $settings, int $level): voi */ public function dataProviderPrint(): array { return [ - DefaultSettings::class => [ - '~default-settings.graphql', - new DefaultSettings(), + GraphQLSettings::class => [ + '~graphql-settings.graphql', + new GraphQLSettings(), 0, ], TestSettings::class => [ diff --git a/src/SchemaPrinter/IntrospectionPrinterTest~default-settings.graphql b/src/SchemaPrinter/IntrospectionPrinterTest~graphql-settings.graphql similarity index 100% rename from src/SchemaPrinter/IntrospectionPrinterTest~default-settings.graphql rename to src/SchemaPrinter/IntrospectionPrinterTest~graphql-settings.graphql diff --git a/src/SchemaPrinter/Printer.php b/src/SchemaPrinter/Printer.php index 5b341719..dd59555b 100644 --- a/src/SchemaPrinter/Printer.php +++ b/src/SchemaPrinter/Printer.php @@ -12,7 +12,7 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\GraphQLSettings; use Nuwave\Lighthouse\Schema\DirectiveLocator; use Nuwave\Lighthouse\Schema\ExecutableTypeNodeConverter; @@ -29,7 +29,7 @@ public function __construct( protected ExecutableTypeNodeConverter $converter, Settings $settings = null, ) { - $this->settings = $settings ?? new DefaultSettings(); + $this->settings = $settings ?? new GraphQLSettings(); } public function getLevel(): int { diff --git a/src/SchemaPrinter/PrinterTest.php b/src/SchemaPrinter/PrinterTest.php index 0776fc78..48d6c060 100644 --- a/src/SchemaPrinter/PrinterTest.php +++ b/src/SchemaPrinter/PrinterTest.php @@ -12,7 +12,7 @@ use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\UnionType; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\GraphQLSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use Nuwave\Lighthouse\Schema\DirectiveLocator; @@ -126,9 +126,9 @@ public static function definition(): string { */ public function dataProviderPrint(): array { return [ - DefaultSettings::class => [ + GraphQLSettings::class => [ [ - 'schema' => '~default-settings.graphql', + 'schema' => '~graphql-settings.graphql', 'types' => [ 'String', 'Boolean', @@ -151,7 +151,7 @@ public function dataProviderPrint(): array { // empty ], ], - new DefaultSettings(), + new GraphQLSettings(), 0, ], TestSettings::class => [ diff --git a/src/SchemaPrinter/PrinterTest~default-settings.graphql b/src/SchemaPrinter/PrinterTest~graphql-settings.graphql similarity index 100% rename from src/SchemaPrinter/PrinterTest~default-settings.graphql rename to src/SchemaPrinter/PrinterTest~graphql-settings.graphql diff --git a/src/SchemaPrinter/Settings/DefaultSettings.php b/src/SchemaPrinter/Settings/GraphQLSettings.php similarity index 92% rename from src/SchemaPrinter/Settings/DefaultSettings.php rename to src/SchemaPrinter/Settings/GraphQLSettings.php index 3a55dd64..f8144e12 100644 --- a/src/SchemaPrinter/Settings/DefaultSettings.php +++ b/src/SchemaPrinter/Settings/GraphQLSettings.php @@ -4,7 +4,10 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\DirectiveFilter; -class DefaultSettings extends ImmutableSettings { +/** + * These settings close as possible to {@see \GraphQL\Utils\SchemaPrinter}. + */ +class GraphQLSettings extends ImmutableSettings { protected string $space = ' '; protected string $indent = ' '; protected string $fileEnd = "\n"; From b021d5eb89b230aecf1c9d7adbe03ca00782eee6 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 30 Jan 2022 16:34:24 +0400 Subject: [PATCH 87/90] `GraphQLSettings` will print `@deprecated`, also added `DefaultSettings` settings. --- src/SchemaPrinter/IntrospectionPrinter.php | 11 +- src/SchemaPrinter/Printer.php | 7 +- src/SchemaPrinter/PrinterTest.php | 55 +++++- .../PrinterTest~default-settings.graphql | 160 ++++++++++++++++++ .../PrinterTest~graphql-settings.graphql | 6 + .../Settings/DefaultSettings.php | 29 ++++ .../Settings/GraphQLDirectiveFilter.php | 18 ++ .../Settings/GraphQLSettings.php | 8 +- 8 files changed, 283 insertions(+), 11 deletions(-) create mode 100644 src/SchemaPrinter/PrinterTest~default-settings.graphql create mode 100644 src/SchemaPrinter/Settings/DefaultSettings.php create mode 100644 src/SchemaPrinter/Settings/GraphQLDirectiveFilter.php diff --git a/src/SchemaPrinter/IntrospectionPrinter.php b/src/SchemaPrinter/IntrospectionPrinter.php index e4cefe9e..4be0ec29 100644 --- a/src/SchemaPrinter/IntrospectionPrinter.php +++ b/src/SchemaPrinter/IntrospectionPrinter.php @@ -5,9 +5,10 @@ use GraphQL\Type\Definition\Directive; use GraphQL\Type\Introspection; use GraphQL\Type\Schema; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Printer\DefinitionList; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\ImmutableSettings; /** @@ -18,15 +19,15 @@ * - {@see Settings::isPrintDirectiveDefinitions()} */ class IntrospectionPrinter extends Printer { - public function setSettings(Settings $settings): static { + public function setSettings(?Settings $settings): static { return parent::setSettings( - ImmutableSettings::createFrom($settings) + ImmutableSettings::createFrom($settings ?? new DefaultSettings()) ->setPrintUnusedDefinitions(true) ->setPrintDirectiveDefinitions(true), ); } - protected function getTypeDefinitions(PrinterSettings $settings, Schema $schema): DefinitionList { + protected function getTypeDefinitions(PrinterSettings $settings, Schema $schema): BlockList { $blocks = $this->getDefinitionList($settings); foreach (Introspection::getTypes() as $type) { @@ -36,7 +37,7 @@ protected function getTypeDefinitions(PrinterSettings $settings, Schema $schema) return $blocks; } - protected function getDirectiveDefinitions(PrinterSettings $settings, Schema $schema): DefinitionList { + protected function getDirectiveDefinitions(PrinterSettings $settings, Schema $schema): BlockList { $blocks = $this->getDefinitionList($settings); $directives = $schema->getDirectives(); diff --git a/src/SchemaPrinter/Printer.php b/src/SchemaPrinter/Printer.php index dd59555b..9993aea8 100644 --- a/src/SchemaPrinter/Printer.php +++ b/src/SchemaPrinter/Printer.php @@ -12,6 +12,7 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\GraphQLSettings; use Nuwave\Lighthouse\Schema\DirectiveLocator; use Nuwave\Lighthouse\Schema\ExecutableTypeNodeConverter; @@ -29,7 +30,7 @@ public function __construct( protected ExecutableTypeNodeConverter $converter, Settings $settings = null, ) { - $this->settings = $settings ?? new GraphQLSettings(); + $this->setSettings($settings); } public function getLevel(): int { @@ -46,8 +47,8 @@ public function getSettings(): Settings { return $this->settings; } - public function setSettings(Settings $settings): static { - $this->settings = $settings; + public function setSettings(?Settings $settings): static { + $this->settings = $settings ?? new DefaultSettings(); return $this; } diff --git a/src/SchemaPrinter/PrinterTest.php b/src/SchemaPrinter/PrinterTest.php index 48d6c060..93a7b92b 100644 --- a/src/SchemaPrinter/PrinterTest.php +++ b/src/SchemaPrinter/PrinterTest.php @@ -12,6 +12,7 @@ use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\UnionType; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\GraphQLSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\SchemaPrinter\TestSettings; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; @@ -33,7 +34,7 @@ class PrinterTest extends TestCase { * * @param array{schema: string, types: array, directives: array} */ - public function testPrint(array $expected, Settings $settings, int $level): void { + public function testPrint(array $expected, ?Settings $settings, int $level): void { // Types $directives = $this->app->make(DirectiveLocator::class); $registry = $this->app->make(TypeRegistry::class); @@ -126,6 +127,56 @@ public static function definition(): string { */ public function dataProviderPrint(): array { return [ + 'null' => [ + [ + 'schema' => '~default-settings.graphql', + 'types' => [ + 'String', + 'Boolean', + 'SchemaType', + 'SchemaEnum', + 'SchemaInput', + 'SchemaUnion', + 'SchemaScalar', + 'SchemaInterfaceB', + 'CodeScalar', + 'CodeInput', + 'CodeUnion', + 'CodeEnum', + 'CodeType', + ], + 'directives' => [ + '@deprecated', + ], + ], + null, + 0, + ], + DefaultSettings::class => [ + [ + 'schema' => '~default-settings.graphql', + 'types' => [ + 'String', + 'Boolean', + 'SchemaType', + 'SchemaEnum', + 'SchemaInput', + 'SchemaUnion', + 'SchemaScalar', + 'SchemaInterfaceB', + 'CodeScalar', + 'CodeInput', + 'CodeUnion', + 'CodeEnum', + 'CodeType', + ], + 'directives' => [ + '@deprecated', + ], + ], + new DefaultSettings(), + 0, + ], GraphQLSettings::class => [ [ 'schema' => '~graphql-settings.graphql', @@ -148,7 +199,7 @@ public function dataProviderPrint(): array { 'SchemaScalarUnused', ], 'directives' => [ - // empty + '@deprecated', ], ], new GraphQLSettings(), diff --git a/src/SchemaPrinter/PrinterTest~default-settings.graphql b/src/SchemaPrinter/PrinterTest~default-settings.graphql new file mode 100644 index 00000000..b090a8c7 --- /dev/null +++ b/src/SchemaPrinter/PrinterTest~default-settings.graphql @@ -0,0 +1,160 @@ +enum CodeEnum { + A + B + C +} + +enum SchemaEnum { + A + @deprecated + + """ + Description + """ + B +} + +""" +Description +""" +input CodeInput { + a: Boolean +} + +input SchemaInput { + a: CodeScalar + b: CodeEnum + c: SchemaScalar + d: SchemaEnum + + """ + Recursion + """ + e: SchemaInput + + f: [String!] +} + +""" +Lighthouse not yet support "implements" for interface... + +@see https://github.com/webonyx/graphql-php/issues/728 +""" +interface SchemaInterfaceB { + a: Boolean! + + """ + Deprecated field + """ + b: [String]! + @deprecated + + c( + """ + aaa + """ + a: String + + """ + bbb + """ + b: [SchemaScalar!]! + + c: SchemaEnum + ): CodeUnion + + d: CodeScalar + e: CodeEnum +} + +""" +The `String` scalar type represents textual data, represented as UTF-8 +character sequences. The String type is most often used by GraphQL to +represent free-form human-readable text. +""" +scalar CodeScalar + +""" +The `String` scalar type represents textual data, represented as UTF-8 +character sequences. The String type is most often used by GraphQL to +represent free-form human-readable text. +""" +scalar SchemaScalar + +""" +Description +""" +type CodeType { + a: Boolean +} + +type Query { + a: SchemaType + @deprecated(reason: "deprecated reason") + + b: SchemaEnum + @deprecated + + c( + a: SchemaInput = { + a: "aaa" + b: A + c: "ccc" + d: A + e: { + a: "aaa" + b: A + c: "ccc" + d: A + f: ["aaa", "bbb", "ccc", "ddd"] + } + } + ): CodeScalar + + d(a: SchemaInput = {}): CodeType +} + +type SchemaType +implements + & SchemaInterfaceB +{ + a: Boolean! + + """ + Deprecated field + """ + b: [String]! + @deprecated + + c( + """ + aaa + """ + a: String + + """ + bbb + """ + b: [SchemaScalar!]! + + c: CodeInput + ): CodeUnion + + d: CodeScalar + e: CodeEnum + + f( + a: [String!] = [ + "very very very long line of text" + "very very very long line of text" + "very very very long line of text" + ] + ): SchemaUnion +} + +union CodeUnion = + | CodeType + +union SchemaUnion = + | CodeType + | SchemaType diff --git a/src/SchemaPrinter/PrinterTest~graphql-settings.graphql b/src/SchemaPrinter/PrinterTest~graphql-settings.graphql index 61451717..8264b035 100644 --- a/src/SchemaPrinter/PrinterTest~graphql-settings.graphql +++ b/src/SchemaPrinter/PrinterTest~graphql-settings.graphql @@ -6,6 +6,7 @@ enum CodeEnum { enum SchemaEnum { A + @deprecated """ Description @@ -79,6 +80,7 @@ interface SchemaInterfaceB { Deprecated field """ b: [String]! + @deprecated c( """ @@ -134,7 +136,10 @@ type CodeType { type Query { a: SchemaType + @deprecated(reason: "deprecated reason") + b: SchemaEnum + @deprecated c( a: SchemaInput = { @@ -162,6 +167,7 @@ type SchemaType implements SchemaInterfaceB { Deprecated field """ b: [String]! + @deprecated c( """ diff --git a/src/SchemaPrinter/Settings/DefaultSettings.php b/src/SchemaPrinter/Settings/DefaultSettings.php new file mode 100644 index 00000000..280febca --- /dev/null +++ b/src/SchemaPrinter/Settings/DefaultSettings.php @@ -0,0 +1,29 @@ +name === Directive::DEPRECATED_NAME; + } +} diff --git a/src/SchemaPrinter/Settings/GraphQLSettings.php b/src/SchemaPrinter/Settings/GraphQLSettings.php index f8144e12..d1bce16a 100644 --- a/src/SchemaPrinter/Settings/GraphQLSettings.php +++ b/src/SchemaPrinter/Settings/GraphQLSettings.php @@ -13,7 +13,7 @@ class GraphQLSettings extends ImmutableSettings { protected string $fileEnd = "\n"; protected string $lineEnd = "\n"; protected int $lineLength = 80; - protected bool $printDirectives = false; + protected bool $printDirectives = true; protected bool $printDirectiveDefinitions = false; protected bool $printDirectivesInDescription = false; protected bool $printUnusedDefinitions = true; @@ -29,4 +29,10 @@ class GraphQLSettings extends ImmutableSettings { protected bool $alwaysMultilineInterfaces = false; protected bool $alwaysMultilineDirectiveLocations = false; protected ?DirectiveFilter $directiveFilter = null; + + public function __construct() { + parent::__construct(); + + $this->directiveFilter = new GraphQLDirectiveFilter(); + } } From 243f929ead66e650d2470943549e211462819ac8 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 30 Jan 2022 16:40:29 +0400 Subject: [PATCH 88/90] `Printer` will correctly print schema when `isPrintDirectiveDefinitions()` is `false`. --- src/SchemaPrinter/Printer.php | 20 +- src/SchemaPrinter/PrinterTest.php | 38 +++- ...settings-no-directives-definitions.graphql | 178 ++++++++++++++++++ 3 files changed, 223 insertions(+), 13 deletions(-) create mode 100644 src/SchemaPrinter/PrinterTest~test-settings-no-directives-definitions.graphql diff --git a/src/SchemaPrinter/Printer.php b/src/SchemaPrinter/Printer.php index 9993aea8..8f61c860 100644 --- a/src/SchemaPrinter/Printer.php +++ b/src/SchemaPrinter/Printer.php @@ -13,7 +13,6 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\DirectiveResolver; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Misc\PrinterSettings; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; -use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\GraphQLSettings; use Nuwave\Lighthouse\Schema\DirectiveLocator; use Nuwave\Lighthouse\Schema\ExecutableTypeNodeConverter; @@ -139,10 +138,11 @@ protected function getDirectiveDefinitions(PrinterSettings $settings, Schema $sc * @return array */ protected function getUsedDefinitions(PrinterSettings $settings, Schema $schema, Block $root): array { - $resolver = $settings->getResolver(); - $directives = $this->getDefinitionList($settings); - $types = $this->getDefinitionList($settings); - $stack = $root->getUsedDirectives() + $root->getUsedTypes(); + $directivesDefinitions = $settings->isPrintDirectiveDefinitions(); + $directivesResolver = $settings->getResolver(); + $directives = $this->getDefinitionList($settings); + $types = $this->getDefinitionList($settings); + $stack = $root->getUsedDirectives() + $root->getUsedTypes(); while ($stack) { // Added? @@ -156,11 +156,13 @@ protected function getUsedDefinitions(PrinterSettings $settings, Schema $schema, $block = null; if (str_starts_with($name, '@')) { - $directive = $resolver->getDefinition(substr($name, 1)); + if ($directivesDefinitions) { + $directive = $directivesResolver->getDefinition(substr($name, 1)); - if ($this->isDirective($directive)) { - $block = $this->getDefinitionBlock($settings, $directive); - $directives[$name] = $block; + if ($this->isDirective($directive)) { + $block = $this->getDefinitionBlock($settings, $directive); + $directives[$name] = $block; + } } } else { $type = $schema->getType($name); diff --git a/src/SchemaPrinter/PrinterTest.php b/src/SchemaPrinter/PrinterTest.php index 93a7b92b..96162ca6 100644 --- a/src/SchemaPrinter/PrinterTest.php +++ b/src/SchemaPrinter/PrinterTest.php @@ -127,7 +127,7 @@ public static function definition(): string { */ public function dataProviderPrint(): array { return [ - 'null' => [ + 'null' => [ [ 'schema' => '~default-settings.graphql', 'types' => [ @@ -152,7 +152,7 @@ public function dataProviderPrint(): array { null, 0, ], - DefaultSettings::class => [ + DefaultSettings::class => [ [ 'schema' => '~default-settings.graphql', 'types' => [ @@ -177,7 +177,7 @@ public function dataProviderPrint(): array { new DefaultSettings(), 0, ], - GraphQLSettings::class => [ + GraphQLSettings::class => [ [ 'schema' => '~graphql-settings.graphql', 'types' => [ @@ -205,7 +205,7 @@ public function dataProviderPrint(): array { new GraphQLSettings(), 0, ], - TestSettings::class => [ + TestSettings::class => [ [ 'schema' => '~test-settings.graphql', 'types' => [ @@ -234,6 +234,36 @@ public function dataProviderPrint(): array { new TestSettings(), 0, ], + TestSettings::class.' (no directives definitions)' => [ + [ + 'schema' => '~test-settings-no-directives-definitions.graphql', + 'types' => [ + 'String', + 'Boolean', + 'SchemaType', + 'SchemaEnum', + 'SchemaInput', + 'SchemaUnion', + 'SchemaScalar', + 'SchemaInterfaceB', + 'CodeScalar', + 'CodeInput', + 'CodeUnion', + 'CodeEnum', + 'CodeType', + ], + 'directives' => [ + '@schemaDirective', + '@codeDirective', + '@deprecated', + '@scalar', + '@mock', + ], + ], + (new TestSettings()) + ->setPrintDirectiveDefinitions(false), + 0, + ], ]; } // diff --git a/src/SchemaPrinter/PrinterTest~test-settings-no-directives-definitions.graphql b/src/SchemaPrinter/PrinterTest~test-settings-no-directives-definitions.graphql new file mode 100644 index 00000000..392a9406 --- /dev/null +++ b/src/SchemaPrinter/PrinterTest~test-settings-no-directives-definitions.graphql @@ -0,0 +1,178 @@ +enum CodeEnum { + A + B + C +} + +enum SchemaEnum { + A + @deprecated + + """ + Description + """ + B +} + +""" +Description +""" +input CodeInput +@schemaDirective +{ + a: Boolean +} + +input SchemaInput +@schemaDirective +{ + a: CodeScalar + b: CodeEnum + c: SchemaScalar + d: SchemaEnum + + """ + Recursion + """ + e: SchemaInput + + f: [String!] +} + +""" +Lighthouse not yet support "implements" for interface... + +@see https://github.com/webonyx/graphql-php/issues/728 +""" +interface SchemaInterfaceB +@schemaDirective +{ + a: Boolean! + + """ + Deprecated field + """ + b: [String]! + @deprecated + + c( + """ + aaa + """ + a: String + + """ + bbb + """ + b: [SchemaScalar!]! + + c: SchemaEnum + ): CodeUnion + + d: CodeScalar + e: CodeEnum +} + +""" +The `String` scalar type represents textual data, represented as UTF-8 +character sequences. The String type is most often used by GraphQL to +represent free-form human-readable text. +""" +scalar CodeScalar + +""" +The `String` scalar type represents textual data, represented as UTF-8 +character sequences. The String type is most often used by GraphQL to +represent free-form human-readable text. +""" +scalar SchemaScalar +@scalar(class: "GraphQL\\Type\\Definition\\StringType") +@codeDirective + +""" +Description +""" +type CodeType +@schemaDirective +{ + a: Boolean +} + +type Query { + a: SchemaType + @deprecated(reason: "deprecated reason") + @codeDirective + @mock + + b: SchemaEnum + @deprecated + @mock + + c( + a: SchemaInput = { + a: "aaa" + b: A + c: "ccc" + d: A + e: { + a: "aaa" + b: A + c: "ccc" + d: A + f: ["aaa", "bbb", "ccc", "ddd"] + } + } + ): CodeScalar + @mock + + d(a: SchemaInput = {}): CodeType + @mock +} + +type SchemaType +implements + & SchemaInterfaceB +@schemaDirective +{ + a: Boolean! + + """ + Deprecated field + """ + b: [String]! + @deprecated + + c( + """ + aaa + """ + a: String + + """ + bbb + """ + b: [SchemaScalar!]! + + c: CodeInput + ): CodeUnion + + d: CodeScalar + e: CodeEnum + + f( + a: [String!] = [ + "very very very long line of text" + "very very very long line of text" + "very very very long line of text" + ] + ): SchemaUnion +} + +union CodeUnion = + | CodeType + +union SchemaUnion +@schemaDirective += + | CodeType + | SchemaType From b3edd76f2f86e3444d78171873cd6e0993f40504 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 30 Jan 2022 16:49:29 +0400 Subject: [PATCH 89/90] More tests. --- src/SchemaPrinter/PrinterTest.php | 70 ++++++ ...settings-directives-in-description.graphql | 195 ++++++++++++++++ ...est~test-settings-no-normalization.graphql | 216 ++++++++++++++++++ 3 files changed, 481 insertions(+) create mode 100644 src/SchemaPrinter/PrinterTest~test-settings-directives-in-description.graphql create mode 100644 src/SchemaPrinter/PrinterTest~test-settings-no-normalization.graphql diff --git a/src/SchemaPrinter/PrinterTest.php b/src/SchemaPrinter/PrinterTest.php index 96162ca6..c8a78a09 100644 --- a/src/SchemaPrinter/PrinterTest.php +++ b/src/SchemaPrinter/PrinterTest.php @@ -264,6 +264,76 @@ public function dataProviderPrint(): array { ->setPrintDirectiveDefinitions(false), 0, ], + TestSettings::class.' (directives in description)' => [ + [ + 'schema' => '~test-settings-directives-in-description.graphql', + 'types' => [ + 'String', + 'Boolean', + 'SchemaType', + 'SchemaEnum', + 'SchemaInput', + 'SchemaUnion', + 'SchemaScalar', + 'SchemaInterfaceB', + 'CodeScalar', + 'CodeInput', + 'CodeUnion', + 'CodeEnum', + 'CodeType', + ], + 'directives' => [ + // empty + ], + ], + (new TestSettings()) + ->setPrintDirectives(false) + ->setPrintDirectiveDefinitions(false) + ->setPrintDirectivesInDescription(true), + 0, + ], + TestSettings::class.' (no normalization)' => [ + [ + 'schema' => '~test-settings-no-normalization.graphql', + 'types' => [ + 'String', + 'Boolean', + 'SchemaType', + 'SchemaEnum', + 'SchemaInput', + 'SchemaUnion', + 'SchemaScalar', + 'SchemaInterfaceB', + 'CodeScalar', + 'CodeInput', + 'CodeUnion', + 'CodeEnum', + 'CodeType', + ], + 'directives' => [ + '@schemaDirective', + '@codeDirective', + '@deprecated', + '@scalar', + '@mock', + ], + ], + (new TestSettings()) + ->setNormalizeSchema(false) + ->setNormalizeUnions(false) + ->setNormalizeEnums(false) + ->setNormalizeInterfaces(false) + ->setNormalizeFields(false) + ->setNormalizeArguments(false) + ->setNormalizeDescription(false) + ->setNormalizeDirectiveLocations(false) + ->setAlwaysMultilineUnions(false) + ->setAlwaysMultilineInterfaces(false) + ->setAlwaysMultilineDirectiveLocations(false), + 0, + ], + + // Settings ]; } // diff --git a/src/SchemaPrinter/PrinterTest~test-settings-directives-in-description.graphql b/src/SchemaPrinter/PrinterTest~test-settings-directives-in-description.graphql new file mode 100644 index 00000000..facc0ed9 --- /dev/null +++ b/src/SchemaPrinter/PrinterTest~test-settings-directives-in-description.graphql @@ -0,0 +1,195 @@ +enum CodeEnum { + A + B + C +} + +enum SchemaEnum { + """ + @deprecated + """ + A + + """ + Description + """ + B +} + +""" +Description + +@schemaDirective +""" +input CodeInput { + a: Boolean +} + +""" +@schemaDirective +""" +input SchemaInput { + a: CodeScalar + b: CodeEnum + c: SchemaScalar + d: SchemaEnum + + """ + Recursion + """ + e: SchemaInput + + f: [String!] +} + +""" +Lighthouse not yet support "implements" for interface... + +@see https://github.com/webonyx/graphql-php/issues/728 + +@schemaDirective +""" +interface SchemaInterfaceB { + a: Boolean! + + """ + Deprecated field + + @deprecated + """ + b: [String]! + + c( + """ + aaa + """ + a: String + + """ + bbb + """ + b: [SchemaScalar!]! + + c: SchemaEnum + ): CodeUnion + + d: CodeScalar + e: CodeEnum +} + +""" +The `String` scalar type represents textual data, represented as UTF-8 +character sequences. The String type is most often used by GraphQL to +represent free-form human-readable text. +""" +scalar CodeScalar + +""" +The `String` scalar type represents textual data, represented as UTF-8 +character sequences. The String type is most often used by GraphQL to +represent free-form human-readable text. + +@scalar(class: "GraphQL\\Type\\Definition\\StringType") +@codeDirective +""" +scalar SchemaScalar + +""" +Description + +@schemaDirective +""" +type CodeType { + a: Boolean +} + +type Query { + """ + @deprecated(reason: "deprecated reason") + @codeDirective + @mock + """ + a: SchemaType + + """ + @deprecated + @mock + """ + b: SchemaEnum + + """ + @mock + """ + c( + a: SchemaInput = { + a: "aaa" + b: A + c: "ccc" + d: A + e: { + a: "aaa" + b: A + c: "ccc" + d: A + f: ["aaa", "bbb", "ccc", "ddd"] + } + } + ): CodeScalar + + """ + @mock + """ + d(a: SchemaInput = {}): CodeType +} + +""" +@schemaDirective +""" +type SchemaType +implements + & SchemaInterfaceB +{ + a: Boolean! + + """ + Deprecated field + + @deprecated + """ + b: [String]! + + c( + """ + aaa + """ + a: String + + """ + bbb + """ + b: [SchemaScalar!]! + + c: CodeInput + ): CodeUnion + + d: CodeScalar + e: CodeEnum + + f( + a: [String!] = [ + "very very very long line of text" + "very very very long line of text" + "very very very long line of text" + ] + ): SchemaUnion +} + +union CodeUnion = + | CodeType + +""" +@schemaDirective +""" +union SchemaUnion = + | CodeType + | SchemaType diff --git a/src/SchemaPrinter/PrinterTest~test-settings-no-normalization.graphql b/src/SchemaPrinter/PrinterTest~test-settings-no-normalization.graphql new file mode 100644 index 00000000..377b6b03 --- /dev/null +++ b/src/SchemaPrinter/PrinterTest~test-settings-no-normalization.graphql @@ -0,0 +1,216 @@ +type Query { + a: SchemaType + @deprecated(reason: "deprecated reason") + @codeDirective + @mock + + b: SchemaEnum + @deprecated + @mock + + c( + a: SchemaInput = { + e: { + f: ["aaa", "bbb", "ccc", "ddd"] + d: A + c: "ccc" + b: A + a: "aaa" + } + d: A + c: "ccc" + b: A + a: "aaa" + } + ): CodeScalar + @mock + + d(a: SchemaInput = {}): CodeType + @mock +} + +""" +Description +""" +type CodeType +@schemaDirective +{ + a: Boolean +} + +input SchemaInput +@schemaDirective +{ + f: [String!] + + """ + Recursion + """ + e: SchemaInput + + d: SchemaEnum + c: SchemaScalar + b: CodeEnum + a: CodeScalar +} + +enum CodeEnum { + C + B + A +} + +""" +The `String` scalar type represents textual data, represented as UTF-8 +character sequences. The String type is most often used by GraphQL to +represent free-form human-readable text. +""" +scalar SchemaScalar +@scalar(class: "GraphQL\\Type\\Definition\\StringType") +@codeDirective + +""" +The `String` scalar type represents textual data, represented as UTF-8 +character sequences. The String type is most often used by GraphQL to +represent free-form human-readable text. +""" +scalar CodeScalar + +enum SchemaEnum { + A + @deprecated + + """ + Description + """ + B +} + +type SchemaType implements SchemaInterfaceB +@schemaDirective +{ + a: Boolean! + + """ + Deprecated field + """ + b: [String]! + @deprecated + + c( + """ + aaa + """ + a: String + + """ + bbb + """ + b: [SchemaScalar!]! + + c: CodeInput + ): CodeUnion + + d: CodeScalar + e: CodeEnum + + f( + a: [String!] = [ + "very very very long line of text" + "very very very long line of text" + "very very very long line of text" + ] + ): SchemaUnion +} + +union SchemaUnion +@schemaDirective += SchemaType | CodeType + +""" +Description +""" +input CodeInput +@schemaDirective +{ + a: Boolean +} + +union CodeUnion = CodeType + +""" +Lighthouse not yet support "implements" for interface... + +@see https://github.com/webonyx/graphql-php/issues/728 +""" +interface SchemaInterfaceB +@schemaDirective +{ + a: Boolean! + + """ + Deprecated field + """ + b: [String]! + @deprecated + + c( + """ + aaa + """ + a: String + + """ + bbb + """ + b: [SchemaScalar!]! + + c: SchemaEnum + ): CodeUnion + + d: CodeScalar + e: CodeEnum +} + +""" +Directive +""" +directive @schemaDirective( + """ + Directive argument + """ + message: String +) +on + | SCHEMA + | FIELD + | ARGUMENT_DEFINITION + | INTERFACE + | OBJECT + | UNION + | INPUT_OBJECT + | SCALAR + +""" +Reference a class implementing a scalar definition. +""" +directive @scalar( + """ + Reference to a class that extends `\GraphQL\Type\Definition\ScalarType`. + """ + class: String! +) +on | SCALAR + +""" +Allows you to easily hook up a resolver for an endpoint. +""" +directive @mock( + """ + Specify a unique key for the mock resolver. + """ + key: String = "default" +) +on | FIELD_DEFINITION + +directive @codeDirective repeatable on SCHEMA | SCALAR | INTERFACE From 70b8d3f6a15ede0a9d9a6613f9de8cbe33dab6c9 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 30 Jan 2022 17:36:48 +0400 Subject: [PATCH 90/90] Code cleanup and fixes. --- src/SchemaPrinter/Blocks/Ast/ListValueList.php | 1 + src/SchemaPrinter/Blocks/Ast/ObjectValueList.php | 1 + src/SchemaPrinter/Blocks/BlockList.php | 2 -- src/SchemaPrinter/Blocks/BlockListTest.php | 6 +++--- src/SchemaPrinter/Blocks/BlockTest.php | 2 +- src/SchemaPrinter/Blocks/Types/DescriptionTest.php | 2 +- .../Blocks/Types/EnumTypeDefinitionBlockTest.php | 2 +- .../Blocks/Types/FieldDefinitionBlockTest.php | 2 +- .../Blocks/Types/InputValueDefinitionBlockTest.php | 2 +- .../Blocks/Types/InterfaceTypeDefinitionBlockTest.php | 2 +- .../Blocks/Types/SchemaDefinitionBlockTest.php | 2 +- src/SchemaPrinter/Blocks/Types/StringBlockTest.php | 2 +- src/SchemaPrinter/Blocks/Types/TypeBlockTest.php | 2 +- .../Blocks/Types/UnionTypeDefinitionBlockTest.php | 2 +- src/SchemaPrinter/IntrospectionPrinterTest.php | 2 +- src/SchemaPrinter/Misc/DirectiveResolver.php | 6 +++--- src/SchemaPrinter/PrintedSchema.php | 10 ++++------ src/SchemaPrinter/Printer.php | 10 +++++----- src/SchemaPrinter/PrinterTest.php | 10 ++++++---- 19 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/SchemaPrinter/Blocks/Ast/ListValueList.php b/src/SchemaPrinter/Blocks/Ast/ListValueList.php index bb5b6917..6de64f80 100644 --- a/src/SchemaPrinter/Blocks/Ast/ListValueList.php +++ b/src/SchemaPrinter/Blocks/Ast/ListValueList.php @@ -2,6 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; /** diff --git a/src/SchemaPrinter/Blocks/Ast/ObjectValueList.php b/src/SchemaPrinter/Blocks/Ast/ObjectValueList.php index edc47cc3..3ed2cc2b 100644 --- a/src/SchemaPrinter/Blocks/Ast/ObjectValueList.php +++ b/src/SchemaPrinter/Blocks/Ast/ObjectValueList.php @@ -2,6 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Ast; +use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\Block; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Blocks\BlockList; /** diff --git a/src/SchemaPrinter/Blocks/BlockList.php b/src/SchemaPrinter/Blocks/BlockList.php index 2babdc58..0ec315f2 100644 --- a/src/SchemaPrinter/Blocks/BlockList.php +++ b/src/SchemaPrinter/Blocks/BlockList.php @@ -208,8 +208,6 @@ protected function analyze(Block $block): Block { /** * @param TBlock $value - * - * @return mixed */ protected function isValidBlock(Block $value): bool { return !$value->isEmpty(); diff --git a/src/SchemaPrinter/Blocks/BlockListTest.php b/src/SchemaPrinter/Blocks/BlockListTest.php index dfe6ac06..2fe01525 100644 --- a/src/SchemaPrinter/Blocks/BlockListTest.php +++ b/src/SchemaPrinter/Blocks/BlockListTest.php @@ -712,7 +712,7 @@ public function dataProviderToString(): array { */ class BlockListTest__BlockList extends BlockList { public function __construct( - Settings $settings, + PrinterSettings $settings, int $level, int $used, private bool $normalized, @@ -813,8 +813,8 @@ protected function space(): string { */ class BlockListTest__StatisticsBlock extends Block { /** - * @param array $types - * @param array $directives + * @param array $types + * @param array $directives * * @noinspection PhpMissingParentConstructorInspection */ diff --git a/src/SchemaPrinter/Blocks/BlockTest.php b/src/SchemaPrinter/Blocks/BlockTest.php index a6fa6ba4..8640a88f 100644 --- a/src/SchemaPrinter/Blocks/BlockTest.php +++ b/src/SchemaPrinter/Blocks/BlockTest.php @@ -99,7 +99,7 @@ public function testIsEmpty(bool $expected, string $content): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderIsMultiline(): array { $settings = new TestSettings(); diff --git a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php index 6c07194f..7a0616f6 100644 --- a/src/SchemaPrinter/Blocks/Types/DescriptionTest.php +++ b/src/SchemaPrinter/Blocks/Types/DescriptionTest.php @@ -50,7 +50,7 @@ public function testToString( // // ========================================================================= /** - * @return array|null}> + * @return array|null}> */ public function dataProviderToString(): array { $settings = (new TestSettings()) diff --git a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php index a1e7ec90..9755556c 100644 --- a/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/EnumTypeDefinitionBlockTest.php @@ -49,7 +49,7 @@ public function testToString( // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { $settings = (new TestSettings()) diff --git a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php index 126dc8ee..9f29ac1c 100644 --- a/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/FieldDefinitionBlockTest.php @@ -67,7 +67,7 @@ public function testStatistics(): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { $settings = (new TestSettings()) diff --git a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php index df07d181..57a5a82e 100644 --- a/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InputValueDefinitionBlockTest.php @@ -67,7 +67,7 @@ public function testStatistics(): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { $settings = new TestSettings(); diff --git a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php index 50833d81..5257e6ba 100644 --- a/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/InterfaceTypeDefinitionBlockTest.php @@ -82,7 +82,7 @@ public function testStatistics(): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { $settings = (new TestSettings()) diff --git a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php index a8736dcd..27d3d409 100644 --- a/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/SchemaDefinitionBlockTest.php @@ -47,7 +47,7 @@ public function testToString( // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { $settings = (new TestSettings()) diff --git a/src/SchemaPrinter/Blocks/Types/StringBlockTest.php b/src/SchemaPrinter/Blocks/Types/StringBlockTest.php index f6714e54..e1f87613 100644 --- a/src/SchemaPrinter/Blocks/Types/StringBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/StringBlockTest.php @@ -44,7 +44,7 @@ public function testToString( // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { $settings = new TestSettings(); diff --git a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php index d8ace899..ea1be3f3 100644 --- a/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/TypeBlockTest.php @@ -60,7 +60,7 @@ public function testStatistics(): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { $settings = new TestSettings(); diff --git a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php index 08dbf40b..612accba 100644 --- a/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php +++ b/src/SchemaPrinter/Blocks/Types/UnionTypeDefinitionBlockTest.php @@ -68,7 +68,7 @@ public function testStatistics(): void { // // ========================================================================= /** - * @return array + * @return array */ public function dataProviderToString(): array { $settings = (new TestSettings()) diff --git a/src/SchemaPrinter/IntrospectionPrinterTest.php b/src/SchemaPrinter/IntrospectionPrinterTest.php index e20eb59d..7fd49fb9 100644 --- a/src/SchemaPrinter/IntrospectionPrinterTest.php +++ b/src/SchemaPrinter/IntrospectionPrinterTest.php @@ -37,7 +37,7 @@ public function testPrint(string $expected, Settings $settings, int $level): voi */ public function dataProviderPrint(): array { return [ - GraphQLSettings::class => [ + GraphQLSettings::class => [ '~graphql-settings.graphql', new GraphQLSettings(), 0, diff --git a/src/SchemaPrinter/Misc/DirectiveResolver.php b/src/SchemaPrinter/Misc/DirectiveResolver.php index b65f21a6..9df0de70 100644 --- a/src/SchemaPrinter/Misc/DirectiveResolver.php +++ b/src/SchemaPrinter/Misc/DirectiveResolver.php @@ -30,7 +30,7 @@ class DirectiveResolver { protected array $directives; /** - * @param array $directives + * @param array $directives */ public function __construct( protected DirectiveLocator $locator, @@ -68,8 +68,8 @@ public function getDefinitions(): array { $directives = $this->directives; foreach ($this->locator->definitions() as $definition) { - $directive = $this->factory->handle($definition); - $directives[$directives->name] = $directive; + $directive = $this->factory->handle($definition); + $directives[$directive->name] = $directive; } return $directives; diff --git a/src/SchemaPrinter/PrintedSchema.php b/src/SchemaPrinter/PrintedSchema.php index 438c3f8d..9674917f 100644 --- a/src/SchemaPrinter/PrintedSchema.php +++ b/src/SchemaPrinter/PrintedSchema.php @@ -6,8 +6,6 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Statistics; use Stringable; -use function array_values; - class PrintedSchema implements Statistics, Stringable { public function __construct( protected Block $schema, @@ -20,16 +18,16 @@ public function __toString(): string { } /** - * @return array + * @inheritDoc */ public function getUsedTypes(): array { - return array_values($this->schema->getUsedTypes()); + return $this->schema->getUsedTypes(); } /** - * @return array + * @inheritDoc */ public function getUsedDirectives(): array { - return array_values($this->schema->getUsedDirectives()); + return $this->schema->getUsedDirectives(); } } diff --git a/src/SchemaPrinter/Printer.php b/src/SchemaPrinter/Printer.php index 8f61c860..7c6eb129 100644 --- a/src/SchemaPrinter/Printer.php +++ b/src/SchemaPrinter/Printer.php @@ -167,7 +167,7 @@ protected function getUsedDefinitions(PrinterSettings $settings, Schema $schema, } else { $type = $schema->getType($name); - if ($this->isType($type)) { + if ($type && $this->isType($type)) { $block = $this->getDefinitionBlock($settings, $type); $types[$name] = $block; } @@ -198,11 +198,11 @@ protected function getDefinitionBlock( return new DefinitionBlock($settings, $this->getLevel(), $definition); } - private function isType(Type $type): bool { - return !Type::isBuiltInType($type); + private function isType(?Type $type): bool { + return $type && !Type::isBuiltInType($type); } - private function isDirective(Directive $directive): bool { - return !Directive::isSpecifiedDirective($directive); + private function isDirective(?Directive $directive): bool { + return $directive && !Directive::isSpecifiedDirective($directive); } } diff --git a/src/SchemaPrinter/PrinterTest.php b/src/SchemaPrinter/PrinterTest.php index c8a78a09..db957f1a 100644 --- a/src/SchemaPrinter/PrinterTest.php +++ b/src/SchemaPrinter/PrinterTest.php @@ -20,6 +20,8 @@ use Nuwave\Lighthouse\Schema\Directives\BaseDirective; use Nuwave\Lighthouse\Schema\TypeRegistry; +use function array_values; + /** * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Printer @@ -32,7 +34,7 @@ class PrinterTest extends TestCase { * * @dataProvider dataProviderPrint * - * @param array{schema: string, types: array, directives: array} + * @param array{schema: string, types: array, directives: array} $expected */ public function testPrint(array $expected, ?Settings $settings, int $level): void { // Types @@ -115,15 +117,15 @@ public static function definition(): string { $actual = $printer->print($schema); self::assertEquals($output, (string) $actual); - self::assertEqualsCanonicalizing($expected['types'], $actual->getUsedTypes()); - self::assertEqualsCanonicalizing($expected['directives'], $actual->getUsedDirectives()); + self::assertEqualsCanonicalizing($expected['types'], array_values($actual->getUsedTypes())); + self::assertEqualsCanonicalizing($expected['directives'], array_values($actual->getUsedDirectives())); } // // // ========================================================================= /** - * @return array + * @return array> */ public function dataProviderPrint(): array { return [