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() |
||
| 13 | { |
||
| 14 | if (!class_exists('\\Doctrine\\Common\\Annotations\\AnnotationReader') || |
||
| 15 | !class_exists('\\Doctrine\\Common\\Annotations\\AnnotationRegistry') |
||
| 16 | ) { |
||
| 17 | throw new \Exception('In order to use annotation, you need to require doctrine ORM'); |
||
| 18 | } |
||
| 19 | |||
| 20 | $loader = require __DIR__ . '/../../../../autoload.php'; |
||
| 21 | |||
| 22 | \Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(array($loader, 'loadClass')); |
||
|
|
|||
| 23 | \Doctrine\Common\Annotations\AnnotationRegistry::registerFile(__DIR__.'/../../Annotation/GraphQLAnnotation.php'); |
||
| 24 | $reader = new \Doctrine\Common\Annotations\AnnotationReader(); |
||
| 25 | |||
| 26 | return $reader; |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * {@inheritdoc} |
||
| 31 | * |
||
| 32 | * @throws \ReflectionException |
||
| 33 | * @throws InvalidArgumentException |
||
| 34 | */ |
||
| 35 | public static function parse(\SplFileInfo $file, ContainerBuilder $container) |
||
| 36 | { |
||
| 37 | $reader = self::getAnnotationReader(); |
||
| 38 | $container->addResource(new FileResource($file->getRealPath())); |
||
| 39 | try { |
||
| 40 | $fileContent = file_get_contents($file->getRealPath()); |
||
| 41 | |||
| 42 | $entityName = substr($file->getFilename(), 0, -4); |
||
| 43 | if (preg_match('#namespace (.+);#', $fileContent, $namespace)) { |
||
| 44 | $className = $namespace[1] . '\\' . $entityName; |
||
| 45 | } else { |
||
| 46 | $className = $entityName; |
||
| 47 | } |
||
| 48 | |||
| 49 | $reflexionEntity = new \ReflectionClass($className); |
||
| 50 | |||
| 51 | $annotations = $reader->getClassAnnotations($reflexionEntity); |
||
| 52 | $annotations = self::parseAnnotation($annotations); |
||
| 53 | |||
| 54 | $alias = self::getGraphQLAlias($annotations) ?: $entityName; |
||
| 55 | $type = self::getGraphQLType($annotations); |
||
| 56 | |||
| 57 | switch ($type) { |
||
| 58 | case 'enum': |
||
| 59 | return self::formatEnumType($alias, $entityName, $reflexionEntity->getProperties()); |
||
| 60 | case 'custom-scalar': |
||
| 61 | return self::formatCustomScalarType($alias, $type, $className); |
||
| 62 | default: |
||
| 63 | return self::formatScalarType($alias, $type, $entityName, $reflexionEntity->getProperties()); |
||
| 64 | } |
||
| 65 | } catch (\InvalidArgumentException $e) { |
||
| 66 | throw new InvalidArgumentException(sprintf('Unable to parse file "%s".', $file), $e->getCode(), $e); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get the graphQL alias |
||
| 72 | * |
||
| 73 | * @param $annotation |
||
| 74 | * |
||
| 75 | * @return string|null |
||
| 76 | */ |
||
| 77 | View Code Duplication | protected static function getGraphQLAlias($annotation) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Get the graphQL type |
||
| 88 | * |
||
| 89 | * @param $annotation |
||
| 90 | * |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | View Code Duplication | protected static function getGraphQLType($annotation) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Format enum type |
||
| 104 | * |
||
| 105 | * @param string $alias |
||
| 106 | * @param string $entityName |
||
| 107 | * @param \ReflectionProperty[] $properties |
||
| 108 | * |
||
| 109 | * @return array |
||
| 110 | */ |
||
| 111 | protected static function formatEnumType($alias, $entityName, $properties) |
||
| 112 | { |
||
| 113 | $reader = self::getAnnotationReader(); |
||
| 114 | |||
| 115 | $typesConfig = [ |
||
| 116 | $alias => [ |
||
| 117 | 'type' => 'enum', |
||
| 118 | 'config' => [ |
||
| 119 | 'description' => $entityName . ' type', |
||
| 120 | ], |
||
| 121 | ] |
||
| 122 | ]; |
||
| 123 | |||
| 124 | $values = []; |
||
| 125 | /** @var \ReflectionProperty $property */ |
||
| 126 | foreach ($properties as $property) { |
||
| 127 | $propertyName = $property->getName(); |
||
| 128 | |||
| 129 | $propertyAnnotation = $reader->getPropertyAnnotations($property); |
||
| 130 | $propertyAnnotation = self::parseAnnotation($propertyAnnotation); |
||
| 131 | |||
| 132 | $values[$propertyName] = [ |
||
| 133 | 'value' => $propertyAnnotation, |
||
| 134 | ]; |
||
| 135 | |||
| 136 | if (array_key_exists('GraphQLDescription', $propertyAnnotation) && !empty($test['GraphQLDescription']['description'])) { |
||
| 137 | $values[$propertyName]['description'] = $test['GraphQLDescription']['description']; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | $typesConfig[$alias]['config']['values'] = $values; |
||
| 142 | |||
| 143 | return $typesConfig; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Format custom scalar type |
||
| 148 | * |
||
| 149 | * @param string $alias |
||
| 150 | * @param string $type |
||
| 151 | * @param string $className |
||
| 152 | * |
||
| 153 | * @return array |
||
| 154 | */ |
||
| 155 | protected static function formatCustomScalarType($alias, $type, $className) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Format scalar type |
||
| 173 | * |
||
| 174 | * @param string $alias |
||
| 175 | * @param string $type |
||
| 176 | * @param string $entityName |
||
| 177 | * @param \ReflectionProperty[] $properties |
||
| 178 | * |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | protected static function formatScalarType($alias, $type, $entityName, $properties) |
||
| 182 | { |
||
| 183 | $reader = self::getAnnotationReader(); |
||
| 184 | |||
| 185 | $typesConfig = [ |
||
| 186 | $alias => [ |
||
| 187 | 'type' => $type, |
||
| 188 | 'config' => [ |
||
| 189 | 'description' => $entityName . ' type', |
||
| 190 | 'fields' => [], |
||
| 191 | ], |
||
| 192 | ] |
||
| 193 | ]; |
||
| 194 | |||
| 195 | foreach ($properties as $property) { |
||
| 196 | $propertyName = $property->getName(); |
||
| 197 | $propertyAnnotation = $reader->getPropertyAnnotations($property); |
||
| 198 | $propertyAnnotation = self::parseAnnotation($propertyAnnotation); |
||
| 199 | |||
| 200 | if (!$graphQlType = self::getGraphQLFieldType($propertyName, $propertyAnnotation)) { |
||
| 201 | continue; |
||
| 202 | } |
||
| 203 | |||
| 204 | if ($graphQlAccessControl = self::getGraphQLAccessControl($propertyAnnotation)) { |
||
| 205 | $graphQlType['access'] = $graphQlAccessControl; |
||
| 206 | } |
||
| 207 | |||
| 208 | if ($graphQlPublicControl = self::getGraphQLPublicControl($propertyAnnotation)) { |
||
| 209 | $graphQlType['public'] = $graphQlPublicControl; |
||
| 210 | } |
||
| 211 | |||
| 212 | $typesConfig[$alias]['config']['fields'][$propertyName] = $graphQlType; |
||
| 213 | } |
||
| 214 | |||
| 215 | return empty($typesConfig[$alias]['config']['fields']) |
||
| 216 | ? [] |
||
| 217 | : $typesConfig; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Return the graphQL type for the named field |
||
| 222 | * |
||
| 223 | * @param string $name |
||
| 224 | * @param array $annotation |
||
| 225 | * |
||
| 226 | * @return array|null |
||
| 227 | */ |
||
| 228 | protected static function getGraphQLFieldType($name, $annotation) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Return the common field type, like ID, Int, String, and other user-created type |
||
| 243 | * |
||
| 244 | * @param string $name |
||
| 245 | * @param array $annotation |
||
| 246 | * |
||
| 247 | * @return array|null |
||
| 248 | */ |
||
| 249 | protected static function getGraphQLScalarFieldType($name, $annotation) |
||
| 250 | { |
||
| 251 | // Get the current type, depending on current annotation |
||
| 252 | $type = $graphQLType = null; |
||
| 253 | $nullable = $isMultiple = false; |
||
| 254 | if (array_key_exists('GraphQLColumn', $annotation) && array_key_exists('type', $annotation['GraphQLColumn'])) { |
||
| 255 | $annotation = $annotation['GraphQLColumn']; |
||
| 256 | $type = $annotation['type']; |
||
| 257 | View Code Duplication | } elseif (array_key_exists('GraphQLToMany', $annotation) && array_key_exists('target', $annotation['GraphQLToMany'])) { |
|
| 258 | $annotation = $annotation['GraphQLToMany']; |
||
| 259 | $type = $annotation['target']; |
||
| 260 | $isMultiple = $nullable = true; |
||
| 261 | } elseif (array_key_exists('GraphQLToOne', $annotation) && array_key_exists('target', $annotation['GraphQLToOne'])) { |
||
| 262 | $annotation = $annotation['GraphQLToOne']; |
||
| 263 | $type = $annotation['target']; |
||
| 264 | $nullable = true; |
||
| 265 | View Code Duplication | } elseif (array_key_exists('OneToMany', $annotation) && array_key_exists('targetEntity', $annotation['OneToMany'])) { |
|
| 266 | $annotation = $annotation['OneToMany']; |
||
| 267 | $type = $annotation['targetEntity']; |
||
| 268 | $isMultiple = $nullable = true; |
||
| 269 | } elseif (array_key_exists('OneToOne', $annotation) && array_key_exists('targetEntity', $annotation['OneToOne'])) { |
||
| 270 | $annotation = $annotation['OneToOne']; |
||
| 271 | $type = $annotation['targetEntity']; |
||
| 272 | $nullable = true; |
||
| 273 | View Code Duplication | } elseif (array_key_exists('ManyToMany', $annotation) && array_key_exists('targetEntity', $annotation['ManyToMany'])) { |
|
| 274 | $annotation = $annotation['ManyToMany']; |
||
| 275 | $type = $annotation['targetEntity']; |
||
| 276 | $isMultiple = $nullable = true; |
||
| 277 | } elseif (array_key_exists('ManyToOne', $annotation) && array_key_exists('targetEntity', $annotation['ManyToOne'])) { |
||
| 278 | $annotation = $annotation['ManyToOne']; |
||
| 279 | $type = $annotation['targetEntity']; |
||
| 280 | $nullable = true; |
||
| 281 | } elseif (array_key_exists('Column', $annotation) && array_key_exists('type', $annotation['Column'])) { |
||
| 282 | $annotation = $annotation['Column']; |
||
| 283 | $type = $annotation['type']; |
||
| 284 | } |
||
| 285 | |||
| 286 | if (!$type) { |
||
| 287 | return null; |
||
| 288 | } |
||
| 289 | |||
| 290 | if (array_key_exists('nullable', $annotation)) { |
||
| 291 | $nullable = $annotation['nullable'] == 'true' |
||
| 292 | ? true |
||
| 293 | : false; |
||
| 294 | } |
||
| 295 | |||
| 296 | $type = explode('\\', $type); |
||
| 297 | $type = $type[count($type)-1]; |
||
| 298 | |||
| 299 | // Get the graphQL type representation |
||
| 300 | // Specific case for ID and relation |
||
| 301 | if ($name === 'id' && $type === 'integer') { |
||
| 302 | $graphQLType = 'ID'; |
||
| 303 | } else { |
||
| 304 | // Make the relation between doctrine Column type and graphQL type |
||
| 305 | switch ($type) { |
||
| 306 | case 'integer'; |
||
| 307 | $graphQLType = 'Int'; |
||
| 308 | break; |
||
| 309 | case 'string': |
||
| 310 | case 'text': |
||
| 311 | $graphQLType = 'String'; |
||
| 312 | break; |
||
| 313 | case 'bool': |
||
| 314 | case 'boolean': |
||
| 315 | $graphQLType = 'Boolean'; |
||
| 316 | break; |
||
| 317 | case 'float': |
||
| 318 | $graphQLType = 'Float'; |
||
| 319 | break; |
||
| 320 | default: |
||
| 321 | // No maching: considering is custom-scalar graphQL type |
||
| 322 | $graphQLType = $type; |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | if ($isMultiple) { |
||
| 327 | $graphQLType = '['.$graphQLType.']'; |
||
| 328 | } |
||
| 329 | |||
| 330 | if (!$nullable) { |
||
| 331 | $graphQLType .= '!'; |
||
| 332 | } |
||
| 333 | |||
| 334 | return ['type' => $graphQLType]; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get the graphql query formatted field |
||
| 339 | * |
||
| 340 | * @param array $annotation |
||
| 341 | * |
||
| 342 | * @return array|null |
||
| 343 | */ |
||
| 344 | protected static function getGraphQLQueryField($annotation) |
||
| 345 | { |
||
| 346 | if (!array_key_exists('GraphQLQuery', $annotation)) { |
||
| 347 | return null; |
||
| 348 | } |
||
| 349 | |||
| 350 | $annotationQuery = $annotation['GraphQLQuery']; |
||
| 351 | |||
| 352 | $ret = [ |
||
| 353 | 'type' => $annotationQuery['type'], |
||
| 354 | ]; |
||
| 355 | |||
| 356 | $method = $annotationQuery['method']; |
||
| 357 | $args = $queryArgs = []; |
||
| 358 | if (array_key_exists('GraphQLInputArgs', $annotation)) { |
||
| 359 | $annotationArgs = $annotation['GraphQLInputArgs']; |
||
| 360 | if (!array_key_exists(0, $annotationArgs)) { |
||
| 361 | $annotationArgs = [$annotationArgs]; |
||
| 362 | } |
||
| 363 | |||
| 364 | foreach ($annotationArgs as $arg) { |
||
| 365 | $args[$arg['name']] = [ |
||
| 366 | 'type' => $arg['type'], |
||
| 367 | ]; |
||
| 368 | |||
| 369 | if (!empty($arg['description'])) { |
||
| 370 | $args[$arg['name']]['description'] = $arg['description']; |
||
| 371 | } |
||
| 372 | |||
| 373 | $queryArgs[] = $arg['target']; |
||
| 374 | } |
||
| 375 | |||
| 376 | $ret['args'] = $args; |
||
| 377 | } |
||
| 378 | |||
| 379 | if (!empty($queryArgs)) { |
||
| 380 | $query = "'".$method."', [".implode(', ', $queryArgs)."]"; |
||
| 381 | } else { |
||
| 382 | $query = "'".$method."'"; |
||
| 383 | } |
||
| 384 | |||
| 385 | $ret['resolve'] = "@=resolver(".$query.")"; |
||
| 386 | |||
| 387 | return $ret; |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Get the formatted graphQL mutation field |
||
| 392 | * |
||
| 393 | * @param array $annotation |
||
| 394 | * |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | protected static function getGraphQLMutationField($annotation) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Get the formatted graphQL relay mutation field |
||
| 408 | * |
||
| 409 | * @param array $annotation |
||
| 410 | * |
||
| 411 | * @return array|null |
||
| 412 | */ |
||
| 413 | protected static function getGraphQLRelayMutationField($annotation) |
||
| 414 | { |
||
| 415 | if (!array_key_exists('GraphQLRelayMutation', $annotation)) { |
||
| 416 | return null; |
||
| 417 | } |
||
| 418 | |||
| 419 | $annotation = $annotation['GraphQLRelayMutation']; |
||
| 420 | if (array_key_exists('args', $annotation)) { |
||
| 421 | $mutate = "'".$annotation['method']."', [".implode(', ', $annotation['args'])."]"; |
||
| 422 | } else { |
||
| 423 | $mutate = "'".$annotation['method']."'"; |
||
| 424 | } |
||
| 425 | |||
| 426 | return [ |
||
| 427 | "builder" => "Relay::Mutation", |
||
| 428 | "builderConfig" => [ |
||
| 429 | "inputType" => $annotation['input'], |
||
| 430 | "payloadType" => $annotation['payload'], |
||
| 431 | "mutateAndGetPayload" => "@=mutation(".$mutate.")", |
||
| 432 | ], |
||
| 433 | ]; |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Get graphql access control annotation |
||
| 438 | * |
||
| 439 | * @param $annotation |
||
| 440 | * |
||
| 441 | * @return null|string |
||
| 442 | */ |
||
| 443 | View Code Duplication | protected static function getGraphQLAccessControl($annotation) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Get graphql public control |
||
| 454 | * |
||
| 455 | * @param $annotation |
||
| 456 | * |
||
| 457 | * @return null|string |
||
| 458 | */ |
||
| 459 | View Code Duplication | protected static function getGraphQLPublicControl($annotation) |
|
| 467 | |||
| 468 | /** |
||
| 469 | * Parse annotation |
||
| 470 | * |
||
| 471 | * @param mixed $annotation |
||
| 472 | * |
||
| 473 | * @return array |
||
| 474 | */ |
||
| 475 | protected static function parseAnnotation($annotations) |
||
| 498 | } |
||
| 499 |
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.