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
Push — 0.9 ( e8e8ed...548911 )
by Jérémiah
18:15
created

Configuration   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 258
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 99.07%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
lcom 1
cbo 7
dl 0
loc 258
ccs 214
cts 216
cp 0.9907
rs 10
c 2
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C getConfigTreeBuilder() 0 171 7
B addBuilderSection() 0 32 4
B addSecurityQuerySection() 0 34 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\Validator\Rules\QueryComplexity;
15
use GraphQL\Validator\Rules\QueryDepth;
16
use Overblog\GraphQLBundle\Error\ErrorHandler;
17
use Overblog\GraphQLBundle\Resolver\Resolver;
18
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
19
use Symfony\Component\Config\Definition\ConfigurationInterface;
20
21
class Configuration implements ConfigurationInterface
22
{
23
    private $debug;
24
25
    private $cacheDir;
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param bool        $debug    Whether to use the debug mode
31
     * @param null|string $cacheDir
32
     */
33 25
    public function __construct($debug, $cacheDir = null)
34
    {
35 25
        $this->debug = (bool) $debug;
36 25
        $this->cacheDir = $cacheDir;
37 25
    }
38
39 25
    public function getConfigTreeBuilder()
40
    {
41 25
        $treeBuilder = new TreeBuilder();
42 25
        $rootNode = $treeBuilder->root('overblog_graphql');
43
44
        $rootNode
45 25
            ->children()
46 25
                ->enumNode('batching_method')
47 25
                    ->values(['relay', 'apollo'])
48 25
                    ->defaultValue('relay')
49 25
                ->end()
50 25
                ->arrayNode('definitions')
51 25
                    ->addDefaultsIfNotSet()
52 25
                    ->children()
53 25
                        ->scalarNode('internal_error_message')->defaultNull()->end()
54 25
                        ->variableNode('default_resolver')->defaultValue([Resolver::class, 'defaultResolveFn'])->end()
55 25
                        ->scalarNode('class_namespace')->defaultValue('Overblog\\GraphQLBundle\\__DEFINITIONS__')->end()
56 25
                        ->scalarNode('cache_dir')->defaultValue($this->cacheDir.'/overblog/graphql-bundle/__definitions__')->end()
57 25
                        ->booleanNode('use_classloader_listener')->defaultTrue()->end()
58 25
                        ->booleanNode('show_debug_info')->defaultFalse()->end()
59 25
                        ->booleanNode('config_validation')->defaultValue($this->debug)->end()
60 25
                        ->arrayNode('schema')
61 25
                            ->beforeNormalization()
62 25
                                ->ifTrue(function ($v) {
63 20
                                    $needNormalization = isset($v['query']) && is_string($v['query']) ||
64
                                        isset($v['mutation']) && is_string($v['mutation']) ||
65 20
                                        isset($v['subscription']) && is_string($v['subscription']);
66
67 20
                                    return $needNormalization;
68 25
                                })
69 25
                                ->then(function ($v) {
70 20
                                    return ['default' => $v];
71 25
                                })
72 25
                            ->end()
73 25
                            ->useAttributeAsKey('name')
74 25
                            ->prototype('array')
75 25
                                ->addDefaultsIfNotSet()
76 25
                                ->children()
77 25
                                    ->scalarNode('query')->defaultNull()->end()
78 25
                                    ->scalarNode('mutation')->defaultNull()->end()
79 25
                                    ->scalarNode('subscription')->defaultNull()->end()
80 25
                                ->end()
81 25
                            ->end()
82 25
                        ->end()
83 25
                        ->arrayNode('auto_mapping')
84 25
                            ->treatFalseLike(['enabled' => false])
85 25
                            ->treatTrueLike(['enabled' => true])
86 25
                            ->treatNullLike(['enabled' => true])
87 25
                            ->addDefaultsIfNotSet()
88 25
                            ->children()
89 25
                                ->booleanNode('enabled')->defaultTrue()->end()
90 25
                                ->arrayNode('directories')
91 25
                                    ->info('List of directories containing GraphQL classes.')
92 25
                                    ->prototype('scalar')->end()
93 25
                                ->end()
94 25
                            ->end()
95 25
                        ->end()
96 25
                        ->arrayNode('mappings')
97 25
                            ->children()
98 25
                                ->arrayNode('auto_discover')
99 25
                                    ->treatFalseLike(['bundles' => false, 'root_dir' => false])
100 25
                                    ->treatTrueLike(['bundles' => true, 'root_dir' => true])
101 25
                                    ->treatNullLike(['bundles' => true, 'root_dir' => true])
102 25
                                    ->addDefaultsIfNotSet()
103 25
                                    ->children()
104 25
                                        ->booleanNode('bundles')->defaultTrue()->end()
105 25
                                        ->booleanNode('root_dir')->defaultTrue()->end()
106 25
                                    ->end()
107 25
                                ->end()
108 25
                                ->arrayNode('types')
109 25
                                    ->prototype('array')
110 25
                                        ->addDefaultsIfNotSet()
111 25
                                        ->beforeNormalization()
112 25
                                            ->ifTrue(function ($v) {
113 19
                                                return isset($v['type']) && $v['type'] === 'yml';
114 25
                                            })
115 25
                                            ->then(function ($v) {
116 4
                                                $v['type'] = 'yaml';
117
118 4
                                                return $v;
119 25
                                            })
120 25
                                        ->end()
121 25
                                        ->children()
122 25
                                            ->enumNode('type')->values(['yaml', 'xml'])->defaultNull()->end()
123 25
                                            ->scalarNode('dir')->defaultNull()->end()
124 25
                                            ->scalarNode('suffix')->defaultValue(OverblogGraphQLTypesExtension::DEFAULT_TYPES_SUFFIX)->end()
125 25
                                        ->end()
126 25
                                    ->end()
127 25
                                ->end()
128 25
                            ->end()
129 25
                        ->end()
130 25
                        ->arrayNode('exceptions')
131 25
                            ->addDefaultsIfNotSet()
132 25
                            ->children()
133 25
                                ->arrayNode('warnings')
134 25
                                    ->treatNullLike([])
135 25
                                    ->prototype('scalar')->end()
136 25
                                ->end()
137 25
                                ->arrayNode('errors')
138 25
                                    ->treatNullLike([])
139 25
                                    ->prototype('scalar')->end()
140 25
                                ->end()
141 25
                                ->arrayNode('types')
142 25
                                    ->addDefaultsIfNotSet()
143 25
                                    ->children()
144 25
                                        ->scalarNode('warnings')
145 25
                                            ->defaultValue(ErrorHandler::DEFAULT_USER_WARNING_CLASS)
146 25
                                        ->end()
147 25
                                        ->scalarNode('errors')
148 25
                                            ->defaultValue(ErrorHandler::DEFAULT_USER_ERROR_CLASS)
149 25
                                        ->end()
150 25
                                    ->end()
151 25
                                ->end()
152 25
                            ->end()
153 25
                        ->end()
154
155 25
                        ->arrayNode('builders')
156 25
                            ->children()
157 25
                                ->append($this->addBuilderSection('field'))
158 25
                                ->append($this->addBuilderSection('args'))
159 25
                            ->end()
160 25
                        ->end()
161
162 25
                    ->end()
163 25
                ->end()
164 25
                ->arrayNode('templates')
165 25
                    ->addDefaultsIfNotSet()
166 25
                    ->children()
167 25
                        ->scalarNode('graphiql')
168 25
                            ->defaultValue('@OverblogGraphQL/GraphiQL/index.html.twig')
169 25
                        ->end()
170 25
                    ->end()
171 25
                ->end()
172 25
                ->arrayNode('services')
173 25
                    ->addDefaultsIfNotSet()
174 25
                    ->children()
175 25
                        ->scalarNode('executor')
176 25
                            ->defaultValue('overblog_graphql.executor.default')
177 25
                        ->end()
178 25
                        ->scalarNode('promise_adapter')
179 25
                            ->defaultValue('overblog_graphql.promise_adapter.default')
180 25
                        ->end()
181 25
                        ->scalarNode('expression_language')
182 25
                            ->defaultValue('overblog_graphql.expression_language.default')
183 25
                        ->end()
184 25
                        ->scalarNode('cache_expression_language_parser')
185 25
                            ->defaultValue('overblog_graphql.cache_expression_language_parser.default')
186 25
                        ->end()
187 25
                    ->end()
188 25
                ->end()
189 25
                ->arrayNode('security')
190 25
                    ->addDefaultsIfNotSet()
191 25
                    ->children()
192 25
                        ->append($this->addSecurityQuerySection('query_max_depth', QueryDepth::DISABLED))
193 25
                        ->append($this->addSecurityQuerySection('query_max_complexity', QueryComplexity::DISABLED))
194 25
                        ->booleanNode('handle_cors')->defaultFalse()->end()
195 25
                    ->end()
196 25
                ->end()
197 25
                ->arrayNode('versions')
198 25
                    ->addDefaultsIfNotSet()
199 25
                    ->children()
200 25
                        ->scalarNode('graphiql')->defaultValue('0.11')->end()
201 25
                        ->scalarNode('react')->defaultValue('15.6')->end()
202 25
                        ->scalarNode('fetch')->defaultValue('2.0')->end()
203 25
                        ->enumNode('relay')->values(['modern', 'classic'])->defaultValue('classic')->end()
204 25
                    ->end()
205 25
                ->end()
206 25
            ->end();
207
208 25
        return $treeBuilder;
209
    }
210
211 25
    private function addBuilderSection($name)
212
    {
213 25
        $builder = new TreeBuilder();
214 25
        $node = $builder->root($name);
215 25
        $node->beforeNormalization()
216 25
            ->ifTrue(function ($v) {
217 1
                return is_array($v) && !empty($v);
218 25
            })
219 25
            ->then(function ($v) {
220 1
                foreach ($v as $key => &$config) {
221 1
                    if (is_string($config)) {
222
                        $config = [
223 1
                            'alias' => $key,
224 1
                            'class' => $config,
225
                        ];
226
                    }
227
                }
228
229 1
                return $v;
230 25
            })
231 25
        ->end();
232
233 25
        $node->prototype('array')
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method children() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
234 25
            ->children()
235 25
                ->scalarNode('alias')->isRequired()->end()
236 25
                ->scalarNode('class')->isRequired()->end()
237 25
            ->end()
238 25
        ->end()
239
        ;
240
241 25
        return $node;
242
    }
243
244 25
    private function addSecurityQuerySection($name, $disabledValue)
245
    {
246 25
        $builder = new TreeBuilder();
247 25
        $node = $builder->root($name, 'scalar');
248 25
        $node->beforeNormalization()
249 25
                ->ifTrue(function ($v) {
250 4
                    return is_string($v) && is_numeric($v);
251 25
                })
252 25
                ->then(function ($v) {
253 2
                    return (int) $v;
254 25
                })
255 25
            ->end();
256
257
        $node
258 25
            ->info('Disabled if equal to false.')
259 25
            ->beforeNormalization()
260 25
                ->ifTrue(function ($v) {
261 4
                    return false === $v;
262 25
                })
263 25
                ->then(function () use ($disabledValue) {
264
                    return $disabledValue;
265 25
                })
266 25
            ->end()
267 25
            ->defaultFalse()
268 25
            ->validate()
269 25
                ->ifTrue(function ($v) {
270 4
                    return is_int($v) && $v < 0;
271 25
                })
272 25
                ->thenInvalid('"overblog_graphql.security.'.$name.'" must be greater or equal to 0.')
273 25
            ->end()
274
        ;
275
276 25
        return $node;
277
    }
278
}
279