Scrutinizer GitHub App not installed

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

Install GitHub App

Completed
Pull Request — master (#225)
by Renato
21:19
created

setDefinitionParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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