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 (#213)
by Jérémiah
32:24 queued 10:43
created

OverblogGraphQLExtension::prepend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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