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

Passed
Pull Request — master (#213)
by Jérémiah
12:14
created

OverblogGraphQLExtension::setConfigBuilders()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

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