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 (#198)
by Jérémiah
06:23
created

OverblogGraphQLExtension::setServicesAliases()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 3
eloc 5
nc 3
nop 2
crap 3
1
<?php
2
3
/*
4
 * This file is part of the OverblogGraphQLBundle package.
5
 *
6
 * (c) Overblog <http://github.com/overblog/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Overblog\GraphQLBundle\DependencyInjection;
13
14
use GraphQL\Type\Schema;
15
use Overblog\GraphQLBundle\Config\TypeWithOutputFieldsDefinition;
16
use Overblog\GraphQLBundle\EventListener\ClassLoaderListener;
17
use Symfony\Component\Cache\Adapter\ArrayAdapter;
18
use Symfony\Component\Config\FileLocator;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Definition;
21
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
22
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
23
use Symfony\Component\DependencyInjection\Reference;
24
use Symfony\Component\ExpressionLanguage\ParserCache\ArrayParserCache;
25
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
26
use Symfony\Component\HttpKernel\Kernel;
27
28
class OverblogGraphQLExtension extends Extension implements PrependExtensionInterface
29
{
30 20
    public function load(array $configs, ContainerBuilder $container)
31
    {
32 20
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
33 20
        $loader->load('services.yml');
34 20
        $loader->load('graphql_types.yml');
35
36 20
        $config = $this->treatConfigs($configs, $container);
37
38 20
        $this->setBatchingMethod($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 36 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...
39 20
        $this->setExpressionLanguageDefaultParser($container);
40 20
        $this->setServicesAliases($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 36 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...
41 20
        $this->setSchemaBuilderArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 36 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...
42 20
        $this->setSchemaArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 36 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...
43 20
        $this->setErrorHandlerArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 36 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...
44 20
        $this->setGraphiQLTemplate($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 36 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...
45 20
        $this->setSecurity($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 36 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...
46 20
        $this->setConfigBuilders($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 36 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...
47 20
        $this->setVersions($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 36 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...
48 20
        $this->setShowDebug($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 36 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...
49 20
        $this->setDefinitionParameters($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 36 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...
50 20
        $this->setClassLoaderListener($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 36 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...
51
52 20
        $container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources'));
53 20
    }
54
55 18
    public function prepend(ContainerBuilder $container)
56
    {
57 18
        $configs = $container->getExtensionConfig($this->getAlias());
58 18
        $configs = $container->getParameterBag()->resolveValue($configs);
59 18
        $config = $this->treatConfigs($configs, $container, true);
60
61
        /** @var OverblogGraphQLTypesExtension $typesExtension */
62 18
        $typesExtension = $container->getExtension($this->getAlias().'_types');
63 18
        $typesExtension->containerPrependExtensionConfig($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container, true) on line 59 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...
64 18
    }
65
66 20
    public function getAlias()
67
    {
68 20
        return 'overblog_graphql';
69
    }
70
71 20
    public function getConfiguration(array $config, ContainerBuilder $container)
72
    {
73 20
        return new Configuration(
74 20
            $container->getParameter('kernel.debug'),
75 20
            $container->hasParameter('kernel.cache_dir') ? $container->getParameter('kernel.cache_dir') : null
76
        );
77
    }
78
79 20
    private function setClassLoaderListener(array $config, ContainerBuilder $container)
80
    {
81 20
        $container->setParameter($this->getAlias().'.use_classloader_listener', $config['definitions']['use_classloader_listener']);
82 20
        if ($config['definitions']['use_classloader_listener']) {
83 18
            $definition = $container->setDefinition(
84 18
                $this->getAlias().'.event_listener.classloader_listener',
85 18
                new Definition(ClassLoaderListener::class)
86
            );
87 18
            $definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]);
88 18
            $definition->addTag('kernel.event_listener', ['event' => 'kernel.request', 'method' => 'load', 'priority' => 255]);
89 18
            $definition->addTag('kernel.event_listener', ['event' => 'console.command', 'method' => 'load', 'priority' => 255]);
90
        }
91 20
    }
92
93 20
    private function setDefinitionParameters(array $config, ContainerBuilder $container)
94
    {
95
        // auto mapping
96 20
        $container->setParameter($this->getAlias().'.auto_mapping.enabled', $config['definitions']['auto_mapping']['enabled']);
97 20
        $container->setParameter($this->getAlias().'.auto_mapping.directories', $config['definitions']['auto_mapping']['directories']);
98
        // generator and config
99 20
        $container->setParameter($this->getAlias().'.default_resolver', $config['definitions']['default_resolver']);
100 20
        $container->setParameter($this->getAlias().'.class_namespace', $config['definitions']['class_namespace']);
101 20
        $container->setParameter($this->getAlias().'.cache_dir', $config['definitions']['cache_dir']);
102 20
    }
103
104 20
    private function setBatchingMethod(array $config, ContainerBuilder $container)
105
    {
106 20
        $container->setParameter($this->getAlias().'.batching_method', $config['batching_method']);
107 20
    }
108
109 20
    private function setExpressionLanguageDefaultParser(ContainerBuilder $container)
110
    {
111 20
        $class = version_compare(Kernel::VERSION, '3.2.0', '>=') ? ArrayAdapter::class : ArrayParserCache::class;
112 20
        $definition = new Definition($class);
113 20
        $definition->setPublic(false);
114 20
        $container->setDefinition($this->getAlias().'.cache_expression_language_parser.default', $definition);
115 20
    }
116
117 20
    private function setShowDebug(array $config, ContainerBuilder $container)
