Completed
Push — master ( cbfed7...c39fb6 )
by Rafael
05:06
created

AnnotationParserHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 8.33 %

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
dl 4
loc 48
ccs 12
cts 14
cp 0.8571
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultName() 0 9 2
A getObjectDefinition() 4 14 4

How to fix   Duplicated Code   

Duplicated Code

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:

1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Definition\Loader\Annotation;
12
13
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface;
14
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionManager;
15
16
/**
17
 * Common trait used un multiple parsers
18
 */
19
trait AnnotationParserHelper
20
{
21
    /**
22
     * Get default name based in given class using naming convention
23
     *
24
     * @param \ReflectionClass  $refClass
25
     * @param DefinitionManager $definitionManager
26
     *
27
     * @return string
28
     */
29 1
    public function getDefaultName(\ReflectionClass $refClass, DefinitionManager $definitionManager): string
30
    {
31 1
        if ($definitionManager->hasTypeForClass($refClass->getName())) {
32 1
            return $definitionManager->getTypeForClass($refClass->getName());
33
        }
34
35 1
        preg_match('/\w+$/', $refClass->getName(), $matches);
36
37 1
        return lcfirst($matches[0] ?? '');
38
    }
39
40
    /**
41
     * Get object type using naming convention
42
     * if Query is placed under User\AllUsers namespace, then "User" is the object type
43
     *
44
     * Mutation\User\UpdateUser -> User
45
     * Query\User\Users -> User
46
     * Form\Input\User\AddUserInput -> User
47
     *
48
     * @param \ReflectionClass  $refClass
49
     * @param DefinitionManager $definitionManager
50
     *
51
     * @return ObjectDefinitionInterface
52
     */
53 1
    public function getObjectDefinition(\ReflectionClass $refClass, DefinitionManager $definitionManager): ObjectDefinitionInterface
54
    {
55 1
        if ($definitionManager->hasTypeForClass($refClass->getName())) {
56 1
            return $definitionManager->getType($definitionManager->getTypeForClass($refClass->getName()));
57
        }
58
59 1
        $objectType = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $objectType is dead and can be removed.
Loading history...
60 1
        preg_match('/(\w+)(\\\\w+)?\\\\(\w+)$/', $refClass->getName(), $matches);
61 1 View Code Duplication
        if (!isset($matches[1]) || !$definitionManager->hasType($matches[1])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
            $error = sprintf('Can`t resolve a valid object type for "%s"', $refClass->getName());
63
            throw new \RuntimeException($error);
64
        }
65
66 1
        return $definitionManager->getType($matches[1]);
67
    }
68
}
69