Passed
Pull Request — master (#5)
by Yonel Ceruto
10:31 queued 05:04
created

StandaloneFieldParser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 88.46%

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 52
ccs 23
cts 26
cp 0.8846
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 3 1
C parse() 0 37 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 21
    public function supports($annotation): bool
30
    {
31 21
        return $annotation instanceof Annotation\Field;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 21
    public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint)
38
    {
39
        /** @var Annotation\Field $annotation */
40
41 21
        $field = new FieldDefinition();
42
43 21
        if ($annotation->name) {
44
            $field->setName($annotation->name);
45
        } else {
46 21
            $field->setName(lcfirst(ClassUtils::getDefaultName($refClass->getName())));
47
        }
48
49 21
        $objectType = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $objectType is dead and can be removed.
Loading history...
50 21
        preg_match('/(\w+)\\\\Field\\\\(\w+)$/', $refClass->getName(), $matches);
51 21
        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 21
        $objectDefinition = $endpoint->getType($matches[1]);
56 21
        $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 21
        $argAnnotations = $this->reader->getClassAnnotations($refClass);
59 21
        foreach ($argAnnotations as $argAnnotation) {
60 21
            if ($argAnnotation instanceof Annotation\Argument) {
61 21
                $this->resolveArgument($field, $argAnnotation);
62
            }
63
        }
64
65 21
        $field->setType(TypeUtil::normalize($annotation->type));
66 21
        $field->setList(TypeUtil::isTypeList($annotation->type));
67 21
        $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 21
        $field->setDeprecationReason($annotation->deprecationReason);
69 21
        $field->setDescription($annotation->description);
70 21
        $field->setComplexity($annotation->complexity);
71
72 21
        foreach ($annotation->options as $option => $value) {
73 21
            $field->setMeta($option, $value);
74
        }
75 21
    }
76
}
77