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 (#64)
by Adrian
05:08
created

OverblogGraphQLExtension::setSecurity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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 Overblog\GraphQLBundle\Config\TypeWithOutputFieldsDefinition;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
18
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
19
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
20
21
class OverblogGraphQLExtension extends Extension implements PrependExtensionInterface
22
{
23 11
    public function load(array $configs, ContainerBuilder $container)
24
    {
25 11
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
26 11
        $loader->load('services.yml');
27 11
        $loader->load('graphql_types.yml');
28 11
        $loader->load('graphql_resolvers.yml');
29
30 11
        $config = $this->treatConfigs($configs, $container);
31
32 11
        $this->setServicesAliases($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 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...
33 11
        $this->setSchemaBuilderArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 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...
34 11
        $this->setSchemaArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 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...
35 11
        $this->setErrorHandlerArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 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...
36 11
        $this->setGraphiQLTemplate($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 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...
37 11
        $this->setSecurity($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 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...
38 11
        $this->setConfigBuilders($config);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 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...
39 11
        $this->setVersions($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 30 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...
40
41 11
        $container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources'));
42 11
    }
43
44 9
    public function prepend(ContainerBuilder $container)
45
    {
46 9
        $configs = $container->getExtensionConfig($this->getAlias());
47 9
        $configs = $container->getParameterBag()->resolveValue($configs);
48 9
        $config = $this->treatConfigs($configs, $container, true);
49
50
        /** @var OverblogGraphQLTypesExtension $typesExtension */
51 9
        $typesExtension = $container->getExtension($this->getAlias().'_types');
52 9
        $typesExtension->containerPrependExtensionConfig($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container, true) on line 48 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...
53 9
    }
54
55 11
    private function setVersions(array $config, ContainerBuilder $container)
56
    {
57 11
        $container->setParameter($this->getAlias().'.versions.graphiql', $config['versions']['graphiql']);
58 11
        $container->setParameter($this->getAlias().'.versions.react', $config['versions']['react']);
59 11
    }
60
61 11
    private function setConfigBuilders(array $config)
62
    {
63 11
        foreach (['args', 'field'] as $category) {
64 11
            if (!empty($config['definitions']['builders'][$category])) {
65 1
                $method = 'add'.ucfirst($category).'BuilderClass';
66
67 1
                foreach ($config['definitions']['builders'][$category] as $params) {
68 1
                    TypeWithOutputFieldsDefinition::$method($params['alias'], $params['class']);
69 1
                }
70 1
            }
71 11
        }
72 11
    }
73
74 11
    private function treatConfigs(array $configs, ContainerBuilder $container, $forceReload = false)
75
    {
76 11
        static $config = null;
77
78 11
        if ($forceReload || null === $config) {
79 11
            $configuration = $this->getConfiguration($configs, $container);
80 11
            $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Bug introduced by
It seems like $configuration defined by $this->getConfiguration($configs, $container) on line 79 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...
81 11
        }
82
83 11
        return $config;
84
    }
85
86 11
    private function setSecurity(array $config, ContainerBuilder $container)
87
    {
88 11
        $container->setParameter($this->getAlias().'.query_max_depth', $config['security']['query_max_depth']);
89 11
        $container->setParameter($this->getAlias().'.query_max_complexity', $config['security']['query_max_complexity']);
90 11
    }
91
92 11
    private function setGraphiQLTemplate(array $config, ContainerBuilder $container)
93
    {
94 11
        $container->setParameter($this->getAlias().'.graphiql_template', $config['templates']['graphiql']);
95 11
    }
96
97 11
    private function setErrorHandlerArguments(array $config, ContainerBuilder $container)
98
    {
99 11
        if (isset($config['definitions']['internal_error_message'])) {
100
            $container
101 1
                ->getDefinition($this->getAlias().'.error_handler')
102 1
                ->replaceArgument(0, $config['definitions']['internal_error_message'])
103
            ;
104 1
        }
105
106 11
        if (isset($config['definitions']['exceptions'])) {
107
            $container
108 2
                ->getDefinition($this->getAlias().'.error_handler')
109 2
                ->replaceArgument(2, $this->buildExceptionMap($config['definitions']['exceptions']))
110
            ;
111 2
        }
112 11
    }
113
114 11
    private function setSchemaBuilderArguments(array $config, ContainerBuilder $container)
115
    {
116 11
        $container->getDefinition($this->getAlias().'.schema_builder')
117 11
            ->replaceArgument(1, $config['definitions']['config_validation']);
118 11
    }
119
120 11
    private function setSchemaArguments(array $config, ContainerBuilder $container)
121
    {
122 11
        if (isset($config['definitions']['schema'])) {
123
            $container
124 11
                ->getDefinition($this->getAlias().'.schema')
125 11
                ->replaceArgument(0, $config['definitions']['schema']['query'])
126 11
                ->replaceArgument(1, $config['definitions']['schema']['mutation'])
127 11
                ->replaceArgument(2, $config['definitions']['schema']['subscription'])
128 11
                ->setPublic(true)
129
            ;
130 11
        }
131 11
    }
132
133 11
    private function setServicesAliases(array $config, ContainerBuilder $container)
134
    {
135 11
        if (isset($config['services'])) {
136 11
            foreach ($config['services'] as $name => $id) {
137 11
                $alias = sprintf('%s.%s', $this->getAlias(), $name);
138 11
                $container->setAlias($alias, $id);
139 11
            }
140 11
        }
141 11
    }
142
143 11
    public function getAlias()
144
    {
145 11
        return 'overblog_graphql';
146
    }
147
148 11
    public function getConfiguration(array $config, ContainerBuilder $container)
149
    {
150 11
        return new Configuration($container->getParameter('kernel.debug'));
151
    }
152
153
    /**
154
     * Returns a list of custom exceptions mapped to error/warning classes.
155
     *
156
     * @param array $config
0 ignored issues
show
Documentation introduced by
There is no parameter named $config. Did you maybe mean $exceptionConfig?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
157
     * @return array Custom exception map, [exception => UserError/UserWarning].
158
     */
159 2
    private function buildExceptionMap(array $exceptionConfig)
160
    {
161 2
        $exceptionMap = [];
162 2
        $typeMap = $exceptionConfig['types'];
163
164 2
        foreach ($exceptionConfig as $type => $exceptionList) {
165 2
            if ('types' === $type) {
166 2
                continue;
167
            }
168
169 2
            foreach ($exceptionList as $exception) {
170 2
                $exceptionMap[$exception] = $typeMap[$type];
171 2
            }
172 2
        }
173
174 2
        return $exceptionMap;
175
    }
176
}
177