Skip to content

Make class Printer fully static #1749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/class-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,8 @@ $printed = GraphQL\Language\Printer::doPrint($ast);
*
* Handles both executable definitions and schema definitions.
*
* @throws \JsonException
*
* @api
*/
static function doPrint(GraphQL\Language\AST\Node $ast): string
Expand Down
279 changes: 134 additions & 145 deletions src/Language/Printer.php

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/Type/Definition/BooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function parseValue($value): bool
throw new Error("Boolean cannot represent a non boolean value: {$notBoolean}");
}

/**
* @throws \JsonException
* @throws Error
*/
public function parseLiteral(Node $valueNode, ?array $variables = null): bool
{
if ($valueNode instanceof BooleanValueNode) {
Expand Down
1 change: 1 addition & 0 deletions src/Type/Definition/EnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ public function parseValue($value)
}

/**
* @throws \JsonException
* @throws Error
* @throws InvariantViolation
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Type/Definition/FloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public function parseValue($value): float
return $float;
}

/**
* @throws \JsonException
* @throws Error
*/
public function parseLiteral(Node $valueNode, ?array $variables = null)
{
if ($valueNode instanceof FloatValueNode || $valueNode instanceof IntValueNode) {
Expand Down
4 changes: 4 additions & 0 deletions src/Type/Definition/IDType.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public function parseValue($value): string
throw new Error("ID cannot represent a non-string and non-integer value: {$notID}");
}

/**
* @throws \JsonException
* @throws Error
*/
public function parseLiteral(Node $valueNode, ?array $variables = null): string
{
if ($valueNode instanceof StringValueNode || $valueNode instanceof IntValueNode) {
Expand Down
4 changes: 4 additions & 0 deletions src/Type/Definition/IntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public function parseValue($value): int
return (int) $value;
}

/**
* @throws \JsonException
* @throws Error
*/
public function parseLiteral(Node $valueNode, ?array $variables = null): int
{
if ($valueNode instanceof IntValueNode) {
Expand Down
4 changes: 4 additions & 0 deletions src/Type/Definition/StringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public function parseValue($value): string
return $value;
}

/**
* @throws \JsonException
* @throws Error
*/
public function parseLiteral(Node $valueNode, ?array $variables = null): string
{
if ($valueNode instanceof StringValueNode) {
Expand Down
3 changes: 3 additions & 0 deletions src/Validator/Rules/OverlappingFieldsCanBeMerged.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ protected function findConflict(
/**
* @param NodeList<ArgumentNode> $arguments1 keep
* @param NodeList<ArgumentNode> $arguments2 keep
*
* @throws \JsonException
*/
protected function sameArguments(NodeList $arguments1, NodeList $arguments2): bool
{
Expand Down Expand Up @@ -459,6 +461,7 @@ protected function sameArguments(NodeList $arguments1, NodeList $arguments2): bo
return true;
}

/** @throws \JsonException */
protected function sameValue(Node $value1, Node $value2): bool
{
return Printer::doPrint($value1) === Printer::doPrint($value2);
Expand Down
6 changes: 5 additions & 1 deletion src/Validator/Rules/ValuesOfCorrectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ public function getVisitor(QueryValidationContext $context): array
];
}

/** @param VariableNode|NullValueNode|IntValueNode|FloatValueNode|StringValueNode|BooleanValueNode|EnumValueNode|ListValueNode|ObjectValueNode $node */
/**
* @param VariableNode|NullValueNode|IntValueNode|FloatValueNode|StringValueNode|BooleanValueNode|EnumValueNode|ListValueNode|ObjectValueNode $node
*
* @throws \JsonException
*/
protected function isValidValueNode(QueryValidationContext $context, ValueNode $node): void
{
// Report any error at the full type expected by the location.
Expand Down
4 changes: 4 additions & 0 deletions tests/Executor/TestClasses/ComplexScalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public function parseValue($value): string
throw new Error("Cannot represent value as ComplexScalar: {$notComplexScalar}");
}

/**
* @throws \JsonException
* @throws Error
*/
public function parseLiteral(Node $valueNode, ?array $variables = null): string
{
$value = property_exists($valueNode, 'value')
Expand Down
1 change: 1 addition & 0 deletions tests/TestCaseBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ protected function assertDidNotCrash(): void
$this->addToAssertionCount(1);
}

/** @throws \JsonException */
protected static function assertASTMatches(string $expected, ?Node $node): void
{
self::assertInstanceOf(Node::class, $node);
Expand Down
6 changes: 5 additions & 1 deletion tests/Utils/BuildSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ private static function assertCycle(string $sdl): void
self::assertSame($sdl, $cycled);
}

/** @param ScalarType|ObjectType|InterfaceType|UnionType|EnumType|InputObjectType $obj */
/**
* @param ScalarType|ObjectType|InterfaceType|UnionType|EnumType|InputObjectType $obj
*
* @throws \JsonException
*/
private function printAllASTNodes(NamedType $obj): string
{
self::assertNotNull($obj->astNode);
Expand Down
8 changes: 7 additions & 1 deletion tests/Utils/SchemaExtenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@
/** @phpstan-import-type UnnamedFieldDefinitionConfig from FieldDefinition */
final class SchemaExtenderTest extends TestCaseBase
{
/** @param NamedType|Schema $obj */
/**
* @param NamedType|Schema $obj
*
* @throws \JsonException
*/
private function printExtensionNodes($obj): string
{
assert(isset($obj->extensionASTNodes));
Expand Down Expand Up @@ -92,6 +96,8 @@ private static function printSchemaChanges(Schema $schema, Schema $extendedSchem
/**
* graphql-js uses printASTNode() everywhere, but our Schema doesn't have astNode property,
* hence this helper method that calls getAstNode() instead.
*
* @throws \JsonException
*/
private function printASTSchema(Schema $schema): string
{
Expand Down
Loading