We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AnnotationParser 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 AnnotationParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class AnnotationParser implements ParserInterface |
||
| 11 | { |
||
| 12 | public static function getAnnotationReader() |
||
| 28 | |||
| 29 | /** |
||
| 30 | * {@inheritdoc} |
||
| 31 | * |
||
| 32 | * @throws \ReflectionException |
||
| 33 | * @throws InvalidArgumentException |
||
| 34 | */ |
||
| 35 | public static function parse(\SplFileInfo $file, ContainerBuilder $container) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Get the graphQL alias. |
||
| 74 | * |
||
| 75 | * @param $annotation |
||
| 76 | * |
||
| 77 | * @return string|null |
||
| 78 | */ |
||
| 79 | protected static function getGraphQLAlias($annotation) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get the graphQL type. |
||
| 90 | * |
||
| 91 | * @param $annotation |
||
| 92 | * |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | protected static function getGraphQLType($annotation) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param string $type |
||
| 110 | * @param string $alias |
||
| 111 | * @param array $classAnnotations |
||
| 112 | * @param PropertyReflection[] $properties |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | * |
||
| 116 | * @throws \Exception |
||
| 117 | */ |
||
| 118 | protected static function formatRelay($type, $alias, $classAnnotations, $properties) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Format enum type. |
||
| 167 | * |
||
| 168 | * @param string $alias |
||
| 169 | * @param string $entityName |
||
| 170 | * @param \ReflectionProperty[] $properties |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | protected static function formatEnumType($alias, $entityName, $properties) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Format custom scalar type. |
||
| 211 | * |
||
| 212 | * @param string $alias |
||
| 213 | * @param string $type |
||
| 214 | * @param string $className |
||
| 215 | * @param array $annotations |
||
| 216 | * |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | protected static function formatCustomScalarType($alias, $type, $className, $annotations) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Format scalar type. |
||
| 248 | * |
||
| 249 | * @param string $alias |
||
| 250 | * @param string $type |
||
| 251 | * @param string $entityName |
||
| 252 | * @param \ReflectionProperty[] $properties |
||
| 253 | * |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | protected static function formatScalarType($alias, $type, $entityName, $properties) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Return the graphQL type for the named field. |
||
| 297 | * |
||
| 298 | * @param string $name |
||
| 299 | * @param array $annotation |
||
| 300 | * |
||
| 301 | * @return array|null |
||
| 302 | */ |
||
| 303 | protected static function getGraphQLFieldType($name, $annotation) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Return the common field type, like ID, Int, String, and other user-created type. |
||
| 318 | * |
||
| 319 | * @param string $name |
||
| 320 | * @param array $annotation |
||
| 321 | * |
||
| 322 | * @return array|null |
||
| 323 | */ |
||
| 324 | protected static function getGraphQLScalarFieldType($name, $annotation) |
||
| 325 | { |
||
| 326 | // Get the current type, depending on current annotation |
||
| 327 | $type = $graphQLType = null; |
||
| 328 | $nullable = $isMultiple = false; |
||
| 329 | if (array_key_exists('GraphQLColumn', $annotation) && array_key_exists('type', $annotation['GraphQLColumn'])) { |
||
| 330 | $annotation = $annotation['GraphQLColumn']; |
||
| 331 | $type = $annotation['type']; |
||
| 332 | View Code Duplication | } elseif (array_key_exists('GraphQLToMany', $annotation) && array_key_exists('target', $annotation['GraphQLToMany'])) { |
|
| 333 | $annotation = $annotation['GraphQLToMany']; |
||
| 334 | $type = $annotation['target']; |
||
| 335 | $isMultiple = $nullable = true; |
||
| 336 | } elseif (array_key_exists('GraphQLToOne', $annotation) && array_key_exists('target', $annotation['GraphQLToOne'])) { |
||
| 337 | $annotation = $annotation['GraphQLToOne']; |
||
| 338 | $type = $annotation['target']; |
||
| 339 | $nullable = true; |
||
| 340 | View Code Duplication | } elseif (array_key_exists('OneToMany', $annotation) && array_key_exists('targetEntity', $annotation['OneToMany'])) { |
|
| 341 | $annotation = $annotation['OneToMany']; |
||
| 342 | $type = $annotation['targetEntity']; |
||
| 343 | $isMultiple = $nullable = true; |
||
| 344 | } elseif (array_key_exists('OneToOne', $annotation) && array_key_exists('targetEntity', $annotation['OneToOne'])) { |
||
| 345 | $annotation = $annotation['OneToOne']; |
||
| 346 | $type = $annotation['targetEntity']; |
||
| 347 | $nullable = true; |
||
| 348 | View Code Duplication | } elseif (array_key_exists('ManyToMany', $annotation) && array_key_exists('targetEntity', $annotation['ManyToMany'])) { |
|
| 349 | $annotation = $annotation['ManyToMany']; |
||
| 350 | $type = $annotation['targetEntity']; |
||
| 351 | $isMultiple = $nullable = true; |
||
| 352 | } elseif (array_key_exists('ManyToOne', $annotation) && array_key_exists('targetEntity', $annotation['ManyToOne'])) { |
||
| 353 | $annotation = $annotation['ManyToOne']; |
||
| 354 | $type = $annotation['targetEntity']; |
||
| 355 | $nullable = true; |
||
| 356 | } elseif (array_key_exists('Column', $annotation) && array_key_exists('type', $annotation['Column'])) { |
||
| 357 | $annotation = $annotation['Column']; |
||
| 358 | $type = $annotation['type']; |
||
| 359 | } |
||
| 360 | |||
| 361 | if (!$type) { |
||
| 362 | return null; |
||
| 363 | } |
||
| 364 | |||
| 365 | if (array_key_exists('nullable', $annotation)) { |
||
| 366 | $nullable = 'true' == $annotation['nullable'] |
||
| 367 | ? true |
||
| 368 | : false; |
||
| 369 | } |
||
| 370 | |||
| 371 | $type = explode('\\', $type); |
||
| 372 | $type = $type[count($type) - 1]; |
||
| 373 | |||
| 374 | // Get the graphQL type representation |
||
| 375 | // Specific case for ID and relation |
||
| 376 | if ('id' === $name && ('integer' === $type || array_key_exists('Id', $annotation))) { |
||
| 377 | $graphQLType = 'ID'; |
||
| 378 | } else { |
||
| 379 | // Make the relation between doctrine Column type and graphQL type |
||
| 380 | switch ($type) { |
||
| 381 | case 'integer': |
||
| 382 | $graphQLType = 'Int'; |
||
| 383 | break; |
||
| 384 | case 'string': |
||
| 385 | case 'text': |
||
| 386 | $graphQLType = 'String'; |
||
| 387 | break; |
||
| 388 | case 'bool': |
||
| 389 | case 'boolean': |
||
| 390 | $graphQLType = 'Boolean'; |
||
| 391 | break; |
||
| 392 | case 'float': |
||
| 393 | case 'decimal': |
||
| 394 | $graphQLType = 'Float'; |
||
| 395 | break; |
||
| 396 | default: |
||
| 397 | // No maching: considering is custom-scalar graphQL type |
||
| 398 | $graphQLType = $type; |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | if ($isMultiple) { |
||
| 403 | $graphQLType = '['.$graphQLType.']'; |
||
| 404 | } |
||
| 405 | |||
| 406 | if (!$nullable) { |
||
| 407 | $graphQLType .= '!'; |
||
| 408 | } |
||
| 409 | |||
| 410 | return ['type' => $graphQLType]; |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Get the graphql query formatted field. |
||
| 415 | * |
||
| 416 | * @param array $annotation |
||
| 417 | * |
||
| 418 | * @return array|null |
||
| 419 | */ |
||
| 420 | protected static function getGraphQLQueryField($annotation) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Get the formatted graphQL mutation field. |
||
| 476 | * |
||
| 477 | * @param array $annotation |
||
| 478 | * |
||
| 479 | * @return array |
||
| 480 | */ |
||
| 481 | protected static function getGraphQLMutationField($annotation) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Get the formatted graphQL relay mutation field. |
||
| 503 | * |
||
| 504 | * @param array $annotation |
||
| 505 | * |
||
| 506 | * @return array|null |
||
| 507 | */ |
||
| 508 | protected static function getGraphQLRelayMutationField($annotation) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Get graphql access control annotation. |
||
| 533 | * |
||
| 534 | * @param $annotation |
||
| 535 | * |
||
| 536 | * @return null|string |
||
| 537 | */ |
||
| 538 | View Code Duplication | protected static function getGraphQLAccessControl($annotation) |
|
| 546 | |||
| 547 | /** |
||
| 548 | * Get graphql public control. |
||
| 549 | * |
||
| 550 | * @param $annotation |
||
| 551 | * |
||
| 552 | * @return null|string |
||
| 553 | */ |
||
| 554 | View Code Duplication | protected static function getGraphQLPublicControl($annotation) |
|
| 562 | |||
| 563 | /** |
||
| 564 | * Parse annotation. |
||
| 565 | * |
||
| 566 | * @param mixed $annotation |
||
| 567 | * |
||
| 568 | * @return array |
||
| 569 | */ |
||
| 570 | protected static function parseAnnotation($annotations) |
||
| 588 | } |
||
| 589 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.