Completed
Push — master ( f8b59e...f061a0 )
by Rafael
13:35 queued 07:59
created

StandaloneFieldParser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 53
ccs 24
cts 27
cp 0.8889
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 3 1
C parse() 0 38 7
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\Annotation;
14
use Ynlo\GraphQLBundle\Definition\FieldDefinition;
15
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
16
use Ynlo\GraphQLBundle\Util\ClassUtils;
17
use Ynlo\GraphQLBundle\Util\TypeUtil;
18
19
/**
20
 * Resolve field of types queries using naming conventions
21
 */
22
class StandaloneFieldParser extends QueryAnnotationParser
23
{
24
    use AnnotationReaderAwareTrait;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 22
    public function supports($annotation): bool
30
    {
31 22
        return $annotation instanceof Annotation\Field;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 22
    public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint)
38
    {
39
        /** @var Annotation\Field $annotation */
40
41 22
        $field = new FieldDefinition();
42
43 22
        if ($annotation->name) {
44
            $field->setName($annotation->name);
45
        } else {
46 22
            $field->setName(lcfirst(ClassUtils::getDefaultName($refClass->getName())));
47
        }
48
49 22
        $objectType = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $objectType is dead and can be removed.
Loading history...
50 22
        preg_match('/(\w+)\\\\Field\\\\(\w+)$/', $refClass->getName(), $matches);
51 22
        if (!isset($matches[1]) || !$endpoint->hasType($matches[1])) {
52
            $error = sprintf('Can`t resolve a valid object type for field "%s"', $refClass->getName());
53
            throw new \RuntimeException($error);
54
        }
55 22
        $objectDefinition = $endpoint->getType($matches[1]);
56 22
        $objectDefinition->addField($field);
0 ignored issues
show
Bug introduced by
The method addField() does not exist on Ynlo\GraphQLBundle\Definition\DefinitionInterface. It seems like you code against a sub-type of Ynlo\GraphQLBundle\Definition\DefinitionInterface such as Ynlo\GraphQLBundle\Definition\ImplementorInterface or Ynlo\GraphQLBundle\Defin...wareDefinitionInterface or Ynlo\GraphQLBundle\Defin...jectDefinitionInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
        $objectDefinition->/** @scrutinizer ignore-call */ 
57
                           addField($field);
Loading history...
57
58 22
        $argAnnotations = $this->reader->getClassAnnotations($refClass);
59 22
        foreach ($argAnnotations as $argAnnotation) {
60 22
            if ($argAnnotation instanceof Annotation\Argument) {
61 22
                $this->resolveArgument($field, $argAnnotation);
62
            }
63
        }
64
65 22
        $field->setType(TypeUtil::normalize($annotation->type));
66 22
        $field->setList(TypeUtil::isTypeList($annotation->type));
67 22
        $field->setResolver($annotation->resolver ?? $refClass->getName());
0 ignored issues
show
Bug introduced by
The property resolver does not seem to exist on Ynlo\GraphQLBundle\Annotation\Field.
Loading history...
68 22
        $field->setDeprecationReason($annotation->deprecationReason);
69 22
        $field->setDescription($annotation->description);
70 22
        $field->setComplexity($annotation->complexity);
71 22
        $field->setMaxConcurrentUsage($annotation->maxConcurrentUsage);
72
73 22
        foreach ($annotation->options as $option => $value) {
74 22
            $field->setMeta($option, $value);
75
        }
76 22
    }
77
}
78