| Total Complexity | 58 |
| Total Lines | 408 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Printer 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 Printer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Printer implements PrinterInterface |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @inheritdoc |
||
| 37 | * @throws LanguageException |
||
| 38 | */ |
||
| 39 | public function print(NodeInterface $node): string |
||
| 40 | { |
||
| 41 | if ($node instanceof NameNode) { |
||
| 42 | return $this->printName($node); |
||
| 43 | } |
||
| 44 | if ($node instanceof VariableNode) { |
||
| 45 | return $this->printVariable($node); |
||
| 46 | } |
||
| 47 | if ($node instanceof DocumentNode) { |
||
| 48 | return $this->printDocument($node); |
||
| 49 | } |
||
| 50 | if ($node instanceof OperationDefinitionNode) { |
||
| 51 | return $this->printOperationDefinition($node); |
||
| 52 | } |
||
| 53 | if ($node instanceof VariableDefinitionNode) { |
||
| 54 | return $this->printVariableDefinition($node); |
||
| 55 | } |
||
| 56 | if ($node instanceof SelectionSetNode) { |
||
| 57 | return $this->printSelectionSet($node); |
||
| 58 | } |
||
| 59 | if ($node instanceof FieldNode) { |
||
| 60 | return $this->printField($node); |
||
| 61 | } |
||
| 62 | if ($node instanceof ArgumentNode) { |
||
| 63 | return $this->printArgument($node); |
||
| 64 | } |
||
| 65 | if ($node instanceof FragmentSpreadNode) { |
||
| 66 | return $this->printFragmentSpread($node); |
||
| 67 | } |
||
| 68 | if ($node instanceof InlineFragmentNode) { |
||
| 69 | return $this->printInlineFragment($node); |
||
| 70 | } |
||
| 71 | if ($node instanceof FragmentDefinitionNode) { |
||
| 72 | return $this->printFragmentDefinition($node); |
||
| 73 | } |
||
| 74 | if ($node instanceof IntValueNode) { |
||
| 75 | return $this->printIntValue($node); |
||
| 76 | } |
||
| 77 | if ($node instanceof FloatValueNode) { |
||
| 78 | return $this->printFloatValue($node); |
||
| 79 | } |
||
| 80 | if ($node instanceof StringValueNode) { |
||
| 81 | return $this->printStringValue($node); |
||
| 82 | } |
||
| 83 | if ($node instanceof BooleanValueNode) { |
||
| 84 | return $this->printBooleanValue($node); |
||
| 85 | } |
||
| 86 | if ($node instanceof NullValueNode) { |
||
| 87 | return $this->printNullValue($node); |
||
| 88 | } |
||
| 89 | if ($node instanceof EnumValueNode) { |
||
| 90 | return $this->printEnumValue($node); |
||
| 91 | } |
||
| 92 | if ($node instanceof ListValueNode) { |
||
| 93 | return $this->printListValue($node); |
||
| 94 | } |
||
| 95 | if ($node instanceof ObjectValueNode) { |
||
| 96 | return $this->printObjectValue($node); |
||
| 97 | } |
||
| 98 | if ($node instanceof ObjectFieldNode) { |
||
| 99 | return $this->printObjectField($node); |
||
| 100 | } |
||
| 101 | if ($node instanceof DirectiveNode) { |
||
| 102 | return $this->printDirective($node); |
||
| 103 | } |
||
| 104 | if ($node instanceof NamedTypeNode) { |
||
| 105 | return $this->printNamedType($node); |
||
| 106 | } |
||
| 107 | if ($node instanceof ListTypeNode) { |
||
| 108 | return $this->printListType($node); |
||
| 109 | } |
||
| 110 | if ($node instanceof NonNullTypeNode) { |
||
| 111 | return $this->printNonNullType($node); |
||
| 112 | } |
||
| 113 | |||
| 114 | throw new LanguageException(\sprintf('Invalid AST Node: %s.', toString($node))); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param NameNode $node |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | protected function printName(NameNode $node): string |
||
| 122 | { |
||
| 123 | return $node->getValue(); |
||
|
|
|||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param VariableNode $node |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | protected function printVariable(VariableNode $node): string |
||
| 131 | { |
||
| 132 | return '$' . $node->getName(); |
||
| 133 | } |
||
| 134 | |||
| 135 | // Document |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param DocumentNode $node |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | protected function printDocument(DocumentNode $node): string |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param OperationDefinitionNode $node |
||
| 148 | * @return string |
||
| 149 | * @throws LanguageException |
||
| 150 | */ |
||
| 151 | protected function printOperationDefinition(OperationDefinitionNode $node): string |
||
| 168 | ]); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param VariableDefinitionNode $node |
||
| 173 | * @return string |
||
| 174 | * @throws LanguageException |
||
| 175 | */ |
||
| 176 | protected function printVariableDefinition(VariableDefinitionNode $node): string |
||
| 177 | { |
||
| 178 | $variable = $this->one($node->getVariable()); |
||
| 179 | $type = $this->one($node->getType()); |
||
| 180 | $defaultValue = $this->one($node->getDefaultValue()); |
||
| 181 | |||
| 182 | return $variable . ': ' . $type . wrap(' = ', $defaultValue); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param SelectionSetNode $node |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | protected function printSelectionSet(SelectionSetNode $node): string |
||
| 190 | { |
||
| 191 | return block($this->many($node->getSelections())); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param FieldNode $node |
||
| 196 | * @return string |
||
| 197 | * @throws LanguageException |
||
| 198 | */ |
||
| 199 | protected function printField(FieldNode $node): string |
||
| 200 | { |
||
| 201 | $alias = $this->one($node->getAlias()); |
||
| 202 | $name = $this->one($node->getName()); |
||
| 203 | $arguments = $this->many($node->getArguments()); |
||
| 204 | $directives = $this->many($node->getDirectives()); |
||
| 205 | $selectionSet = $this->one($node->getSelectionSet()); |
||
| 206 | |||
| 207 | return \implode(' ', [ |
||
| 208 | wrap('', $alias, ': ') . $name . wrap('(', \implode(', ', $arguments), ')'), |
||
| 209 | \implode(' ', $directives), |
||
| 210 | $selectionSet, |
||
| 211 | ]); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param ArgumentNode $node |
||
| 216 | * @return string |
||
| 217 | * @throws LanguageException |
||
| 218 | */ |
||
| 219 | protected function printArgument(ArgumentNode $node): string |
||
| 220 | { |
||
| 221 | $name = $this->one($node->getName()); |
||
| 222 | $value = $this->one($node->getValue()); |
||
| 223 | |||
| 224 | return $name . ': ' . $value; |
||
| 225 | } |
||
| 226 | |||
| 227 | // Fragments |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param FragmentSpreadNode $node |
||
| 231 | * @return string |
||
| 232 | * @throws LanguageException |
||
| 233 | */ |
||
| 234 | protected function printFragmentSpread(FragmentSpreadNode $node): string |
||
| 235 | { |
||
| 236 | $name = $this->one($node->getName()); |
||
| 237 | $directives = $this->many($node->getDirectives()); |
||
| 238 | |||
| 239 | return '...' . $name . wrap(' ', \implode(' ', $directives)); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param InlineFragmentNode $node |
||
| 244 | * @return string |
||
| 245 | * @throws LanguageException |
||
| 246 | */ |
||
| 247 | protected function printInlineFragment(InlineFragmentNode $node): string |
||
| 248 | { |
||
| 249 | $typeCondition = $this->one($node->getTypeCondition()); |
||
| 250 | $directives = $this->many($node->getDirectives()); |
||
| 251 | $selectionSet = $this->one($node->getSelectionSet()); |
||
| 252 | |||
| 253 | return \implode(' ', [ |
||
| 254 | '...', wrap('on ', $typeCondition), |
||
| 255 | \implode(' ', $directives), |
||
| 256 | $selectionSet |
||
| 257 | ]); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param FragmentDefinitionNode $node |
||
| 262 | * @return string |
||
| 263 | * @throws LanguageException |
||
| 264 | */ |
||
| 265 | protected function printFragmentDefinition(FragmentDefinitionNode $node): string |
||
| 266 | { |
||
| 267 | $name = $this->one($node->getName()); |
||
| 268 | $typeCondition = $this->one($node->getTypeCondition()); |
||
| 269 | $variableDefinitions = $this->many($node->getVariableDefinitions()); |
||
| 270 | $directives = $this->many($node->getDirectives()); |
||
| 271 | $selectionSet = $this->one($node->getSelectionSet()); |
||
| 272 | |||
| 273 | // Note: fragment variable definitions are experimental and may be changed |
||
| 274 | // or removed in the future. |
||
| 275 | return \implode(' ', [ |
||
| 276 | 'fragment ' . $name . wrap('(', \implode(', ', $variableDefinitions), ')'), |
||
| 277 | 'on ' . $typeCondition . ' ' . \implode(' ', $directives), |
||
| 278 | $selectionSet |
||
| 279 | ]); |
||
| 280 | } |
||
| 281 | |||
| 282 | // Value |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param IntValueNode $node |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | protected function printIntValue(IntValueNode $node): string |
||
| 289 | { |
||
| 290 | return $node->getValue(); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param FloatValueNode $node |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | protected function printFloatValue(FloatValueNode $node): string |
||
| 298 | { |
||
| 299 | return $node->getValue(); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param StringValueNode $node |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | protected function printStringValue(StringValueNode $node): string |
||
| 307 | { |
||
| 308 | $value = $node->getValue(); |
||
| 309 | |||
| 310 | return $node->isBlock() |
||
| 311 | ? printBlockString($value, false) |
||
| 312 | : \json_encode($value, JSON_UNESCAPED_UNICODE); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param BooleanValueNode $node |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | protected function printBooleanValue(BooleanValueNode $node): string |
||
| 320 | { |
||
| 321 | return $node->getValue() ? 'true' : 'false'; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param NullValueNode $node |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | protected function printNullValue(NullValueNode $node): string |
||
| 329 | { |
||
| 330 | return 'null'; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param EnumValueNode $node |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | protected function printEnumValue(EnumValueNode $node): string |
||
| 338 | { |
||
| 339 | return $node->getValue(); |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param ListValueNode $node |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | protected function printListValue(ListValueNode $node): string |
||
| 347 | { |
||
| 348 | $values = $this->many($node->getValues()); |
||
| 349 | return wrap('[', \implode(', ', $values), ']'); |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param ObjectValueNode $node |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | protected function printObjectValue(ObjectValueNode $node): string |
||
| 357 | { |
||
| 358 | $fields = $this->many($node->getFields()); |
||
| 359 | return wrap('{', \implode(', ', $fields), '}'); |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param ObjectFieldNode $node |
||
| 364 | * @return string |
||
| 365 | * @throws LanguageException |
||
| 366 | */ |
||
| 367 | protected function printObjectField(ObjectFieldNode $node): string |
||
| 373 | } |
||
| 374 | |||
| 375 | // Directive |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param DirectiveNode $node |
||
| 379 | * @return string |
||
| 380 | * @throws LanguageException |
||
| 381 | */ |
||
| 382 | protected function printDirective(DirectiveNode $node): string |
||
| 383 | { |
||
| 384 | $name = $this->one($node->getName()); |
||
| 385 | $arguments = $this->many($node->getArguments()); |
||
| 386 | |||
| 387 | return '@' . $name . wrap('(', \implode(', ', $arguments), ')'); |
||
| 388 | } |
||
| 389 | |||
| 390 | // Type |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param NamedTypeNode $node |
||
| 394 | * @return string |
||
| 395 | * @throws LanguageException |
||
| 396 | */ |
||
| 397 | protected function printNamedType(NamedTypeNode $node): string |
||
| 398 | { |
||
| 399 | return $this->one($node->getName()); |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param ListTypeNode $node |
||
| 404 | * @return string |
||
| 405 | * @throws LanguageException |
||
| 406 | */ |
||
| 407 | protected function printListType(ListTypeNode $node): string |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @param NonNullTypeNode $node |
||
| 414 | * @return string |
||
| 415 | * @throws LanguageException |
||
| 416 | */ |
||
| 417 | protected function printNonNullType(NonNullTypeNode $node): string |
||
| 418 | { |
||
| 419 | return $this->one($node->getType()) . '!'; |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @param NodeInterface|null $node |
||
| 424 | * @return string |
||
| 425 | * @throws LanguageException |
||
| 426 | */ |
||
| 427 | protected function one(?NodeInterface $node): string |
||
| 428 | { |
||
| 429 | return null !== $node ? $this->print($node) : ''; |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param array $nodes |
||
| 434 | * @return array |
||
| 435 | */ |
||
| 436 | protected function many(array $nodes): array |
||
| 441 | } |
||
| 442 | } |
||
| 443 |