118
    {
119 20
        $container->getDefinition($this->getAlias().'.request_executor')->replaceArgument(4, $config['definitions']['show_debug_info']);
120 20
    }
121
122 20
    private function setVersions(array $config, ContainerBuilder $container)
123
    {
124 20
        foreach ($config['versions'] as $key => $version) {
125 20
            $container->setParameter(sprintf('%s.versions.%s', $this->getAlias(), $key), $version);
126
        }
127 20
    }
128
129 20
    private function setConfigBuilders(array $config, ContainerBuilder $container)
130
    {
131 20
        foreach (['args', 'field'] as $category) {
132 20
            if (!empty($config['definitions']['builders'][$category])) {
133 1
                $method = 'add'.ucfirst($category).'BuilderClass';
134
135 1
                foreach ($config['definitions']['builders'][$category] as $params) {
136 1
                    $container->addClassResource(new \ReflectionClass($params['class']));
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Depend...der::addClassResource() has been deprecated with message: since version 3.3, to be removed in 4.0. Use addObjectResource() or getReflectionClass() instead.

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

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

Loading history...
137 20
                    TypeWithOutputFieldsDefinition::$method($params['alias'], $params['class']);
138
                }
139
            }
140
        }
141 20
    }
142
143 20
    private function treatConfigs(array $configs, ContainerBuilder $container, $forceReload = false)
144
    {
145 20
        static $config = null;
146
147 20
        if ($forceReload || null === $config) {
148 20
            $configuration = $this->getConfiguration($configs, $container);
149 20
            $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Bug introduced by
It seems like $configuration defined by $this->getConfiguration($configs, $container) on line 148 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...
150
        }
151
152 20
        return $config;
153
    }
154
155 20
    private function setSecurity(array $config, ContainerBuilder $container)
156
    {
157 20
        foreach ($config['security'] as $key => $value) {
158 20
            $container->setParameter(sprintf('%s.%s', $this->getAlias(), $key), $value);
159
        }
160 20
    }
161
162 20
    private function setGraphiQLTemplate(array $config, ContainerBuilder $container)
163
    {
164 20
        $container->setParameter($this->getAlias().'.graphiql_template', $config['templates']['graphiql']);
165 20
    }
166
167 20
    private function setErrorHandlerArguments(array $config, ContainerBuilder $container)
168
    {
169 20
        $errorHandlerDefinition = $container->getDefinition($this->getAlias().'.error_handler');
170
171 20
        if (isset($config['definitions']['internal_error_message'])) {
172 1
            $errorHandlerDefinition->replaceArgument(0, $config['definitions']['internal_error_message']);
173
        }
174
175 20
        if (isset($config['definitions']['exceptions'])) {
176
            $errorHandlerDefinition
177 20
                ->replaceArgument(2, $this->buildExceptionMap($config['definitions']['exceptions']))
178 20
                ->addMethodCall('setUserWarningClass', [$config['definitions']['exceptions']['types']['warnings']])
179 20
                ->addMethodCall('setUserErrorClass', [$config['definitions']['exceptions']['types']['errors']])
180
            ;
181
        }
182 20
    }
183
184 20
    private function setSchemaBuilderArguments(array $config, ContainerBuilder $container)
185
    {
186 20
        $container->getDefinition($this->getAlias().'.schema_builder')
187 20
            ->replaceArgument(1, $config['definitions']['config_validation']);
188 20
    }
189
190 20
    private function setSchemaArguments(array $config, ContainerBuilder $container)
191
    {
192 20
        if (isset($config['definitions']['schema'])) {
193 20
            $executorDefinition = $container->getDefinition($this->getAlias().'.request_executor');
194
195 20
            foreach ($config['definitions']['schema'] as $schemaName => $schemaConfig) {
196 17
                $schemaID = sprintf('%s.schema_%s', $this->getAlias(), $schemaName);
197 17
                $definition = new Definition(Schema::class);
198 17
                $definition->setFactory([new Reference('overblog_graphql.schema_builder'), 'create']);
199 17
                $definition->setArguments([$schemaConfig['query'], $schemaConfig['mutation'], $schemaConfig['subscription']]);
200 17
                $definition->setPublic(false);
201 17
                $container->setDefinition($schemaID, $definition);
202
203 17
                $executorDefinition->addMethodCall('addSchema', [$schemaName, new Reference($schemaID)]);
204
            }
205
        }
206 20
    }
207
208 20
    private function setServicesAliases(array $config, ContainerBuilder $container)
209
    {
210 20
        if (isset($config['services'])) {
211 20
            foreach ($config['services'] as $name => $id) {
212 20
                $alias = sprintf('%s.%s', $this->getAlias(), $name);
213 20
                $container->setAlias($alias, $id);
214
            }
215
        }
216 20
    }
217
218
    /**
219
     * Returns a list of custom exceptions mapped to error/warning classes.
220
     *
221
     * @param array $exceptionConfig
222
     *
223
     * @return array Custom exception map, [exception => UserError/UserWarning]
224
     */
225 20
    private function buildExceptionMap(array $exceptionConfig)
226
    {
227 20
        $exceptionMap = [];
228 20
        $typeMap = $exceptionConfig['types'];
229
230 20
        foreach ($exceptionConfig as $type => $exceptionList) {
231 20
            if ('types' === $type) {
232 20
                continue;
233
            }
234
235 20
            foreach ($exceptionList as $exception) {
236 20
                $exceptionMap[$exception] = $typeMap[$type];
237
            }
238
        }
239
240 20
        return $exceptionMap;
241
    }
242
}
243