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

AnnotationParserHelper::getObjectDefinition()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 4
Ratio 28.57 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 3
nop 2
dl 4
loc 14
ccs 7
cts 9
cp 0.7778
crap 4.1755
rs 9.2
c 0
b 0
f 0
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