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 (#264)
by Jérémiah
12:16
created

OverblogGraphQLExtension::setShowDebug()   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
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
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\Processor\BuilderProcessor;
8
use Overblog\GraphQLBundle\Event\Events;
9
use Overblog\GraphQLBundle\EventListener\ClassLoaderListener;
10
use Overblog\GraphQLBundle\EventListener\DebugListener;
11
use Symfony\Component\Cache\Adapter\ArrayAdapter;
12
use Symfony\Component\Config\FileLocator;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Definition;
15
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
16
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
17
use Symfony\Component\DependencyInjection\Reference;
18
use Symfony\Component\ExpressionLanguage\ParserCache\ArrayParserCache;
19
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
20
use Symfony\Component\HttpKernel\Kernel;
21
22
class OverblogGraphQLExtension extends Extension implements PrependExtensionInterface
23
{
24 27
    public function load(array $configs, ContainerBuilder $container)
25
    {
26 27
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
27 27
        $loader->load('services.yml');
28 27
        $loader->load('graphql_types.yml');
29
30 27
        $config = $this->treatConfigs($configs, $container);
31
32 27
        $this->setBatchingMethod($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 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...
33 27
        $this->setExpressionLanguageDefaultParser($container);
34 27
        $this->setServicesAliases($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 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...
35 27
        $this->setSchemaBuilderArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 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...
36 27
        $this->setSchemaArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 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...
37 27
        $this->setErrorHandlerArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 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...
38 27
        $this->setSecurity($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 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...
39 27
        $this->setConfigBuilders($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 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...
40 27
        $this->setDebugListener($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 can also be of type null; however, Overblog\GraphQLBundle\D...ion::setDebugListener() 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 27
        $this->setDefinitionParameters($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 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...
42 27
        $this->setClassLoaderListener($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 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...
43 27
        $this->setCompilerCacheWarmer($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 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...
44
45 27
        $container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources'));
46 27
    }
47
48 25
    public function prepend(ContainerBuilder $container)
49
    {
50 25
        $configs = $container->getExtensionConfig($this->getAlias());
51 25
        $configs = $container->getParameterBag()->resolveValue($configs);
52 25
        $config = $this->treatConfigs($configs, $container, true);
53
54
        /** @var OverblogGraphQLTypesExtension $typesExtension */
55 25
        $typesExtension = $container->getExtension($this->getAlias().'_types');
56 25
        $typesExtension->containerPrependExtensionConfig($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container, true) on line 52 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...
57 25
    }
58
59 27
    public function getAlias()
60
    {
61 27
        return 'overblog_graphql';
62
    }
63
64 27
    public function getConfiguration(array $config, ContainerBuilder $container)
65
    {
66 27
        return new Configuration(
67 27
            $container->getParameter('kernel.debug'),
68 27
            $container->hasParameter('kernel.cache_dir') ? $container->getParameter('kernel.cache_dir') : null
69
        );
70
    }
71
72 27
    private function setCompilerCacheWarmer(array $config, ContainerBuilder $container)
73
    {
74 27
        if ($config['definitions']['auto_compile']) {
75 26
            $definition = $container->setDefinition(
76 26
                CompileCacheWarmer::class,
77 26
                new Definition(CompileCacheWarmer::class)
78
            );
79 26
            $definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]);
80 26
            $definition->addTag('kernel.cache_warmer', ['priority' => 50]);
81
        }
82 27
    }
83
84 27
    private function setClassLoaderListener(array $config, ContainerBuilder $container)
85
    {
86 27
        $container->setParameter($this->getAlias().'.use_classloader_listener', $config['definitions']['use_classloader_listener']);
87 27
        if ($config['definitions']['use_classloader_listener']) {
88 26
            $definition = $container->setDefinition(
89 26
                $this->getAlias().'.event_listener.classloader_listener',
90 26
                new Definition(ClassLoaderListener::class)
91
            );
92 26
            $definition->setPublic(true);
93 26
            $definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]);
94 26
            $definition->addTag('kernel.event_listener', ['event' => 'kernel.request', 'method' => 'load', 'priority' => 255]);
95 26
            $definition->addTag('kernel.event_listener', ['event' => 'console.command', 'method' => 'load', 'priority' => 255]);
96
        }
97 27
    }
98
99 27
    private function setDefinitionParameters(array $config, ContainerBuilder $container)
100
    {
101
        // auto mapping
102 27
        $container->setParameter($this->getAlias().'.auto_mapping.enabled', $config['definitions']['auto_mapping']['enabled']);
103 27
        $container->setParameter($this->getAlias().'.auto_mapping.directories', $config['definitions']['auto_mapping']['directories']);
104
        // generator and config
105 27
        $container->setParameter($this->getAlias().'.default_resolver', $config['definitions']['default_resolver']);
106 27
        $container->setParameter($this->getAlias().'.class_namespace', $config['definitions']['class_namespace']);
107 27
        $container->setParameter($this->getAlias().'.cache_dir', $config['definitions']['cache_dir']);
108 27
    }
109
110 27
    private function setBatchingMethod(array $config, ContainerBuilder $container)
111
    {
112 27
        $container->setParameter($this->getAlias().'.batching_method', $config['batching_method']);
113 27
    }
114
115 27
    private function setExpressionLanguageDefaultParser(ContainerBuilder $container)
116
    {
117 27
        $class = version_compare(Kernel::VERSION, '3.2.0', '>=') ? ArrayAdapter::class : ArrayParserCache::class;
118 27
        $definition = new Definition($class);
119 27
        $definition->setPublic(false);
120 27
        $container->setDefinition($this->getAlias().'.cache_expression_language_parser.default', $definition);
121 27
    }
122
123 27
    private function setDebugListener(array $config, ContainerBuilder $container)
124
    {
125 27
        if ($config['definitions']['show_debug_info']) {
126 1
            $definition = $container->setDefinition(
127 1
                DebugListener::class,
128 1
                new Definition(DebugListener::class)
129
            );
130 1
            $definition->addTag('kernel.event_listener', ['event' => Events::PRE_EXECUTOR, 'method' => 'onPreExecutor']);
131 1
            $definition->addTag('kernel.event_listener', ['event' => Events::POST_EXECUTOR, 'method' => 'onPostExecutor']);
132
        }
133 27
    }
134
135 27
    private function setConfigBuilders(array $config, ContainerBuilder $container)
136
    {
137 27
        $useObjectToAddResource = method_exists($container, 'addObjectResource');
138 27
        $objectToAddResourceMethod = $useObjectToAddResource ? 'addObjectResource' : 'addClassResource';
139
140 27
        foreach (BuilderProcessor::BUILDER_TYPES as $type) {
141 27
            if (!empty($config['definitions']['builders'][$type])) {
142 1
                foreach ($config['definitions']['builders'][$type] as $params) {
143 1
                    $object = $useObjectToAddResource ? $params['class'] : new \ReflectionClass($params['class']);
144 1
                    $container->$objectToAddResourceMethod($object);
145 27
                    BuilderProcessor::addBuilderClass($params['alias'], $type, $params['class']);
146
                }
147
            }
148
        }
149 27
    }
150
151 27
    private function treatConfigs(array $configs, ContainerBuilder $container, $forceReload = false)
152
    {
153 27
        static $config = null;
154
155 27
        if ($forceReload || null === $config) {
156 27
            $configuration = $this->getConfiguration($configs, $container);
157 27
            $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 27
        return $config;
161
    }
162
163 27
    private function setSecurity(array $config, ContainerBuilder $container)
164
    {
165 27
        foreach ($config['security'] as $key => $value) {
166 27
            $container->setParameter(sprintf('%s.%s', $this->getAlias(), $key), $value);
167
        }
168 27
    }
169
170 27
    private function setErrorHandlerArguments(array $config, ContainerBuilder $container)
171
    {
172 27
        $errorHandlerDefinition = $container->getDefinition($this->getAlias().'.error_handler');
173
174 27
        if (isset($config['definitions']['internal_error_message'])) {
175 27
            $errorHandlerDefinition->replaceArgument(0, $config['definitions']['internal_error_message']);
176
        }
177
178 27
        if (isset($config['definitions']['map_exceptions_to_parent'])) {
179 27
            $errorHandlerDefinition->replaceArgument(
180 27
                3,
181 27
                $config['definitions']['map_exceptions_to_parent']
182
            );
183
        }
184
185 27
        if (isset($config['definitions']['exceptions'])) {
186
            $errorHandlerDefinition
187 27
                ->replaceArgument(2, $this->buildExceptionMap($config['definitions']['exceptions']))
188 27
                ->addMethodCall('setUserWarningClass', [$config['definitions']['exceptions']['types']['warnings']])
189 27
                ->addMethodCall('setUserErrorClass', [$config['definitions']['exceptions']['types']['errors']])
190 27
                ->addMethodCall('setErrorFormatter', [$config['definitions']['error_formatter']])
191
            ;
192
        }
193 27
    }
194
195 27
    private function setSchemaBuilderArguments(array $config, ContainerBuilder $container)
196
    {
197 27
        $container->getDefinition($this->getAlias().'.schema_builder')
198 27
            ->replaceArgument(1, $config['definitions']['config_validation']);
199 27
    }
200
201 27
    private function setSchemaArguments(array $config, ContainerBuilder $container)
202
    {
203 27
        if (isset($config['definitions']['schema'])) {
204 27
            $executorDefinition = $container->getDefinition($this->getAlias().'.request_executor');
205
206 27
            foreach ($config['definitions']['schema'] as $schemaName => $schemaConfig) {
207 23
                $schemaID = sprintf('%s.schema_%s', $this->getAlias(), $schemaName);
208 23
                $definition = new Definition(Schema::class);
209 23
                $definition->setFactory([new Reference('overblog_graphql.schema_builder'), 'create']);
210 23
                $definition->setArguments([$schemaConfig['query'], $schemaConfig['mutation'], $schemaConfig['subscription']]);
211 23
                $definition->setPublic(false);
212 23
                $container->setDefinition($schemaID, $definition);
213
214 23
                $executorDefinition->addMethodCall('addSchema', [$schemaName, new Reference($schemaID)]);
215
            }
216
        }
217 27
    }
218
219 27
    private function setServicesAliases(array $config, ContainerBuilder $container)
220
    {
221 27
        if (isset($config['services'])) {
222 27
            foreach ($config['services'] as $name => $id) {
223 27
                $alias = sprintf('%s.%s', $this->getAlias(), $name);
224 27
                $container->setAlias($alias, $id);
225
            }
226
        }
227 27
    }
228
229
    /**
230
     * Returns a list of custom exceptions mapped to error/warning classes.
231
     *
232
     * @param array $exceptionConfig
233
     *
234
     * @return array Custom exception map, [exception => UserError/UserWarning]
235
     */
236 27
    private function buildExceptionMap(array $exceptionConfig)
237
    {
238 27
        $exceptionMap = [];
239 27
        $typeMap = $exceptionConfig['types'];
240
241 27
        foreach ($exceptionConfig as $type => $exceptionList) {
242 27
            if ('types' === $type) {
243 27
                continue;
244
            }
245
246 27
            foreach ($exceptionList as $exception) {
247 27
                $exceptionMap[$exception] = $typeMap[$type];
248
            }
249
        }
250
251 27
        return $exceptionMap;
252
    }
253
}
254