| Total Complexity | 64 |
| Total Lines | 531 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DefinitionPrinter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DefinitionPrinter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class DefinitionPrinter implements DefinitionPrinterInterface |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $options; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @inheritdoc |
||
| 40 | * @throws PrintException |
||
| 41 | * @throws InvariantException |
||
| 42 | */ |
||
| 43 | public function printSchema(Schema $schema, array $options = []): string |
||
| 44 | { |
||
| 45 | $this->options = $options; |
||
| 46 | |||
| 47 | return $this->printFilteredSchema( |
||
| 48 | $schema, |
||
| 49 | function (Directive $directive): bool { |
||
| 50 | return !isSpecifiedDirective($directive); |
||
| 51 | }, |
||
| 52 | function (NamedTypeInterface $type): bool { |
||
| 53 | return !isSpecifiedScalarType($type) && !isIntrospectionType($type); |
||
| 54 | } |
||
| 55 | ); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @inheritdoc |
||
| 60 | * @throws PrintException |
||
| 61 | * @throws InvariantException |
||
| 62 | */ |
||
| 63 | public function printIntrospectionSchema(Schema $schema, array $options = []): string |
||
| 64 | { |
||
| 65 | $this->options = $options; |
||
| 66 | |||
| 67 | return $this->printFilteredSchema( |
||
| 68 | $schema, |
||
| 69 | function (Directive $directive): bool { |
||
| 70 | return isSpecifiedDirective($directive); |
||
| 71 | }, |
||
| 72 | function (NamedTypeInterface $type): bool { |
||
| 73 | return isIntrospectionType($type); |
||
| 74 | } |
||
| 75 | ); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param DefinitionInterface $definition |
||
| 80 | * @return string |
||
| 81 | * @throws PrintException |
||
| 82 | * @throws InvariantException |
||
| 83 | */ |
||
| 84 | public function print(DefinitionInterface $definition): string |
||
| 85 | { |
||
| 86 | if ($definition instanceof Schema) { |
||
| 87 | return $this->printSchemaDefinition($definition); |
||
| 88 | } |
||
| 89 | |||
| 90 | if ($definition instanceof Directive) { |
||
| 91 | return $this->printDirectiveDefinition($definition); |
||
| 92 | } |
||
| 93 | |||
| 94 | if ($definition instanceof NamedTypeInterface) { |
||
| 95 | return $this->printType($definition); |
||
| 96 | } |
||
| 97 | |||
| 98 | throw new PrintException(\sprintf('Invalid definition object: %s.', toString($definition))); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param Schema $schema |
||
| 103 | * @param callable $directiveFilter |
||
| 104 | * @param callable $typeFilter |
||
| 105 | * @return string |
||
| 106 | * @throws PrintException |
||
| 107 | * @throws InvariantException |
||
| 108 | */ |
||
| 109 | protected function printFilteredSchema( |
||
| 110 | Schema $schema, |
||
| 111 | callable $directiveFilter, |
||
| 112 | callable $typeFilter |
||
| 113 | ): string { |
||
| 114 | /** @noinspection PhpParamsInspection */ |
||
| 115 | $lines = \array_filter(\array_merge( |
||
| 116 | [$this->printOne($schema)], |
||
| 117 | $this->printMany($this->getSchemaDirectives($schema, $directiveFilter)), |
||
| 118 | $this->printMany($this->getSchemaTypes($schema, $typeFilter)) |
||
| 119 | )); |
||
| 120 | |||
| 121 | return printArray("\n\n", $lines) . "\n"; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param Schema $schema |
||
| 126 | * @param callable $filter |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | protected function getSchemaDirectives(Schema $schema, callable $filter): array |
||
| 130 | { |
||
| 131 | return \array_filter($schema->getDirectives(), $filter); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param Schema $schema |
||
| 136 | * @param callable $filter |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | protected function getSchemaTypes(Schema $schema, callable $filter): array |
||
| 140 | { |
||
| 141 | $types = \array_filter(\array_values($schema->getTypeMap()), $filter); |
||
| 142 | |||
| 143 | \usort($types, function (NamedTypeInterface $typeA, NamedTypeInterface $typeB) { |
||
| 144 | return \strcasecmp($typeA->getName(), $typeB->getName()); |
||
| 145 | }); |
||
| 146 | |||
| 147 | return $types; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param Schema $definition |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | protected function printSchemaDefinition(Schema $definition): string |
||
| 155 | { |
||
| 156 | if ($this->isSchemaOfCommonNames($definition)) { |
||
| 157 | return ''; |
||
| 158 | } |
||
| 159 | |||
| 160 | $operationTypes = []; |
||
| 161 | |||
| 162 | if (null !== ($queryType = $definition->getQueryType())) { |
||
| 163 | $operationTypes[] = " query: {$queryType->getName()}"; |
||
| 164 | } |
||
| 165 | |||
| 166 | if (null !== ($mutationType = $definition->getMutationType())) { |
||
| 167 | $operationTypes[] = " mutation: {$mutationType->getName()}"; |
||
| 168 | } |
||
| 169 | |||
| 170 | if (null !== ($subscriptionType = $definition->getSubscriptionType())) { |
||
| 171 | $operationTypes[] = " subscription: {$subscriptionType->getName()}"; |
||
| 172 | } |
||
| 173 | |||
| 174 | return printLines([ |
||
| 175 | 'schema {', |
||
| 176 | printLines($operationTypes), |
||
| 177 | '}' |
||
| 178 | ]); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * GraphQL schema define root types for each type of operation. These types are |
||
| 183 | * the same as any other type and can be named in any manner, however there is |
||
| 184 | * a common naming convention: |
||
| 185 | * |
||
| 186 | * schema { |
||
| 187 | * query: Query |
||
| 188 | * mutation: Mutation |
||
| 189 | * subscription: Subscription |
||
| 190 | * } |
||
| 191 | * |
||
| 192 | * When using this naming convention, the schema description can be omitted. |
||
| 193 | * |
||
| 194 | * @param Schema $schema |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | protected function isSchemaOfCommonNames(Schema $schema): bool |
||
| 198 | { |
||
| 199 | if (null !== ($queryType = $schema->getQueryType()) && |
||
| 200 | $queryType->getName() !== 'Query') { |
||
| 201 | return false; |
||
| 202 | } |
||
| 203 | |||
| 204 | if (null !== ($mutationType = $schema->getMutationType()) && |
||
| 205 | $mutationType->getName() !== 'Mutation') { |
||
| 206 | return false; |
||
| 207 | } |
||
| 208 | |||
| 209 | if (null !== ($subscriptionType = $schema->getSubscriptionType()) && |
||
| 210 | $subscriptionType->getName() !== 'Subscription') { |
||
| 211 | return false; |
||
| 212 | } |
||
| 213 | |||
| 214 | return true; |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param Directive $directive |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function printDirectiveDefinition(Directive $directive): string |
||
| 222 | { |
||
| 223 | $description = $this->printDescription($directive); |
||
| 224 | $name = $directive->getName(); |
||
| 225 | $arguments = $this->printArguments($directive->getArguments()); |
||
| 226 | $locations = implode(' | ', $directive->getLocations()); |
||
| 227 | |||
| 228 | return printLines([ |
||
| 229 | $description, |
||
| 230 | "directive @{$name}{$arguments} on {$locations}", |
||
| 231 | ]); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param NamedTypeInterface $type |
||
| 236 | * @return string |
||
| 237 | * @throws PrintException |
||
| 238 | * @throws InvariantException |
||
| 239 | */ |
||
| 240 | protected function printType(NamedTypeInterface $type): string |
||
| 241 | { |
||
| 242 | if ($type instanceof ScalarType) { |
||
| 243 | return $this->printScalarType($type); |
||
| 244 | } |
||
| 245 | if ($type instanceof ObjectType) { |
||
| 246 | return $this->printObjectType($type); |
||
| 247 | } |
||
| 248 | if ($type instanceof InterfaceType) { |
||
| 249 | return $this->printInterfaceType($type); |
||
| 250 | } |
||
| 251 | if ($type instanceof UnionType) { |
||
| 252 | return $this->printUnionType($type); |
||
| 253 | } |
||
| 254 | if ($type instanceof EnumType) { |
||
| 255 | return $this->printEnumType($type); |
||
| 256 | } |
||
| 257 | if ($type instanceof InputObjectType) { |
||
| 258 | return $this->printInputObjectType($type); |
||
| 259 | } |
||
| 260 | |||
| 261 | throw new PrintException(\sprintf('Unknown type: %s', (string)$type)); |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param ScalarType $type |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | protected function printScalarType(ScalarType $type): string |
||
| 269 | { |
||
| 270 | return printLines([ |
||
| 271 | $this->printDescription($type), |
||
| 272 | "scalar {$type->getName()}" |
||
| 273 | ]); |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param ObjectType $type |
||
| 278 | * @return string |
||
| 279 | * @throws InvariantException |
||
| 280 | */ |
||
| 281 | protected function printObjectType(ObjectType $type): string |
||
| 282 | { |
||
| 283 | $description = $this->printDescription($type); |
||
| 284 | $name = $type->getName(); |
||
| 285 | $implements = $type->hasInterfaces() |
||
| 286 | ? ' implements ' . printArray(' & ', \array_map(function (InterfaceType $interface) { |
||
| 287 | return $interface->getName(); |
||
| 288 | }, $type->getInterfaces())) |
||
| 289 | : ''; |
||
| 290 | $fields = $this->printFields($type->getFields()); |
||
| 291 | |||
| 292 | return printLines([ |
||
| 293 | $description, |
||
| 294 | "type {$name}{$implements} {", |
||
| 295 | $fields, |
||
| 296 | '}' |
||
| 297 | ]); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param InterfaceType $type |
||
| 302 | * @return string |
||
| 303 | * @throws InvariantException |
||
| 304 | */ |
||
| 305 | protected function printInterfaceType(InterfaceType $type): string |
||
| 306 | { |
||
| 307 | $description = $this->printDescription($type); |
||
| 308 | $fields = $this->printFields($type->getFields()); |
||
| 309 | |||
| 310 | return printLines([ |
||
| 311 | $description, |
||
| 312 | "interface {$type->getName()} {", |
||
| 313 | $fields, |
||
| 314 | '}' |
||
| 315 | ]); |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param UnionType $type |
||
| 320 | * @return string |
||
| 321 | * @throws InvariantException |
||
| 322 | */ |
||
| 323 | protected function printUnionType(UnionType $type): string |
||
| 324 | { |
||
| 325 | $description = $this->printDescription($type); |
||
| 326 | $types = printArray(' | ', $type->getTypes()); |
||
| 327 | |||
| 328 | return printLines([ |
||
| 329 | $description, |
||
| 330 | "union {$type->getName()} = {$types}" |
||
| 331 | ]); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param EnumType $type |
||
| 336 | * @return string |
||
| 337 | * @throws InvariantException |
||
| 338 | */ |
||
| 339 | protected function printEnumType(EnumType $type): string |
||
| 340 | { |
||
| 341 | $description = $this->printDescription($type); |
||
| 342 | $values = $this->printEnumValues($type->getValues()); |
||
| 343 | |||
| 344 | return printLines([ |
||
| 345 | $description, |
||
| 346 | "enum {$type->getName()} {", |
||
| 347 | $values, |
||
| 348 | '}' |
||
| 349 | ]); |
||
| 350 | } |
||
| 351 | |||
| 352 | protected function printEnumValues(array $values): string |
||
| 353 | { |
||
| 354 | return printLines(\array_map(function (EnumValue $value): string { |
||
| 355 | $description = $this->printDescription($value, ' '); |
||
| 356 | $name = $value->getName(); |
||
| 357 | $deprecated = $this->printDeprecated($value); |
||
| 358 | $enum = empty($deprecated) ? $name : "{$name} {$deprecated}"; |
||
| 359 | |||
| 360 | return printLines([ |
||
| 361 | $description, |
||
| 362 | " {$enum}" |
||
| 363 | ]); |
||
| 364 | }, $values)); |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param InputObjectType $type |
||
| 369 | * @return string |
||
| 370 | * @throws InvariantException |
||
| 371 | */ |
||
| 372 | protected function printInputObjectType(InputObjectType $type): string |
||
| 373 | { |
||
| 374 | $description = $this->printDescription($type); |
||
| 375 | $fields = \array_map(function (InputField $field): string { |
||
| 376 | $description = $this->printDescription($field, ' '); |
||
| 377 | $inputValue = $this->printInputValue($field); |
||
| 378 | return printLines([ |
||
| 379 | $description, |
||
| 380 | " {$inputValue}" |
||
| 381 | ]); |
||
| 382 | }, \array_values($type->getFields())); |
||
| 383 | |||
| 384 | return printLines([ |
||
| 385 | $description, |
||
| 386 | "input {$type->getName()} {", |
||
| 387 | printLines($fields), |
||
| 388 | '}' |
||
| 389 | ]); |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param InputValueInterface $inputValue |
||
| 394 | * @return string |
||
| 395 | * @throws InvariantException |
||
| 396 | * @throws \Digia\GraphQL\Language\SyntaxErrorException |
||
| 397 | * @throws \Digia\GraphQL\Util\ConversionException |
||
| 398 | */ |
||
| 399 | protected function printInputValue(InputValueInterface $inputValue): string |
||
| 400 | { |
||
| 401 | $type = $inputValue->getType(); |
||
| 402 | $name = $inputValue->getName(); |
||
| 403 | |||
| 404 | $defaultValue = $inputValue->hasDefaultValue() |
||
| 405 | ? printNode(ValueConverter::convert($inputValue->getDefaultValue(), $type)) |
||
| 406 | : null; |
||
| 407 | |||
| 408 | return $defaultValue !== null |
||
| 409 | ? "{$name}: {$type} = {$defaultValue}" |
||
| 410 | : "{$name}: {$type}"; |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param array $fields |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | protected function printFields(array $fields): string |
||
| 418 | { |
||
| 419 | return printLines(\array_map(function (Field $field): string { |
||
| 420 | $description = $this->printDescription($field, ' '); |
||
| 421 | $name = $field->getName(); |
||
| 422 | $arguments = $this->printArguments($field->getArguments()); |
||
| 423 | $type = (string)$field->getType(); |
||
| 424 | $deprecated = $this->printDeprecated($field); |
||
| 425 | return printLines([ |
||
| 426 | $description, |
||
| 427 | " {$name}{$arguments}: {$type}{$deprecated}" |
||
| 428 | ]); |
||
| 429 | }, \array_values($fields))); |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param array $arguments |
||
| 434 | * @param string $indentation |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | protected function printArguments(array $arguments, string $indentation = ''): string |
||
| 438 | { |
||
| 439 | if (empty($arguments)) { |
||
| 440 | return ''; |
||
| 441 | } |
||
| 442 | |||
| 443 | // If every arg does not have a description, print them on one line. |
||
| 444 | if (arrayEvery($arguments, function (Argument $argument): bool { |
||
| 445 | return !$argument->hasDescription(); |
||
| 446 | })) { |
||
| 447 | return printInputFields(\array_map(function (Argument $argument) { |
||
| 448 | return $this->printInputValue($argument); |
||
| 449 | }, $arguments)); |
||
| 450 | } |
||
| 451 | |||
| 452 | $args = \array_map(function (Argument $argument) use ($indentation) { |
||
| 453 | $description = $this->printDescription($argument, ' '); |
||
| 454 | $inputValue = $this->printInputValue($argument); |
||
| 455 | return printLines([ |
||
| 456 | "{$indentation}{$description}", |
||
| 457 | " {$indentation}{$inputValue}" |
||
| 458 | ]); |
||
| 459 | }, $arguments); |
||
| 460 | |||
| 461 | return printLines([ |
||
| 462 | '(', |
||
| 463 | implode("", $args), |
||
| 464 | $indentation . ')' |
||
| 465 | ]); |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @param DeprecationAwareInterface $fieldOrEnumValue |
||
| 470 | * @return string |
||
| 471 | * @throws InvariantException |
||
| 472 | * @throws \Digia\GraphQL\Language\SyntaxErrorException |
||
| 473 | * @throws \Digia\GraphQL\Util\ConversionException |
||
| 474 | */ |
||
| 475 | protected function printDeprecated(DeprecationAwareInterface $fieldOrEnumValue): string |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @param DescriptionAwareInterface $definition |
||
| 494 | * @param string $indentation |
||
| 495 | * @param bool $isFirstInBlock |
||
| 496 | * @return string |
||
| 497 | */ |
||
| 498 | protected function printDescription( |
||
| 499 | DescriptionAwareInterface $definition, |
||
| 500 | string $indentation = '', |
||
| 501 | bool $isFirstInBlock = true |
||
| 502 | ): string { |
||
| 503 | // Don't print anything if the type has no description |
||
| 504 | if ($definition->getDescription() === null) { |
||
| 505 | return ''; |
||
| 506 | } |
||
| 507 | |||
| 508 | $lines = descriptionLines($definition->getDescription(), 120 - \strlen($indentation)); |
||
| 509 | |||
| 510 | if (isset($this->options['commentDescriptions']) && true === $this->options['commentDescriptions']) { |
||
| 511 | return $this->printDescriptionWithComments($lines, $indentation, $isFirstInBlock); |
||
| 512 | } |
||
| 513 | |||
| 514 | $text = \implode("\n", $lines); |
||
| 515 | $preferMultipleLines = \strlen($text) > 70; |
||
| 516 | $blockString = printBlockString($text, '', $preferMultipleLines); |
||
| 517 | $prefix = strlen($indentation) > 0 && !$isFirstInBlock ? "\n" . $indentation : $indentation; |
||
| 518 | |||
| 519 | return $prefix . \str_replace("\n", "\n" . $indentation, $blockString); |
||
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @param array $lines |
||
| 524 | * @param string $indentation |
||
| 525 | * @param bool $isFirstInBlock |
||
| 526 | * @return string |
||
| 527 | */ |
||
| 528 | protected function printDescriptionWithComments(array $lines, string $indentation, bool $isFirstInBlock): string |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * @param DefinitionInterface $definition |
||
| 544 | * @return string |
||
| 545 | * @throws PrintException |
||
| 546 | * @throws InvariantException |
||
| 547 | */ |
||
| 548 | protected function printOne(DefinitionInterface $definition): string |
||
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @param DefinitionInterface[] $definitions |
||
| 555 | * @return array |
||
| 556 | */ |
||
| 557 | protected function printMany(array $definitions): array |
||
| 562 | } |
||
| 563 | } |
||
| 564 |