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
Push — master ( 6eaf22...743834 )
by Renato
56s
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 158
Code Lines 147

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 150
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 158
ccs 150
cts 150
cp 1
rs 8.1463
c 1
b 0
f 0
cc 5
eloc 147
nc 1
nop 0
crap 5

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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