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
Push — master ( cc83a8...7c2c4f )
by Jérémiah
48s
created

OverblogGraphQLExtension::setGraphiQLTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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