Total Complexity | 47 |
Total Lines | 159 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like DocumentASTBuilder 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 DocumentASTBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class DocumentASTBuilder extends AbstractASTBuilder |
||
12 | { |
||
13 | /** |
||
14 | * @param LexerInterface $lexer |
||
15 | * @return bool |
||
16 | */ |
||
17 | public function supportsBuilder(string $kind): bool |
||
18 | { |
||
19 | return $kind === ASTKindEnum::DOCUMENT; |
||
20 | } |
||
21 | |||
22 | /** |
||
23 | * @inheritdoc |
||
24 | * @throws SyntaxErrorException |
||
25 | */ |
||
26 | public function build(LexerInterface $lexer, array $params): ?array |
||
27 | { |
||
28 | $start = $lexer->getToken(); |
||
29 | |||
30 | $this->expect($lexer, TokenKindEnum::SOF); |
||
31 | |||
32 | $definitions = []; |
||
33 | |||
34 | do { |
||
35 | $definitions[] = $this->parseDefinition($lexer); |
||
36 | } while (!$this->skip($lexer, TokenKindEnum::EOF)); |
||
37 | |||
38 | return [ |
||
39 | 'kind' => NodeKindEnum::DOCUMENT, |
||
40 | 'definitions' => $definitions, |
||
41 | 'loc' => $this->buildLocation($lexer, $start), |
||
42 | ]; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param LexerInterface $lexer |
||
47 | * @return array |
||
48 | * @throws SyntaxErrorException |
||
49 | * @throws \ReflectionException |
||
50 | */ |
||
51 | protected function parseDefinition(LexerInterface $lexer): array |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param LexerInterface $lexer |
||
84 | * @return array |
||
85 | * @throws SyntaxErrorException |
||
86 | */ |
||
87 | protected function parseExecutableDefinition(LexerInterface $lexer): array |
||
88 | { |
||
89 | if ($this->peek($lexer, TokenKindEnum::NAME)) { |
||
90 | // valid names are: query, mutation, subscription and fragment |
||
91 | switch ($lexer->getToken()->getValue()) { |
||
92 | case KeywordEnum::QUERY: |
||
93 | case KeywordEnum::MUTATION: |
||
94 | case KeywordEnum::SUBSCRIPTION: |
||
95 | return $this->buildAST(ASTKindEnum::OPERATION_DEFINITION, $lexer); |
||
|
|||
96 | case KeywordEnum::FRAGMENT: |
||
97 | return $this->buildAST(ASTKindEnum::FRAGMENT_DEFINITION, $lexer); |
||
98 | } |
||
99 | } elseif ($this->peek($lexer, TokenKindEnum::BRACE_L)) { |
||
100 | // Anonymous query |
||
101 | return $this->buildAST(ASTKindEnum::OPERATION_DEFINITION, $lexer); |
||
102 | } |
||
103 | |||
104 | throw $this->unexpected($lexer); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param LexerInterface $lexer |
||
109 | * @return array |
||
110 | * @throws SyntaxErrorException |
||
111 | */ |
||
112 | protected function parseTypeSystemDefinition(LexerInterface $lexer): array |
||
113 | { |
||
114 | // Many definitions begin with a description and require a lookahead. |
||
115 | $keywordToken = $this->peekDescription($lexer) ? $lexer->lookahead() : $lexer->getToken(); |
||
116 | |||
117 | if ($keywordToken->getKind() === TokenKindEnum::NAME) { |
||
118 | switch ($keywordToken->getValue()) { |
||
119 | case KeywordEnum::SCHEMA: |
||
120 | return $this->buildAST(ASTKindEnum::SCHEMA_DEFINITION, $lexer); |
||
121 | case KeywordEnum::SCALAR: |
||
122 | return $this->buildAST(ASTKindEnum::SCALAR_TYPE_DEFINITION, $lexer); |
||
123 | case KeywordEnum::TYPE: |
||
124 | return $this->buildAST(ASTKindEnum::OBJECT_TYPE_DEFINITION, $lexer); |
||
125 | case KeywordEnum::INTERFACE: |
||
126 | return $this->buildAST(ASTKindEnum::INTERFACE_TYPE_DEFINITION, $lexer); |
||
127 | case KeywordEnum::UNION: |
||
128 | return $this->buildAST(ASTKindEnum::UNION_TYPE_DEFINITION, $lexer); |
||
129 | case KeywordEnum::ENUM: |
||
130 | return $this->buildAST(ASTKindEnum::ENUM_TYPE_DEFINITION, $lexer); |
||
131 | case KeywordEnum::INPUT: |
||
132 | return $this->buildAST(ASTKindEnum::INPUT_OBJECT_TYPE_DEFINITION, $lexer); |
||
133 | case KeywordEnum::DIRECTIVE: |
||
134 | return $this->buildAST(ASTKindEnum::DIRECTIVE_DEFINITION, $lexer); |
||
135 | case KeywordEnum::EXTEND: |
||
136 | return $this->parseTypeSystemExtension($lexer); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | throw $this->unexpected($lexer, $keywordToken); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param LexerInterface $lexer |
||
145 | * @return array |
||
146 | * @throws SyntaxErrorException |
||
147 | */ |
||
148 | protected function parseTypeSystemExtension(LexerInterface $lexer): array |
||
170 | } |
||
171 | } |
||
172 |