4141 * sortTypes?: bool,
4242 * includeAppliedDirectives?: bool,
4343 * }
44+ * @phpstan-type AppliedDirectiveDefinition Schema|Argument|Directive|FieldDefinition|InputObjectField|EnumValueDefinition|ScalarType|ObjectType|InterfaceType|UnionType|EnumType|InputObjectType
4445 *
4546 * @see \GraphQL\Tests\Utils\SchemaPrinterTest
4647 */
@@ -612,11 +613,15 @@ private static function shouldIncludeAppliedDirectives(array $options): bool
612613 * @param array<string> $excludedDirectiveNames
613614 *
614615 * @phpstan-param Options $options
616+ * @phpstan-param AppliedDirectiveDefinition $definition
615617 *
616618 * @throws \JsonException
617619 */
618- private static function printAppliedDirectivesIfEnabled (array $ options , object $ definition , array $ excludedDirectiveNames = []): string
619- {
620+ private static function printAppliedDirectivesIfEnabled (
621+ array $ options ,
622+ object $ definition ,
623+ array $ excludedDirectiveNames = []
624+ ): string {
620625 if (! self ::shouldIncludeAppliedDirectives ($ options )) {
621626 return '' ;
622627 }
@@ -629,47 +634,60 @@ private static function printAppliedDirectivesIfEnabled(array $options, object $
629634 /**
630635 * @param array<string> $excludedDirectiveNames
631636 *
637+ * @phpstan-param AppliedDirectiveDefinition $definition
638+ *
632639 * @return array<DirectiveNode>
633640 */
634641 private static function collectAppliedDirectives (object $ definition , array $ excludedDirectiveNames = []): array
635642 {
636643 $ directives = [];
637644
638645 if ($ definition instanceof Schema) {
639- foreach ($ definition ->getConfig ()->schemaDirectives as $ directive ) {
640- if (! in_array ($ directive ->name ->value , $ excludedDirectiveNames , true )) {
641- $ directives [] = $ directive ;
642- }
646+ self ::appendConfiguredDirectives ($ directives , $ definition ->getConfig ()->schemaDirectives , $ excludedDirectiveNames );
647+ self ::appendDirectivesFromAstNode ($ directives , $ definition ->astNode , $ excludedDirectiveNames );
648+
649+ foreach ($ definition ->extensionASTNodes as $ extensionASTNode ) {
650+ self ::appendDirectivesFromAstNode ($ directives , $ extensionASTNode , $ excludedDirectiveNames );
643651 }
652+ } elseif ($ definition instanceof Directive) {
653+ self ::appendDirectivesFromAstNode ($ directives , $ definition ->astNode , $ excludedDirectiveNames );
644654 } else {
645- $ definitionVars = get_object_vars ($ definition );
646- $ configuredDirectives = $ definitionVars ['directives ' ] ?? null ;
647-
648- if (is_iterable ($ configuredDirectives )) {
649- foreach ($ configuredDirectives as $ directive ) {
650- if ($ directive instanceof DirectiveNode && ! in_array ($ directive ->name ->value , $ excludedDirectiveNames , true )) {
651- $ directives [] = $ directive ;
652- }
655+ self ::appendConfiguredDirectives ($ directives , $ definition ->directives , $ excludedDirectiveNames );
656+ self ::appendDirectivesFromAstNode ($ directives , $ definition ->astNode , $ excludedDirectiveNames );
657+
658+ if (
659+ $ definition instanceof ScalarType
660+ || $ definition instanceof ObjectType
661+ || $ definition instanceof InterfaceType
662+ || $ definition instanceof UnionType
663+ || $ definition instanceof EnumType
664+ || $ definition instanceof InputObjectType
665+ ) {
666+ foreach ($ definition ->extensionASTNodes as $ extensionASTNode ) {
667+ self ::appendDirectivesFromAstNode ($ directives , $ extensionASTNode , $ excludedDirectiveNames );
653668 }
654669 }
655670 }
656671
657- $ definitionVars = get_object_vars ($ definition );
658- $ astNode = $ definitionVars ['astNode ' ] ?? null ;
659- self ::appendDirectivesFromAstNode ($ directives , $ astNode instanceof Node ? $ astNode : null , $ excludedDirectiveNames );
672+ return $ directives ;
673+ }
660674
661- $ extensionASTNodes = $ definitionVars [ ' extensionASTNodes ' ] ?? null ;
662- if ( is_iterable ( $ extensionASTNodes )) {
663- foreach ( $ extensionASTNodes as $ extensionASTNode ) {
664- self :: appendDirectivesFromAstNode (
665- $ directives ,
666- $ extensionASTNode instanceof Node ? $ extensionASTNode : null ,
667- $ excludedDirectiveNames
668- );
669- }
675+ /**
676+ * @param array<DirectiveNode> $directives
677+ * @param iterable<DirectiveNode>|null $configuredDirectives
678+ * @param array<string> $excludedDirectiveNames
679+ */
680+ private static function appendConfiguredDirectives ( array & $ directives , ? iterable $ configuredDirectives , array $ excludedDirectiveNames = []): void
681+ {
682+ if ( $ configuredDirectives === null ) {
683+ return ;
670684 }
671685
672- return $ directives ;
686+ foreach ($ configuredDirectives as $ directive ) {
687+ if (! in_array ($ directive ->name ->value , $ excludedDirectiveNames , true )) {
688+ $ directives [] = $ directive ;
689+ }
690+ }
673691 }
674692
675693 /**
0 commit comments