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
Pull Request — master (#225)
by Renato
07:00
created

Configuration   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 99.01%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
c 2
b 0
f 0
lcom 1
cbo 7
dl 0
loc 249
ccs 200
cts 202
cp 0.9901
rs 10

4 Methods

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