Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#225)
by Renato
08:03
created

OverblogGraphQLExtension::setGraphiQLTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Overblog\GraphQLBundle\DependencyInjection;
4
5
use GraphQL\Type\Schema;
6
use Overblog\GraphQLBundle\CacheWarmer\CompileCacheWarmer;
7
use Overblog\GraphQLBundle\Config\TypeWithOutputFieldsDefinition;
8
use Overblog\GraphQLBundle\EventListener\ClassLoaderListener;
9
use Symfony\Component\Cache\Adapter\ArrayAdapter;
10
use Symfony\Component\Config\FileLocator;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
use Symfony\Component\DependencyInjection\Definition;
13
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
14
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
15
use Symfony\Component\DependencyInjection\Reference;
16
use Symfony\Component\ExpressionLanguage\ParserCache\ArrayParserCache;
17
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
18
use Symfony\Component\HttpKernel\Kernel;
19
20
class OverblogGraphQLExtension extends Extension implements PrependExtensionInterface
21
{
22 24
    public function load(array $configs, ContainerBuilder $container)
23
    {
24 24
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
25 24
        $loader->load('services.yml');
26 24
        $loader->load('graphql_types.yml');
27
28 24
        $config = $this->treatConfigs($configs, $container);
29
30 24
        $this->setBatchingMethod($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 can also be of type null; however, Overblog\GraphQLBundle\D...on::setBatchingMethod() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
31 24
        $this->setExpressionLanguageDefaultParser($container);
32 24
        $this->setServicesAliases($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 can also be of type null; however, Overblog\GraphQLBundle\D...n::setServicesAliases() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
33 24
        $this->setSchemaBuilderArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 can also be of type null; however, Overblog\GraphQLBundle\D...chemaBuilderArguments() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
34 24
        $this->setSchemaArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 can also be of type null; however, Overblog\GraphQLBundle\D...n::setSchemaArguments() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
35 24
        $this->setErrorHandlerArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 can also be of type null; however, Overblog\GraphQLBundle\D...ErrorHandlerArguments() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
36 24
        $this->setSecurity($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 can also be of type null; however, Overblog\GraphQLBundle\D...xtension::setSecurity() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
37 24
        $this->setConfigBuilders($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 can also be of type null; however, Overblog\GraphQLBundle\D...on::setConfigBuilders() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
38 24
        $this->setVersions($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 can also be of type null; however, Overblog\GraphQLBundle\D...xtension::setVersions() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
39 24
        $this->setShowDebug($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 can also be of type null; however, Overblog\GraphQLBundle\D...tension::setShowDebug() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
40 24
        $this->setDefinitionParameters($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 can also be of type null; however, Overblog\GraphQLBundle\D...tDefinitionParameters() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
41 24
        $this->setClassLoaderListener($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 can also be of type null; however, Overblog\GraphQLBundle\D...etClassLoaderListener() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
42 24
        $this->setCompilerCacheWarmer($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 can also be of type null; however, Overblog\GraphQLBundle\D...etCompilerCacheWarmer() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
43
44 24
        $container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources'));
45 24
    }
46
47 22
    public function prepend(ContainerBuilder $container)
48
    {
49 22
        $configs = $container->getExtensionConfig($this->getAlias());
50 22
        $configs = $container->getParameterBag()->resolveValue($configs);
51 22
        $config = $this->treatConfigs($configs, $container, true);
52
53
        /** @var OverblogGraphQLTypesExtension $typesExtension */
54 22
        $typesExtension = $container->getExtension($this->getAlias().'_types');
55 22
        $typesExtension->containerPrependExtensionConfig($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container, true) on line 51 can also be of type null; however, Overblog\GraphQLBundle\D...rependExtensionConfig() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
56 22
    }
57
58 24
    public function getAlias()
59
    {
60 24
        return 'overblog_graphql';
61
    }
62
63 24
    public function getConfiguration(array $config, ContainerBuilder $container)
64
    {
65 24
        return new Configuration(
66 24
            $container->getParameter('kernel.debug'),
67 24
            $container->hasParameter('kernel.cache_dir') ? $container->getParameter('kernel.cache_dir') : null
68
        );
69
    }
70
71 24
    private function setCompilerCacheWarmer(array $config, ContainerBuilder $container)
72
    {
73 24
        if ($config['definitions']['auto_compile']) {
74 23
            $definition = $container->setDefinition(
75 23
                CompileCacheWarmer::class,
76 23
                new Definition(CompileCacheWarmer::class)
77
            );
78 23
            $definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]);
79 23
            $definition->addTag('kernel.cache_warmer', ['priority' => 50]);
80
        }
81 24
    }
82
83 24
    private function setClassLoaderListener(array $config, ContainerBuilder $container)
84
    {
85 24
        $container->setParameter($this->getAlias().'.use_classloader_listener', $config['definitions']['use_classloader_listener']);
86 24
        if ($config['definitions']['use_classloader_listener']) {
87 23
            $definition = $container->setDefinition(
88 23
                $this->getAlias().'.event_listener.classloader_listener',
89 23
                new Definition(ClassLoaderListener::class)
90
            );
91 23
            $definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]);
92 23
            $definition->addTag('kernel.event_listener', ['event' => 'kernel.request', 'method' => 'load', 'priority' => 255]);
93 23
            $definition->addTag('kernel.event_listener', ['event' => 'console.command', 'method' => 'load', 'priority' => 255]);
94
        }
95 24
    }
96
97 24
    private function setDefinitionParameters(array $config, ContainerBuilder $container)
98
    {
99
        // auto mapping
100 24
        $container->setParameter($this->getAlias().'.auto_mapping.enabled', $config['definitions']['auto_mapping']['enabled']);
101 24
        $container->setParameter($this->getAlias().'.auto_mapping.directories', $config['definitions']['auto_mapping']['directories']);
102
        // generator and config
103 24
        $container->setParameter($this->getAlias().'.default_resolver', $config['definitions']['default_resolver']);
104 24
        $container->setParameter($this->getAlias().'.class_namespace', $config['definitions']['class_namespace']);
105 24
        $container->setParameter($this->getAlias().'.cache_dir', $config['definitions']['cache_dir']);
106 24
    }
107
108 24
    private function setBatchingMethod(array $config, ContainerBuilder $container)
109
    {
110 24
        $container->setParameter($this->getAlias().'.batching_method', $config['batching_method']);
111 24
    }
112
113 24
    private function setExpressionLanguageDefaultParser(ContainerBuilder $container)
114
    {
115 24
        $class = version_compare(Kernel::VERSION, '3.2.0', '>=') ? ArrayAdapter::class : ArrayParserCache::class;
116 24
        $definition = new Definition($class);
117 24
        $definition->setPublic(false);
118 24
        $container->setDefinition($this->getAlias().'.cache_expression_language_parser.default', $definition);
119 24
    }
120
121 24
    private function setShowDebug(array $config, ContainerBuilder $container)
122
    {
123 24
        $container->getDefinition($this->getAlias().'.request_executor')->replaceArgument(4, $config['definitions']['show_debug_info']);
124 24
    }
125
126 24
    private function setVersions(array $config, ContainerBuilder $container)
127
    {
128 24
        foreach ($config['versions'] as $key => $version) {
129 24
            $container->setParameter(sprintf('%s.versions.%s', $this->getAlias(), $key), $version);
130
        }
131 24
    }
132
133 24
    private function setConfigBuilders(array $config, ContainerBuilder $container)
134
    {
135 24
        $useObjectToAddResource = method_exists($container, 'addObjectResource');
136 24
        $objectToAddResourceMethod = $useObjectToAddResource ? 'addObjectResource' : 'addClassResource';
137
138 24
        foreach (['args', 'field'] as $category) {
139 24
            if (!empty($config['definitions']['builders'][$category])) {
140 1
                $method = 'add'.ucfirst($category).'BuilderClass';
141
142 1
                foreach ($config['definitions']['builders'][$category] as $params) {
143 1
                    $object = $useObjectToAddResource ? $params['class'] : new \ReflectionClass($params['class']);
144 1
                    $container->$objectToAddResourceMethod($object);
145 24
                    TypeWithOutputFieldsDefinition::$method($params['alias'], $params['class']);
146
                }
147
            }
148
        }
149 24
    }
150
151 24
    private function treatConfigs(array $configs, ContainerBuilder $container, $forceReload = false)
152
    {
153 24
        static $config = null;
154
155 24
        if ($forceReload || null === $config) {
156 24
            $configuration = $this->getConfiguration($configs, $container);
157 24
            $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Bug introduced by
It seems like $configuration defined by $this->getConfiguration($configs, $container) on line 156 can be null; however, Symfony\Component\Depend...:processConfiguration() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
158
        }
159
160 24
        return $config;
161
    }
162
163 24
    private function setSecurity(array $config, ContainerBuilder $container)
164
    {
165 24
        foreach ($config['security'] as $key => $value) {
166 24
            $container->setParameter(sprintf('%s.%s', $this->getAlias(), $key), $value);
167
        }
168 24
    }
169
170 24
    private function setErrorHandlerArguments(array $config, ContainerBuilder $container)
171
    {
172 24
        $errorHandlerDefinition = $container->getDefinition($this->getAlias().'.error_handler');
173
174 24
        if (isset($config['definitions']['internal_error_message'])) {
175 2
            $errorHandlerDefinition->replaceArgument(0, $config['definitions']['internal_error_message']);
176
        }
177
178 24
        if (isset($config['definitions']['map_exceptions_to_parent'])) {
179 24
            $errorHandlerDefinition->replaceArgument(
180 24
                3,
181 24
                $config['definitions']['map_exceptions_to_parent']
182
            );
183
        }
184
185 24
        if (isset($config['definitions']['exceptions'])) {
186
            $errorHandlerDefinition
187 24
                ->replaceArgument(2, $this->buildExceptionMap($config['definitions']['exceptions']))
188 24
                ->addMethodCall('setUserWarningClass', [$config['definitions']['exceptions']['types']['warnings']])
189 24
                ->addMethodCall('setUserErrorClass', [$config['definitions']['exceptions']['types']['errors']])
190
            ;
191
        }
192 24
    }
193
194 24
    private function setSchemaBuilderArguments(array $config, ContainerBuilder $container)
195
    {
196 24
        $container->getDefinition($this->getAlias().'.schema_builder')
197 24
            ->replaceArgument(1, $config['definitions']['config_validation']);
198 24
    }
199
200 24
    private function setSchemaArguments(array $config, ContainerBuilder $container)
201
    {
202 24
        if (isset($config['definitions']['schema'])) {
203 24
            $executorDefinition = $container->getDefinition($this->getAlias().'.request_executor');
204
205 24
            foreach ($config['definitions']['schema'] as $schemaName => $schemaConfig) {
206 20
                $schemaID = sprintf('%s.schema_%s', $this->getAlias(), $schemaName);
207 20
                $definition = new Definition(Schema::class);
208 20
                $definition->setFactory([new Reference('overblog_graphql.schema_builder'), 'create']);
209 20
                $definition->setArguments([$schemaConfig['query'], $schemaConfig['mutation'], $schemaConfig['subscription']]);
210 20
                $definition->setPublic(false);
211 20
                $container->setDefinition($schemaID, $definition);
212
213 20
                $executorDefinition->addMethodCall('addSchema', [$schemaName, new Reference($schemaID)]);
214
            }
215
        }
216 24
    }
217
218 24
    private function setServicesAliases(array $config, ContainerBuilder $container)
219
    {
220 24
        if (isset($config['services'])) {
221 24
            foreach ($config['services'] as $name => $id) {
222 24
                $alias = sprintf('%s.%s', $this->getAlias(), $name);
223 24
                $container->setAlias($alias, $id);
224
            }
225
        }
226 24
    }
227
228
    /**
229
     * Returns a list of custom exceptions mapped to error/warning classes.
230
     *
231
     * @param array $exceptionConfig
232
     *
233
     * @return array Custom exception map, [exception => UserError/UserWarning]
234
     */
235 24
    private function buildExceptionMap(array $exceptionConfig)
236
    {
237 24
        $exceptionMap = [];
238 24
        $typeMap = $exceptionConfig['types'];
239
240 24
        foreach ($exceptionConfig as $type => $exceptionList) {
241 24
            if ('types' === $type) {
242 24
                continue;
243
            }
244
245 24
            foreach ($exceptionList as $exception) {
246 24
                $exceptionMap[$exception] = $typeMap[$type];
247
            }
248
        }
249
250 24
        return $exceptionMap;
251
    }
252
}
253