Passed
Push — master ( 38b002...67e4f3 )
by Rafael
05:37
created

CleanUpDefinitionPlugin::used()   C

Complexity

Conditions 14
Paths 52

Size

Total Lines 43
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 14.3828

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 23
c 3
b 0
f 0
dl 0
loc 43
ccs 21
cts 24
cp 0.875
rs 6.2666
cc 14
nc 52
nop 1
crap 14.3828

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*******************************************************************************
4
 *  This file is part of the GraphQL Bundle package.
5
 *
6
 *  (c) YnloUltratech <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 ******************************************************************************/
11
12
namespace Ynlo\GraphQLBundle\Definition\Plugin;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Ynlo\GraphQLBundle\Definition\DefinitionInterface;
16
use Ynlo\GraphQLBundle\Definition\FieldsAwareDefinitionInterface;
17
use Ynlo\GraphQLBundle\Definition\ImplementorInterface;
18
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition;
19
use Ynlo\GraphQLBundle\Definition\QueryDefinition;
20
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry;
21
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
22
use Ynlo\GraphQLBundle\Definition\UnionDefinition;
23
24
/**
25
 * This plugin remove non used definitions
26
 */
27
class CleanUpDefinitionPlugin extends AbstractDefinitionPlugin
28
{
29
    /**
30
     * @var Endpoint
31
     */
32
    protected $endpoint;
33
34
    protected $used = [];
35
36
    /**
37
     * {@inheritDoc}
38
     */
39
    public function buildConfig(ArrayNodeDefinition $root): void
40
    {
41
        $root
42
            ->info('Remove non used definitions')
43
            ->scalarPrototype();
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49 2
    public function configureEndpoint(Endpoint $endpoint): void
50
    {
51 2
        if ($endpoint->getName() === DefinitionRegistry::DEFAULT_ENDPOINT) {
52 1
            return;
53
        }
54
55 1
        $this->used = [];
56 1
        $this->endpoint = $endpoint;
57
58 1
        $this->processOperations($endpoint->allQueries());
59 1
        $this->processOperations($endpoint->allMutations());
60 1
        $this->processOperations($endpoint->allSubscriptions());
61
62 1
        foreach ($endpoint->allTypes() as $type) {
63 1
            if (!\in_array($type->getName(), $this->used)) {
64 1
                $endpoint->removeType($type->getName());
65
            }
66
        }
67 1
    }
68
69
    /**
70
     * @param QueryDefinition[] $operations
71
     */
72 1
    protected function processOperations($operations)
73
    {
74 1
        foreach ($operations as $operation) {
75 1
            $this->used($operation->getType());
76 1
            foreach ($operation->getArguments() as $argument) {
77 1
                $this->used($argument->getType());
78
            }
79
        }
80 1
    }
81
82
    /**
83
     * @param string|DefinitionInterface $definition
84
     */
85 1
    protected function used($definition): void
86
    {
87 1
        if (!$definition) {
88
            return;
89
        }
90
91 1
        if (\is_string($definition)) {
92 1
            if (!$this->endpoint->hasType($definition)) {
93 1
                return;
94
            }
95
96 1
            $definition = $this->endpoint->getType($definition);
97
        }
98
99 1
        if (\in_array($definition->getName(), $this->used)) {
100 1
            return;
101
        }
102
103 1
        $this->used[] = $definition->getName();
104 1
        if ($definition instanceof FieldsAwareDefinitionInterface) {
105 1
            foreach ($definition->getFields() as $field) {
106 1
                $this->used($field->getType());
107 1
                foreach ($field->getArguments() as $argument) {
108 1
                    $this->used($argument->getType());
109
                }
110
            }
111
        }
112
113 1
        if ($definition instanceof InterfaceDefinition) {
114 1
            foreach ($definition->getImplementors() as $implementor) {
115 1
                $this->used($implementor);
116
            }
117
        }
118
119 1
        if ($definition instanceof UnionDefinition) {
120
            foreach ($definition->getTypes() as $type) {
121
                $this->used($type->getType());
122
            }
123
        }
124
125 1
        if ($definition instanceof ImplementorInterface) {
126 1
            foreach ($definition->getInterfaces() as $interface) {
127 1
                $this->used($interface);
128
            }
129
        }
130 1
    }
131
}
132