NamingConventionFilterResolver::resolve()   F
last analyzed

Complexity

Conditions 22
Paths 1600

Size

Total Lines 85
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 44
CRAP Score 25.0744

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 54
c 4
b 0
f 0
dl 0
loc 85
ccs 44
cts 54
cp 0.8148
rs 0
cc 22
nc 1600
nop 3
crap 25.0744

How to fix   Long Method    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
 *  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\Filter\Resolver;
12
13
use Doctrine\Common\Annotations\Reader;
14
use Symfony\Component\Finder\Finder;
15
use Symfony\Component\HttpKernel\Kernel;
16
use Symfony\Component\HttpKernel\KernelInterface;
17
use Ynlo\GraphQLBundle\Annotation\Filter;
18
use Ynlo\GraphQLBundle\Definition\ExecutableDefinitionInterface;
19
use Ynlo\GraphQLBundle\Definition\FieldDefinition;
20
use Ynlo\GraphQLBundle\Definition\ImplementorInterface;
21
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition;
22
use Ynlo\GraphQLBundle\Definition\NodeAwareDefinitionInterface;
23
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface;
24
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
25
use Ynlo\GraphQLBundle\Filter\FilterResolverInterface;
26
use Ynlo\GraphQLBundle\Util\TypeUtil;
27
28
/**
29
 * Resolve custom filters using naming convention.
30
 * Filters should me placed under Filter folder on each bundle or App namespace
31
 * using the following format:
32
 *
33
 * {BundleNamespace}\Filter\{Node}\{FilterName}
34
 *
35
 * > Filters must implements FilterInterface and has the Filter annotation
36
 */
37
class NamingConventionFilterResolver implements FilterResolverInterface
38
{
39
    /**
40
     * @var KernelInterface
41
     */
42
    private $kernel;
43
44
    /**
45
     * @var Reader
46
     */
47
    private $reader;
48
49
    /**
50
     * @param KernelInterface $kernel
51
     * @param Reader          $reader
52
     */
53 1
    public function __construct(KernelInterface $kernel, Reader $reader)
54
    {
55 1
        $this->kernel = $kernel;
56 1
        $this->reader = $reader;
57 1
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62 1
    public function resolve(ExecutableDefinitionInterface $executableDefinition, ObjectDefinitionInterface $node, Endpoint $endpoint): array
63
    {
64 1
        $types = [];
65 1
        if ($node instanceof ImplementorInterface && $node->getInterfaces()) {
66 1
            $types = array_merge($types, $node->getInterfaces());
67
        }
68 1
        if ($node instanceof NodeAwareDefinitionInterface && $node->getNode()) {
69 1
            $types[] = $node->getNode();
70
        }
71 1
        if ($node instanceof InterfaceDefinition) {
72
            $types[] = $node->getName();
73
        }
74
75 1
        $paths = [];
76 1
        $bundles = $this->kernel->getBundles();
77 1
        foreach ($types as $type) {
78
            //search for filters using naming convention inside Bundle/Filter/...
79 1
            foreach ($bundles as $bundle) {
80 1
                $path = "{$bundle->getPath()}/Filter/$type";
81 1
                if (file_exists($path)) {
82 1
                    $paths[$path] = "{$bundle->getNamespace()}\Filter\\$type";
83
                }
84
            }
85
86
            //symfony ^4.0
87 1
            if (Kernel::VERSION_ID >= 40000) {
88 1
                $path = "{$this->kernel->getRootDir()}/Filter/$type";
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\HttpKe...Interface::getRootDir() has been deprecated: since Symfony 4.2 ( Ignorable by Annotation )

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

88
                $path = "{/** @scrutinizer ignore-deprecated */ $this->kernel->getRootDir()}/Filter/$type";

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
89 1
                if (file_exists($path)) {
90
                    $paths[$path] = "App\Filter\\$type";
91
                }
92
            }
93
        }
94
95 1
        $resolvedFilters = [];
96 1
        foreach ($paths as $path => $namespace) {
97 1
            $finder = new Finder();
98 1
            foreach ($finder->in($path)->name('/.php$/')->getIterator() as $file) {
99 1
                $className = preg_replace('/.php$/', null, $file->getFilename());
100 1
                if ($file->getRelativePath()) {
101
                    $subNamespace = str_replace('/', '\\', $file->getRelativePath());
102
                    $fullyClassName = $namespace.'\\'.$subNamespace.'\\'.$className;
103
                } else {
104 1
                    $fullyClassName = $namespace.'\\'.$className;
105
                }
106
107 1
                if (class_exists($fullyClassName)) {
108 1
                    preg_match('/\w+$/', $fullyClassName, $matches);
109 1
                    $name = lcfirst($matches[0]);
110
111
                    /** @var Filter|null $filter */
112 1
                    $filter = $this->reader->getClassAnnotation(new \ReflectionClass($fullyClassName), Filter::class);
113 1
                    if ($filter) {
114 1
                        $filter->resolver = $fullyClassName;
115 1
                        $filter->name = $filter->name ?: $name;
116
117
                        // convert standard defined type to advanced mode
118
                        // example [Enum] => EnumComparisonExpression
119 1
                        if ($filter->type) {
120 1
                            $field = new FieldDefinition();
121 1
                            $field->setName($filter->name);
122 1
                            $field->setType(TypeUtil::normalize($filter->type));
123 1
                            $field->setNonNull(TypeUtil::isTypeNonNull($filter->type));
124 1
                            $field->setList(TypeUtil::isTypeList($filter->type));
125 1
                            $field->setNonNullList(TypeUtil::isTypeNonNullList($filter->type));
126 1
                            $guessedFilter = FilterUtil::createFilter($endpoint, $field);
127 1
                            if ($guessedFilter->type) {
128 1
                                $filter->type = $guessedFilter->type;
129
                            }
130
                        }
131
132 1
                        if (!$filter->type && $node->hasField($filter->name)) {
133
                            $guessedFilter = FilterUtil::createFilter($endpoint, $node->getField($filter->name));
134
                            $filter->type = $guessedFilter->type;
135
                        }
136 1
                        $resolvedFilters[] = $filter;
137
                    } elseif ($node->hasField($name)) {
138
                        $filter = FilterUtil::createFilter($endpoint, $node->getField($name));
139
                        $filter->resolver = $fullyClassName;
140
                        $resolvedFilters[] = $filter;
141
                    }
142
                }
143
            }
144
        }
145
146 1
        return $resolvedFilters;
147
    }
148
}
149