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
08:03
created

Configuration   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 257
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 99.52%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
lcom 1
cbo 7
dl 0
loc 257
ccs 206
cts 207
cp 0.9952
rs 10
c 1
b 0
f 0

4 Methods

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