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 (#240)
by Renato
21:21 queued 17:44
created

Configuration   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 99.53%

Importance

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

4 Methods

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